From 553e8b58a5139435c66c943c6414db696ea42306 Mon Sep 17 00:00:00 2001 From: Zachary Hall Date: Wed, 15 Jan 2025 12:27:41 -0800 Subject: [PATCH] Use menu items for titlebar buttons, so the titlebar looks better. --- backends/ui/imgui/RendererBackend.cpp | 33 +++++++++++++++++++-------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/backends/ui/imgui/RendererBackend.cpp b/backends/ui/imgui/RendererBackend.cpp index 881525d..4063f8a 100644 --- a/backends/ui/imgui/RendererBackend.cpp +++ b/backends/ui/imgui/RendererBackend.cpp @@ -498,21 +498,34 @@ void RendererBackend::EndMainMenuBar() { int btn_count = 3; ImVec2 btn_size(btnw, btnw); ImGui::SetCursorPosY(0); - ImGui::SetCursorPosX(size.x - (btnw * 3)); - if (ImGui::Button(ICON_FK_WINDOW_MINIMIZE, btn_size)) { + static const size_t titlebar_btn_count = 3; + const char *titlebar_icons[titlebar_btn_count] = { + ICON_FK_WINDOW_MINIMIZE, + is_maximized() ? ICON_FK_WINDOW_RESTORE : ICON_FK_WINDOW_MAXIMIZE, + ICON_FK_WINDOW_CLOSE + }; + float tmp = size.x; + float padding = ImGui::GetStyle().FramePadding.x; + float spacing = ImGui::GetStyle().ItemSpacing.x; + for (size_t i = 0; i < titlebar_btn_count; i++) { + tmp += padding; + // No need to use the correct index, as long as this is hit only once. + if (i == 0) tmp += spacing / 2; + else tmp += spacing; + tmp += ImGui::CalcTextSize(titlebar_icons[i]).x; + } + title_btn_start = std::ceil(tmp); + ImGui::SetCursorPosX(title_btn_start); + if (ImGui::MenuItem(titlebar_icons[0])) { SDL_MinimizeWindow(window); } - ImGui::SetCursorPosX(size.x - (btnw * 2)); - if (is_maximized()) { - if (ImGui::Button(ICON_FK_WINDOW_RESTORE, btn_size)) SDL_RestoreWindow(window); - } else { - if (ImGui::Button(ICON_FK_WINDOW_MAXIMIZE, btn_size)) SDL_MaximizeWindow(window); + if (ImGui::MenuItem(titlebar_icons[1])) { + if (is_maximized()) SDL_RestoreWindow(window); + else SDL_MaximizeWindow(window); } - ImGui::SetCursorPosX(size.x - btnw); - if (ImGui::Button(ICON_FK_WINDOW_CLOSE, btn_size)) { + if (ImGui::MenuItem(titlebar_icons[2])) { done = true; } - title_btn_start = size.x - (btnw * 3); #endif ImGui::EndMainMenuBar(); }