Skip to content

Truly independent web browser

UI/Qt: Open a middle-clicked bookmark menu entry in a new tab only

Tim Ledbettera941af3abdf4
diff --git a/UI/Qt/BookmarksBar.cpp b/UI/Qt/BookmarksBar.cpp
index 9e920f05cb4..848c2f9df73 100644
--- a/UI/Qt/BookmarksBar.cpp
+++ b/UI/Qt/BookmarksBar.cpp
@@ -201,6 +201,12 @@ static void install_menu_event_filter(QObject* filter, QMenu* menu)
}
}
+static void close_menu_chain(QMenu& menu)
+{
+ for (auto* open_menu = &menu; open_menu; open_menu = as_if<QMenu>(open_menu->parentWidget()))
+ open_menu->close();
+}
+
BookmarksBar::BookmarksBar(QWidget* parent)
: QToolBar(parent)
{
@@ -439,8 +445,7 @@ bool BookmarksBar::eventFilter(QObject* object, QEvent* event)
// NB: A folder's menu is its button's action's.
auto* folder_menu = button && button->defaultAction() ? button->defaultAction()->menu() : nullptr;
if (folder_menu && !folder_menu->isVisible()) {
- for (auto* open_menu = menu; open_menu; open_menu = as_if<QMenu>(open_menu->parentWidget()))
- open_menu->close();
+ close_menu_chain(*menu);
QTimer::singleShot(0, button, [button = QPointer { button }] {
if (button)
button->showMenu();
@@ -452,6 +457,7 @@ bool BookmarksBar::eventFilter(QObject* object, QEvent* event)
if (event->type() == QEvent::MouseButtonPress) {
auto& mouse_event = as<QMouseEvent>(*event);
+ m_opened_new_tab_on_menu_press = false;
if (mouse_event.button() == Qt::LeftButton)
return handle_left_mouse_click(&mouse_event, object);
@@ -461,6 +467,14 @@ bool BookmarksBar::eventFilter(QObject* object, QEvent* event)
return handle_right_mouse_click(&mouse_event, object);
}
+ // Qt takes a release whose press it did not see as a choice of the entry.
+ if (m_opened_new_tab_on_menu_press && event->type() == QEvent::MouseButtonRelease && as_if<QMenu>(object)) {
+ m_opened_new_tab_on_menu_press = false;
+ if (auto* popup = as_if<QMenu>(QApplication::activePopupWidget()))
+ close_menu_chain(*popup);
+ return true;
+ }
+
return QToolBar::eventFilter(object, event);
}
@@ -485,8 +499,10 @@ bool BookmarksBar::handle_middle_mouse_click(QMouseEvent* event, QObject* item)
if (auto* action = menu->actionAt(event->pos())) {
extract_item_properties(action);
- if (m_selected_bookmark_menu_item_type == "bookmark")
+ if (m_selected_bookmark_menu_item_type == "bookmark") {
Application::bookmarks().open_in_new_tab(m_selected_bookmark_menu_item_id, activate_tab);
+ m_opened_new_tab_on_menu_press = true;
+ }
}
}
diff --git a/UI/Qt/BookmarksBar.h b/UI/Qt/BookmarksBar.h
index 6c21730a952..b1c0c86eb8a 100644
--- a/UI/Qt/BookmarksBar.h
+++ b/UI/Qt/BookmarksBar.h
@@ -66,6 +66,8 @@ private:
Optional<String> m_selected_bookmark_menu_target_folder_id;
Optional<String> m_selected_bookmark_menu_parent_folder_id;
+ bool m_opened_new_tab_on_menu_press { false };
+
QPointer<QToolButton> m_overflow_button;
};