Serenity Operating System
1/*
2 * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this
9 * list of conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice,
12 * this list of conditions and the following disclaimer in the documentation
13 * and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
22 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#pragma once
28
29#include "Menu.h"
30#include "MenuBar.h"
31#include "Window.h"
32#include <AK/HashMap.h>
33#include <LibCore/Object.h>
34
35namespace WindowServer {
36
37class MenuManager final : public Core::Object {
38 C_OBJECT(MenuManager)
39public:
40 static MenuManager& the();
41
42 MenuManager();
43 virtual ~MenuManager() override;
44
45 void refresh();
46
47 bool is_open(const Menu&) const;
48 bool has_open_menu() const { return !m_open_menu_stack.is_empty(); }
49
50 Gfx::Rect menubar_rect() const;
51 static int menubar_menu_margin() { return 16; }
52
53 void set_needs_window_resize();
54
55 Menu* current_menu() { return m_current_menu.ptr(); }
56 void set_current_menu(Menu*, bool is_submenu = false);
57 void open_menu(Menu&);
58 void toggle_menu(Menu&);
59
60 MenuBar* current_menubar() { return m_current_menubar.ptr(); }
61 void set_current_menubar(MenuBar*);
62 void close_menubar(MenuBar&);
63
64 void close_bar();
65 void close_everyone();
66 void close_everyone_not_in_lineage(Menu&);
67 void close_menu_and_descendants(Menu&);
68
69 void close_all_menus_from_client(Badge<ClientConnection>, ClientConnection&);
70
71 void toggle_system_menu()
72 {
73 if (m_system_menu)
74 toggle_menu(*m_system_menu);
75 }
76
77 Menu* system_menu() { return m_system_menu; }
78 void set_system_menu(Menu&);
79
80 int theme_index() const { return m_theme_index; }
81
82 Window& window() { return *m_window; }
83
84 template<typename Callback>
85 void for_each_active_menubar_menu(Callback callback)
86 {
87 if (system_menu()) {
88 if (callback(*system_menu()) == IterationDecision::Break)
89 return;
90 }
91 if (m_current_menubar)
92 m_current_menubar->for_each_menu(callback);
93 }
94
95 void did_change_theme();
96
97private:
98 void close_menus(const Vector<Menu*>&);
99
100 const Window& window() const { return *m_window; }
101
102 virtual void event(Core::Event&) override;
103 void handle_mouse_event(MouseEvent&);
104 void handle_menu_mouse_event(Menu&, const MouseEvent&);
105
106 void draw();
107
108 RefPtr<Window> m_window;
109
110 WeakPtr<Menu> m_current_menu;
111 Vector<WeakPtr<Menu>> m_open_menu_stack;
112
113 WeakPtr<Menu> m_system_menu;
114
115 bool m_needs_window_resize { false };
116 bool m_bar_open { false };
117
118 int m_theme_index { 0 };
119
120 WeakPtr<MenuBar> m_current_menubar;
121};
122
123}