mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-21 01:01:43 +00:00
[wpigui] Add hooks for custom load/save settings
Add GetPlatformSaveFileDir().
This commit is contained in:
@@ -146,7 +146,12 @@ bool gui::Initialize(const char* title, int width, int height) {
|
||||
iniHandler.WriteAllFn = IniWriteAll;
|
||||
ImGui::GetCurrentContext()->SettingsHandlers.push_back(iniHandler);
|
||||
|
||||
io.IniFilename = gContext->iniPath.c_str();
|
||||
if (gContext->loadSettings) {
|
||||
gContext->loadSettings();
|
||||
io.IniFilename = nullptr;
|
||||
} else {
|
||||
io.IniFilename = gContext->iniPath.c_str();
|
||||
}
|
||||
|
||||
for (auto&& initialize : gContext->initializers) {
|
||||
if (initialize) {
|
||||
@@ -155,7 +160,11 @@ bool gui::Initialize(const char* title, int width, int height) {
|
||||
}
|
||||
|
||||
// Load INI file
|
||||
ImGui::LoadIniSettingsFromDisk(io.IniFilename);
|
||||
if (gContext->loadIniSettings) {
|
||||
gContext->loadIniSettings();
|
||||
} else if (io.IniFilename) {
|
||||
ImGui::LoadIniSettingsFromDisk(io.IniFilename);
|
||||
}
|
||||
|
||||
// Set initial window settings
|
||||
glfwWindowHint(GLFW_MAXIMIZED, gContext->maximized ? GLFW_TRUE : GLFW_FALSE);
|
||||
@@ -283,6 +292,20 @@ void gui::Main() {
|
||||
// Poll and handle events (inputs, window resize, etc.)
|
||||
glfwPollEvents();
|
||||
PlatformRenderFrame();
|
||||
|
||||
// custom saving
|
||||
if (gContext->saveSettings) {
|
||||
auto& io = ImGui::GetIO();
|
||||
if (io.WantSaveIniSettings) {
|
||||
gContext->saveSettings(false);
|
||||
io.WantSaveIniSettings = false; // reset flag
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save (if custom save)
|
||||
if (gContext->saveSettings) {
|
||||
gContext->saveSettings(true);
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
@@ -292,7 +315,7 @@ void gui::Main() {
|
||||
ImGui::DestroyContext();
|
||||
|
||||
// Delete the save file if requested.
|
||||
if (gContext->resetOnExit) {
|
||||
if (!gContext->saveSettings && gContext->resetOnExit) {
|
||||
fs::remove(fs::path{gContext->iniPath});
|
||||
}
|
||||
|
||||
@@ -367,6 +390,14 @@ void gui::AddLateExecute(std::function<void()> execute) {
|
||||
}
|
||||
}
|
||||
|
||||
void gui::ConfigureCustomSaveSettings(std::function<void()> load,
|
||||
std::function<void()> loadIni,
|
||||
std::function<void(bool)> save) {
|
||||
gContext->loadSettings = load;
|
||||
gContext->loadIniSettings = loadIni;
|
||||
gContext->saveSettings = save;
|
||||
}
|
||||
|
||||
GLFWwindow* gui::GetSystemWindow() {
|
||||
return gContext->window;
|
||||
}
|
||||
@@ -416,27 +447,31 @@ void gui::SetClearColor(ImVec4 color) {
|
||||
gContext->clearColor = color;
|
||||
}
|
||||
|
||||
void gui::ConfigurePlatformSaveFile(const std::string& name) {
|
||||
gContext->iniPath = name;
|
||||
std::string gui::GetPlatformSaveFileDir() {
|
||||
#if defined(_MSC_VER)
|
||||
const char* env = std::getenv("APPDATA");
|
||||
if (env) {
|
||||
gContext->iniPath = env + std::string("/" + name);
|
||||
return env + std::string("/");
|
||||
}
|
||||
#elif defined(__APPLE__)
|
||||
const char* env = std::getenv("HOME");
|
||||
if (env) {
|
||||
gContext->iniPath = env + std::string("/Library/Preferences/" + name);
|
||||
return env + std::string("/Library/Preferences/");
|
||||
}
|
||||
#else
|
||||
const char* xdg = std::getenv("XDG_CONFIG_HOME");
|
||||
const char* env = std::getenv("HOME");
|
||||
if (xdg) {
|
||||
gContext->iniPath = xdg + std::string("/" + name);
|
||||
return xdg + std::string("/");
|
||||
} else if (env) {
|
||||
gContext->iniPath = env + std::string("/.config/" + name);
|
||||
return env + std::string("/.config/");
|
||||
}
|
||||
#endif
|
||||
return "";
|
||||
}
|
||||
|
||||
void gui::ConfigurePlatformSaveFile(const std::string& name) {
|
||||
gContext->iniPath = GetPlatformSaveFileDir() + name;
|
||||
}
|
||||
|
||||
void gui::EmitViewMenu() {
|
||||
@@ -473,7 +508,9 @@ void gui::EmitViewMenu() {
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
|
||||
ImGui::MenuItem("Reset UI on Exit?", nullptr, &gContext->resetOnExit);
|
||||
if (!gContext->saveSettings) {
|
||||
ImGui::MenuItem("Reset UI on Exit?", nullptr, &gContext->resetOnExit);
|
||||
}
|
||||
ImGui::EndMenu();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user