1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
|
/*
* Copyright (c) 2021, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/Painter.h>
#include <LibGUI/Tray.h>
#include <LibGfx/Font.h>
#include <LibGfx/Palette.h>
#include <LibGfx/StylePainter.h>
REGISTER_WIDGET(GUI, Tray);
namespace GUI {
Tray::Tray()
{
set_fill_with_background_color(true);
set_background_role(Gfx::ColorRole::Tray);
set_focus_policy(GUI::FocusPolicy::TabFocus);
}
Gfx::IntRect Tray::Item::rect(Tray const& tray) const
{
static constexpr int item_height = 22;
return Gfx::IntRect {
tray.frame_thickness(),
tray.frame_thickness() + static_cast<int>(index) * item_height,
tray.frame_inner_rect().width(),
item_height,
};
}
size_t Tray::add_item(String text, RefPtr<Gfx::Bitmap> bitmap, String custom_data)
{
auto new_index = m_items.size();
m_items.append(Item {
.text = move(text),
.bitmap = move(bitmap),
.custom_data = move(custom_data),
.index = new_index,
});
update();
return new_index;
}
void Tray::set_item_checked(size_t index, bool checked)
{
if (checked) {
m_checked_item_index = index;
} else {
if (m_checked_item_index == index)
m_checked_item_index = {};
}
update();
}
void Tray::paint_event(GUI::PaintEvent& event)
{
GUI::Frame::paint_event(event);
GUI::Painter painter(*this);
painter.add_clip_rect(event.rect());
for (auto& item : m_items) {
auto rect = item.rect(*this);
bool is_pressed = item.index == m_pressed_item_index;
bool is_hovered = item.index == m_hovered_item_index;
bool is_checked = item.index == m_checked_item_index;
Gfx::StylePainter::paint_button(painter, rect, palette(), Gfx::ButtonStyle::Tray, is_pressed && is_hovered, is_hovered, is_checked, is_enabled());
Gfx::IntRect icon_rect {
rect.x() + 4,
0,
16,
16,
};
icon_rect.center_vertically_within(rect);
Gfx::IntRect text_rect {
icon_rect.right() + 5,
rect.y(),
rect.width(),
rect.height(),
};
text_rect.intersect(rect);
if (is_pressed && is_hovered) {
icon_rect.translate_by(1, 1);
text_rect.translate_by(1, 1);
}
if (item.bitmap)
painter.blit(icon_rect.location(), *item.bitmap, item.bitmap->rect());
auto const& font = is_checked ? this->font().bold_variant() : this->font();
painter.draw_text(text_rect, item.text, font, Gfx::TextAlignment::CenterLeft, palette().color(Gfx::ColorRole::TrayText));
}
}
void Tray::mousemove_event(GUI::MouseEvent& event)
{
auto* hovered_item = item_at(event.position());
if (!hovered_item) {
if (m_hovered_item_index.has_value())
update();
m_hovered_item_index = {};
return;
}
if (m_hovered_item_index != hovered_item->index) {
m_hovered_item_index = hovered_item->index;
update();
}
}
void Tray::mousedown_event(GUI::MouseEvent& event)
{
if (event.button() != GUI::MouseButton::Primary)
return;
auto* pressed_item = item_at(event.position());
if (!pressed_item)
return;
if (m_pressed_item_index != pressed_item->index) {
m_pressed_item_index = pressed_item->index;
update();
}
}
void Tray::mouseup_event(GUI::MouseEvent& event)
{
if (event.button() != GUI::MouseButton::Primary)
return;
if (auto* pressed_item = item_at(event.position()); pressed_item && m_pressed_item_index == pressed_item->index) {
on_item_activation(pressed_item->custom_data);
}
m_pressed_item_index = {};
update();
}
void Tray::leave_event(Core::Event&)
{
m_hovered_item_index = {};
update();
}
Tray::Item* Tray::item_at(Gfx::IntPoint const& position)
{
for (auto& item : m_items) {
if (item.rect(*this).contains(position))
return &item;
}
return nullptr;
}
void Tray::focusin_event(GUI::FocusEvent&)
{
if (m_items.is_empty())
return;
m_hovered_item_index = 0;
update();
}
void Tray::focusout_event(GUI::FocusEvent&)
{
if (m_items.is_empty())
return;
m_hovered_item_index = {};
update();
}
void Tray::keydown_event(GUI::KeyEvent& event)
{
if (m_items.is_empty() || event.modifiers())
return Frame::keydown_event(event);
if (event.key() == KeyCode::Key_Down) {
if (!m_hovered_item_index.has_value())
m_hovered_item_index = 0;
else
m_hovered_item_index = (*m_hovered_item_index + 1) % m_items.size();
update();
return;
}
if (event.key() == KeyCode::Key_Up) {
if (!m_hovered_item_index.has_value() || m_hovered_item_index == 0u)
m_hovered_item_index = m_items.size() - 1;
else
m_hovered_item_index = *m_hovered_item_index - 1;
update();
return;
}
if (event.key() == KeyCode::Key_Return) {
if (m_hovered_item_index.has_value())
on_item_activation(m_items[*m_hovered_item_index].custom_data);
return;
}
Frame::keydown_event(event);
}
}
|