summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGUI/Toolbar.h
blob: 9024a64d32032a46bc131b7def29ca0976e1805c (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2022, the SerenityOS developers.
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include <LibGUI/Button.h>
#include <LibGUI/Menu.h>
#include <LibGUI/Widget.h>

namespace GUI {

class Toolbar : public Widget {
    C_OBJECT(Toolbar)
public:
    virtual ~Toolbar() override = default;

    ErrorOr<NonnullRefPtr<GUI::Button>> try_add_action(GUI::Action&);
    ErrorOr<void> try_add_separator();

    GUI::Button& add_action(GUI::Action&);
    void add_separator();

    bool is_collapsible() const { return m_collapsible; }
    void set_collapsible(bool b) { m_collapsible = b; }
    bool is_grouped() const { return m_grouped; }
    void set_grouped(bool b) { m_grouped = b; }

    virtual Optional<UISize> calculated_preferred_size() const override;
    virtual Optional<UISize> calculated_min_size() const override;

protected:
    explicit Toolbar(Gfx::Orientation = Gfx::Orientation::Horizontal, int button_size = 24);

    virtual void paint_event(PaintEvent&) override;
    virtual void resize_event(GUI::ResizeEvent&) override;

    ErrorOr<void> update_overflow_menu();
    ErrorOr<void> create_overflow_objects();

private:
    struct Item {
        enum class Type {
            Invalid,
            Separator,
            Action
        };
        Type type { Type::Invalid };
        RefPtr<Action> action;
        RefPtr<Widget> widget;
    };
    Vector<NonnullOwnPtr<Item>> m_items;
    RefPtr<Menu> m_overflow_menu;
    RefPtr<Action> m_overflow_action;
    RefPtr<Button> m_overflow_button;
    const Gfx::Orientation m_orientation;
    int m_button_size { 24 };
    bool m_collapsible { false };
    bool m_grouped { false };
};

}