From c88d77361c7a5f07648125a0ac23be29e9109e3c Mon Sep 17 00:00:00 2001 From: thankyouverycool <66646555+thankyouverycool@users.noreply.github.com> Date: Mon, 26 Sep 2022 08:42:36 -0400 Subject: LibGUI: Allow adding previously constructed menus to Menubar --- Userland/Libraries/LibGUI/Menubar.cpp | 6 ++++++ Userland/Libraries/LibGUI/Menubar.h | 1 + Userland/Libraries/LibGUI/Window.cpp | 10 ++++++++++ Userland/Libraries/LibGUI/Window.h | 1 + 4 files changed, 18 insertions(+) diff --git a/Userland/Libraries/LibGUI/Menubar.cpp b/Userland/Libraries/LibGUI/Menubar.cpp index 587dd229c8..6399d1cf90 100644 --- a/Userland/Libraries/LibGUI/Menubar.cpp +++ b/Userland/Libraries/LibGUI/Menubar.cpp @@ -10,6 +10,12 @@ namespace GUI { +ErrorOr Menubar::try_add_menu(Badge, NonnullRefPtr menu) +{ + TRY(m_menus.try_append(menu)); + return {}; +} + ErrorOr> Menubar::try_add_menu(Badge, String name) { auto menu = TRY(try_add(move(name))); diff --git a/Userland/Libraries/LibGUI/Menubar.h b/Userland/Libraries/LibGUI/Menubar.h index 884c5b475a..ada28df1f5 100644 --- a/Userland/Libraries/LibGUI/Menubar.h +++ b/Userland/Libraries/LibGUI/Menubar.h @@ -23,6 +23,7 @@ class Menubar : public Core::Object { public: virtual ~Menubar() override = default; + ErrorOr try_add_menu(Badge, NonnullRefPtr); ErrorOr> try_add_menu(Badge, String name); Menu& add_menu(Badge, String name); diff --git a/Userland/Libraries/LibGUI/Window.cpp b/Userland/Libraries/LibGUI/Window.cpp index 417096cf12..bff9902653 100644 --- a/Userland/Libraries/LibGUI/Window.cpp +++ b/Userland/Libraries/LibGUI/Window.cpp @@ -1272,6 +1272,16 @@ Gfx::Bitmap* Window::back_bitmap() return m_back_store ? &m_back_store->bitmap() : nullptr; } +ErrorOr Window::try_add_menu(NonnullRefPtr menu) +{ + TRY(m_menubar->try_add_menu({}, move(menu))); + if (m_window_id) { + menu->realize_menu_if_needed(); + ConnectionToWindowServer::the().async_add_menu(m_window_id, menu->menu_id()); + } + return {}; +} + ErrorOr> Window::try_add_menu(String name) { auto menu = TRY(m_menubar->try_add_menu({}, move(name))); diff --git a/Userland/Libraries/LibGUI/Window.h b/Userland/Libraries/LibGUI/Window.h index 55e60a26db..290eba0bbb 100644 --- a/Userland/Libraries/LibGUI/Window.h +++ b/Userland/Libraries/LibGUI/Window.h @@ -222,6 +222,7 @@ public: Menu& add_menu(String name); ErrorOr> try_add_menu(String name); + ErrorOr try_add_menu(NonnullRefPtr menu); void flash_menubar_menu_for(MenuItem const&); void flush_pending_paints_immediately(); -- cgit v1.2.3