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
|
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, the SerenityOS developers.
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGUI/BoxLayout.h>
#include <LibGUI/Breadcrumbbar.h>
#include <LibGUI/Button.h>
#include <LibGUI/Painter.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Palette.h>
REGISTER_WIDGET(GUI, Breadcrumbbar)
namespace GUI {
class BreadcrumbButton : public Button {
C_OBJECT(BreadcrumbButton);
public:
virtual ~BreadcrumbButton() override = default;
virtual bool is_uncheckable() const override { return false; }
virtual void drop_event(DropEvent& event) override
{
if (on_drop)
on_drop(event);
}
virtual void drag_enter_event(DragEvent& event) override
{
update();
if (on_drag_enter)
on_drag_enter(event);
}
virtual void drag_leave_event(Event&) override
{
update();
}
virtual void paint_event(PaintEvent& event) override
{
Button::paint_event(event);
if (has_pending_drop()) {
Painter painter(*this);
painter.draw_rect(rect(), palette().selection(), true);
}
}
Function<void(DropEvent&)> on_drop;
Function<void(DragEvent&)> on_drag_enter;
private:
BreadcrumbButton() = default;
};
Breadcrumbbar::Breadcrumbbar()
{
auto& layout = set_layout<HorizontalBoxLayout>();
layout.set_spacing(0);
}
void Breadcrumbbar::clear_segments()
{
m_segments.clear();
remove_all_children();
m_selected_segment = {};
}
void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* icon, DeprecatedString data, DeprecatedString tooltip)
{
auto& button = add<BreadcrumbButton>();
button.set_button_style(Gfx::ButtonStyle::Coolbar);
button.set_text_deprecated(text);
button.set_icon(icon);
button.set_tooltip(move(tooltip));
button.set_focus_policy(FocusPolicy::TabFocus);
button.set_checkable(true);
button.set_exclusive(true);
button.on_click = [this, index = m_segments.size()](auto) {
if (on_segment_click)
on_segment_click(index);
if (on_segment_change && m_selected_segment != index)
on_segment_change(index);
};
button.on_focus_change = [this, index = m_segments.size()](auto has_focus, auto) {
if (has_focus && on_segment_change && m_selected_segment != index)
on_segment_change(index);
};
button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
if (on_segment_drop)
on_segment_drop(index, drop_event);
};
button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
if (on_segment_drag_enter)
on_segment_drag_enter(index, event);
};
m_segments.append(Segment {
.icon = icon,
.text = move(text),
.data = move(data),
.width = 0,
.shrunken_width = 0,
.button = button.make_weak_ptr<GUI::Button>(),
});
relayout();
}
void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
{
while (segment_count() > start_segment_index) {
auto segment = m_segments.take_last();
remove_child(*segment.button);
}
if (m_selected_segment.has_value() && *m_selected_segment >= start_segment_index)
m_selected_segment = {};
}
Optional<size_t> Breadcrumbbar::find_segment_with_data(DeprecatedString const& data)
{
for (size_t i = 0; i < segment_count(); ++i) {
if (segment_data(i) == data)
return i;
}
return {};
}
void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
{
if (m_selected_segment == index)
return;
m_selected_segment = index;
if (!index.has_value()) {
for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
button.set_checked(false);
return IterationDecision::Continue;
});
return;
}
auto& segment = m_segments[index.value()];
VERIFY(segment.button);
segment.button->set_checked(true);
if (on_segment_change)
on_segment_change(index);
relayout();
}
void Breadcrumbbar::doubleclick_event(MouseEvent& event)
{
if (on_doubleclick)
on_doubleclick(event);
}
void Breadcrumbbar::resize_event(ResizeEvent&)
{
relayout();
}
void Breadcrumbbar::did_change_font()
{
Widget::did_change_font();
relayout();
}
void Breadcrumbbar::relayout()
{
auto total_width = 0;
for (auto& segment : m_segments) {
VERIFY(segment.button);
auto& button = *segment.button;
// NOTE: We use our own font instead of the button's font here in case we're being notified about
// a system font change, and the button hasn't been notified yet.
auto button_text_width = font().width(segment.text);
auto icon_width = button.icon() ? button.icon()->width() : 0;
auto icon_padding = button.icon() ? 4 : 0;
int const max_button_width = 100;
segment.width = static_cast<int>(ceilf(min(button_text_width + icon_width + icon_padding + 16, max_button_width)));
segment.shrunken_width = icon_width + icon_padding + (button.icon() ? 4 : 16);
button.set_max_size(segment.width, 16 + 8);
button.set_min_size(segment.shrunken_width, 16 + 8);
total_width += segment.width;
}
auto remaining_width = total_width;
for (auto& segment : m_segments) {
if (remaining_width > width() && !segment.button->is_checked()) {
segment.button->set_preferred_width(segment.shrunken_width);
remaining_width -= (segment.width - segment.shrunken_width);
continue;
}
segment.button->set_preferred_width(segment.width);
}
}
}
|