From eda8048a982f88611710bcded1d2245e9d42d95e Mon Sep 17 00:00:00 2001 From: Kawe Mazidjatari <48657826+Mauler125@users.noreply.github.com> Date: Thu, 29 Feb 2024 16:02:18 +0100 Subject: [PATCH] ImGui:: correctly calculate item width Fixed bug where changing the server browser causes the alignment of the buttons with the right side border to mess up. --- src/gameui/IBrowser.cpp | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/gameui/IBrowser.cpp b/src/gameui/IBrowser.cpp index b8db7498..f44dffd4 100644 --- a/src/gameui/IBrowser.cpp +++ b/src/gameui/IBrowser.cpp @@ -305,8 +305,15 @@ void CBrowser::BrowserPanel(void) ImGui::Separator(); - const ImVec2 contentRegionMax = ImGui::GetContentRegionAvail(); - ImGui::PushItemWidth(contentRegionMax.x / 4); + const ImVec2 regionAvail = ImGui::GetContentRegionAvail(); + const ImGuiStyle& style = ImGui::GetStyle(); + + // 4 elements means 3 spacings between items, this has to be subtracted + // from the remaining available region to get correct results on all + // window padding values!!! + const float itemWidth = (regionAvail.x - (3 * style.ItemSpacing.x)) / 4; + + ImGui::PushItemWidth(itemWidth); { ImGui::InputTextWithHint("##ServerBrowser_ServerCon", "Server address and port", m_szServerAddressBuffer, IM_ARRAYSIZE(m_szServerAddressBuffer)); @@ -314,7 +321,7 @@ void CBrowser::BrowserPanel(void) ImGui::InputTextWithHint("##ServerBrowser_ServerKey", "Encryption key", m_szServerEncKeyBuffer, IM_ARRAYSIZE(m_szServerEncKeyBuffer)); ImGui::SameLine(); - if (ImGui::Button("Connect", ImVec2(contentRegionMax.x / 4.3f, ImGui::GetFrameHeight()))) + if (ImGui::Button("Connect", ImVec2(itemWidth, ImGui::GetFrameHeight()))) { if (m_szServerAddressBuffer[0]) { @@ -323,12 +330,14 @@ void CBrowser::BrowserPanel(void) } ImGui::SameLine(); - if (ImGui::Button("Private servers", ImVec2(contentRegionMax.x / 4.3f, ImGui::GetFrameHeight()))) + if (ImGui::Button("Private servers", ImVec2(itemWidth, ImGui::GetFrameHeight()))) { ImGui::OpenPopup("Private Server"); } + HiddenServersModal(); } + ImGui::PopItemWidth(); }