1
1
/*
2
- * Copyright (c) 2018-2020 , Andreas Kling <kling@serenityos.org>
2
+ * Copyright (c) 2018-2021 , Andreas Kling <kling@serenityos.org>
3
3
* All rights reserved.
4
4
*
5
5
* Redistribution and use in source and binary forms, with or without
28
28
#include < LibGUI/Action.h>
29
29
#include < LibGUI/ActionGroup.h>
30
30
#include < LibGUI/Button.h>
31
+ #include < LibGUI/Menu.h>
31
32
#include < LibGUI/Painter.h>
32
33
#include < LibGfx/Font.h>
33
34
#include < LibGfx/FontDatabase.h>
@@ -62,7 +63,9 @@ void Button::paint_event(PaintEvent& event)
62
63
Painter painter (*this );
63
64
painter.add_clip_rect (event.rect ());
64
65
65
- Gfx::StylePainter::paint_button (painter, rect (), palette (), m_button_style, is_being_pressed (), is_hovered (), is_checked (), is_enabled (), is_focused ());
66
+ bool paint_pressed = is_being_pressed () || (m_menu && m_menu->is_visible ());
67
+
68
+ Gfx::StylePainter::paint_button (painter, rect (), palette (), m_button_style, paint_pressed, is_hovered (), is_checked (), is_enabled (), is_focused ());
66
69
67
70
if (text ().is_empty () && !m_icon)
68
71
return ;
@@ -72,9 +75,9 @@ void Button::paint_event(PaintEvent& event)
72
75
if (m_icon && !text ().is_empty ())
73
76
icon_location.set_x (content_rect.x ());
74
77
75
- if (is_being_pressed () || is_checked ())
78
+ if (paint_pressed || is_checked ()) {
76
79
painter.translate (1 , 1 );
77
- else if (m_icon && is_enabled () && is_hovered () && button_style () == Gfx::ButtonStyle::CoolBar) {
80
+ } else if (m_icon && is_enabled () && is_hovered () && button_style () == Gfx::ButtonStyle::CoolBar) {
78
81
auto shadow_color = palette ().button ().darkened (0 .7f );
79
82
painter.blit_filtered (icon_location.translated (1 , 1 ), *m_icon, m_icon->rect (), [&shadow_color](auto ) {
80
83
return shadow_color;
@@ -167,4 +170,28 @@ bool Button::is_uncheckable() const
167
170
return m_action->group ()->is_unchecking_allowed ();
168
171
}
169
172
173
+ void Button::set_menu (RefPtr<GUI::Menu> menu)
174
+ {
175
+ if (m_menu == menu)
176
+ return ;
177
+ if (m_menu)
178
+ m_menu->on_visibility_change = nullptr ;
179
+ m_menu = menu;
180
+ if (m_menu) {
181
+ m_menu->on_visibility_change = [&](bool ) {
182
+ update ();
183
+ };
184
+ }
185
+ }
186
+
187
+ void Button::mousedown_event (MouseEvent& event)
188
+ {
189
+ if (m_menu) {
190
+ m_menu->popup (screen_relative_rect ().top_left ());
191
+ update ();
192
+ return ;
193
+ }
194
+ AbstractButton::mousedown_event (event);
195
+ }
196
+
170
197
}
0 commit comments