From 60d67218dbb190ebaf96aa57eb442b301ff49c2f Mon Sep 17 00:00:00 2001 From: Gary Talent Date: Sun, 3 Apr 2022 00:32:23 -0500 Subject: [PATCH] [nostalgia/studio] Make opening a file switch to its tab --- src/nostalgia/studio/studioapp.cpp | 19 +++++++++++++++++-- src/nostalgia/studio/studioapp.hpp | 1 + 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/src/nostalgia/studio/studioapp.cpp b/src/nostalgia/studio/studioapp.cpp index c98046fe..5adc62fd 100644 --- a/src/nostalgia/studio/studioapp.cpp +++ b/src/nostalgia/studio/studioapp.cpp @@ -183,9 +183,15 @@ void StudioUI::drawTabs() noexcept { for (auto it = m_editors.begin(); it != m_editors.end();) { auto const &e = *it; auto open = true; - const auto flags = e->unsavedChanges() ? ImGuiTabItemFlags_UnsavedDocument : 0; + const auto unsavedChanges = e->unsavedChanges() ? ImGuiTabItemFlags_UnsavedDocument : 0; + const auto selected = m_activeEditorUpdatePending && e.get() == m_acitveEditor ? + ImGuiTabItemFlags_SetSelected : 0; + const auto flags = unsavedChanges | selected; if (ImGui::BeginTabItem(e->itemDisplayName().c_str(), &open, flags)) { - m_acitveEditor = e.get(); + if (selected) { + m_acitveEditor = e.get(); + m_activeEditorUpdatePending = false; + } e->draw(m_ctx); ImGui::EndTabItem(); } @@ -283,6 +289,13 @@ ox::Error StudioUI::openProject(const ox::String &path) noexcept { ox::Error StudioUI::openFile(const ox::String &path) noexcept { if (m_openFiles.contains(path)) { + for (auto &e : m_editors) { + if (e->itemName() == path) { + m_acitveEditor = e.get(); + m_activeEditorUpdatePending = true; + break; + } + } return OxError(0); } oxRequire(ext, studio::fileExt(path)); @@ -298,6 +311,8 @@ ox::Error StudioUI::openFile(const ox::String &path) noexcept { editor->closed.connect(this, &StudioUI::closeFile); m_editors.emplace_back(editor); m_openFiles.emplace_back(path); + m_acitveEditor = m_editors.back().value.get(); + m_activeEditorUpdatePending = true; // save to config studio::editConfig(m_ctx, [&](StudioConfig *config) { if (!config->openFiles.contains((path))) { diff --git a/src/nostalgia/studio/studioapp.hpp b/src/nostalgia/studio/studioapp.hpp index 046e9078..ffbb825e 100644 --- a/src/nostalgia/studio/studioapp.hpp +++ b/src/nostalgia/studio/studioapp.hpp @@ -30,6 +30,7 @@ class StudioUI: public ox::SignalHandler { ProjectExplorer *m_projectExplorer = nullptr; ox::Vector m_openFiles; studio::Editor *m_acitveEditor = nullptr; + bool m_activeEditorUpdatePending = false; bool m_saveEnabled = false; bool m_aboutEnabled = false; bool m_showProjectExplorer = true;