LibGUI: Enable icons for SubMenus

It doesn't make sense for a top-level menu to have an icon, however
we do not have dedicated classes to distinguish these.

Furthermore, the only other place to store an icon is MenuItem.
Storing it there would be highly confusing, as MenuItem-with-Action
then would have two icons: one in Action and one in MenuItem.
And because we need to be able to replace the icon during realization,
this would need to write-through to Action somehow.

That's why I went with Menu, not MenuItem.
This commit is contained in:
Ben Wiederhake 2020-07-27 01:57:09 +02:00 committed by Andreas Kling
parent 048f149f51
commit 70fe126d01
Notes: sideshowbarker 2024-07-19 04:31:25 +09:00
2 changed files with 10 additions and 1 deletions

View file

@ -63,6 +63,11 @@ Menu::~Menu()
unrealize_menu();
}
void Menu::set_icon(const Gfx::Bitmap* icon)
{
m_icon = icon;
}
void Menu::add_action(NonnullRefPtr<Action> action)
{
m_items.append(make<MenuItem>(m_menu_id, move(action)));
@ -143,7 +148,8 @@ int Menu::realize_menu(RefPtr<Action> default_action)
if (item.type() == MenuItem::Type::Submenu) {
auto& submenu = *item.submenu();
submenu.realize_if_needed(default_action);
WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuItem>(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", -1, false);
int icon_buffer_id = ensure_realized_icon(submenu);
WindowServerConnection::the().send_sync<Messages::WindowServer::AddMenuItem>(m_menu_id, i, submenu.menu_id(), submenu.name(), true, false, false, false, "", icon_buffer_id, false);
continue;
}
if (item.type() == MenuItem::Type::Action) {

View file

@ -47,6 +47,8 @@ public:
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);
@ -66,6 +68,7 @@ private:
int m_menu_id { -1 };
String m_name;
RefPtr<Gfx::Bitmap> m_icon;
NonnullOwnPtrVector<MenuItem> m_items;
WeakPtr<Action> m_last_default_action;
};