1
0
mirror of synced 2025-02-17 18:59:21 +01:00

fix: Workspaces not being deletable correctly in all cases

This commit is contained in:
WerWolv 2024-05-30 16:56:39 +02:00
parent 89eee104ab
commit 63f66662ce
2 changed files with 35 additions and 22 deletions

View File

@ -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");
}
}

View File

@ -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);
}