summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Breadcrumbbar.cpp
blob: 94d1ccf4dbe46c150016fd61d554d5b40cff6807 (plain)
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
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
 *
 * 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.h>
#include <LibGfx/Palette.h>

REGISTER_WIDGET(GUI, Breadcrumbbar)

namespace GUI {

class BreadcrumbButton : public Button {
    C_OBJECT(BreadcrumbButton);

public:
    virtual ~BreadcrumbButton() override { }

    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() { }
};

Breadcrumbbar::Breadcrumbbar()
{
    auto& layout = set_layout<HorizontalBoxLayout>();
    layout.set_spacing(0);
}

Breadcrumbbar::~Breadcrumbbar()
{
}

void Breadcrumbbar::clear_segments()
{
    m_segments.clear();
    remove_all_children();
}

void Breadcrumbbar::append_segment(String text, Gfx::Bitmap const* icon, String data, String tooltip)
{
    auto& button = add<BreadcrumbButton>();
    button.set_button_style(Gfx::ButtonStyle::Coolbar);
    button.set_text(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);
    };
    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);
    };

    auto button_text_width = button.font().width(text);
    auto icon_width = icon ? icon->width() : 0;
    auto icon_padding = icon ? 4 : 0;

    const int max_button_width = 100;

    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);

    button.set_fixed_size(button_width, 16 + 8);

    Segment segment { icon, text, data, button_width, shrunken_width, button.make_weak_ptr<GUI::Button>() };

    m_segments.append(move(segment));
    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);
    }
}

Optional<size_t> Breadcrumbbar::find_segment_with_data(String 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 (!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);
    relayout();
}

void Breadcrumbbar::doubleclick_event(MouseEvent& event)
{
    if (on_doubleclick)
        on_doubleclick(event);
}

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()) {
            segment.button->set_fixed_width(segment.shrunken_width);
            remaining_width -= (segment.width - segment.shrunken_width);
            continue;
        }
        segment.button->set_fixed_width(segment.width);
    }
}

}