Serenity Operating System
1/*
2 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
3 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
4 * Copyright (c) 2022, the SerenityOS developers.
5 *
6 * SPDX-License-Identifier: BSD-2-Clause
7 */
8
9#include <LibGUI/BoxLayout.h>
10#include <LibGUI/Breadcrumbbar.h>
11#include <LibGUI/Button.h>
12#include <LibGUI/Painter.h>
13#include <LibGfx/Font/Font.h>
14#include <LibGfx/Palette.h>
15
16REGISTER_WIDGET(GUI, Breadcrumbbar)
17
18namespace GUI {
19
20class BreadcrumbButton : public Button {
21 C_OBJECT(BreadcrumbButton);
22
23public:
24 virtual ~BreadcrumbButton() override = default;
25
26 virtual bool is_uncheckable() const override { return false; }
27 virtual void drop_event(DropEvent& event) override
28 {
29 if (on_drop)
30 on_drop(event);
31 }
32
33 virtual void drag_enter_event(DragEvent& event) override
34 {
35 update();
36 if (on_drag_enter)
37 on_drag_enter(event);
38 }
39
40 virtual void drag_leave_event(Event&) override
41 {
42 update();
43 }
44
45 virtual void paint_event(PaintEvent& event) override
46 {
47 Button::paint_event(event);
48 if (has_pending_drop()) {
49 Painter painter(*this);
50 painter.draw_rect(rect(), palette().selection(), true);
51 }
52 }
53
54 Function<void(DropEvent&)> on_drop;
55 Function<void(DragEvent&)> on_drag_enter;
56
57private:
58 BreadcrumbButton() = default;
59};
60
61Breadcrumbbar::Breadcrumbbar()
62{
63 set_layout<HorizontalBoxLayout>(GUI::Margins {}, 0);
64}
65
66void Breadcrumbbar::clear_segments()
67{
68 m_segments.clear();
69 remove_all_children();
70 m_selected_segment = {};
71}
72
73void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* icon, DeprecatedString data, DeprecatedString tooltip)
74{
75 auto& button = add<BreadcrumbButton>();
76 button.set_button_style(Gfx::ButtonStyle::Coolbar);
77 button.set_text(String::from_deprecated_string(text).release_value_but_fixme_should_propagate_errors());
78 button.set_icon(icon);
79 button.set_tooltip(move(tooltip));
80 button.set_focus_policy(FocusPolicy::TabFocus);
81 button.set_checkable(true);
82 button.set_exclusive(true);
83 button.on_click = [this, index = m_segments.size()](auto) {
84 if (on_segment_click)
85 on_segment_click(index);
86 if (on_segment_change && m_selected_segment != index)
87 on_segment_change(index);
88 };
89 button.on_double_click = [this](auto modifiers) {
90 if (on_doubleclick)
91 on_doubleclick(modifiers);
92 };
93 button.on_focus_change = [this, index = m_segments.size()](auto has_focus, auto) {
94 if (has_focus && on_segment_change && m_selected_segment != index)
95 on_segment_change(index);
96 };
97 button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
98 if (on_segment_drop)
99 on_segment_drop(index, drop_event);
100 };
101 button.on_drag_enter = [this, index = m_segments.size()](auto& event) {
102 if (on_segment_drag_enter)
103 on_segment_drag_enter(index, event);
104 };
105
106 m_segments.append(Segment {
107 .icon = icon,
108 .text = move(text),
109 .data = move(data),
110 .width = 0,
111 .shrunken_width = 0,
112 .button = button.make_weak_ptr<GUI::Button>(),
113 });
114 relayout();
115}
116
117void Breadcrumbbar::remove_end_segments(size_t start_segment_index)
118{
119 while (segment_count() > start_segment_index) {
120 auto segment = m_segments.take_last();
121 remove_child(*segment.button);
122 }
123 if (m_selected_segment.has_value() && *m_selected_segment >= start_segment_index)
124 m_selected_segment = {};
125}
126
127Optional<size_t> Breadcrumbbar::find_segment_with_data(DeprecatedString const& data)
128{
129 for (size_t i = 0; i < segment_count(); ++i) {
130 if (segment_data(i) == data)
131 return i;
132 }
133 return {};
134}
135
136void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
137{
138 if (m_selected_segment == index)
139 return;
140 m_selected_segment = index;
141
142 if (!index.has_value()) {
143 for_each_child_of_type<GUI::AbstractButton>([&](auto& button) {
144 button.set_checked(false);
145 return IterationDecision::Continue;
146 });
147 return;
148 }
149
150 auto& segment = m_segments[index.value()];
151 VERIFY(segment.button);
152 segment.button->set_checked(true);
153 if (on_segment_change)
154 on_segment_change(index);
155 relayout();
156}
157
158void Breadcrumbbar::doubleclick_event(MouseEvent& event)
159{
160 if (on_doubleclick)
161 on_doubleclick(event.modifiers());
162}
163
164void Breadcrumbbar::resize_event(ResizeEvent&)
165{
166 relayout();
167}
168
169void Breadcrumbbar::did_change_font()
170{
171 Widget::did_change_font();
172 relayout();
173}
174
175void Breadcrumbbar::relayout()
176{
177 auto total_width = 0;
178 for (auto& segment : m_segments) {
179 VERIFY(segment.button);
180 auto& button = *segment.button;
181 // NOTE: We use our own font instead of the button's font here in case we're being notified about
182 // a system font change, and the button hasn't been notified yet.
183 auto button_text_width = font().width(segment.text);
184 auto icon_width = button.icon() ? button.icon()->width() : 0;
185 auto icon_padding = button.icon() ? 4 : 0;
186
187 int const max_button_width = 100;
188
189 segment.width = static_cast<int>(ceilf(min(button_text_width + icon_width + icon_padding + 16, max_button_width)));
190 segment.shrunken_width = icon_width + icon_padding + (button.icon() ? 4 : 16);
191
192 button.set_max_size(segment.width, 16 + 8);
193 button.set_min_size(segment.shrunken_width, 16 + 8);
194
195 total_width += segment.width;
196 }
197
198 auto remaining_width = total_width;
199
200 for (auto& segment : m_segments) {
201 if (remaining_width > width() && !segment.button->is_checked()) {
202 segment.button->set_preferred_width(segment.shrunken_width);
203 remaining_width -= (segment.width - segment.shrunken_width);
204 continue;
205 }
206 segment.button->set_preferred_width(segment.width);
207 }
208}
209
210}