Skip to content

Commit 14f75cc

Browse files
committed
Fixed search bar rendering overflow (Fixes #773)
1 parent bd6ba46 commit 14f75cc

2 files changed

Lines changed: 23 additions & 26 deletions

File tree

Sources/OvUI/include/OvUI/Widgets/InputFields/InputText.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ namespace OvUI::Widgets::InputFields
3535
bool focusOnNextDraw = false;
3636
bool fullWidth = false;
3737
uint32_t iconTextureID = 0;
38-
float iconSize = 16.f;
3938
OvTools::Eventing::Event<std::string> ContentChangedEvent;
4039
OvTools::Eventing::Event<std::string> EnterPressedEvent;
4140
};

Sources/OvUI/src/OvUI/Widgets/InputFields/InputText.cpp

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,45 +17,43 @@ void OvUI::Widgets::InputFields::InputText::_Draw_Impl()
1717
{
1818
std::string previousContent = content;
1919

20-
if (focusOnNextDraw)
21-
{
22-
ImGui::SetKeyboardFocusHere();
23-
focusOnNextDraw = false;
24-
}
20+
bool needFocus = focusOnNextDraw;
21+
focusOnNextDraw = false;
2522

2623
const bool hasIcon = iconTextureID != 0;
24+
const float buttonSize = ImGui::GetFrameHeight();
25+
const float innerSize = buttonSize - 2.0f * ImGui::GetStyle().FramePadding.x;
2726

2827
if (hasIcon)
2928
{
30-
const ImVec2 padding = ImGui::GetStyle().FramePadding;
31-
ImGui::PushStyleVar(ImGuiStyleVar_FramePadding, ImVec2(padding.x + iconSize + padding.x, padding.y));
29+
const ImVec4 frameBg = ImGui::GetStyleColorVec4(ImGuiCol_FrameBg);
30+
ImGui::PushStyleColor(ImGuiCol_Button, frameBg);
31+
ImGui::PushStyleColor(ImGuiCol_ButtonHovered, frameBg);
32+
ImGui::PushStyleColor(ImGuiCol_ButtonActive, frameBg);
33+
const bool iconClicked = ImGui::ImageButton(
34+
("icon" + m_widgetID).c_str(),
35+
(ImTextureID)(uintptr_t)iconTextureID,
36+
ImVec2(innerSize, innerSize),
37+
ImVec2(0.f, 1.f), ImVec2(1.f, 0.f)
38+
);
39+
ImGui::PopStyleColor(3);
40+
41+
if (iconClicked)
42+
needFocus = true;
43+
44+
ImGui::SameLine(0, 0);
3245
}
3346

3447
if (fullWidth)
3548
ImGui::SetNextItemWidth(-FLT_MIN);
3649

50+
if (needFocus)
51+
ImGui::SetKeyboardFocusHere(0);
52+
3753
content.resize(256, '\0');
3854
bool enterPressed = ImGui::InputText((label + m_widgetID).c_str(), &content[0], 256, ImGuiInputTextFlags_EnterReturnsTrue | (selectAllOnClick ? ImGuiInputTextFlags_AutoSelectAll : 0));
3955
content = content.c_str();
4056

41-
if (hasIcon)
42-
{
43-
ImGui::PopStyleVar();
44-
45-
ImDrawList* drawList = ImGui::GetWindowDrawList();
46-
const ImVec2 rectMin = ImGui::GetItemRectMin();
47-
const ImVec2 rectMax = ImGui::GetItemRectMax();
48-
const float frameHeight = rectMax.y - rectMin.y;
49-
const float padding = ImGui::GetStyle().FramePadding.y;
50-
const float fitSize = std::min(iconSize, frameHeight - padding * 2.0f);
51-
const float centerY = (rectMin.y + rectMax.y) * 0.5f;
52-
const float iconX = rectMin.x + ImGui::GetStyle().FramePadding.x;
53-
drawList->AddImage(iconTextureID,
54-
ImVec2(iconX, centerY - fitSize * 0.5f),
55-
ImVec2(iconX + fitSize, centerY + fitSize * 0.5f),
56-
ImVec2(0.f, 1.f), ImVec2(1.f, 0.f));
57-
}
58-
5957
if (content != previousContent)
6058
{
6159
ContentChangedEvent.Invoke(content);

0 commit comments

Comments
 (0)