mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-25 03:31:55 +00:00
This makes most operations thread safe, especially so that they can safely be used in the Kernel. This includes obtaining a strong reference from a weak reference, which now requires an explicit call to WeakPtr::strong_ref(). Another major change is that Weakable::make_weak_ref() may require the explicit target type. Previously we used reinterpret_cast in WeakPtr, assuming that it can be properly converted. But WeakPtr does not necessarily have the knowledge to be able to do this. Instead, we now ask the class itself to deliver a WeakPtr to the type that we want. Also, WeakLink is no longer specific to a target type. The reason for this is that we want to be able to safely convert e.g. WeakPtr<T> to WeakPtr<U>, and before this we just reinterpret_cast the internal WeakLink<T> to WeakLink<U>, which is a bold assumption that it would actually produce the correct code. Instead, WeakLink now operates on just a raw pointer and we only make those constructors/operators available if we can verify that it can be safely cast. In order to guarantee thread safety, we now use the least significant bit in the pointer for locking purposes. This also means that only properly aligned pointers can be used.
168 lines
5.6 KiB
C++
168 lines
5.6 KiB
C++
/*
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
* All rights reserved.
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
* list of conditions and the following disclaimer.
|
|
*
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
* and/or other materials provided with the distribution.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
|
#include <AK/String.h>
|
|
#include <AK/WeakPtr.h>
|
|
#include <LibCore/Object.h>
|
|
#include <LibGfx/Font.h>
|
|
#include <LibGfx/Forward.h>
|
|
#include <LibGfx/Rect.h>
|
|
#include <WindowServer/Cursor.h>
|
|
#include <WindowServer/MenuItem.h>
|
|
#include <WindowServer/Window.h>
|
|
|
|
namespace WindowServer {
|
|
|
|
class ClientConnection;
|
|
class MenuBar;
|
|
class Event;
|
|
|
|
class Menu final : public Core::Object {
|
|
C_OBJECT(Menu)
|
|
public:
|
|
Menu(ClientConnection*, int menu_id, const String& name);
|
|
virtual ~Menu() override;
|
|
|
|
ClientConnection* client() { return m_client; }
|
|
const ClientConnection* client() const { return m_client; }
|
|
int menu_id() const { return m_menu_id; }
|
|
|
|
MenuBar* menubar() { return m_menubar; }
|
|
const MenuBar* menubar() const { return m_menubar; }
|
|
void set_menubar(MenuBar* menubar) { m_menubar = menubar; }
|
|
|
|
bool is_empty() const { return m_items.is_empty(); }
|
|
int item_count() const { return m_items.size(); }
|
|
const MenuItem& item(int index) const { return m_items.at(index); }
|
|
MenuItem& item(int index) { return m_items.at(index); }
|
|
|
|
void add_item(NonnullOwnPtr<MenuItem>&& 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);
|
|
}
|
|
|
|
Gfx::IntRect text_rect_in_menubar() const { return m_text_rect_in_menubar; }
|
|
void set_text_rect_in_menubar(const Gfx::IntRect& rect) { m_text_rect_in_menubar = rect; }
|
|
|
|
Gfx::IntRect rect_in_menubar() const { return m_rect_in_menubar; }
|
|
void set_rect_in_menubar(const Gfx::IntRect& rect) { m_rect_in_menubar = rect; }
|
|
|
|
Window* menu_window() { return m_menu_window.ptr(); }
|
|
Window& ensure_menu_window();
|
|
|
|
Window* window_menu_of() { return m_window_menu_of; }
|
|
void set_window_menu_of(Window& window) { m_window_menu_of = window; }
|
|
bool is_window_menu_open() { return m_is_window_menu_open; }
|
|
void set_window_menu_open(bool is_open) { m_is_window_menu_open = is_open; }
|
|
|
|
bool activate_default();
|
|
|
|
int content_width() const;
|
|
|
|
int item_height() const { return 20; }
|
|
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 Gfx::Font& font() const;
|
|
const Gfx::Font& title_font() const;
|
|
void set_title_font(const Gfx::Font& font);
|
|
|
|
MenuItem* item_with_identifier(unsigned);
|
|
void redraw();
|
|
|
|
MenuItem* hovered_item() const;
|
|
|
|
void set_hovered_item(int index)
|
|
{
|
|
m_hovered_item_index = index;
|
|
update_for_new_hovered_item();
|
|
}
|
|
|
|
void clear_hovered_item();
|
|
|
|
Function<void(MenuItem&)> on_item_activation;
|
|
|
|
void close();
|
|
|
|
void popup(const Gfx::IntPoint&);
|
|
void do_popup(const Gfx::IntPoint&, bool);
|
|
|
|
bool is_menu_ancestor_of(const Menu&) const;
|
|
|
|
void redraw_if_theme_changed();
|
|
|
|
bool is_scrollable() const { return m_scrollable; }
|
|
int scroll_offset() const { return m_scroll_offset; }
|
|
|
|
void descend_into_submenu_at_hovered_item();
|
|
void open_hovered_item();
|
|
|
|
private:
|
|
virtual void event(Core::Event&) override;
|
|
|
|
RefPtr<Gfx::Font> m_title_font { &Gfx::Font::default_font() };
|
|
|
|
void handle_mouse_move_event(const MouseEvent&);
|
|
int visible_item_count() const;
|
|
|
|
int item_index_at(const Gfx::IntPoint&);
|
|
int padding_between_text_and_shortcut() const { return 50; }
|
|
void did_activate(MenuItem&);
|
|
void update_for_new_hovered_item(bool make_input = false);
|
|
|
|
ClientConnection* m_client { nullptr };
|
|
int m_menu_id { 0 };
|
|
String m_name;
|
|
Gfx::IntRect m_rect_in_menubar;
|
|
Gfx::IntRect m_text_rect_in_menubar;
|
|
MenuBar* m_menubar { nullptr };
|
|
NonnullOwnPtrVector<MenuItem> m_items;
|
|
RefPtr<Window> m_menu_window;
|
|
|
|
WeakPtr<Window> m_window_menu_of;
|
|
bool m_is_window_menu_open = { false };
|
|
Gfx::IntPoint m_last_position_in_hover;
|
|
int m_theme_index_at_last_paint { -1 };
|
|
int m_hovered_item_index { -1 };
|
|
|
|
bool m_scrollable { false };
|
|
int m_scroll_offset { 0 };
|
|
int m_max_scroll_offset { 0 };
|
|
};
|
|
|
|
}
|