Button to close a window
| 898 | |
| 899 | // Button to close a window |
| 900 | bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) |
| 901 | { |
| 902 | ImGuiContext& g = *GImGui; |
| 903 | ImGuiWindow* window = g.CurrentWindow; |
| 904 | |
| 905 | // 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) |
| 906 | // 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? |
| 907 | const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize)); |
| 908 | ImRect bb_interact = bb; |
| 909 | const float area_to_visible_ratio = window->OuterRectClipped.GetArea() / bb.GetArea(); |
| 910 | if (area_to_visible_ratio < 1.5f) |
| 911 | bb_interact.Expand(ImTrunc(bb_interact.GetSize() * -0.25f)); |
| 912 | |
| 913 | // Tweak 2: We intentionally allow interaction when clipped so that a mechanical Alt,Right,Activate sequence can always close a window. |
| 914 | // (this isn't the common behavior of buttons, but it doesn't affect the user because navigation tends to keep items visible in scrolling layer). |
| 915 | bool is_clipped = !ItemAdd(bb_interact, id); |
| 916 | |
| 917 | bool hovered, held; |
| 918 | bool pressed = ButtonBehavior(bb_interact, id, &hovered, &held); |
| 919 | if (is_clipped) |
| 920 | return pressed; |
| 921 | |
| 922 | // Render |
| 923 | ImU32 bg_col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); |
| 924 | if (hovered) |
| 925 | window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); |
| 926 | RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact); |
| 927 | const ImU32 cross_col = GetColorU32(ImGuiCol_Text); |
| 928 | const ImVec2 cross_center = bb.GetCenter() - ImVec2(0.5f, 0.5f); |
| 929 | const float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; |
| 930 | const float cross_thickness = 1.0f * (float)(int)g.Style._MainScale; // FIXME-DPI |
| 931 | window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, +cross_extent), cross_center + ImVec2(-cross_extent, -cross_extent), cross_col, cross_thickness); |
| 932 | window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, -cross_extent), cross_center + ImVec2(-cross_extent, +cross_extent), cross_col, cross_thickness); |
| 933 | |
| 934 | return pressed; |
| 935 | } |
| 936 | |
| 937 | // The Collapse button also functions as a Dock Menu button. |
| 938 | bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos, ImGuiDockNode* dock_node) |