feat: Allow layouts to be locked

This commit is contained in:
WerWolv
2023-12-06 13:49:58 +01:00
parent ba8430d9e7
commit 370ca740e3
5 changed files with 77 additions and 9 deletions

View File

@@ -1,11 +1,11 @@
#include <hex/api/layout_manager.hpp>
#include <hex/helpers/fs.hpp>
#include <hex/helpers/logger.hpp>
#include <wolv/utils/string.hpp>
#include <imgui.h>
#include <fmt/format.h>
#include <hex/api/content_registry.hpp>
namespace hex {
@@ -15,6 +15,8 @@ namespace hex {
std::optional<std::string> s_layoutStringToLoad;
std::vector<LayoutManager::Layout> s_layouts;
bool s_layoutLocked = false;
}
@@ -32,8 +34,22 @@ namespace hex {
std::transform(fileName.begin(), fileName.end(), fileName.begin(), tolower);
fileName += ".hexlyt";
const auto path = hex::fs::getDefaultPaths(fs::ImHexPath::Layouts).front() / fileName;
ImGui::SaveIniSettingsToDisk(wolv::util::toUTF8String(path).c_str());
std::fs::path layoutPath;
for (const auto &path : hex::fs::getDefaultPaths(fs::ImHexPath::Layouts)) {
if (!hex::fs::isPathWritable(layoutPath))
continue;
layoutPath = path / fileName;
}
if (layoutPath.empty()) {
log::error("Failed to save layout '{}'. No writable path found", name);
return;
}
const auto pathString = wolv::util::toUTF8String(layoutPath);
ImGui::SaveIniSettingsToDisk(pathString.c_str());
log::info("Layout '{}' saved to {}", name, pathString);
LayoutManager::reload();
}
@@ -44,13 +60,16 @@ namespace hex {
void LayoutManager::process() {
if (s_layoutPathToLoad.has_value()) {
ImGui::LoadIniSettingsFromDisk(wolv::util::toUTF8String(*s_layoutPathToLoad).c_str());
const auto pathString = wolv::util::toUTF8String(*s_layoutPathToLoad);
ImGui::LoadIniSettingsFromDisk(pathString.c_str());
s_layoutPathToLoad = std::nullopt;
log::info("Loaded layout from {}", pathString);
}
if (s_layoutStringToLoad.has_value()) {
ImGui::LoadIniSettingsFromMemory(s_layoutStringToLoad->c_str());
s_layoutStringToLoad = std::nullopt;
log::info("Loaded layout from string");
}
}
@@ -85,4 +104,13 @@ namespace hex {
s_layouts.clear();
}
}
bool LayoutManager::isLayoutLocked() {
return s_layoutLocked;
}
void LayoutManager::lockLayout(bool locked) {
log::info("Layout {}", locked ? "locked" : "unlocked");
s_layoutLocked = locked;
}
}