Internals: Settings: Added FindWindowSettingsByName() (replacing old version by ID) FindWindowSettingsByWindow() + remove unnecessary FindOrCreateWindowSettings().

In 1 code path we do a redundant hash but this happens only once per window per session so we are ok.
This commit is contained in:
ocornut 2023-02-10 15:33:33 +01:00
parent 99c0bd65df
commit 0b865136e3
2 changed files with 22 additions and 14 deletions

View File

@ -5376,7 +5376,7 @@ static ImGuiWindow* CreateNewWindow(const char* name, ImGuiWindowFlags flags)
// User can disable loading and saving of settings. Tooltip and child windows also don't store settings.
if (!(flags & ImGuiWindowFlags_NoSavedSettings))
if (ImGuiWindowSettings* settings = ImGui::FindWindowSettings(window->ID))
if (ImGuiWindowSettings* settings = ImGui::FindWindowSettingsByWindow(window))
{
// Retrieve settings from .ini file
window->SettingsOffset = g.SettingsWindows.offset_from_ptr(settings);
@ -12453,8 +12453,8 @@ void ImGui::LogButtons()
// - UpdateSettings() [Internal]
// - MarkIniSettingsDirty() [Internal]
// - CreateNewWindowSettings() [Internal]
// - FindWindowSettings() [Internal]
// - FindOrCreateWindowSettings() [Internal]
// - FindWindowSettingsByName() [Internal]
// - FindWindowSettingsByWindow() [Internal]
// - FindSettingsHandler() [Internal]
// - ClearIniSettings() [Internal]
// - LoadIniSettingsFromDisk()
@ -12529,20 +12529,24 @@ ImGuiWindowSettings* ImGui::CreateNewWindowSettings(const char* name)
return settings;
}
ImGuiWindowSettings* ImGui::FindWindowSettings(ImGuiID id)
// This is called once per window .ini entry + once per newly instanciated window.
ImGuiWindowSettings* ImGui::FindWindowSettingsByName(const char* name)
{
ImGuiContext& g = *GImGui;
ImGuiID id = ImHashStr(name);
for (ImGuiWindowSettings* settings = g.SettingsWindows.begin(); settings != NULL; settings = g.SettingsWindows.next_chunk(settings))
if (settings->ID == id)
return settings;
return NULL;
}
ImGuiWindowSettings* ImGui::FindOrCreateWindowSettings(const char* name)
// This is faster if you are holding on a Window already as we don't need to perform a search.
ImGuiWindowSettings* ImGui::FindWindowSettingsByWindow(ImGuiWindow* window)
{
if (ImGuiWindowSettings* settings = FindWindowSettings(ImHashStr(name)))
return settings;
return CreateNewWindowSettings(name);
ImGuiContext& g = *GImGui;
if (window->SettingsOffset != -1)
return g.SettingsWindows.ptr_from_offset(window->SettingsOffset);
return FindWindowSettingsByName(window->Name); // Actual search executed once, so at this point we don't mind the redundant hashing.
}
void ImGui::AddSettingsHandler(const ImGuiSettingsHandler* handler)
@ -12569,6 +12573,7 @@ ImGuiSettingsHandler* ImGui::FindSettingsHandler(const char* type_name)
return NULL;
}
// Clear all settings (windows, tables, docking etc.)
void ImGui::ClearIniSettings()
{
ImGuiContext& g = *GImGui;
@ -12703,9 +12708,12 @@ static void WindowSettingsHandler_ClearAll(ImGuiContext* ctx, ImGuiSettingsHandl
static void* WindowSettingsHandler_ReadOpen(ImGuiContext*, ImGuiSettingsHandler*, const char* name)
{
ImGuiWindowSettings* settings = ImGui::FindOrCreateWindowSettings(name);
ImGuiID id = settings->ID;
*settings = ImGuiWindowSettings(); // Clear existing if recycling previous entry
ImGuiID id = ImHashStr(name);
ImGuiWindowSettings* settings = ImGui::FindWindowSettingsByName(name);
if (settings)
*settings = ImGuiWindowSettings(); // Clear existing if recycling previous entry
else
settings = ImGui::CreateNewWindowSettings(name);
settings->ID = id;
settings->WantApply = true;
return (void*)settings;
@ -12745,7 +12753,7 @@ static void WindowSettingsHandler_WriteAll(ImGuiContext* ctx, ImGuiSettingsHandl
if (window->Flags & ImGuiWindowFlags_NoSavedSettings)
continue;
ImGuiWindowSettings* settings = (window->SettingsOffset != -1) ? g.SettingsWindows.ptr_from_offset(window->SettingsOffset) : ImGui::FindWindowSettings(window->ID);
ImGuiWindowSettings* settings = ImGui::FindWindowSettingsByWindow(window);
if (!settings)
{
settings = ImGui::CreateNewWindowSettings(window->Name);

View File

@ -2764,8 +2764,8 @@ namespace ImGui
IMGUI_API void MarkIniSettingsDirty(ImGuiWindow* window);
IMGUI_API void ClearIniSettings();
IMGUI_API ImGuiWindowSettings* CreateNewWindowSettings(const char* name);
IMGUI_API ImGuiWindowSettings* FindWindowSettings(ImGuiID id);
IMGUI_API ImGuiWindowSettings* FindOrCreateWindowSettings(const char* name);
IMGUI_API ImGuiWindowSettings* FindWindowSettingsByName(const char* name);
IMGUI_API ImGuiWindowSettings* FindWindowSettingsByWindow(ImGuiWindow* window);
IMGUI_API void AddSettingsHandler(const ImGuiSettingsHandler* handler);
IMGUI_API void RemoveSettingsHandler(const char* type_name);
IMGUI_API ImGuiSettingsHandler* FindSettingsHandler(const char* type_name);