Read the settings file once, keeping Dear ImGui's section as it parses
This commit is contained in:
1 parent
91cc11aa3b
commit
4b17e9b328
3 files changed
+8
-53
No files matched your search
+2
-2
@@ -158,8 +158,8 @@ static bool window_open(App& app)
|
||||
io.KeyRepeatRate = settings.keyboardRepeatRate;
|
||||
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
||||
ImGui::GetStyle().FontScaleMain = percent_to_unit(settings.uiScale);
|
||||
if (auto imguiData = Settings::imgui_data_load(settings_path_get()); !imguiData.empty())
|
||||
ImGui::LoadIniSettingsFromMemory(imguiData.c_str(), imguiData.size());
|
||||
if (!settings.imguiData.empty())
|
||||
ImGui::LoadIniSettingsFromMemory(settings.imguiData.c_str(), settings.imguiData.size());
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+4
-49
@@ -252,8 +252,12 @@ Settings::Settings(const std::filesystem::path& path)
|
||||
{
|
||||
if (line == "[Settings]" || line.empty())
|
||||
continue;
|
||||
// The rest of the file is Dear ImGui's own settings (window layout).
|
||||
if (line == "# Dear ImGui")
|
||||
{
|
||||
std::getline(file, imguiData, '\0');
|
||||
break;
|
||||
}
|
||||
|
||||
auto eq = line.find('=');
|
||||
if (eq == std::string::npos)
|
||||
@@ -278,55 +282,6 @@ Settings::Settings(const std::filesystem::path& path)
|
||||
*scale = std::clamp(*scale < min ? unit_to_percent(*scale) : *scale, min, max);
|
||||
}
|
||||
|
||||
std::string Settings::imgui_data_load(const std::filesystem::path& path)
|
||||
{
|
||||
auto pathUtf8 = path_to_utf8(path);
|
||||
std::string fileData;
|
||||
if (!file_text_load(path, &fileData, "rb"))
|
||||
{
|
||||
logger.error(
|
||||
std::format("Failed to open settings file for Dear ImGui data: {}; using Dear ImGui defaults", pathUtf8));
|
||||
return {};
|
||||
}
|
||||
std::istringstream file(fileData);
|
||||
|
||||
std::string line;
|
||||
std::ostringstream dataStream;
|
||||
bool isImGuiSection = false;
|
||||
bool isContent = false;
|
||||
|
||||
while (std::getline(file, line))
|
||||
{
|
||||
if (!isImGuiSection)
|
||||
{
|
||||
if (line == "# Dear ImGui")
|
||||
isImGuiSection = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isContent)
|
||||
dataStream << "\n";
|
||||
dataStream << line;
|
||||
isContent = true;
|
||||
}
|
||||
|
||||
if (!isImGuiSection)
|
||||
{
|
||||
logger.warning(
|
||||
std::format("Dear ImGui section missing from settings file: {}; using Dear ImGui defaults", pathUtf8));
|
||||
return {};
|
||||
}
|
||||
|
||||
if (!isContent)
|
||||
{
|
||||
logger.warning(
|
||||
std::format("Dear ImGui section empty in settings file: {}; using Dear ImGui defaults", pathUtf8));
|
||||
return {};
|
||||
}
|
||||
|
||||
return dataStream.str();
|
||||
}
|
||||
|
||||
void Settings::save(const std::filesystem::path& path, const std::string& imguiData)
|
||||
{
|
||||
auto pathUtf8 = path_to_utf8(path);
|
||||
|
||||
+2
-2
@@ -321,13 +321,13 @@ public:
|
||||
SETTINGS_MEMBERS SETTINGS_SHORTCUTS SETTINGS_WINDOWS
|
||||
#undef X
|
||||
|
||||
bool isDefault = false;
|
||||
std::string imguiData;
|
||||
bool isDefault = false;
|
||||
|
||||
Settings() = default;
|
||||
|
||||
Settings(const std::filesystem::path&);
|
||||
void save(const std::filesystem::path&, const std::string&);
|
||||
static std::string imgui_data_load(const std::filesystem::path&);
|
||||
};
|
||||
|
||||
constexpr StringType SHORTCUT_STRING_TYPES[] = {
|
||||
|
||||
Reference in new issue
Block a user