Skip to content

Commit ec6fb8d

Browse files
committed
Impl idea
1 parent c97f2ac commit ec6fb8d

File tree

2 files changed

+45
-8
lines changed

2 files changed

+45
-8
lines changed

examples/example_win32_directx9/main.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,30 @@ void CleanupDeviceD3D();
2525
void ResetDevice();
2626
LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
2727

28+
bool close_button_no_focus = false;
29+
30+
static void example() {
31+
static bool b = true, c = true;
32+
const ImGuiWindowFlags flags = ImGuiWindowFlags_AlwaysAutoResize | ImGuiWindowFlags_NoSavedSettings;
33+
if (ImGui::Begin("Window A", 0, flags)) {
34+
ImGui::Checkbox("Close button: don't focus window", &close_button_no_focus);
35+
ImGui::Checkbox("Window B", &b);
36+
ImGui::Checkbox("Window C", &c);
37+
38+
}
39+
ImGui::End();
40+
if (b) {
41+
ImGui::Begin("Window B", &b, flags);
42+
ImGui::Text("--- Window B ---");
43+
ImGui::End();
44+
}
45+
if (c) {
46+
ImGui::Begin("Window C", &c, flags);
47+
ImGui::Text("--- Window C ---");
48+
ImGui::End();
49+
}
50+
}
51+
2852
// Main code
2953
int main(int, char**)
3054
{
@@ -154,6 +178,8 @@ int main(int, char**)
154178
ImGui::End();
155179
}
156180

181+
example();
182+
157183
// 3. Show another simple window.
158184
if (show_another_window)
159185
{

imgui_widgets.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,10 @@ void ImGui::BulletTextV(const char* fmt, va_list args)
498498
// with same ID and different MouseButton (see #8030). You can fix it by:
499499
// (1) switching to use a single ButtonBehavior() with multiple _MouseButton flags.
500500
// or (2) surrounding those calls with PushItemFlag(ImGuiItemFlags_AllowDuplicateId, true); ... PopItemFlag()
501+
502+
extern bool close_button_no_focus;
503+
static bool button_behavior_no_focus = false;
504+
501505
bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool* out_held, ImGuiButtonFlags flags)
502506
{
503507
ImGuiContext& g = *GImGui;
@@ -541,7 +545,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
541545
{
542546
pressed = true;
543547
g.DragDropHoldJustPressedId = id;
544-
FocusWindow(window);
548+
/*if(!button_behavior_no_focus) */FocusWindow(window);
545549
}
546550
}
547551

@@ -580,12 +584,13 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
580584
g.ActiveIdMouseButton = mouse_button_clicked;
581585
if (!(flags & ImGuiButtonFlags_NoNavFocus))
582586
{
583-
SetFocusID(id, window);
584-
FocusWindow(window);
587+
// (Code path used by CloseButton)
588+
if (!button_behavior_no_focus) SetFocusID(id, window);
589+
if (!button_behavior_no_focus) FocusWindow(window);
585590
}
586591
else
587592
{
588-
FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
593+
/*if (!button_behavior_no_focus)*/ FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
589594
}
590595
}
591596
if ((flags & ImGuiButtonFlags_PressedOnClick) || ((flags & ImGuiButtonFlags_PressedOnDoubleClick) && g.IO.MouseClickedCount[mouse_button_clicked] == 2))
@@ -598,12 +603,12 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
598603
g.ActiveIdMouseButton = mouse_button_clicked;
599604
if (!(flags & ImGuiButtonFlags_NoNavFocus))
600605
{
601-
SetFocusID(id, window);
602-
FocusWindow(window);
606+
/*if (!button_behavior_no_focus)*/ SetFocusID(id, window);
607+
/*if (!button_behavior_no_focus)*/ FocusWindow(window);
603608
}
604609
else
605610
{
606-
FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
611+
/*if (!button_behavior_no_focus)*/ FocusWindow(window, ImGuiFocusRequestFlags_RestoreFocusedChild); // Still need to focus and bring to front, but try to avoid losing NavId when navigating a child
607612
}
608613
}
609614
}
@@ -615,7 +620,7 @@ bool ImGui::ButtonBehavior(const ImRect& bb, ImGuiID id, bool* out_hovered, bool
615620
if (!has_repeated_at_least_once)
616621
pressed = true;
617622
if (!(flags & ImGuiButtonFlags_NoNavFocus))
618-
SetFocusID(id, window); // FIXME: Lack of FocusWindow() call here is inconsistent with other paths. Research why.
623+
/*if (!button_behavior_no_focus)*/ SetFocusID(id, window); // FIXME: Lack of FocusWindow() call here is inconsistent with other paths. Research why.
619624
ClearActiveID();
620625
}
621626
}
@@ -858,7 +863,13 @@ bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)
858863
bool is_clipped = !ItemAdd(bb_interact, id);
859864

860865
bool hovered, held;
866+
if (close_button_no_focus) {
867+
button_behavior_no_focus = true;
868+
}
861869
bool pressed = ButtonBehavior(bb_interact, id, &hovered, &held);
870+
if (close_button_no_focus) {
871+
button_behavior_no_focus = false;
872+
}
862873
if (is_clipped)
863874
return pressed;
864875

0 commit comments

Comments
 (0)