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.
This commit is contained in:
Kawe Mazidjatari 2024-02-29 16:02:18 +01:00
parent 26b99359d6
commit ead3634a01

View File

@ -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();
}