Skip to content

Truly independent web browser

UI/Qt: Open history entries in a new tab on middle-click

Shannon Booth38531538077c
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index defdd054c74..2ec9fe92234 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -477,6 +477,18 @@ void BrowserWindow::duplicate_tab(Tab& source_tab)
duplicate.navigate(source_url);
}
+void BrowserWindow::open_history_entry_in_new_tab(Tab& source_tab, int delta)
+{
+ auto& view = source_tab.view();
+ auto history = view.session_history_snapshot();
+ history.current_used_step_index = view.traversable().effective_current_session_history_step_index() + delta;
+ if (history.current_used_step_index >= history.used_steps.size())
+ return;
+
+ auto& tab = create_new_tab(Web::HTML::ActivateTab::No, TabLocation::after_tab(source_tab));
+ (void)tab.view().restore_session_history_from_snapshot(AK::move(history));
+}
+
Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Optional<WebView::CanonicalTraversable&> traversable)
{
cancel_window_close_preflight();
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index ce6832e91d6..63ba67d1f2b 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -150,6 +150,7 @@ public:
Tab& create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocation);
void duplicate_tab(Tab&);
+ void open_history_entry_in_new_tab(Tab&, int delta);
Tab* current_tab() const { return m_current_tab; }
bool activate_tab_with_url(URL::URL const&);
// Asks every tab's page if the window may close, without closing it, and reports the answer. Once the pages agree,
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 2c327c01c0e..5d852592cbc 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -69,6 +69,8 @@ public:
using QToolButton::QToolButton;
+ Function<void()> on_middle_click;
+
protected:
virtual void paintEvent(QPaintEvent*) override
{
@@ -80,6 +82,25 @@ protected:
painter.drawComplexControl(QStyle::CC_ToolButton, option);
}
+
+ virtual void mousePressEvent(QMouseEvent* event) override
+ {
+ if (event->button() == Qt::MiddleButton && on_middle_click)
+ event->accept();
+ else
+ QToolButton::mousePressEvent(event);
+ }
+
+ virtual void mouseReleaseEvent(QMouseEvent* event) override
+ {
+ if (event->button() == Qt::MiddleButton && on_middle_click) {
+ if (rect().contains(event->position().toPoint()))
+ on_middle_click();
+ event->accept();
+ } else {
+ QToolButton::mouseReleaseEvent(event);
+ }
+ }
};
class HamburgerButton final : public ToolbarButton {
@@ -192,7 +213,7 @@ private:
static constexpr int TOOLBAR_BUTTON_SIZE = 34;
-static QToolButton* create_toolbar_button(QWidget& parent, QAction& action)
+static ToolbarButton* create_toolbar_button(QWidget& parent, QAction& action)
{
auto* button = new ToolbarButton(&parent);
button->setDefaultAction(&action);
@@ -208,9 +229,13 @@ static QToolButton* create_toolbar_button(QWidget& parent, QAction& action)
return button;
}
-static QToolButton* create_navigation_history_toolbar_button(QWidget& parent, QAction& action, WebContentView& view, int direction)
+QToolButton* Tab::create_navigation_history_toolbar_button(QWidget& parent, QAction& action, int direction)
{
+ auto& view = this->view();
auto* button = create_toolbar_button(parent, action);
+ button->on_middle_click = [this, direction] {
+ m_window->open_history_entry_in_new_tab(*this, direction);
+ };
auto* menu = new QMenu(button);
QObject::connect(menu, &QMenu::aboutToShow, button, [menu, &view, direction] {
populate_session_history_traversal_menu(*menu, view, direction);
@@ -737,8 +762,8 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers
navigation_button_layout->addWidget(m_left_toggle_vertical_tabs_expanded_button);
navigation_button_layout->addItem(m_sidebar_toggle_navigation_spacer);
- navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_back_action, view(), -1));
- navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_forward_action, view(), 1));
+ navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_back_action, -1));
+ navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_forward_action, 1));
navigation_button_layout->addWidget(create_toolbar_button(*navigation_button_cluster, *m_reload_action));
auto* location_edit_container = new QWidget(m_toolbar);
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index dff8b032d35..1a254ac2213 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -153,6 +153,7 @@ private:
void recreate_toolbar_icons();
void update_vertical_tabs_toolbar_button_placement();
void connect_hamburger_menu();
+ QToolButton* create_navigation_history_toolbar_button(QWidget& parent, QAction&, int direction);
void update_chrome_style();
void update_tab_title();
void update_downloads_button();