feat: Added workspaces

This commit is contained in:
WerWolv
2023-12-11 15:54:22 +01:00
parent cc4d61f8f5
commit 91230ba438
30 changed files with 273 additions and 67 deletions

View File

@@ -6,6 +6,7 @@
#include <imgui.h>
#include <hex/api/content_registry.hpp>
#include <hex/api/imhex_api.hpp>
namespace hex {
@@ -24,7 +25,7 @@ namespace hex {
s_layoutPathToLoad = path;
}
void LayoutManager::loadString(const std::string &content) {
void LayoutManager::loadFromString(const std::string &content) {
s_layoutStringToLoad = content;
}
@@ -54,6 +55,11 @@ namespace hex {
LayoutManager::reload();
}
std::string LayoutManager::saveToString() {
return ImGui::SaveIniSettingsToMemory();
}
std::vector<LayoutManager::Layout> LayoutManager::getLayouts() {
return s_layouts;
}

View File

@@ -0,0 +1,102 @@
#include <hex/api/workspace_manager.hpp>
#include <hex/api/layout_manager.hpp>
#include <hex/helpers/logger.hpp>
#include <wolv/io/file.hpp>
#include <nlohmann/json.hpp>
#include <nlohmann/json_fwd.hpp>
namespace hex {
std::map<std::string, WorkspaceManager::Workspace> WorkspaceManager::s_workspaces;
decltype(WorkspaceManager::s_workspaces)::iterator WorkspaceManager::s_currentWorkspace = WorkspaceManager::s_workspaces.end();
void WorkspaceManager::createWorkspace(const std::string& name, const std::string &layout) {
s_workspaces[name] = Workspace {
.layout = layout.empty() ? LayoutManager::saveToString() : layout,
.path = {}
};
WorkspaceManager::switchWorkspace(name);
for (const auto &path : fs::getDefaultPaths(fs::ImHexPath::Workspaces)) {
if (WorkspaceManager::exportToFile(path / (name + ".hexws")))
break;
}
}
void WorkspaceManager::switchWorkspace(const std::string& name) {
if (s_currentWorkspace != s_workspaces.end()) {
auto &[name, workspace] = *s_currentWorkspace;
workspace.layout = LayoutManager::saveToString();
WorkspaceManager::exportToFile(workspace.path);
}
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) {
wolv::io::File file(path, wolv::io::File::Mode::Read);
if (!file.isValid()) {
log::error("Failed to load workspace from file '{}'", path.string());
return;
}
auto content = file.readString();
try {
auto json = nlohmann::json::parse(content.begin(), content.end());
std::string name = json["name"];
std::string layout = json["layout"];
s_workspaces[name] = Workspace {
.layout = std::move(layout),
.path = path
};
} catch (nlohmann::json::exception &e) {
log::error("Failed to load workspace from file '{}': {}", path.string(), e.what());
}
}
bool WorkspaceManager::exportToFile(std::fs::path path) {
if (path.empty()) {
if (s_currentWorkspace == s_workspaces.end())
return false;
path = s_currentWorkspace->second.path;
}
wolv::io::File file(path, wolv::io::File::Mode::Create);
if (!file.isValid())
return false;
nlohmann::json json;
json["name"] = s_currentWorkspace->first;
json["layout"] = LayoutManager::saveToString();
file.writeString(json.dump(4));
return true;
}
void WorkspaceManager::reset() {
s_workspaces.clear();
s_currentWorkspace = {};
}
}

View File

@@ -423,6 +423,9 @@ namespace hex::fs {
case ImHexPath::Layouts:
result = appendPath(getDataPaths(), "layouts");
break;
case ImHexPath::Workspaces:
result = appendPath(getDataPaths(), "workspaces");
break;
}
// Remove all paths that don't exist if requested