From 71eba489bc891d0cb277f09ddc9542ebd865cb14 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sun, 21 Sep 2025 20:26:06 +0100 Subject: [PATCH] Codechange: Deduplicate DrawButtonDropdown. (#14646) Use Rect methods to position components. --- src/widget.cpp | 22 +++++++++------------- 1 file changed, 9 insertions(+), 13 deletions(-) diff --git a/src/widget.cpp b/src/widget.cpp index 0c5977d174..f917901802 100644 --- a/src/widget.cpp +++ b/src/widget.cpp @@ -745,21 +745,17 @@ void DrawCaption(const Rect &r, Colours colour, Owner owner, TextColour text_col */ static inline void DrawButtonDropdown(const Rect &r, Colours colour, bool clicked_button, bool clicked_dropdown, std::string_view str, StringAlignment align) { - int dd_width = NWidgetLeaf::dropdown_dimension.width; + bool rtl = _current_text_dir == TD_RTL; - if (_current_text_dir == TD_LTR) { - DrawFrameRect(r.left, r.top, r.right - dd_width, r.bottom, colour, clicked_button ? FrameFlag::Lowered : FrameFlags{}); - DrawImageButtons(r.WithWidth(dd_width, true), WWT_DROPDOWN, colour, clicked_dropdown, SPR_ARROW_DOWN, SA_CENTER); - if (!str.empty()) { - DrawString(r.left + WidgetDimensions::scaled.dropdowntext.left, r.right - dd_width - WidgetDimensions::scaled.dropdowntext.right, CentreBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL)), str, TC_BLACK, align); - } - } else { - DrawFrameRect(r.left + dd_width, r.top, r.right, r.bottom, colour, clicked_button ? FrameFlag::Lowered : FrameFlags{}); - DrawImageButtons(r.WithWidth(dd_width, false), WWT_DROPDOWN, colour, clicked_dropdown, SPR_ARROW_DOWN, SA_CENTER); - if (!str.empty()) { - DrawString(r.left + dd_width + WidgetDimensions::scaled.dropdowntext.left, r.right - WidgetDimensions::scaled.dropdowntext.right, CentreBounds(r.top, r.bottom, GetCharacterHeight(FS_NORMAL)), str, TC_BLACK, align); - } + Rect text = r.Indent(NWidgetLeaf::dropdown_dimension.width, !rtl); + DrawFrameRect(text, colour, clicked_button ? FrameFlag::Lowered : FrameFlags{}); + if (!str.empty()) { + text = text.CentreTo(text.Width(), GetCharacterHeight(FS_NORMAL)).Shrink(WidgetDimensions::scaled.dropdowntext, RectPadding::zero); + DrawString(text, str, TC_BLACK, align); } + + Rect button = r.WithWidth(NWidgetLeaf::dropdown_dimension.width, !rtl); + DrawImageButtons(button, WWT_DROPDOWN, colour, clicked_dropdown, SPR_ARROW_DOWN, SA_CENTER); } /**