ladybird/Userland/Libraries/LibGUI/Menu.h
sin-ack 611370e7dc LibGUI, WindowServer: Greatly simplify menubar logic
Currently, any number of menubars can be plugged in and out of a window.
This is unnecessary complexity, since we only need one menubar on a
window. This commit removes most of the logic for dynamically attaching
and detaching menubars and makes one menubar always available. The
menubar is only considered existent if it has at least a single menu in
it (in other words, an empty menubar will not be shown).

This commit additionally fixes a bug wherein menus added after a menubar
has been attached would not have their rects properly setup, and would
therefore appear glitched out on the top left corner of the menubar.
2021-08-02 00:39:15 +02:00

68 lines
1.6 KiB
C++

/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/NonnullOwnPtrVector.h>
#include <AK/WeakPtr.h>
#include <LibCore/Object.h>
#include <LibGUI/Action.h>
#include <LibGUI/Event.h>
#include <LibGUI/Forward.h>
#include <LibGfx/Forward.h>
namespace GUI {
class Menu final : public Core::Object {
C_OBJECT(Menu)
public:
virtual ~Menu() override;
void realize_menu_if_needed();
static Menu* from_menu_id(int);
int menu_id() const { return m_menu_id; }
const String& name() const { return m_name; }
const Gfx::Bitmap* icon() const { return m_icon.ptr(); }
void set_icon(const Gfx::Bitmap*);
Action* action_at(size_t);
void add_action(NonnullRefPtr<Action>);
void add_separator();
Menu& add_submenu(const String& name);
void popup(const Gfx::IntPoint& screen_position, const RefPtr<Action>& default_action = nullptr);
void dismiss();
void visibility_did_change(Badge<WindowServerConnection>, bool visible);
Function<void(bool)> on_visibility_change;
bool is_visible() const { return m_visible; }
private:
friend class Menubar;
explicit Menu(String name = "");
int realize_menu(RefPtr<Action> default_action = nullptr);
void unrealize_menu();
void realize_if_needed(const RefPtr<Action>& default_action);
void realize_menu_item(MenuItem&, int item_id);
int m_menu_id { -1 };
String m_name;
RefPtr<Gfx::Bitmap> m_icon;
NonnullOwnPtrVector<MenuItem> m_items;
WeakPtr<Action> m_current_default_action;
bool m_visible { false };
};
}