From 2c5668af4696ea4e5f6ec6d7d72f9965ca082af6 Mon Sep 17 00:00:00 2001 From: Prateek Machiraju Date: Sat, 5 Dec 2020 21:04:02 -0500 Subject: [PATCH] [wpigui] Add platform-specific preferences save --- glass/src/app/native/cpp/main.cpp | 2 +- wpigui/src/main/native/cpp/wpigui.cpp | 21 +++++++++++++++++++ wpigui/src/main/native/include/wpigui.h | 10 +++++++++ .../src/main/native/include/wpigui_internal.h | 2 ++ 4 files changed, 34 insertions(+), 1 deletion(-) diff --git a/glass/src/app/native/cpp/main.cpp b/glass/src/app/native/cpp/main.cpp index 4a0f1b790d..44ce0c294e 100644 --- a/glass/src/app/native/cpp/main.cpp +++ b/glass/src/app/native/cpp/main.cpp @@ -91,7 +91,7 @@ int main() { gPlotProvider = std::make_unique("Plot"); gNtProvider = std::make_unique("NTProvider"); - gui::AddInit([] { ImGui::GetIO().IniFilename = "glass.ini"; }); + gui::ConfigurePlatformSaveFile("glass.ini"); gPlotProvider->GlobalInit(); gui::AddInit([] { gPlotProvider->ResetTime(); }); gNtProvider->GlobalInit(); diff --git a/wpigui/src/main/native/cpp/wpigui.cpp b/wpigui/src/main/native/cpp/wpigui.cpp index 955b3f52e7..cbaa3b84fa 100644 --- a/wpigui/src/main/native/cpp/wpigui.cpp +++ b/wpigui/src/main/native/cpp/wpigui.cpp @@ -140,6 +140,8 @@ 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(); + for (auto&& initialize : gContext->initializers) { if (initialize) initialize(); } @@ -331,6 +333,25 @@ void gui::SetStyle(Style style) { void gui::SetClearColor(ImVec4 color) { gContext->clearColor = color; } +void gui::ConfigurePlatformSaveFile(const std::string& name) { + gContext->iniPath = name; +#if defined(_MSC_VER) + const char* env = std::getenv("APPDATA"); + if (env) gContext->iniPath = env + std::string("/" + name); +#elif defined(__APPLE__) + const char* env = std::getenv("HOME"); + if (env) + gContext->iniPath = env + std::string("/Library/Preferences/" + name); +#else + const char* xdg = std::getenv("XDG_CONFIG_HOME"); + const char* env = std::getenv("HOME"); + if (xdg) + gContext->iniPath = xdg + std::string("/" + name); + else if (env) + gContext->iniPath = env + std::string("/.config/" + name); +#endif +} + void gui::EmitViewMenu() { if (ImGui::BeginMenu("View")) { if (ImGui::BeginMenu("Style")) { diff --git a/wpigui/src/main/native/include/wpigui.h b/wpigui/src/main/native/include/wpigui.h index c1dbc15dfa..8a9bb79c38 100644 --- a/wpigui/src/main/native/include/wpigui.h +++ b/wpigui/src/main/native/include/wpigui.h @@ -8,6 +8,7 @@ #pragma once #include +#include #include @@ -123,6 +124,15 @@ void SetStyle(Style style); */ void SetClearColor(ImVec4 color); +/** + * Configures a save file (.ini) in a platform specific location. On Windows, + * the .ini is saved in %APPDATA%; on macOS the .ini is saved in + * ~/Library/Preferences; on Linux the .ini is stored in $XDG_CONFIG_HOME or + * ~/.config if the former is not defined. This must be called before + * gui::Initialize(). + */ +void ConfigurePlatformSaveFile(const std::string& name); + /** * Emits a View menu (e.g. for a main menu bar) that allows setting of * style and zoom. Internally starts with ImGui::BeginMenu("View"). diff --git a/wpigui/src/main/native/include/wpigui_internal.h b/wpigui/src/main/native/include/wpigui_internal.h index 9a2c70f5c2..8cbede0bdd 100644 --- a/wpigui/src/main/native/include/wpigui_internal.h +++ b/wpigui/src/main/native/include/wpigui_internal.h @@ -56,6 +56,8 @@ struct Context : public SavedSettings { int fontScale = 2; // updated by main loop std::vector fonts; + + std::string iniPath = "imgui.ini"; }; extern Context* gContext;