From 817ac9c22bbcc0813409ce8d3f05fcb8b035b898 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 25 May 2019 04:01:22 +0200 Subject: [PATCH] LibGUI: Elide the text in GCheckBox and GRadioButton when low on space. --- LibGUI/GAbstractButton.cpp | 16 +++++++--------- LibGUI/GButton.cpp | 6 +++++- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/LibGUI/GAbstractButton.cpp b/LibGUI/GAbstractButton.cpp index 0e18a8b12c..d9f2b21c60 100644 --- a/LibGUI/GAbstractButton.cpp +++ b/LibGUI/GAbstractButton.cpp @@ -110,19 +110,17 @@ void GAbstractButton::keydown_event(GKeyEvent& event) void GAbstractButton::paint_text(GPainter& painter, const Rect& rect, const Font& font, TextAlignment text_alignment) { + auto clipped_rect = rect.intersected(this->rect()); + if (!is_enabled()) { - painter.draw_text(rect.translated(1, 1), text(), font, text_alignment, Color::White, TextElision::Right); - painter.draw_text(rect, text(), font, text_alignment, Color::from_rgb(0x808080), TextElision::Right); + painter.draw_text(clipped_rect.translated(1, 1), text(), font, text_alignment, Color::White, TextElision::Right); + painter.draw_text(clipped_rect, text(), font, text_alignment, Color::from_rgb(0x808080), TextElision::Right); return; } if (text().is_empty()) return; - painter.draw_text(rect, text(), font, text_alignment, foreground_color(), TextElision::Right); - if (is_focused()) { - Rect focus_rect = { 0, 0, font.width(text()), font.glyph_height() }; - focus_rect.inflate(6, 4); - focus_rect.center_within(rect); - painter.draw_rect(focus_rect, Color(140, 140, 140)); - } + painter.draw_text(clipped_rect, text(), font, text_alignment, foreground_color(), TextElision::Right); + if (is_focused()) + painter.draw_rect(clipped_rect.inflated(6, 4), Color(140, 140, 140)); } diff --git a/LibGUI/GButton.cpp b/LibGUI/GButton.cpp index a00ed10dc0..7cafa76f1a 100644 --- a/LibGUI/GButton.cpp +++ b/LibGUI/GButton.cpp @@ -49,7 +49,11 @@ void GButton::paint_event(GPaintEvent& event) content_rect.set_width(content_rect.width() - m_icon->width() - 4); } - paint_text(painter, content_rect, font, text_alignment()); + Rect text_rect { 0, 0, font.width(text()), font.glyph_height() }; + if (text_rect.width() > content_rect.width()) + text_rect.set_width(content_rect.width()); + text_rect.center_within(content_rect); + paint_text(painter, text_rect, font, text_alignment()); } void GButton::click()