diff --git a/src/hello_imgui/doc_api.md b/src/hello_imgui/doc_api.md index d54b3e6e..dff2cc8a 100644 --- a/src/hello_imgui/doc_api.md +++ b/src/hello_imgui/doc_api.md @@ -326,8 +326,9 @@ void SwitchLayout(const std::string& layoutName); std::string CurrentLayoutName(); // `AddDockableWindow()`: will add a dockable window to the current layout. -// Will dock the window to the dockspace it belongs to. -void AddDockableWindow(const DockableWindow& dockableWindow); +// Will dock the window to the dockspace it belongs to if forceDockspace is true, +// otherwise will dock it to the last space it was docked to (using saved settings) +void AddDockableWindow(const DockableWindow& dockableWindow, bool forceDockspace = false); // `RemoveDockableWindow()`: will remove a dockable window from the current layout. // (dockableWindowName is the label of the window, as provided in the DockableWindow struct) diff --git a/src/hello_imgui/hello_imgui.h b/src/hello_imgui/hello_imgui.h index 0871d7dc..826545ba 100644 --- a/src/hello_imgui/hello_imgui.h +++ b/src/hello_imgui/hello_imgui.h @@ -140,8 +140,9 @@ void SwitchLayout(const std::string& layoutName); std::string CurrentLayoutName(); // `AddDockableWindow()`: will add a dockable window to the current layout. -// Will dock the window to the dockspace it belongs to. -void AddDockableWindow(const DockableWindow& dockableWindow); +// Will dock the window to the dockspace it belongs to if forceDockspace is true, +// otherwise will dock it to the last space it was docked to (using saved settings) +void AddDockableWindow(const DockableWindow& dockableWindow, bool forceDockspace = false); // `RemoveDockableWindow()`: will remove a dockable window from the current layout. // (dockableWindowName is the label of the window, as provided in the DockableWindow struct) diff --git a/src/hello_imgui/internal/docking_details.cpp b/src/hello_imgui/internal/docking_details.cpp index 0a477763..b6f57e7a 100644 --- a/src/hello_imgui/internal/docking_details.cpp +++ b/src/hello_imgui/internal/docking_details.cpp @@ -686,14 +686,15 @@ namespace AddDockableWindowHelper { DockableWindow dockableWindow; DockableWindowAdditionState state = DockableWindowAdditionState::Waiting; + bool forceDockspace; }; std::vector gDockableWindowsToAdd; std::vector gDockableWindowsToRemove; - void AddDockableWindow(const DockableWindow& dockableWindow) + void AddDockableWindow(const DockableWindow& dockableWindow, bool forceDockspace) { - gDockableWindowsToAdd.push_back({dockableWindow, DockableWindowAdditionState::Waiting}); + gDockableWindowsToAdd.push_back({dockableWindow, DockableWindowAdditionState::Waiting, forceDockspace}); } void Callback_1_GuiRender() @@ -702,16 +703,25 @@ namespace AddDockableWindowHelper { if (dockableWindow.state == DockableWindowAdditionState::Waiting) { - auto dockId = HelloImGui::GetRunnerParams()->dockingParams.dockSpaceIdFromName(dockableWindow.dockableWindow.dockSpaceName); - if (dockId.has_value()) + bool doesWindowHavePreviousSetting; { - ImGui::Begin(dockableWindow.dockableWindow.label.c_str()); - ImGui::Dummy(ImVec2(10, 10)); - ImGui::End(); + ImGuiID window_id = ImHashStr(dockableWindow.dockableWindow.label.c_str()); + ImGuiWindowSettings* previousWindowSettings = ImGui::FindWindowSettingsByID(window_id); + doesWindowHavePreviousSetting = (previousWindowSettings != nullptr); + } + if (!doesWindowHavePreviousSetting || dockableWindow.forceDockspace) + { + auto dockId = HelloImGui::GetRunnerParams()->dockingParams.dockSpaceIdFromName(dockableWindow.dockableWindow.dockSpaceName); + if (dockId.has_value()) + { + ImGui::Begin(dockableWindow.dockableWindow.label.c_str()); + ImGui::Dummy(ImVec2(10, 10)); + ImGui::End(); - ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), 21423345); - //ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), dockId.value()); + ImGui::DockBuilderDockWindow(dockableWindow.dockableWindow.label.c_str(), dockId.value()); + } } + dockableWindow.state = DockableWindowAdditionState::AddedAsDummyToImGui; } } @@ -762,9 +772,9 @@ namespace AddDockableWindowHelper } // namespace AddDockableWindowHelper -void AddDockableWindow(const DockableWindow& dockableWindow) +void AddDockableWindow(const DockableWindow& dockableWindow, bool forceDockspace) { - AddDockableWindowHelper::AddDockableWindow(dockableWindow); + AddDockableWindowHelper::AddDockableWindow(dockableWindow, forceDockspace); } void RemoveDockableWindow(const std::string& dockableWindowName) diff --git a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp index 0c9302e3..d1410fe1 100644 --- a/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp +++ b/src/hello_imgui_demos/hello_imgui_demodocking/hello_imgui_demodocking.main.cpp @@ -197,7 +197,11 @@ void DemoShowAdditionalWindow(AppState& appState) additionalWindow.rememberIsVisible = false; // its visibility is not saved in the settings file, additionalWindow.dockSpaceName = "MiscSpace"; // when shown, it will appear in MiscSpace. additionalWindow.GuiFunction = [] { ImGui::Text("This is the additional window"); }; - HelloImGui::AddDockableWindow(additionalWindow); + HelloImGui::AddDockableWindow( + additionalWindow, + false // forceDockspace=false: means that the window will be docked to the last space it was docked to + // i.e. dockSpaceName is ignored if the user previously moved the window to another space + ); } ImGui::SetItemTooltip("By clicking this button, you can show an additional window");