Jelajahi Sumber

LibGUI: Share code for text painting in GAbstractButton.

This gives all the GAbstractButton a consistent disabled appearance.
Andreas Kling 6 tahun lalu
induk
melakukan
c62be7bb2b

+ 20 - 0
LibGUI/GAbstractButton.cpp

@@ -1,4 +1,5 @@
 #include <LibGUI/GAbstractButton.h>
+#include <LibGUI/GPainter.h>
 
 GAbstractButton::GAbstractButton(GWidget* parent)
     : GWidget(parent)
@@ -106,3 +107,22 @@ void GAbstractButton::keydown_event(GKeyEvent& event)
         click();
     GWidget::keydown_event(event);
 }
+
+void GAbstractButton::paint_text(GPainter& painter, const Rect& rect, const Font& font, TextAlignment text_alignment)
+{
+    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);
+        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));
+    }
+}

+ 5 - 0
LibGUI/GAbstractButton.h

@@ -1,6 +1,9 @@
 #pragma once
 
 #include <LibGUI/GWidget.h>
+#include <SharedGraphics/TextAlignment.h>
+
+class GPainter;
 
 class GAbstractButton : public GWidget {
 public:
@@ -35,6 +38,8 @@ protected:
     virtual void enter_event(CEvent&) override;
     virtual void leave_event(CEvent&) override;
 
+    void paint_text(GPainter&, const Rect&, const Font&, TextAlignment);
+
 private:
     String m_text;
     bool m_checked { false };

+ 2 - 14
LibGUI/GButton.cpp

@@ -48,20 +48,8 @@ void GButton::paint_event(GPaintEvent& event)
         content_rect.move_by(m_icon->width() + 4, 0);
         content_rect.set_width(content_rect.width() - m_icon->width() - 4);
     }
-    if (is_enabled()) {
-        if (!text().is_empty()) {
-            painter.draw_text(content_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(content_rect);
-                painter.draw_rect(focus_rect, Color(140, 140, 140));
-            }
-        }
-    } else {
-        painter.draw_text(content_rect.translated(1, 1), text(), font, text_alignment(), Color::White, TextElision::Right);
-        painter.draw_text(content_rect, text(), font, text_alignment(), Color::from_rgb(0x808080), TextElision::Right);
-    }
+
+    paint_text(painter, content_rect, font, text_alignment());
 }
 
 void GButton::click()

+ 1 - 8
LibGUI/GCheckBox.cpp

@@ -66,14 +66,7 @@ void GCheckBox::paint_event(GPaintEvent& event)
         painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
     }
 
-    if (!text().is_empty()) {
-        painter.draw_text(text_rect, text(), TextAlignment::TopLeft, foreground_color());
-        if (is_focused()) {
-            Rect focus_rect = text_rect;
-            focus_rect.inflate(6, 4);
-            painter.draw_rect(focus_rect, Color(140, 140, 140));
-        }
-    }
+    paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
 }
 
 void GCheckBox::click()

+ 3 - 8
LibGUI/GRadioButton.cpp

@@ -45,14 +45,9 @@ void GRadioButton::paint_event(GPaintEvent& event)
     auto& bitmap = circle_bitmap(is_checked(), is_being_pressed());
     painter.blit(circle_rect.location(), bitmap, bitmap.rect());
 
-    if (!text().is_empty()) {
-        Rect text_rect { circle_rect.right() + 4, 0, font().width(text()), font().glyph_height() };
-        text_rect.center_vertically_within(rect());
-        painter.draw_text(text_rect, text(), TextAlignment::CenterLeft, foreground_color());
-
-        if (is_focused())
-            painter.draw_rect(text_rect.inflated(6, 4), Color(140, 140, 140));
-    }
+    Rect text_rect { circle_rect.right() + 4, 0, font().width(text()), font().glyph_height() };
+    text_rect.center_vertically_within(rect());
+    paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
 }
 
 template<typename Callback>