2020-12-14 20:03:16 +01:00
|
|
|
/*
|
|
|
|
|
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
|
2021-09-03 11:14:37 +01:00
|
|
|
* Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
|
2022-02-26 10:50:04 -07:00
|
|
|
* Copyright (c) 2022, the SerenityOS developers.
|
2020-12-14 20:03:16 +01:00
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-12-14 20:03:16 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include <LibGUI/BoxLayout.h>
|
2021-04-13 16:18:20 +02:00
|
|
|
#include <LibGUI/Breadcrumbbar.h>
|
2020-12-14 20:03:16 +01:00
|
|
|
#include <LibGUI/Button.h>
|
2021-01-09 11:04:13 +01:00
|
|
|
#include <LibGUI/Painter.h>
|
2022-04-09 09:28:38 +02:00
|
|
|
#include <LibGfx/Font/Font.h>
|
2021-01-09 11:04:13 +01:00
|
|
|
#include <LibGfx/Palette.h>
|
2020-12-14 20:03:16 +01:00
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
REGISTER_WIDGET(GUI, Breadcrumbbar)
|
2021-01-02 16:30:13 -07:00
|
|
|
|
2020-12-14 20:03:16 +01:00
|
|
|
namespace GUI {
|
|
|
|
|
|
2021-01-09 11:04:13 +01:00
|
|
|
class BreadcrumbButton : public Button {
|
2021-01-08 18:53:37 +01:00
|
|
|
C_OBJECT(BreadcrumbButton);
|
2020-12-14 20:03:16 +01:00
|
|
|
|
|
|
|
|
public:
|
2022-02-26 10:50:04 -07:00
|
|
|
virtual ~BreadcrumbButton() override = default;
|
2020-12-14 20:03:16 +01:00
|
|
|
|
|
|
|
|
virtual bool is_uncheckable() const override { return false; }
|
2021-01-08 18:53:37 +01:00
|
|
|
virtual void drop_event(DropEvent& event) override
|
|
|
|
|
{
|
|
|
|
|
if (on_drop)
|
|
|
|
|
on_drop(event);
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-09 11:04:13 +01:00
|
|
|
virtual void drag_enter_event(DragEvent& event) override
|
|
|
|
|
{
|
2021-01-09 11:52:21 +01:00
|
|
|
update();
|
2021-01-09 11:04:13 +01:00
|
|
|
if (on_drag_enter)
|
|
|
|
|
on_drag_enter(event);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void drag_leave_event(Event&) override
|
|
|
|
|
{
|
2021-01-09 11:52:21 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2021-01-09 11:04:13 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-08 18:53:37 +01:00
|
|
|
Function<void(DropEvent&)> on_drop;
|
2021-01-09 11:04:13 +01:00
|
|
|
Function<void(DragEvent&)> on_drag_enter;
|
2020-12-14 20:03:16 +01:00
|
|
|
|
|
|
|
|
private:
|
2022-02-26 10:50:04 -07:00
|
|
|
BreadcrumbButton() = default;
|
2020-12-14 20:03:16 +01:00
|
|
|
};
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
Breadcrumbbar::Breadcrumbbar()
|
2020-12-14 20:03:16 +01:00
|
|
|
{
|
2023-02-16 21:07:06 +00:00
|
|
|
set_layout<HorizontalBoxLayout>(GUI::Margins {}, 0);
|
2020-12-14 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Breadcrumbbar::clear_segments()
|
2020-12-14 20:03:16 +01:00
|
|
|
{
|
|
|
|
|
m_segments.clear();
|
2020-12-27 23:07:10 +01:00
|
|
|
remove_all_children();
|
2022-08-21 21:45:43 +02:00
|
|
|
m_selected_segment = {};
|
2020-12-14 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
void Breadcrumbbar::append_segment(ByteString text, Gfx::Bitmap const* icon, ByteString data, String tooltip)
|
2020-12-14 20:03:16 +01:00
|
|
|
{
|
2021-01-08 18:53:37 +01:00
|
|
|
auto& button = add<BreadcrumbButton>();
|
2021-04-13 16:18:20 +02:00
|
|
|
button.set_button_style(Gfx::ButtonStyle::Coolbar);
|
2023-12-16 17:49:34 +03:30
|
|
|
button.set_text(String::from_byte_string(text).release_value_but_fixme_should_propagate_errors());
|
2020-12-14 20:03:16 +01:00
|
|
|
button.set_icon(icon);
|
2023-08-23 20:16:52 +02:00
|
|
|
button.set_tooltip(move(tooltip));
|
2020-12-14 21:44:03 +01:00
|
|
|
button.set_focus_policy(FocusPolicy::TabFocus);
|
2020-12-14 20:03:16 +01:00
|
|
|
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);
|
2022-08-23 12:41:31 +02:00
|
|
|
if (on_segment_change && m_selected_segment != index)
|
|
|
|
|
on_segment_change(index);
|
2020-12-14 20:03:16 +01:00
|
|
|
};
|
2023-02-17 11:20:12 +00:00
|
|
|
button.on_double_click = [this](auto modifiers) {
|
|
|
|
|
if (on_doubleclick)
|
|
|
|
|
on_doubleclick(modifiers);
|
|
|
|
|
};
|
2022-02-13 11:21:20 +01:00
|
|
|
button.on_focus_change = [this, index = m_segments.size()](auto has_focus, auto) {
|
2022-08-23 12:41:31 +02:00
|
|
|
if (has_focus && on_segment_change && m_selected_segment != index)
|
|
|
|
|
on_segment_change(index);
|
2022-02-13 11:21:20 +01:00
|
|
|
};
|
2021-01-08 18:53:37 +01:00
|
|
|
button.on_drop = [this, index = m_segments.size()](auto& drop_event) {
|
2021-01-09 11:04:13 +01:00
|
|
|
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);
|
2021-01-08 18:53:37 +01:00
|
|
|
};
|
2020-12-14 20:03:16 +01:00
|
|
|
|
2023-02-01 23:28:18 +01:00
|
|
|
m_segments.append(Segment {
|
|
|
|
|
.icon = icon,
|
|
|
|
|
.text = move(text),
|
|
|
|
|
.data = move(data),
|
|
|
|
|
.width = 0,
|
|
|
|
|
.shrunken_width = 0,
|
|
|
|
|
.button = button.make_weak_ptr<GUI::Button>(),
|
|
|
|
|
});
|
2021-08-29 23:16:21 +02:00
|
|
|
relayout();
|
2020-12-14 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-06-24 15:41:59 +01:00
|
|
|
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);
|
|
|
|
|
}
|
2022-08-21 21:45:43 +02:00
|
|
|
if (m_selected_segment.has_value() && *m_selected_segment >= start_segment_index)
|
|
|
|
|
m_selected_segment = {};
|
2021-06-24 15:41:59 +01:00
|
|
|
}
|
|
|
|
|
|
2023-12-16 17:49:34 +03:30
|
|
|
Optional<size_t> Breadcrumbbar::find_segment_with_data(ByteString const& data)
|
2021-06-24 15:41:59 +01:00
|
|
|
{
|
|
|
|
|
for (size_t i = 0; i < segment_count(); ++i) {
|
|
|
|
|
if (segment_data(i) == data)
|
|
|
|
|
return i;
|
|
|
|
|
}
|
|
|
|
|
return {};
|
|
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Breadcrumbbar::set_selected_segment(Optional<size_t> index)
|
2020-12-14 20:03:16 +01:00
|
|
|
{
|
2022-08-23 12:38:07 +02:00
|
|
|
if (m_selected_segment == index)
|
|
|
|
|
return;
|
2022-08-18 20:43:39 +02:00
|
|
|
m_selected_segment = index;
|
|
|
|
|
|
2020-12-14 20:03:16 +01:00
|
|
|
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()];
|
2021-02-23 20:42:32 +01:00
|
|
|
VERIFY(segment.button);
|
2020-12-14 20:03:16 +01:00
|
|
|
segment.button->set_checked(true);
|
2022-08-21 21:24:29 +02:00
|
|
|
if (on_segment_change)
|
|
|
|
|
on_segment_change(index);
|
2021-08-29 23:16:21 +02:00
|
|
|
relayout();
|
2020-12-14 20:03:16 +01:00
|
|
|
}
|
|
|
|
|
|
2021-04-13 16:18:20 +02:00
|
|
|
void Breadcrumbbar::doubleclick_event(MouseEvent& event)
|
2021-01-09 14:07:57 +11:00
|
|
|
{
|
|
|
|
|
if (on_doubleclick)
|
2023-02-17 11:20:12 +00:00
|
|
|
on_doubleclick(event.modifiers());
|
2021-01-09 14:07:57 +11:00
|
|
|
}
|
|
|
|
|
|
2021-08-29 23:16:21 +02:00
|
|
|
void Breadcrumbbar::resize_event(ResizeEvent&)
|
|
|
|
|
{
|
|
|
|
|
relayout();
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-01 23:28:18 +01:00
|
|
|
void Breadcrumbbar::did_change_font()
|
|
|
|
|
{
|
|
|
|
|
Widget::did_change_font();
|
|
|
|
|
relayout();
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-29 23:16:21 +02:00
|
|
|
void Breadcrumbbar::relayout()
|
|
|
|
|
{
|
2023-02-02 14:18:05 +01:00
|
|
|
auto total_width = 0;
|
2023-02-01 23:28:18 +01:00
|
|
|
for (auto& segment : m_segments) {
|
|
|
|
|
VERIFY(segment.button);
|
|
|
|
|
auto& button = *segment.button;
|
2023-02-02 14:29:34 +01:00
|
|
|
// 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);
|
2023-02-01 23:28:18 +01:00
|
|
|
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);
|
|
|
|
|
|
2023-02-02 14:18:05 +01:00
|
|
|
total_width += segment.width;
|
|
|
|
|
}
|
2021-08-29 23:16:21 +02:00
|
|
|
|
2023-02-02 14:18:05 +01:00
|
|
|
auto remaining_width = total_width;
|
2021-08-29 23:16:21 +02:00
|
|
|
|
|
|
|
|
for (auto& segment : m_segments) {
|
|
|
|
|
if (remaining_width > width() && !segment.button->is_checked()) {
|
2022-10-05 18:29:51 +02:00
|
|
|
segment.button->set_preferred_width(segment.shrunken_width);
|
2021-08-29 23:16:21 +02:00
|
|
|
remaining_width -= (segment.width - segment.shrunken_width);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2022-10-05 18:29:51 +02:00
|
|
|
segment.button->set_preferred_width(segment.width);
|
2021-08-29 23:16:21 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-14 20:03:16 +01:00
|
|
|
}
|