Skip to content

Commit

Permalink
focusDockableWindow() can make the window tab visibile
Browse files Browse the repository at this point in the history
  • Loading branch information
pthom committed Sep 22, 2023
1 parent f4c26c9 commit f75f052
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/hello_imgui/docking_params.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ struct DockableWindow
_Helpers:_
* `DockableWindow * dockableWindowOfName(const std::string & name)`: returns a pointer to a dockable window
* `void focusDockableWindow(const std::string& name)`: will focus a dockable window
* `void focusDockableWindow(const std::string& name)`: will focus a dockable window (and make its tab visible if needed)
* `optional<ImGuiID> dockSpaceIdFromName(const std::string& dockSpaceName)`: may return the ImGuiID corresponding
to the dockspace with this name.
**Warning**: this will work reliably only if layoutCondition = DockingLayoutCondition::ApplicationStart. In other
Expand Down Expand Up @@ -297,7 +297,7 @@ struct DockingParams

// Helpers
DockableWindow * dockableWindowOfName(const std::string& name);
void focusDockableWindow(const std::string& windowName);
bool focusDockableWindow(const std::string& windowName);
std::optional<ImGuiID> dockSpaceIdFromName(const std::string& dockSpaceName);
};
} // namespace HelloImGui
Expand Down
2 changes: 1 addition & 1 deletion src/hello_imgui/hello_imgui_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@ _Members:_
_Helpers:_

* `DockableWindow * dockableWindowOfName(const std::string & name)`: returns a pointer to a dockable window
* `void focusDockableWindow(const std::string& name)`: will focus a dockable window
* `void focusDockableWindow(const std::string& name)`: will focus a dockable window (and make its tab visible if needed)
* `optional<ImGuiID> dockSpaceIdFromName(const std::string& dockSpaceName)`: may return the ImGuiID corresponding
to the dockspace with this name.
**Warning**: this will work reliably only if layoutCondition = DockingLayoutCondition::ApplicationStart. In other
Expand Down
33 changes: 26 additions & 7 deletions src/hello_imgui/internal/docking_details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ std::map<DockSpaceName, ImGuiID> gImGuiSplitIDs;

namespace DockingDetails
{
bool _makeImGuiWindowTabVisible(const std::string& windowName)
{
ImGuiWindow* window = ImGui::FindWindowByName(windowName.c_str());
if (window == NULL || window->DockNode == NULL || window->DockNode->TabBar == NULL)
return false;
window->DockNode->TabBar->NextSelectedTabId = window->TabId;
return true;
}

void DoSplit(const DockingSplit & dockingSplit)
{
Expand Down Expand Up @@ -190,20 +198,21 @@ void ShowViewMenu(RunnerParams & runnerParams)

void ShowDockableWindows(std::vector<DockableWindow>& dockableWindows)
{
bool wereAllDockableWindowsInited = (ImGui::GetFrameCount() > 1);

for (auto& dockableWindow: dockableWindows)
{
if (dockableWindow.focusWindowAtNextFrame)
bool shallFocusWindow = dockableWindow.focusWindowAtNextFrame && wereAllDockableWindowsInited;

if (shallFocusWindow)
dockableWindow.isVisible = true;

if (dockableWindow.isVisible)
{
if (dockableWindow.callBeginEnd)
{
if (dockableWindow.focusWindowAtNextFrame)
{
if (shallFocusWindow)
ImGui::SetNextWindowFocus();
dockableWindow.focusWindowAtNextFrame = false;
}
if (dockableWindow.windowSize.x > 0.f)
ImGui::SetNextWindowSize(dockableWindow.windowSize, dockableWindow.windowSizeCondition);
if (dockableWindow.windowPosition.x > 0.f)
Expand All @@ -216,6 +225,12 @@ void ShowDockableWindows(std::vector<DockableWindow>& dockableWindows)
if (not_collapsed && dockableWindow.GuiFunction)
dockableWindow.GuiFunction();
ImGui::End();

if (shallFocusWindow)
DockingDetails::_makeImGuiWindowTabVisible(dockableWindow.label);

if (shallFocusWindow)
dockableWindow.focusWindowAtNextFrame = false;
}
else
{
Expand Down Expand Up @@ -329,15 +344,19 @@ DockableWindow * DockingParams::dockableWindowOfName(const std::string &name)
return nullptr;
}

void DockingParams::focusDockableWindow(const std::string& windowName)
bool DockingParams::focusDockableWindow(const std::string& windowName)
{
DockableWindow * win = dockableWindowOfName(windowName);
if (win != nullptr)
{
win->focusWindowAtNextFrame = true;
return true;
}
else
fprintf(stderr, "focusDockableWindow(%s) failed, window not found!\n", windowName.c_str());
return false;
}


std::optional<ImGuiID> DockingParams::dockSpaceIdFromName(const std::string& dockSpaceName)
{
if (gImGuiSplitIDs.find(dockSpaceName) == gImGuiSplitIDs.end())
Expand Down

0 comments on commit f75f052

Please sign in to comment.