[wpigui] Add platform-specific preferences save

This commit is contained in:
Prateek Machiraju
2020-12-05 21:04:02 -05:00
committed by Peter Johnson
parent 751dea32ae
commit 2c5668af46
4 changed files with 34 additions and 1 deletions

View File

@@ -91,7 +91,7 @@ int main() {
gPlotProvider = std::make_unique<glass::PlotProvider>("Plot");
gNtProvider = std::make_unique<glass::NetworkTablesProvider>("NTProvider");
gui::AddInit([] { ImGui::GetIO().IniFilename = "glass.ini"; });
gui::ConfigurePlatformSaveFile("glass.ini");
gPlotProvider->GlobalInit();
gui::AddInit([] { gPlotProvider->ResetTime(); });
gNtProvider->GlobalInit();

View File

@@ -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")) {

View File

@@ -8,6 +8,7 @@
#pragma once
#include <functional>
#include <string>
#include <imgui.h>
@@ -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").

View File

@@ -56,6 +56,8 @@ struct Context : public SavedSettings {
int fontScale = 2; // updated by main loop
std::vector<Font> fonts;
std::string iniPath = "imgui.ini";
};
extern Context* gContext;