Serenity Operating System
at portability 153 lines 5.3 kB view raw
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 <AK/NonnullOwnPtrVector.h> 30#include <AK/String.h> 31#include <AK/WeakPtr.h> 32#include <LibCore/Object.h> 33#include <LibGfx/Forward.h> 34#include <LibGfx/Rect.h> 35#include <WindowServer/Cursor.h> 36#include <WindowServer/MenuItem.h> 37#include <WindowServer/Window.h> 38 39namespace WindowServer { 40 41class ClientConnection; 42class MenuBar; 43class Event; 44 45class Menu final : public Core::Object { 46 C_OBJECT(Menu) 47public: 48 Menu(ClientConnection*, int menu_id, const String& name); 49 virtual ~Menu() override; 50 51 ClientConnection* client() { return m_client; } 52 const ClientConnection* client() const { return m_client; } 53 int menu_id() const { return m_menu_id; } 54 55 MenuBar* menubar() { return m_menubar; } 56 const MenuBar* menubar() const { return m_menubar; } 57 void set_menubar(MenuBar* menubar) { m_menubar = menubar; } 58 59 bool is_empty() const { return m_items.is_empty(); } 60 int item_count() const { return m_items.size(); } 61 const MenuItem& item(int index) const { return m_items.at(index); } 62 MenuItem& item(int index) { return m_items.at(index); } 63 64 void add_item(NonnullOwnPtr<MenuItem>&& item) { m_items.append(move(item)); } 65 66 String name() const { return m_name; } 67 68 template<typename Callback> 69 void for_each_item(Callback callback) const 70 { 71 for (auto& item : m_items) 72 callback(item); 73 } 74 75 Gfx::Rect text_rect_in_menubar() const { return m_text_rect_in_menubar; } 76 void set_text_rect_in_menubar(const Gfx::Rect& rect) { m_text_rect_in_menubar = rect; } 77 78 Gfx::Rect rect_in_menubar() const { return m_rect_in_menubar; } 79 void set_rect_in_menubar(const Gfx::Rect& rect) { m_rect_in_menubar = rect; } 80 81 Window* menu_window() { return m_menu_window.ptr(); } 82 Window& ensure_menu_window(); 83 84 Window* window_menu_of() { return m_window_menu_of; } 85 void set_window_menu_of(Window& window) { m_window_menu_of = window.make_weak_ptr(); } 86 bool is_window_menu_open() { return m_is_window_menu_open; } 87 void set_window_menu_open(bool is_open) { m_is_window_menu_open = is_open; } 88 89 int content_width() const; 90 91 int item_height() const { return 20; } 92 int frame_thickness() const { return 3; } 93 int horizontal_padding() const { return left_padding() + right_padding(); } 94 int left_padding() const { return 14; } 95 int right_padding() const { return 14; } 96 97 void draw(); 98 const Gfx::Font& font() const; 99 100 MenuItem* item_with_identifier(unsigned); 101 void redraw(); 102 103 MenuItem* hovered_item() const; 104 void clear_hovered_item(); 105 106 Function<void(MenuItem&)> on_item_activation; 107 108 void close(); 109 110 void popup(const Gfx::Point&, bool is_submenu = false); 111 112 bool is_menu_ancestor_of(const Menu&) const; 113 114 void redraw_if_theme_changed(); 115 116 bool is_scrollable() const { return m_scrollable; } 117 int scroll_offset() const { return m_scroll_offset; } 118 119private: 120 virtual void event(Core::Event&) override; 121 122 void handle_mouse_move_event(const MouseEvent&); 123 int visible_item_count() const; 124 125 int item_index_at(const Gfx::Point&); 126 int padding_between_text_and_shortcut() const { return 50; } 127 void did_activate(MenuItem&); 128 void open_hovered_item(); 129 void update_for_new_hovered_item(); 130 void decend_into_submenu_at_hovered_item(); 131 132 ClientConnection* m_client { nullptr }; 133 int m_menu_id { 0 }; 134 String m_name; 135 Gfx::Rect m_rect_in_menubar; 136 Gfx::Rect m_text_rect_in_menubar; 137 MenuBar* m_menubar { nullptr }; 138 NonnullOwnPtrVector<MenuItem> m_items; 139 RefPtr<Window> m_menu_window; 140 141 WeakPtr<Window> m_window_menu_of; 142 bool m_is_window_menu_open = { false }; 143 Gfx::Point m_last_position_in_hover; 144 int m_theme_index_at_last_paint { -1 }; 145 int m_hovered_item_index { -1 }; 146 bool m_in_submenu { false }; 147 148 bool m_scrollable { false }; 149 int m_scroll_offset { 0 }; 150 int m_max_scroll_offset { 0 }; 151}; 152 153}