Truly independent web browser
UI/Qt: Build bookmarks bar context menus when they are shown
Shannon Booth6891beb8c9ff
diff --git a/UI/Qt/BookmarksBar.cpp b/UI/Qt/BookmarksBar.cppindex 878dbdbc55a..97453c5ba5c 100644--- a/UI/Qt/BookmarksBar.cpp+++ b/UI/Qt/BookmarksBar.cpp@@ -6,7 +6,6 @@#include <AK/HashMap.h>#include <AK/HashTable.h>-#include <AK/ScopeGuard.h>#include <LibWebView/Application.h>#include <LibWebView/BookmarkStore.h>#include <UI/Qt/BookmarksBar.h>@@ -385,16 +384,16 @@ void BookmarksBar::show_context_menu(QPoint position, Optional<WebView::Bookmarkm_selected_bookmark_menu_parent_folder_id = parent_folder_id.copy();if (item->is_bookmark())- execute_context_menu(bookmark_context_menu(), position);+ execute_context_menu_later(m_context_menu, *this, m_tab->view().bookmark_context_menu(), position);else if (item->is_folder())- execute_context_menu(bookmark_folder_context_menu(), position);+ execute_context_menu_later(m_context_menu, *this, m_tab->view().bookmark_folder_context_menu(), position);} else {// NB: Empty space in a folder that about:bookmarks shows adds to that folder, and elsewhere to the bar.m_selected_bookmark_menu_item_id = {};m_selected_bookmark_menu_target_folder_id = target_folder_id.copy();m_selected_bookmark_menu_parent_folder_id = {};- execute_context_menu(bookmarks_bar_context_menu(), position);+ execute_context_menu_later(m_context_menu, *this, m_tab->view().bookmarks_bar_context_menu(), position);}}@@ -471,55 +470,58 @@ bool BookmarksBar::handle_middle_mouse_click(QMouseEvent* event, QObject* item)bool BookmarksBar::handle_right_mouse_click(QMouseEvent* event, QObject* item){- // FIXME: The exec() calls below should use execute_context_menu() instead to use native context menus on macOS.+ // FIXME: The menus below should use execute_context_menu_later() instead to use native context menus on macOS.// However, more work is needed to ensure that opened bookmark folders do not disappear when the native menu// is shown.+ auto position = event->globalPosition().toPoint();+ auto context_menu_for_selected_item = [&]() -> WebView::Menu* {+ if (m_selected_bookmark_menu_item_type == "bookmark")+ return &m_tab->view().bookmark_context_menu();+ if (m_selected_bookmark_menu_item_type == "folder")+ return &m_tab->view().bookmark_folder_context_menu();+ return nullptr;+ };if (is<BookmarksBar>(item)) {m_selected_bookmark_menu_item_id = {};m_selected_bookmark_menu_target_folder_id = {};m_selected_bookmark_menu_parent_folder_id = {};- bookmarks_bar_context_menu().exec(event->globalPosition().toPoint());+ replace_context_menu(m_context_menu, *this, m_tab->view().bookmarks_bar_context_menu()).popup(position);} else if (auto* button = as_if<QToolButton>(item)) {- auto* action = button->defaultAction();- extract_item_properties(action);-- auto set_button_context_menu_property = [button = QPointer { button }](bool open) {- if (button) {- button->setProperty(BOOKMARK_CONTEXT_MENU_OPEN_PROPERTY, open);- button->update();- }- };-- ScopeGuard guard { [&]() { set_button_context_menu_property(false); } };- set_button_context_menu_property(true);+ extract_item_properties(button->defaultAction());+ auto* menu = context_menu_for_selected_item();+ if (!menu)+ return true;- if (m_selected_bookmark_menu_item_type == "bookmark")- bookmark_context_menu().exec(event->globalPosition().toPoint());- else if (m_selected_bookmark_menu_item_type == "folder")- bookmark_folder_context_menu().exec(event->globalPosition().toPoint());- } else if (auto* menu = as_if<QMenu>(item)) {- if (auto* action = menu->actionAt(event->pos())) {+ auto& context_menu = replace_context_menu(m_context_menu, *this, *menu);+ button->setProperty(BOOKMARK_CONTEXT_MENU_OPEN_PROPERTY, true);+ button->update();+ QObject::connect(&context_menu, &QMenu::aboutToHide, button, [button] {+ button->setProperty(BOOKMARK_CONTEXT_MENU_OPEN_PROPERTY, false);+ button->update();+ });+ context_menu.popup(position);+ } else if (auto* dropdown = as_if<QMenu>(item)) {+ if (auto* action = dropdown->actionAt(event->pos())) {QObject* submenu = action->menu();extract_item_properties(submenu ?: action);}if (m_selected_bookmark_menu_item_type.isEmpty())- extract_item_properties(menu);-- // FIXME: We create a temporary context menu parented to the dropdown. Otherwise, Qt complains that the context- // menu's parent does not match the current topmost popup. It would be nice if we could figure out a way- // to avoid this duplicated menu.- QMenu context_menu(menu);+ extract_item_properties(dropdown);- if (m_selected_bookmark_menu_item_type == "bookmark")- repopulate_application_menu(context_menu, m_tab->view().bookmark_context_menu());- else if (m_selected_bookmark_menu_item_type == "folder")- repopulate_application_menu(context_menu, m_tab->view().bookmark_folder_context_menu());+ auto* menu = context_menu_for_selected_item();+ if (!menu)+ return true;- if (!context_menu.isEmpty() && context_menu.exec(event->globalPosition().toPoint()))- menu->close();+ // NB: The context menu is parented to the dropdown. Otherwise, Qt complains that the context menu's parent does+ // not match the current topmost popup.+ auto& context_menu = replace_context_menu(m_context_menu, *dropdown, *menu);+ QObject::connect(&context_menu, &QMenu::triggered, dropdown, [dropdown] {+ dropdown->close();+ });+ context_menu.popup(position);}return true;@@ -539,25 +541,4 @@ void BookmarksBar::extract_item_properties(QObject* item)m_selected_bookmark_menu_parent_folder_id = AK::move(value);}-QMenu& BookmarksBar::bookmarks_bar_context_menu()-{- if (!m_bookmarks_bar_context_menu)- m_bookmarks_bar_context_menu = create_application_menu(*this, m_tab->view().bookmarks_bar_context_menu());- return *m_bookmarks_bar_context_menu;-}--QMenu& BookmarksBar::bookmark_context_menu()-{- if (!m_bookmark_context_menu)- m_bookmark_context_menu = create_application_menu(*this, m_tab->view().bookmark_context_menu());- return *m_bookmark_context_menu;-}--QMenu& BookmarksBar::bookmark_folder_context_menu()-{- if (!m_bookmark_folder_context_menu)- m_bookmark_folder_context_menu = create_application_menu(*this, m_tab->view().bookmark_folder_context_menu());- return *m_bookmark_folder_context_menu;-}-}diff --git a/UI/Qt/BookmarksBar.h b/UI/Qt/BookmarksBar.hindex ae38866ff98..a4ac8e7821d 100644--- a/UI/Qt/BookmarksBar.h+++ b/UI/Qt/BookmarksBar.h@@ -11,6 +11,7 @@#include <AK/kmalloc.h>#include <LibWebView/Forward.h>+#include <QPointer>#include <QToolBar>namespace Ladybird {@@ -42,19 +43,12 @@ private:bool handle_right_mouse_click(QMouseEvent*, QObject*);void extract_item_properties(QObject*);- QMenu& bookmarks_bar_context_menu();- QMenu& bookmark_context_menu();- QMenu& bookmark_folder_context_menu();-void slide_in(QWidget&, int width);void slide_out(int index, QPixmap const& image);Tab* m_tab { nullptr };bool m_has_been_built { false };-- QMenu* m_bookmarks_bar_context_menu { nullptr };- QMenu* m_bookmark_context_menu { nullptr };- QMenu* m_bookmark_folder_context_menu { nullptr };+ QPointer<QMenu> m_context_menu;String m_selected_bookmark_menu_item_id;QString m_selected_bookmark_menu_item_type;diff --git a/UI/Qt/Menu.cpp b/UI/Qt/Menu.cppindex a5bb5c7b6f6..0bc44b446bf 100644--- a/UI/Qt/Menu.cpp+++ b/UI/Qt/Menu.cpp@@ -17,6 +17,7 @@#include <QAction>#include <QMenu>#include <QPointer>+#include <QTimer>#include <QToolButton>#include <QWidget>@@ -391,6 +392,27 @@ QMenu* create_application_menu(QWidget& parent, WebView::Menu& menu)return application_menu;}+QMenu& replace_context_menu(QPointer<QMenu>& shown_menu, QWidget& parent, WebView::Menu& menu)+{+ // NB: A menu is kept until another is shown in its place, as Cocoa delivers a native menu's choice through queued+ // events after the menu closes.+ if (shown_menu)+ shown_menu->deleteLater();+ shown_menu = create_application_menu(parent, menu);+ return *shown_menu;+}++void execute_context_menu_later(QPointer<QMenu>& shown_menu, QWidget& parent, WebView::Menu& menu, QPoint const& global_position)+{+ auto& context_menu = replace_context_menu(shown_menu, parent, menu);++ // NB: Showing a menu runs a nested event loop, which may destroy whatever asked for the menu. So it runs with+ // nothing of the caller's on the stack.+ QTimer::singleShot(0, &context_menu, [&context_menu, global_position] {+ execute_context_menu(context_menu, global_position);+ });+}+void repopulate_application_menu(QMenu& menu, WebView::Menu& source){// NB: A submenu's action belongs to the submenu, so clearing the menu only removes it.diff --git a/UI/Qt/Menu.h b/UI/Qt/Menu.hindex 49a2fdf68bb..e5fe28965a5 100644--- a/UI/Qt/Menu.h+++ b/UI/Qt/Menu.h@@ -9,6 +9,8 @@#include <AK/Platform.h>#include <LibWebView/Menu.h>+#include <QPointer>+class QAction;class QMenu;class QPoint;@@ -35,5 +37,7 @@ void enable_menu_icons(QMenu&);#endifvoid execute_context_menu(QMenu&, QPoint const& global_position);+QMenu& replace_context_menu(QPointer<QMenu>& shown_menu, QWidget& parent, WebView::Menu&);+void execute_context_menu_later(QPointer<QMenu>& shown_menu, QWidget& parent, WebView::Menu&, QPoint const& global_position);}diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex aae637b0e5c..ceadaacf22d 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -2348,18 +2348,7 @@ Gfx::IntPoint WebContentView::to_widget_position(Gfx::IntPoint content_position)void WebContentView::show_context_menu(WebView::Menu& menu, Gfx::IntPoint widget_position){- // NB: A menu is kept until the view shows another, as Cocoa delivers a native menu's choice through queued events- // after the menu closes.- if (m_context_menu)- m_context_menu->deleteLater();- m_context_menu = create_application_menu(*this, menu);- auto global_position = map_point_to_global_position(widget_position);-- // NB: Showing a menu runs a nested event loop, which may destroy the view. So it runs with nothing of the view's on the- // stack.- QTimer::singleShot(0, m_context_menu, [context_menu = m_context_menu.data(), global_position] {- execute_context_menu(*context_menu, global_position);- });+ execute_context_menu_later(m_context_menu, *this, menu, map_point_to_global_position(widget_position));}#if defined(LADYBIRD_QT_USE_IOSURFACE_LAYER) || defined(LADYBIRD_HAS_WAYLAND_GPU_QUERY)