Skip to content

Commit 08b72eb

Browse files
committed
IsWindowFocused() refactor will flags. (#1382)
Marked IsRootWindowFocused() as obsolete in favor of using IsWindowFocused(ImGuiFocusedFlags_RootWindow). Marked IsRootWindowOrAnyChildFocused() as obsolete in favor of using IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows).
1 parent 8d8f493 commit 08b72eb

File tree

3 files changed

+49
-22
lines changed

3 files changed

+49
-22
lines changed

imgui.cpp

+14-15
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,8 @@
213213
Here is a change-log of API breaking changes, if you are using one of the functions listed, expect to have to fix some code.
214214
Also read releases logs https://github.com/ocornut/imgui/releases for more details.
215215

216+
- 2017/12/23 (1.53) - marked IsRootWindowFocused() as obsolete in favor of using IsWindowFocused(ImGuiFocusedFlags_RootWindow).
217+
- marked IsRootWindowOrAnyChildFocused() as obsolete in favor of using IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows).
216218
- 2017/12/12 (1.53) - renamed ImGuiTreeNodeFlags_AllowOverlapMode to ImGuiTreeNodeFlags_AllowItemOverlap. Kept redirection enum (will obsolete).
217219
- 2017/12/10 (1.53) - removed SetNextWindowContentWidth(), prefer using SetNextWindowContentSize(). Kept redirection function (will obsolete).
218220
- 2017/11/27 (1.53) - renamed ImGuiTextBuffer::append() helper to appendf(), appendv() to appendfv(). If you copied the 'Log' demo in your code, it uses appendv() so that needs to be renamed.
@@ -5479,25 +5481,22 @@ bool ImGui::IsWindowHovered(ImGuiHoveredFlags flags)
54795481
return true;
54805482
}
54815483

5482-
bool ImGui::IsWindowFocused()
5484+
bool ImGui::IsWindowFocused(ImGuiFocusedFlags flags)
54835485
{
54845486
ImGuiContext& g = *GImGui;
54855487
IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End()
5486-
return g.NavWindow == g.CurrentWindow;
5487-
}
54885488

5489-
bool ImGui::IsRootWindowFocused()
5490-
{
5491-
ImGuiContext& g = *GImGui;
5492-
IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End()
5493-
return g.NavWindow == g.CurrentWindow->RootWindow;
5494-
}
5495-
5496-
bool ImGui::IsRootWindowOrAnyChildFocused()
5497-
{
5498-
ImGuiContext& g = *GImGui;
5499-
IM_ASSERT(g.CurrentWindow); // Not inside a Begin()/End()
5500-
return g.NavWindow && g.NavWindow->RootWindow == g.CurrentWindow->RootWindow;
5489+
switch (flags & (ImGuiFocusedFlags_RootWindow | ImGuiHoveredFlags_ChildWindows))
5490+
{
5491+
case ImGuiFocusedFlags_RootWindow | ImGuiHoveredFlags_ChildWindows:
5492+
return g.NavWindow && g.CurrentWindow->RootWindow == g.NavWindow->RootWindow;
5493+
case ImGuiFocusedFlags_RootWindow:
5494+
return g.CurrentWindow->RootWindow == g.NavWindow;
5495+
case ImGuiHoveredFlags_ChildWindows:
5496+
return g.NavWindow && IsWindowChildOf(g.NavWindow, g.CurrentWindow);
5497+
default:
5498+
return g.CurrentWindow == g.NavWindow;
5499+
}
55015500
}
55025501

55035502
float ImGui::GetWindowWidth()

imgui.h

+14-5
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ typedef int ImDrawCornerFlags; // flags: for ImDrawList::AddRect*() etc.
8080
typedef int ImGuiColorEditFlags; // flags: for ColorEdit*(), ColorPicker*() // enum ImGuiColorEditFlags_
8181
typedef int ImGuiColumnsFlags; // flags: for *Columns*() // enum ImGuiColumnsFlags_
8282
typedef int ImGuiComboFlags; // flags: for BeginCombo() // enum ImGuiComboFlags_
83-
typedef int ImGuiHoveredFlags; // flags: for IsItemHovered() // enum ImGuiHoveredFlags_
83+
typedef int ImGuiFocusedFlags; // flags: for IsWindowFocused() // enum ImGuiFocusedFlags_
84+
typedef int ImGuiHoveredFlags; // flags: for IsItemHovered() etc. // enum ImGuiHoveredFlags_
8485
typedef int ImGuiInputTextFlags; // flags: for InputText*() // enum ImGuiInputTextFlags_
8586
typedef int ImGuiSelectableFlags; // flags: for Selectable() // enum ImGuiSelectableFlags_
8687
typedef int ImGuiTreeNodeFlags; // flags: for TreeNode*(),CollapsingHeader()// enum ImGuiTreeNodeFlags_
@@ -442,10 +443,8 @@ namespace ImGui
442443
IMGUI_API ImVec2 GetItemRectMax(); // "
443444
IMGUI_API ImVec2 GetItemRectSize(); // "
444445
IMGUI_API void SetItemAllowOverlap(); // allow last item to be overlapped by a subsequent item. sometimes useful with invisible buttons, selectables, etc. to catch unused area.
445-
IMGUI_API bool IsWindowFocused(); // is current Begin()-ed window focused?
446-
IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags = 0); // is current Begin()-ed window hovered (and typically: not blocked by a popup/modal)?
447-
IMGUI_API bool IsRootWindowFocused(); // is current Begin()-ed root window focused (root = top-most parent of a child, otherwise self)?
448-
IMGUI_API bool IsRootWindowOrAnyChildFocused(); // is current Begin()-ed root window or any of its child (including current window) focused?
446+
IMGUI_API bool IsWindowFocused(ImGuiFocusedFlags flags = 0); // is current window focused? or its root/child, depending on flags. see flags for options.
447+
IMGUI_API bool IsWindowHovered(ImGuiHoveredFlags flags = 0); // is current window hovered (and typically: not blocked by a popup/modal)? see flags for options.
449448
IMGUI_API bool IsAnyWindowHovered(); // is mouse hovering any visible window
450449
IMGUI_API bool IsRectVisible(const ImVec2& size); // test if rectangle (of given size, starting from cursor position) is visible / not clipped.
451450
IMGUI_API bool IsRectVisible(const ImVec2& rect_min, const ImVec2& rect_max); // test if rectangle (in screen space) is visible / not clipped. to perform coarse clipping on user's side.
@@ -599,6 +598,14 @@ enum ImGuiComboFlags_
599598
ImGuiComboFlags_HeightMask_ = ImGuiComboFlags_HeightSmall | ImGuiComboFlags_HeightRegular | ImGuiComboFlags_HeightLarge | ImGuiComboFlags_HeightLargest
600599
};
601600

601+
// Flags for ImGui::IsWindowFocused()
602+
enum ImGuiFocusedFlags_
603+
{
604+
ImGuiFocusedFlags_ChildWindows = 1 << 0, // IsWindowFocused(): Return true if any children of the window is focused
605+
ImGuiFocusedFlags_RootWindow = 1 << 1, // IsWindowFocused(): Test from root window (top most parent of the current hierarchy)
606+
ImGuiFocusedFlags_RootAndChildWindows = ImGuiFocusedFlags_RootWindow | ImGuiFocusedFlags_ChildWindows,
607+
};
608+
602609
// Flags for ImGui::IsItemHovered(), ImGui::IsWindowHovered()
603610
enum ImGuiHoveredFlags_
604611
{
@@ -938,6 +945,8 @@ struct ImGuiIO
938945
#ifndef IMGUI_DISABLE_OBSOLETE_FUNCTIONS
939946
namespace ImGui
940947
{
948+
static inline bool IsRootWindowFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootWindow); } // OBSOLETE 1.53+
949+
static inline bool IsRootWindowOrAnyChildFocused() { return IsWindowFocused(ImGuiFocusedFlags_RootAndChildWindows); } // OBSOLETE 1.53+
941950
static inline void SetNextWindowContentWidth(float width) { SetNextWindowContentSize(ImVec2(width, 0.0f)); } // OBSOLETE 1.53+ (nb: original version preserved last Y value set by SetNextWindowContentSize())
942951
static inline bool IsRootWindowOrAnyChildHovered(ImGuiHoveredFlags flags = 0) { return IsItemHovered(flags | ImGuiHoveredFlags_RootWindow | ImGuiHoveredFlags_ChildWindows); } // OBSOLETE 1.53+ use flags directly
943952
bool Begin(const char* name, bool* p_open, const ImVec2& size_on_first_use, float bg_alpha_override = -1.0f, ImGuiWindowFlags flags = 0); // OBSOLETE 1.52+. use SetNextWindowSize() instead if you want to set a window size.

imgui_demo.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -1781,8 +1781,24 @@ void ImGui::ShowTestWindow(bool* p_open)
17811781
ImGui::TreePop();
17821782
}
17831783

1784-
if (ImGui::TreeNode("Hovering"))
1784+
if (ImGui::TreeNode("Focused & Hovered Test"))
17851785
{
1786+
static bool embed_all_inside_a_child_window = false;
1787+
ImGui::Checkbox("Embed everything inside a child window (for additional testing)", &embed_all_inside_a_child_window);
1788+
if (embed_all_inside_a_child_window)
1789+
ImGui::BeginChild("embeddingchild", ImVec2(0, ImGui::GetFontSize() * 25), true);
1790+
1791+
// Testing IsWindowFocused() function with its various flags (note that the flags can be combined)
1792+
ImGui::BulletText(
1793+
"IsWindowFocused() = %d\n"
1794+
"IsWindowFocused(_ChildWindows) = %d\n"
1795+
"IsWindowFocused(_ChildWindows|_RootWindow) = %d\n"
1796+
"IsWindowFocused(_RootWindow) = %d\n",
1797+
ImGui::IsWindowFocused(),
1798+
ImGui::IsWindowFocused(ImGuiHoveredFlags_ChildWindows),
1799+
ImGui::IsWindowFocused(ImGuiHoveredFlags_ChildWindows | ImGuiHoveredFlags_RootWindow),
1800+
ImGui::IsWindowFocused(ImGuiHoveredFlags_RootWindow));
1801+
17861802
// Testing IsWindowHovered() function with its various flags (note that the flags can be combined)
17871803
ImGui::BulletText(
17881804
"IsWindowHovered() = %d\n"
@@ -1813,9 +1829,12 @@ void ImGui::ShowTestWindow(bool* p_open)
18131829
ImGui::IsItemHovered(ImGuiHoveredFlags_RectOnly));
18141830

18151831
ImGui::BeginChild("child", ImVec2(0,50), true);
1816-
ImGui::Text("This is a child window for testing IsWindowHovered() flags.");
1832+
ImGui::Text("This is another child window for testing IsWindowHovered() flags.");
18171833
ImGui::EndChild();
18181834

1835+
if (embed_all_inside_a_child_window)
1836+
EndChild();
1837+
18191838
ImGui::TreePop();
18201839
}
18211840

0 commit comments

Comments
 (0)