summaryrefslogtreecommitdiff
path: root/Servers/WindowServer/WSMenu.h
blob: 0366bfb56e4af0edddc425311e1765c2f7158e6e (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
#pragma once

#include <AK/AKString.h>
#include <AK/Vector.h>
#include <AK/WeakPtr.h>
#include <SharedGraphics/Rect.h>
#include <WindowServer/WSMenuItem.h>
#include <LibCore/CObject.h>

class WSClientConnection;
class WSMenuBar;
class WSEvent;
class WSWindow;
class Font;

class WSMenu final : public CObject {
public:
    WSMenu(WSClientConnection*, int menu_id, String&& name);
    virtual ~WSMenu() override;

    WSClientConnection* client() { return m_client; }
    const WSClientConnection* client() const { return m_client; }
    int menu_id() const { return m_menu_id; }

    WSMenuBar* menubar() { return m_menubar; }
    const WSMenuBar* menubar() const { return m_menubar; }
    void set_menubar(WSMenuBar* menubar) { m_menubar = menubar; }

    bool is_empty() const { return m_items.is_empty(); }
    int item_count() const { return m_items.size(); }
    WSMenuItem* item(int i) { return m_items[i].ptr(); }
    const WSMenuItem* item(int i) const { return m_items[i].ptr(); }

    void add_item(OwnPtr<WSMenuItem>&& item) { m_items.append(move(item)); }

    String name() const { return m_name; }

    template<typename Callback>
    void for_each_item(Callback callback) const
    {
        for (auto& item : m_items)
            callback(*item);
    }

    Rect text_rect_in_menubar() const { return m_text_rect_in_menubar; }
    void set_text_rect_in_menubar(const Rect& rect) { m_text_rect_in_menubar = rect; }

    Rect rect_in_menubar() const { return m_rect_in_menubar; }
    void set_rect_in_menubar(const Rect& rect) { m_rect_in_menubar = rect; }

    WSWindow* menu_window() { return m_menu_window.ptr(); }
    WSWindow& ensure_menu_window();

    int width() const;
    int height() const;

    int item_height() const { return 16; }
    int frame_thickness() const { return 3; }
    int horizontal_padding() const { return left_padding() + right_padding(); }
    int left_padding() const { return 14; }
    int right_padding() const { return 14; }

    void draw();
    const Font& font() const;

    WSMenuItem* item_with_identifier(unsigned);
    WSMenuItem* item_at(const Point&);
    void redraw();

    const WSMenuItem* hovered_item() const { return m_hovered_item; }
    void clear_hovered_item();

    Function<void(WSMenuItem&)> on_item_activation;

    void close();

    void popup(const Point&, bool top_anchored);

private:
    virtual void event(CEvent&) override;

    int padding_between_text_and_shortcut() const { return 50; }
    void did_activate(WSMenuItem&);
    WSClientConnection* m_client { nullptr };
    int m_menu_id { 0 };
    String m_name;
    Rect m_rect_in_menubar;
    Rect m_text_rect_in_menubar;
    WSMenuBar* m_menubar { nullptr };
    WSMenuItem* m_hovered_item { nullptr };
    Vector<OwnPtr<WSMenuItem>> m_items;
    OwnPtr<WSWindow> m_menu_window;
};