1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 19:57:35 +00:00

LibGUI: Share code for text painting in GAbstractButton.

This gives all the GAbstractButton a consistent disabled appearance.
This commit is contained in:
Andreas Kling 2019-05-24 22:54:37 +02:00
parent 149b7f92a7
commit c62be7bb2b
5 changed files with 31 additions and 30 deletions

View file

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

View file

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

View file

@ -48,20 +48,8 @@ void GButton::paint_event(GPaintEvent& event)
content_rect.move_by(m_icon->width() + 4, 0); content_rect.move_by(m_icon->width() + 4, 0);
content_rect.set_width(content_rect.width() - m_icon->width() - 4); content_rect.set_width(content_rect.width() - m_icon->width() - 4);
} }
if (is_enabled()) {
if (!text().is_empty()) { paint_text(painter, content_rect, font, text_alignment());
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);
}
} }
void GButton::click() void GButton::click()

View file

@ -66,14 +66,7 @@ void GCheckBox::paint_event(GPaintEvent& event)
painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color()); painter.draw_bitmap(box_rect.shrunken(4, 4).location(), *s_checked_bitmap, foreground_color());
} }
if (!text().is_empty()) { paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
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));
}
}
} }
void GCheckBox::click() void GCheckBox::click()

View file

@ -45,14 +45,9 @@ void GRadioButton::paint_event(GPaintEvent& event)
auto& bitmap = circle_bitmap(is_checked(), is_being_pressed()); auto& bitmap = circle_bitmap(is_checked(), is_being_pressed());
painter.blit(circle_rect.location(), bitmap, bitmap.rect()); 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() };
Rect text_rect { circle_rect.right() + 4, 0, font().width(text()), font().glyph_height() }; text_rect.center_vertically_within(rect());
text_rect.center_vertically_within(rect()); paint_text(painter, text_rect, font(), TextAlignment::TopLeft);
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));
}
} }
template<typename Callback> template<typename Callback>