diff --git a/lib/libimhex/include/hex/api/workspace_manager.hpp b/lib/libimhex/include/hex/api/workspace_manager.hpp index 6657f764c..419aa1dd5 100644 --- a/lib/libimhex/include/hex/api/workspace_manager.hpp +++ b/lib/libimhex/include/hex/api/workspace_manager.hpp @@ -25,11 +25,13 @@ namespace hex { static void reset(); + static void process(); + private: WorkspaceManager() = default; static std::map s_workspaces; - static decltype(s_workspaces)::iterator s_currentWorkspace; + static decltype(s_workspaces)::iterator s_currentWorkspace, s_previousWorkspace; }; } \ No newline at end of file diff --git a/lib/libimhex/source/api/workspace_manager.cpp b/lib/libimhex/source/api/workspace_manager.cpp index 8a539280d..ed96bcc2b 100644 --- a/lib/libimhex/source/api/workspace_manager.cpp +++ b/lib/libimhex/source/api/workspace_manager.cpp @@ -6,12 +6,14 @@ #include #include -#include + +#include namespace hex { std::map WorkspaceManager::s_workspaces; - decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_currentWorkspace = WorkspaceManager::s_workspaces.end(); + decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_currentWorkspace = WorkspaceManager::s_workspaces.end(); + decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_previousWorkspace = WorkspaceManager::s_workspaces.end(); void WorkspaceManager::createWorkspace(const std::string& name, const std::string &layout) { s_workspaces[name] = Workspace { @@ -19,8 +21,6 @@ namespace hex { .path = {} }; - WorkspaceManager::switchWorkspace(name); - for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) { if (WorkspaceManager::exportToFile(path / (name + ".hexws"))) break; @@ -28,22 +28,12 @@ namespace hex { } void WorkspaceManager::switchWorkspace(const std::string& name) { - if (s_currentWorkspace != s_workspaces.end()) { - auto &[name, workspace] = *s_currentWorkspace; - workspace.layout = LayoutManager::saveToString(); + auto newWorkspace = s_workspaces.find(name); - WorkspaceManager::exportToFile(workspace.path); + if (newWorkspace != s_workspaces.end()) { + s_currentWorkspace = newWorkspace; + log::info("Switching to workspace '{}'", name); } - - auto it = s_workspaces.find(name); - if (it == s_workspaces.end()) { - log::error("Failed to switch workspace. Workspace '{}' does not exist", name); - return; - } - - auto &[newName, newWorkspace] = *it; - s_currentWorkspace = it; - LayoutManager::loadFromString(newWorkspace.layout); } void WorkspaceManager::importFromFile(const std::fs::path& path) { @@ -92,9 +82,22 @@ namespace hex { } + void WorkspaceManager::process() { + if (s_previousWorkspace != s_currentWorkspace) { + if (s_previousWorkspace != s_workspaces.end()) + WorkspaceManager::exportToFile(s_previousWorkspace->second.path); + + ImGui::LoadIniSettingsFromMemory(s_currentWorkspace->second.layout.c_str()); + + s_previousWorkspace = s_currentWorkspace; + } + } + + void WorkspaceManager::reset() { s_workspaces.clear(); - s_currentWorkspace = WorkspaceManager::s_workspaces.end(); + s_currentWorkspace = WorkspaceManager::s_workspaces.end(); + s_previousWorkspace = WorkspaceManager::s_workspaces.end(); } diff --git a/main/gui/include/window.hpp b/main/gui/include/window.hpp index 5d4ff6df8..e281fd8e6 100644 --- a/main/gui/include/window.hpp +++ b/main/gui/include/window.hpp @@ -66,8 +66,6 @@ namespace hex { bool m_frameRateTemporarilyUnlocked = false; double m_frameRateUnlockTime = 0; - bool m_anyViewsOpen = false; - ImGuiExt::ImHexCustomData m_imguiCustomData; }; diff --git a/main/gui/source/window/window.cpp b/main/gui/source/window/window.cpp index c2ad41c77..5ca64b3e7 100644 --- a/main/gui/source/window/window.cpp +++ b/main/gui/source/window/window.cpp @@ -760,8 +760,6 @@ namespace hex { void Window::frame() { auto &io = ImGui::GetIO(); - this->m_anyViewsOpen = ImHexApi::Provider::isValid(); - // Loop through all views and draw them for (auto &[name, view] : ContentRegistry::Views::impl::getEntries()) { ImGui::GetCurrentContext()->NextWindowData.ClearFlags(); @@ -854,8 +852,8 @@ namespace hex { // Process layout load requests // NOTE: This needs to be done before a new frame is started, otherwise ImGui won't handle docking correctly - if (this->m_anyViewsOpen) - LayoutManager::process(); + LayoutManager::process(); + WorkspaceManager::process(); } void Window::initGLFW() { diff --git a/plugins/builtin/source/content/welcome_screen.cpp b/plugins/builtin/source/content/welcome_screen.cpp index fc202377e..3f6842a55 100644 --- a/plugins/builtin/source/content/welcome_screen.cpp +++ b/plugins/builtin/source/content/welcome_screen.cpp @@ -528,11 +528,6 @@ namespace hex::plugin::builtin { } }); - EventProviderCreated::subscribe([](auto) { - if (!isAnyViewOpen()) - loadDefaultLayout(); - }); - EventWindowInitialized::subscribe([] { // Documentation of the value above the setting definition auto allowServerContact = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.server_contact", 2); diff --git a/plugins/builtin/source/content/workspaces.cpp b/plugins/builtin/source/content/workspaces.cpp index 242967861..cd29acfbf 100644 --- a/plugins/builtin/source/content/workspaces.cpp +++ b/plugins/builtin/source/content/workspaces.cpp @@ -1,8 +1,8 @@ #include +#include #include #include -#include namespace hex::plugin::builtin { @@ -21,7 +21,10 @@ namespace hex::plugin::builtin { } std::string currentWorkspace = ContentRegistry::Settings::read("hex.builtin.setting.general", "hex.builtin.setting.general.curr_workspace", "Default"); - WorkspaceManager::switchWorkspace(currentWorkspace); + + TaskManager::doLater([currentWorkspace] { + WorkspaceManager::switchWorkspace(currentWorkspace); + }); } }