From 63f66662ce758afd770a65dcad7cf87e3dc0882a Mon Sep 17 00:00:00 2001 From: WerWolv Date: Thu, 30 May 2024 16:56:39 +0200 Subject: [PATCH] fix: Workspaces not being deletable correctly in all cases --- lib/libimhex/source/api/layout_manager.cpp | 18 ++++----- lib/libimhex/source/api/workspace_manager.cpp | 39 +++++++++++++------ 2 files changed, 35 insertions(+), 22 deletions(-) diff --git a/lib/libimhex/source/api/layout_manager.cpp b/lib/libimhex/source/api/layout_manager.cpp index 0844cc143..761ac4d0a 100644 --- a/lib/libimhex/source/api/layout_manager.cpp +++ b/lib/libimhex/source/api/layout_manager.cpp @@ -71,15 +71,15 @@ namespace hex { void LayoutManager::removeLayout(const std::string& name) { for (const auto &layout : *s_layouts) { if (layout.name == name) { - if (wolv::io::File(layout.path, wolv::io::File::Mode::Write).remove()) { + if (wolv::io::fs::remove(layout.path)) { log::info("Removed layout '{}'", name); - LayoutManager::reload(); } else { log::error("Failed to remove layout '{}'", name); } - return; } } + + LayoutManager::reload(); } @@ -90,21 +90,19 @@ namespace hex { void LayoutManager::process() { if (s_layoutPathToLoad->has_value()) { - const auto pathString = wolv::util::toUTF8String(**s_layoutPathToLoad); - LayoutManager::closeAllViews(); - ImGui::LoadIniSettingsFromDisk(pathString.c_str()); - s_layoutPathToLoad = std::nullopt; - log::info("Loaded layout from {}", pathString); + wolv::io::File file(**s_layoutPathToLoad, wolv::io::File::Mode::Read); + s_layoutStringToLoad = file.readString(); + s_layoutPathToLoad->reset(); } if (s_layoutStringToLoad->has_value()) { LayoutManager::closeAllViews(); ImGui::LoadIniSettingsFromMemory((*s_layoutStringToLoad)->c_str()); - s_layoutStringToLoad = std::nullopt; - log::info("Loaded layout from string"); + s_layoutStringToLoad->reset(); + log::info("Loaded new Layout"); } } diff --git a/lib/libimhex/source/api/workspace_manager.cpp b/lib/libimhex/source/api/workspace_manager.cpp index 849778689..c7fa465f6 100644 --- a/lib/libimhex/source/api/workspace_manager.cpp +++ b/lib/libimhex/source/api/workspace_manager.cpp @@ -43,8 +43,9 @@ namespace hex { } void WorkspaceManager::importFromFile(const std::fs::path& path) { - if (std::ranges::any_of(*s_workspaces, [path](const auto &pair) { return pair.second.path == path; })) + if (std::ranges::any_of(*s_workspaces, [path](const auto &pair) { return pair.second.path == path; })) { return; + } wolv::io::File file(path, wolv::io::File::Mode::Read); if (!file.isValid()) { @@ -74,19 +75,23 @@ namespace hex { bool WorkspaceManager::exportToFile(std::fs::path path, std::string workspaceName, bool builtin) { if (path.empty()) { - if (s_currentWorkspace == s_workspaces->end()) + if (s_currentWorkspace == s_workspaces->end()) { return false; + } path = s_currentWorkspace->second.path; } - if (workspaceName.empty()) + if (workspaceName.empty()) { workspaceName = s_currentWorkspace->first; + } + wolv::io::File file(path, wolv::io::File::Mode::Create); - if (!file.isValid()) + if (!file.isValid()) { return false; + } nlohmann::json json; json["name"] = workspaceName; @@ -99,28 +104,36 @@ namespace hex { } void WorkspaceManager::removeWorkspace(const std::string& name) { + bool deletedCurrentWorkspace = false; for (const auto &[workspaceName, workspace] : *s_workspaces) { if (workspaceName == name) { - log::info("{}", wolv::util::toUTF8String(workspace.path)); - if (wolv::io::File(workspace.path, wolv::io::File::Mode::Write).remove()) { + log::info("Removing workspace file '{}'", wolv::util::toUTF8String(workspace.path)); + if (wolv::io::fs::remove(workspace.path)) { log::info("Removed workspace '{}'", name); - switchWorkspace(s_workspaces->begin()->first); - s_workspaces->erase(workspaceName); + if (workspaceName == s_currentWorkspace->first) { + deletedCurrentWorkspace = true; + } } else { log::error("Failed to remove workspace '{}'", name); } - return; } } + + WorkspaceManager::reload(); + + if (deletedCurrentWorkspace && !s_workspaces->empty()) { + s_currentWorkspace = s_workspaces->begin(); + } } void WorkspaceManager::process() { if (s_previousWorkspace != s_currentWorkspace) { log::info("Updating workspace"); - if (s_previousWorkspace != s_workspaces->end()) + if (s_previousWorkspace != s_workspaces->end()) { exportToFile(s_previousWorkspace->second.path, s_previousWorkspace->first, s_previousWorkspace->second.builtin); + } LayoutManager::closeAllViews(); ImGui::LoadIniSettingsFromMemory(s_currentWorkspace->second.layout.c_str()); @@ -146,12 +159,14 @@ namespace hex { for (const auto &defaultPath : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) { for (const auto &entry : std::fs::directory_iterator(defaultPath)) { - if (!entry.is_regular_file()) + if (!entry.is_regular_file()) { continue; + } const auto &path = entry.path(); - if (path.extension() != ".hexws") + if (path.extension() != ".hexws") { continue; + } WorkspaceManager::importFromFile(path); }