Button to close a window
| 903 | |
| 904 | // Button to close a window |
| 905 | bool ImGui::CloseButton(ImGuiID id, const ImVec2& pos) |
| 906 | { |
| 907 | ImGuiContext& g = *GImGui; |
| 908 | ImGuiWindow* window = g.CurrentWindow; |
| 909 | |
| 910 | // 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) |
| 911 | // 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? |
| 912 | const ImRect bb(pos, pos + ImVec2(g.FontSize, g.FontSize)); |
| 913 | ImRect bb_interact = bb; |
| 914 | const float area_to_visible_ratio = window->OuterRectClipped.GetArea() / bb.GetArea(); |
| 915 | if (area_to_visible_ratio < 1.5f) |
| 916 | bb_interact.Expand(ImTrunc(bb_interact.GetSize() * -0.25f)); |
| 917 | |
| 918 | // Tweak 2: We intentionally allow interaction when clipped so that a mechanical Alt,Right,Activate sequence can always close a window. |
| 919 | // (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). |
| 920 | bool is_clipped = !ItemAdd(bb_interact, id); |
| 921 | |
| 922 | bool hovered, held; |
| 923 | bool pressed = ButtonBehavior(bb_interact, id, &hovered, &held); |
| 924 | if (is_clipped) |
| 925 | return pressed; |
| 926 | |
| 927 | // Render |
| 928 | ImU32 bg_col = GetColorU32(held ? ImGuiCol_ButtonActive : ImGuiCol_ButtonHovered); |
| 929 | if (hovered) |
| 930 | window->DrawList->AddRectFilled(bb.Min, bb.Max, bg_col); |
| 931 | RenderNavCursor(bb, id, ImGuiNavRenderCursorFlags_Compact); |
| 932 | const ImU32 cross_col = GetColorU32(ImGuiCol_Text); |
| 933 | const ImVec2 cross_center = bb.GetCenter() - ImVec2(0.5f, 0.5f); |
| 934 | const float cross_extent = g.FontSize * 0.5f * 0.7071f - 1.0f; |
| 935 | const float cross_thickness = 1.0f * (float)(int)g.Style._MainScale; // FIXME-DPI |
| 936 | window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, +cross_extent), cross_center + ImVec2(-cross_extent, -cross_extent), cross_col, cross_thickness); |
| 937 | window->DrawList->AddLine(cross_center + ImVec2(+cross_extent, -cross_extent), cross_center + ImVec2(-cross_extent, +cross_extent), cross_col, cross_thickness); |
| 938 | |
| 939 | return pressed; |
| 940 | } |
| 941 | |
| 942 | bool ImGui::CollapseButton(ImGuiID id, const ImVec2& pos) |
| 943 | { |