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.KeyRepeatRate = settings.keyboardRepeatRate;
|
||||||
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
io.ConfigWindowsMoveFromTitleBarOnly = true;
|
||||||
ImGui::GetStyle().FontScaleMain = percent_to_unit(settings.uiScale);
|
ImGui::GetStyle().FontScaleMain = percent_to_unit(settings.uiScale);
|
||||||
if (auto imguiData = Settings::imgui_data_load(settings_path_get()); !imguiData.empty())
|
if (!settings.imguiData.empty())
|
||||||
ImGui::LoadIniSettingsFromMemory(imguiData.c_str(), imguiData.size());
|
ImGui::LoadIniSettingsFromMemory(settings.imguiData.c_str(), settings.imguiData.size());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+4
-49
@@ -252,8 +252,12 @@ Settings::Settings(const std::filesystem::path& path)
|
|||||||
{
|
{
|
||||||
if (line == "[Settings]" || line.empty())
|
if (line == "[Settings]" || line.empty())
|
||||||
continue;
|
continue;
|
||||||
|
// The rest of the file is Dear ImGui's own settings (window layout).
|
||||||
if (line == "# Dear ImGui")
|
if (line == "# Dear ImGui")
|
||||||
|
{
|
||||||
|
std::getline(file, imguiData, '\0');
|
||||||
break;
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
auto eq = line.find('=');
|
auto eq = line.find('=');
|
||||||
if (eq == std::string::npos)
|
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);
|
*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)
|
void Settings::save(const std::filesystem::path& path, const std::string& imguiData)
|
||||||
{
|
{
|
||||||
auto pathUtf8 = path_to_utf8(path);
|
auto pathUtf8 = path_to_utf8(path);
|
||||||
|
|||||||
+2
-2
@@ -321,13 +321,13 @@ public:
|
|||||||
SETTINGS_MEMBERS SETTINGS_SHORTCUTS SETTINGS_WINDOWS
|
SETTINGS_MEMBERS SETTINGS_SHORTCUTS SETTINGS_WINDOWS
|
||||||
#undef X
|
#undef X
|
||||||
|
|
||||||
bool isDefault = false;
|
std::string imguiData;
|
||||||
|
bool isDefault = false;
|
||||||
|
|
||||||
Settings() = default;
|
Settings() = default;
|
||||||
|
|
||||||
Settings(const std::filesystem::path&);
|
Settings(const std::filesystem::path&);
|
||||||
void save(const std::filesystem::path&, const std::string&);
|
void save(const std::filesystem::path&, const std::string&);
|
||||||
static std::string imgui_data_load(const std::filesystem::path&);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
constexpr StringType SHORTCUT_STRING_TYPES[] = {
|
constexpr StringType SHORTCUT_STRING_TYPES[] = {
|
||||||
|
|||||||
Reference in new issue
Block a user