1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-19 20:45:08 +00:00

LibGUI+WindowServer: Add support for enabled/disabled actions.

The enabled state of a GAction now propagates both to any toolbar buttons
and any menu items linked to the action. Toolbar buttons are painted in
a grayed out style when disabled. Menu items are gray when disabled. :^)
This commit is contained in:
Andreas Kling 2019-04-12 02:53:27 +02:00
parent 32e5c8c689
commit 054c982181
20 changed files with 308 additions and 53 deletions

View file

@ -2,6 +2,7 @@
#include <LibGUI/GPainter.h>
#include <SharedGraphics/StylePainter.h>
#include <AK/StringBuilder.h>
#include <LibGUI/GAction.h>
//#define GBUTTON_DEBUG
@ -12,6 +13,8 @@ GButton::GButton(GWidget* parent)
GButton::~GButton()
{
if (m_action)
m_action->unregister_button({ }, *this);
}
void GButton::set_caption(const String& caption)
@ -35,7 +38,7 @@ void GButton::paint_event(GPaintEvent& event)
GPainter painter(*this);
painter.add_clip_rect(event.rect());
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered, m_checkable && m_checked);
StylePainter::paint_button(painter, rect(), m_button_style, m_being_pressed, m_hovered, m_checkable && m_checked, is_enabled());
if (m_caption.is_empty() && !m_icon)
return;
@ -46,8 +49,12 @@ void GButton::paint_event(GPaintEvent& event)
content_rect.move_by(1, 1);
icon_location.move_by(1, 1);
}
if (m_icon)
painter.blit(icon_location, *m_icon, m_icon->rect());
if (m_icon) {
if (is_enabled())
painter.blit(icon_location, *m_icon, m_icon->rect());
else
painter.blit_dimmed(icon_location, *m_icon, m_icon->rect());
}
auto& font = (m_checkable && m_checked) ? Font::default_bold_font() : this->font();
painter.draw_text(content_rect, m_caption, font, text_alignment(), foreground_color(), TextElision::Right);
}
@ -55,10 +62,12 @@ void GButton::paint_event(GPaintEvent& event)
void GButton::mousemove_event(GMouseEvent& event)
{
if (event.buttons() == GMouseButton::Left) {
bool being_pressed = rect().contains(event.position());
if (being_pressed != m_being_pressed) {
m_being_pressed = being_pressed;
update();
if (is_enabled()) {
bool being_pressed = rect().contains(event.position());
if (being_pressed != m_being_pressed) {
m_being_pressed = being_pressed;
update();
}
}
}
GWidget::mousemove_event(event);
@ -70,14 +79,18 @@ void GButton::mousedown_event(GMouseEvent& event)
dbgprintf("GButton::mouse_down_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
if (event.button() == GMouseButton::Left) {
m_being_pressed = true;
update();
if (is_enabled()) {
m_being_pressed = true;
update();
}
}
GWidget::mousedown_event(event);
}
void GButton::click()
{
if (!is_enabled())
return;
if (on_click)
on_click(*this);
}
@ -88,11 +101,13 @@ void GButton::mouseup_event(GMouseEvent& event)
dbgprintf("GButton::mouse_up_event: x=%d, y=%d, button=%u\n", event.x(), event.y(), (unsigned)event.button());
#endif
if (event.button() == GMouseButton::Left) {
bool was_being_pressed = m_being_pressed;
m_being_pressed = false;
update();
if (was_being_pressed)
click();
if (is_enabled()) {
bool was_being_pressed = m_being_pressed;
m_being_pressed = false;
update();
if (was_being_pressed)
click();
}
}
GWidget::mouseup_event(event);
}
@ -108,3 +123,10 @@ void GButton::leave_event(CEvent&)
m_hovered = false;
update();
}
void GButton::set_action(GAction& action)
{
m_action = action.make_weak_ptr();
action.register_button({ }, *this);
set_enabled(action.is_enabled());
}