[studio] Add Back/Forward navigation

This commit is contained in:
2025-05-25 22:36:04 -05:00
parent 8c538560ca
commit 55ef2cea51
7 changed files with 144 additions and 43 deletions

View File

@@ -34,18 +34,6 @@ static bool shutdownHandler(turbine::Context &ctx) {
return sctx->ui.handleShutdown();
}
void navigateTo(Context &ctx, ox::StringParam filePath, ox::StringParam navArgs) noexcept {
ox::String path = std::move(filePath);
if (beginsWith(path, "uuid://")) {
auto [p, err] = keel::uuidUrlToPath(keelCtx(ctx), path);
if (err) {
return;
}
path = p;
}
ctx.ui.navigateTo(std::move(path), std::move(navArgs));
}
namespace ig {
extern bool s_mainWinHasFocus;
}
@@ -148,6 +136,9 @@ StudioUI::StudioUI(turbine::Context &ctx, ox::StringParam projectDataDir) noexce
oxLogError(headerizeConfigFile<StudioConfigV1>(kctx));
turbine::setApplicationData(m_tctx, &m_sctx);
turbine::setShutdownHandler(m_tctx, shutdownHandler);
m_sctx.navCallback = [this](ox::StringParam filePath, ox::StringParam navArgs) {
handleNavigationChange(std::move(filePath), std::move(navArgs));
};
m_projectExplorer.fileChosen.connect(this, &StudioUI::openFile);
m_projectExplorer.addDir.connect(this, &StudioUI::addDir);
m_projectExplorer.addItem.connect(this, &StudioUI::addFile);
@@ -187,7 +178,7 @@ void StudioUI::handleKeyEvent(turbine::Key const key, bool const down) noexcept
}
}
void StudioUI::navigateTo(ox::StringParam path, ox::StringParam navArgs) noexcept {
void StudioUI::handleNavigationChange(ox::StringParam path, ox::StringParam navArgs) noexcept {
m_navAction.emplace(std::move(path), std::move(navArgs));
}
@@ -271,7 +262,7 @@ void StudioUI::drawMenu() noexcept {
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Edit")) {
auto undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
auto const undoStack = m_activeEditor ? m_activeEditor->undoStack() : nullptr;
if (ImGui::MenuItem(
"Undo", STUDIO_CTRL "+Z", false, undoStack && undoStack->canUndo())) {
oxLogError(undoStack->undo());
@@ -311,6 +302,17 @@ void StudioUI::drawMenu() noexcept {
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Navigate")) {
if (ImGui::MenuItem("Back", STUDIO_CTRL "+{", false, m_sctx.navIdx > 1)) {
navigateBack(m_sctx);
}
if (ImGui::MenuItem(
"Forward", STUDIO_CTRL "+}", false,
m_sctx.navIdx < m_sctx.navStack.size())) {
navigateForward(m_sctx);
}
ImGui::EndMenu();
}
if (ImGui::BeginMenu("Help")) {
if (ImGui::MenuItem("About")) {
m_aboutPopup.open();
@@ -354,6 +356,13 @@ void StudioUI::drawTabs() noexcept {
}
if (m_activeEditorOnLastDraw != e.get()) [[unlikely]] {
m_activeEditor->onActivated();
if (!m_sctx.navIdx ||
(m_sctx.navIdx <= m_sctx.navStack.size() &&
m_sctx.navStack[m_sctx.navIdx - 1].filePath != m_activeEditor->itemPath())) {
m_sctx.navStack.resize(m_sctx.navIdx);
++m_sctx.navIdx;
m_sctx.navStack.emplace_back(ox::String{m_activeEditor->itemPath()}, ox::String{""});
}
}
if (m_closeActiveTab) [[unlikely]] {
ImGui::SetTabItemClosed(e->itemDisplayName().c_str());
@@ -508,6 +517,22 @@ void StudioUI::handleKeyInput() noexcept {
}
}
}
if constexpr (ox::defines::OS == ox::OS::Darwin) {
if (ImGui::IsKeyPressed(ImGuiKey_LeftBracket)) {
navigateBack(m_sctx);
} else if (ImGui::IsKeyPressed(ImGuiKey_RightBracket)) {
navigateForward(m_sctx);
}
}
}
if constexpr (ox::defines::OS != ox::OS::Darwin) {
if (ImGui::IsKeyDown(ImGuiKey_ModAlt)) {
if (ImGui::IsKeyPressed(ImGuiKey_LeftArrow)) {
navigateBack(m_sctx);
} else if (ImGui::IsKeyPressed(ImGuiKey_RightArrow)) {
navigateForward(m_sctx);
}
}
}
}

View File

@@ -87,7 +87,7 @@ class StudioUI: public ox::SignalHandler {
void handleKeyEvent(turbine::Key, bool down) noexcept;
void navigateTo(ox::StringParam path, ox::StringParam navArgs) noexcept;
void handleNavigationChange(ox::StringParam path, ox::StringParam navArgs) noexcept;
[[nodiscard]]
constexpr Project *project() noexcept {

View File

@@ -15,6 +15,19 @@ namespace studio {
class StudioUI;
struct Context {
friend void navigateTo(Context &ctx, ox::StringParam filePath, ox::StringParam navArgs) noexcept;
friend void navigateBack(Context &ctx) noexcept;
friend void navigateForward(Context &ctx) noexcept;
friend StudioUI;
protected:
struct NavPath {
ox::String filePath;
ox::String navArgs;
};
size_t navIdx{};
ox::Vector<NavPath> navStack;
std::function<void(ox::StringParam filePath, ox::StringParam navArgs)> navCallback;
public:
StudioUI &ui;
Project *project = nullptr;
turbine::Context &tctx;
@@ -29,4 +42,8 @@ inline keel::Context &keelCtx(Context &ctx) noexcept {
void navigateTo(Context &ctx, ox::StringParam filePath, ox::StringParam navArgs = "") noexcept;
void navigateBack(Context &ctx) noexcept;
void navigateForward(Context &ctx) noexcept;
}

View File

@@ -348,6 +348,8 @@ class QuestionPopup: public Popup {
QuestionPopup(ox::StringParam title, ox::StringParam question) noexcept;
void setQuestion(ox::StringParam msg) noexcept;
void draw(Context &ctx) noexcept override;
};

View File

@@ -1,6 +1,7 @@
add_library(
Studio
configio.cpp
context.cpp
editor.cpp
filepickerpopup.cpp
filetreemodel.cpp

View File

@@ -0,0 +1,52 @@
/*
* Copyright 2016 - 2025 Gary Talent (gary@drinkingtea.net). All rights reserved.
*/
#include <studio/context.hpp>
namespace studio {
void navigateTo(Context &ctx, ox::StringParam filePath, ox::StringParam navArgs) noexcept {
ox::String path = std::move(filePath);
if (beginsWith(path, "uuid://")) {
auto [p, err] = keel::uuidUrlToPath(keelCtx(ctx), path);
if (err) {
return;
}
path = p;
}
ctx.navStack.resize(ctx.navIdx + 1);
ctx.navStack.emplace_back(ox::String{path}, ox::String{navArgs.view()});
ctx.navCallback(std::move(path), std::move(navArgs));
}
void navigateBack(Context &ctx) noexcept {
if (!ctx.navIdx) {
return;
}
--ctx.navIdx;
if (ctx.navIdx < ctx.navStack.size() && ctx.navIdx) {
auto const &n = ctx.navStack[ctx.navIdx - 1];
try {
ctx.navCallback(n.filePath, n.navArgs);
} catch (std::exception const &e) {
oxAssert(ctx.navCallback != nullptr, "navCallback is null");
oxErrf("navigateForward failed: {}", e.what());
}
}
}
void navigateForward(Context &ctx) noexcept {
if (ctx.navIdx < ctx.navStack.size()) {
auto const &n = ctx.navStack[ctx.navIdx];
try {
ctx.navCallback(n.filePath, n.navArgs);
} catch (std::exception const &e) {
oxAssert(ctx.navCallback != nullptr, "navCallback is null");
oxErrf("navigateForward failed: {}", e.what());
}
++ctx.navIdx;
}
}
}

View File

@@ -267,6 +267,10 @@ QuestionPopup::QuestionPopup(ox::StringParam title, ox::StringParam question) no
m_question{std::move(question)} {
}
void QuestionPopup::setQuestion(ox::StringParam msg) noexcept {
m_question = std::move(msg);
}
void QuestionPopup::draw(Context &ctx) noexcept {
switch (m_stage) {
case Stage::Closed: