summaryrefslogtreecommitdiff
path: root/Userland/Services/WindowServer/Menubar.h
blob: d05b18dcf097644bc1a385816a02352396a90f5b (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
/*
 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
 * Copyright (c) 2021, sin-ack <sin-ack@protonmail.com>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#pragma once

#include "Menu.h"
#include <AK/Function.h>
#include <AK/IterationDecision.h>
#include <AK/Vector.h>

namespace WindowServer {

class Menubar {
public:
    void add_menu(Menu& menu, Gfx::IntRect window_rect)
    {
        // FIXME: Check against duplicate menu additions.
        m_menus.append(menu);
        layout_menu(menu, window_rect);
    }

    bool flash_menu(Menu* flashed_submenu)
    {
        Menu* const old_flashed_menu = m_flashed_menu;
        m_flashed_menu = nullptr;

        if (flashed_submenu) {
            for_each_menu([&](Menu& menu) {
                if ((&menu) == flashed_submenu || menu.is_menu_ancestor_of(*flashed_submenu)) {
                    m_flashed_menu = &menu;
                    return IterationDecision::Break;
                }
                return IterationDecision::Continue;
            });
        }

        return (old_flashed_menu != m_flashed_menu);
    }

    Menu* flashed_menu() const { return m_flashed_menu; }

    bool has_menus()
    {
        return !m_menus.is_empty();
    }

    void for_each_menu(Function<IterationDecision(Menu&)> callback)
    {
        for (auto& menu : m_menus) {
            if (callback(menu) == IterationDecision::Break)
                return;
        }
    }

    void font_changed(Gfx::IntRect window_rect)
    {
        m_next_menu_location = { 0, 0 };
        for (auto& menu : m_menus)
            layout_menu(menu, window_rect);
    }

private:
    void layout_menu(Menu&, Gfx::IntRect window_rect);

    Vector<Menu&> m_menus;
    Menu* m_flashed_menu { nullptr };

    // FIXME: This doesn't support removing menus from a menubar or inserting a
    //        menu in the middle.
    Gfx::IntPoint m_next_menu_location { 0, 0 };
};

}