Window: Shrink close button hit-testing region when it covers an abnormally high portion of the window visible area (e.g. when window is collapsed and moved in a corner)to facilitate moving the window away. (#3825)
diff --git a/docs/CHANGELOG.txt b/docs/CHANGELOG.txt
index 7a06baf..432976a 100644
--- a/docs/CHANGELOG.txt
+++ b/docs/CHANGELOG.txt
@@ -45,6 +45,8 @@
- DragScalar: Fixed crash when using DragScalar() directly (not via common wrapper like DragFloat() etc.)
with ImGuiSliderFlags_AlwaysClamp + only one of either p_min or p_max set. (#3824) [@harry75369]
+- Window: Shrink close button hit-testing region when it covers an abnormally high portion of the window visible
+ area (e.g. when window is collapsed + moved in a corner) to facilitate moving the window away. (#3825)
- ImDrawList: AddCircle, AddCircleFilled(): Tweaked default segment count calculation to honor MaxError
with more accuracy. Made default segment count always even for better looking result. (#3808) [@thedmd]
- ImDrawList: AddCircle, AddCircleFilled(): New default for style.
diff --git a/imgui_internal.h b/imgui_internal.h
index e85230d..b9e9164 100644
--- a/imgui_internal.h
+++ b/imgui_internal.h
@@ -448,6 +448,7 @@
ImVec2 GetSize() const { return ImVec2(Max.x - Min.x, Max.y - Min.y); }
float GetWidth() const { return Max.x - Min.x; }
float GetHeight() const { return Max.y - Min.y; }
+ float GetArea() const { return (Max.x - Min.x) * (Max.y - Min.y); }
ImVec2 GetTL() const { return Min; } // Top-left
ImVec2 GetTR() const { return ImVec2(Max.x, Min.y); } // Top-right
ImVec2 GetBL() const { return ImVec2(Min.x, Max.y); } // Bottom-left
diff --git a/imgui_widgets.cpp b/imgui_widgets.cpp
index 5a9ca75..c3c45c3 100644
--- a/imgui_widgets.cpp
+++ b/imgui_widgets.cpp
@@ -782,22 +782,30 @@
}
// Button to close a window
-bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)//, float size)
+bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos)
{
ImGuiContext& g = *GImGui;
ImGuiWindow* window = g.CurrentWindow;
- // We intentionally allow interaction when clipped so that a mechanical Alt,Right,Validate sequence close a window.
- // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible).
+ // Tweak 1: Shrink hit-testing area if button covers an abnormally large proportion of the visible region. That's in order to facilitate moving the window away. (#3825)
+ // This may better be applied as a general hit-rect reduction mechanism for all widgets to ensure the area to move window is always accessible?
const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize) + g.Style.FramePadding * 2.0f);
- bool is_clipped = !ItemAdd(bb, id);
+ ImRect bb_interact = bb;
+ const float area_to_visible_ratio = window->OuterRectClipped.GetArea() / bb.GetArea();
+ if (area_to_visible_ratio < 1.5f)
+ bb_interact.Expand(ImFloor(bb_interact.GetSize() * -0.25f));
+
+ // Tweak 2: We intentionally allow interaction when clipped so that a mechanical Alt,Right,Activate sequence can always close a window.
+ // (this isn't the regular behavior of buttons, but it doesn't affect the user much because navigation tends to keep items visible).
+ bool is_clipped = !ItemAdd(bb_interact, id);
bool hovered, held;
- bool pressed = ButtonBehavior(bb, id, &hovered, &held);
+ bool pressed = ButtonBehavior(bb_interact, id, &hovered, &held);
if (is_clipped)
return pressed;
// Render
+ // FIXME: Clarify this mess
ImU32 col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered);
ImVec2 center = bb.GetCenter();
if (hovered)