Skip to content

Commit

Permalink
AddDockableWindow: add param forceDockspace / fix docking issues
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Sep 19, 2024
1 parent a54cc5f commit cb89a2e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 16 deletions.
5 changes: 3 additions & 2 deletions src/hello_imgui/doc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
5 changes: 3 additions & 2 deletions src/hello_imgui/hello_imgui.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
32 changes: 21 additions & 11 deletions src/hello_imgui/internal/docking_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -686,14 +686,15 @@ namespace AddDockableWindowHelper
{
DockableWindow dockableWindow;
DockableWindowAdditionState state = DockableWindowAdditionState::Waiting;
bool forceDockspace;
};

std::vector<DockableWindowWaitingForAddition> gDockableWindowsToAdd;
std::vector<std::string> 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()
Expand All @@ -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;
}
}
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit cb89a2e

Please sign in to comment.