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
|
|
|
{
|
|
|
|
auto& layout = set_layout<HorizontalBoxLayout>();
|
|
|
|
layout.set_spacing(0);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
void Breadcrumbbar::append_segment(DeprecatedString text, Gfx::Bitmap const* icon, DeprecatedString data, DeprecatedString 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);
|
2020-12-14 20:03:16 +01:00
|
|
|
button.set_text(text);
|
|
|
|
button.set_icon(icon);
|
2021-02-20 14:23:21 +01: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
|
|
|
};
|
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
|
|
|
|
|
|
|
auto button_text_width = button.font().width(text);
|
|
|
|
auto icon_width = icon ? icon->width() : 0;
|
|
|
|
auto icon_padding = icon ? 4 : 0;
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
int const max_button_width = 100;
|
2021-08-29 23:16:21 +02:00
|
|
|
|
|
|
|
auto button_width = min(button_text_width + icon_width + icon_padding + 16, max_button_width);
|
|
|
|
auto shrunken_width = icon_width + icon_padding + (icon ? 4 : 16);
|
|
|
|
|
2023-01-03 14:43:07 +01:00
|
|
|
button.set_max_size(static_cast<int>(ceilf(button_width)), 16 + 8);
|
2022-10-05 18:29:51 +02:00
|
|
|
button.set_min_size(shrunken_width, 16 + 8);
|
2021-08-29 23:16:21 +02:00
|
|
|
|
2023-01-03 14:43:07 +01:00
|
|
|
Segment segment { icon, text, data, static_cast<int>(ceilf(button_width)), shrunken_width, button.make_weak_ptr<GUI::Button>() };
|
2020-12-14 20:03:16 +01:00
|
|
|
|
|
|
|
m_segments.append(move(segment));
|
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
|
|
|
}
|
|
|
|
|
2022-12-04 18:02:33 +00:00
|
|
|
Optional<size_t> Breadcrumbbar::find_segment_with_data(DeprecatedString 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)
|
|
|
|
on_doubleclick(event);
|
|
|
|
}
|
|
|
|
|
2021-08-29 23:16:21 +02:00
|
|
|
void Breadcrumbbar::resize_event(ResizeEvent&)
|
|
|
|
{
|
|
|
|
relayout();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Breadcrumbbar::relayout()
|
|
|
|
{
|
|
|
|
auto remaining_width = 0;
|
|
|
|
|
|
|
|
for (auto& segment : m_segments)
|
|
|
|
remaining_width += segment.width;
|
|
|
|
|
|
|
|
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
|
|
|
}
|