Skip to content

Truly independent web browser

UI/Qt: Duplicate the tab on Ctrl/Cmd-click of the navigation buttons

Luke Wilde1caf206d1d8d
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 7d147ebef49..ccaafb08cc2 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -669,7 +669,7 @@ void BrowserWindow::open_new_tab()
tab.focus_location_editor(focus_ring_transition);
}
-void BrowserWindow::duplicate_tab(Tab& source_tab)
+void BrowserWindow::duplicate_tab(Tab& source_tab, Web::HTML::ActivateTab activate_tab, Optional<i32> history_step)
{
// A tab that hasn't loaded keeps its history in the session store.
Optional<WebView::SessionHistorySnapshot> history;
@@ -677,23 +677,33 @@ void BrowserWindow::duplicate_tab(Tab& source_tab)
history = source_tab.view().session_history_snapshot();
else if (auto session_tab_id = source_tab.session_tab_id(); session_tab_id.has_value())
history = m_session->session_store->load_tab_history(*session_tab_id);
- auto source_url = source_tab.url();
+ auto fallback_url = source_tab.url();
- auto& duplicate = create_new_tab(Web::HTML::ActivateTab::Yes, TabLocation::after_tab(source_tab));
+ if (history.has_value() && history_step.has_value()) {
+ auto step_index = history->used_steps.find_first_index(*history_step);
+ if (!step_index.has_value())
+ return;
+ history->current_used_step_index = *step_index;
+
+ auto top_level_entry = history->entries.last_matching([&](auto const& entry) { return entry.step <= *history_step; });
+ if (top_level_entry.has_value())
+ fallback_url = WebView::display_url_for_session_history_entry(*top_level_entry);
+ }
+
+ auto& duplicate = create_new_tab(activate_tab, TabLocation::after_tab(source_tab));
if (!history.has_value() || duplicate.view().restore_session_history_from_snapshot(history.release_value()).is_error())
- duplicate.navigate(source_url);
+ duplicate.navigate(fallback_url);
}
-void BrowserWindow::open_history_entry_in_new_tab(Tab& source_tab, int delta)
+void BrowserWindow::open_history_entry_in_new_tab(Tab& source_tab, int delta, Web::HTML::ActivateTab activate_tab)
{
- 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())
+ auto const& traversable = source_tab.view().traversable();
+ auto used_steps = traversable.session_history().used_steps();
+ auto step_index = traversable.effective_current_session_history_step_index() + delta;
+ if (step_index >= 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));
+ duplicate_tab(source_tab, activate_tab, used_steps[step_index]);
}
void BrowserWindow::restore_tabs(Vector<WebView::RestoredSessionTab> const& tabs, i64 active_tab_index)
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index a3aef9b3859..680e0b23f76 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -172,8 +172,8 @@ public:
void activate_first_unpinned_tab();
Tab& create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocation);
- void duplicate_tab(Tab&);
- void open_history_entry_in_new_tab(Tab&, int delta);
+ void duplicate_tab(Tab&, Web::HTML::ActivateTab = Web::HTML::ActivateTab::Yes, Optional<i32> history_step = {});
+ void open_history_entry_in_new_tab(Tab&, int delta, Web::HTML::ActivateTab);
void update_tab_audio_button(Tab&);
// Opens a window's restored tabs, keeping the session rows the store re-adopted for them.
void restore_tabs(Vector<WebView::RestoredSessionTab> const&, i64 active_tab_index);
diff --git a/UI/Qt/Menu.cpp b/UI/Qt/Menu.cpp
index 93def432c7b..9c29d4cea80 100644
--- a/UI/Qt/Menu.cpp
+++ b/UI/Qt/Menu.cpp
@@ -347,7 +347,7 @@ static void add_items_to_menu(QMenu& qmenu, QWidget& parent, WebView::Menu& menu
#endif
}
-static QAction* create_session_history_traversal_menu_action(QMenu& menu, WebContentView& view, WebView::ViewImplementation::SessionHistoryTraversalMenuItem const& item)
+static QAction* create_session_history_traversal_menu_action(QMenu& menu, WebView::ViewImplementation::SessionHistoryTraversalMenuItem const& item)
{
static constexpr int const MENU_ICON_SIZE = 16;
@@ -357,9 +357,7 @@ static QAction* create_session_history_traversal_menu_action(QMenu& menu, WebCon
action->setIcon(icon_from_png(item.favicon_png->bytes(), MENU_ICON_SIZE));
else
action->setIcon(create_chrome_icon(ChromeIcon::Globe, menu.palette()));
- QObject::connect(action, &QAction::triggered, &view, [&view, step = item.step] {
- view.traverse_the_history_to_step(step);
- });
+ action->setData(item.step);
return action;
}
@@ -370,7 +368,7 @@ static bool append_session_history_traversal_menu_items(QMenu& menu, WebContentV
return false;
for (auto const& item : items)
- menu.addAction(create_session_history_traversal_menu_action(menu, view, item));
+ menu.addAction(create_session_history_traversal_menu_action(menu, item));
#if defined(AK_OS_MACOS)
enable_menu_icons(menu);
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 8a333ce2d54..a4b8413b39b 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -73,6 +73,11 @@ namespace Ladybird {
static constexpr auto WINDOW_DRAG_REGION_PROPERTY = "LadybirdWindowDragRegion";
static constexpr int BOOKMARKS_BAR_HOVER_COLLAPSE_POLL_INTERVAL_MS = 250;
+static Web::HTML::ActivateTab activate_tab_for_modifiers(Qt::KeyboardModifiers modifiers)
+{
+ return modifiers.testFlag(Qt::ShiftModifier) ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No;
+}
+
class ToolbarButton : public QToolButton {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -84,7 +89,12 @@ public:
QObject::connect(&m_icon_motion_animation, &QVariantAnimation::finished, this, [this] { update(); });
}
- Function<void()> on_middle_click;
+ Function<void(Web::HTML::ActivateTab)> on_open_in_new_tab;
+
+ bool click_opens_in_new_tab() const
+ {
+ return can_open_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ControlModifier);
+ }
// A click or a change of state moves the icon a little: back and forward nudge their way, and the downloads arrow
// drops into place as the button appears and bobs once as a download finishes.
@@ -122,6 +132,8 @@ public:
}
protected:
+ virtual bool can_open_in_new_tab() const { return static_cast<bool>(on_open_in_new_tab); }
+
QRect icon_rect() const
{
return QRect(QPoint((width() - iconSize().width()) / 2, (height() - iconSize().height()) / 2), iconSize());
@@ -179,7 +191,7 @@ protected:
virtual void mousePressEvent(QMouseEvent* event) override
{
- if (event->button() == Qt::MiddleButton && on_middle_click)
+ if (event->button() == Qt::MiddleButton && can_open_in_new_tab())
event->accept();
else
QToolButton::mousePressEvent(event);
@@ -187,15 +199,25 @@ protected:
virtual void mouseReleaseEvent(QMouseEvent* event) override
{
- if (event->button() == Qt::MiddleButton && on_middle_click) {
+ if (event->button() == Qt::MiddleButton && can_open_in_new_tab()) {
if (rect().contains(event->position().toPoint()))
- on_middle_click();
+ on_open_in_new_tab(activate_tab_for_modifiers(event->modifiers()));
event->accept();
} else {
QToolButton::mouseReleaseEvent(event);
}
}
+ // QToolButton triggers its default action from here, once a click has completed.
+ virtual void nextCheckState() override
+ {
+ if (click_opens_in_new_tab()) {
+ on_open_in_new_tab(activate_tab_for_modifiers(QApplication::keyboardModifiers()));
+ return;
+ }
+ QToolButton::nextCheckState();
+ }
+
private:
static constexpr int NUDGE_DURATION_MS = 180;
static constexpr qreal NUDGE_DISTANCE = 2;
@@ -260,6 +282,11 @@ public:
}
protected:
+ virtual bool can_open_in_new_tab() const override
+ {
+ return m_visible_mode == Mode::Reload && ToolbarButton::can_open_in_new_tab();
+ }
+
virtual void leaveEvent(QEvent* event) override
{
ToolbarButton::leaveEvent(event);
@@ -267,12 +294,12 @@ protected:
}
// A Shift-click on the reload button reloads bypassing the HTTP cache, and one on the stop button stops the load as
- // any other click there does. QToolButton triggers its default action from here, once a click has completed.
+ // any other click there does.
virtual void nextCheckState() override
{
if (m_visible_mode == Mode::Stop && m_double_click_timer.isActive())
return;
- if (m_visible_mode == Mode::Reload && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {
+ if (m_visible_mode == Mode::Reload && !click_opens_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {
m_reload_bypassing_cache_action->trigger();
return;
}
@@ -522,17 +549,25 @@ QToolButton* Tab::create_navigation_history_toolbar_button(QWidget& parent, QAct
{
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);
+ button->on_open_in_new_tab = [this, direction](auto activate_tab) {
+ m_window->open_history_entry_in_new_tab(*this, direction, activate_tab);
};
auto* menu = new QMenu(button);
QObject::connect(menu, &QMenu::aboutToShow, button, [menu, &view, direction] {
populate_session_history_traversal_menu(*menu, view, direction);
});
+ QObject::connect(menu, &QMenu::triggered, button, [this, &view](QAction* action) {
+ auto step = action->data().toInt();
+ if (auto modifiers = QApplication::keyboardModifiers(); modifiers.testFlag(Qt::ControlModifier))
+ m_window->duplicate_tab(*this, activate_tab_for_modifiers(modifiers), step);
+ else
+ view.traverse_the_history_to_step(step);
+ });
button->setMenu(menu);
button->setPopupMode(QToolButton::DelayedPopup);
QObject::connect(button, &QToolButton::clicked, button, [button, direction] {
- button->play_icon_motion(direction < 0 ? ToolbarButton::IconMotion::NudgeLeft : ToolbarButton::IconMotion::NudgeRight);
+ if (!button->click_opens_in_new_tab())
+ button->play_icon_motion(direction < 0 ? ToolbarButton::IconMotion::NudgeLeft : ToolbarButton::IconMotion::NudgeRight);
});
button->setContextMenuPolicy(Qt::CustomContextMenu);
QObject::connect(button, &QToolButton::customContextMenuRequested, button, [button, menu, &view, direction](QPoint const&) {
@@ -1242,6 +1277,9 @@ void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)
navigation_button_layout->addWidget(create_navigation_history_toolbar_button(*navigation_button_cluster, *m_navigate_forward_action, 1));
m_reload_button = create_toolbar_button<ReloadButton>(*navigation_button_cluster, *m_reload_action);
m_reload_button->set_actions(*m_reload_action, *m_reload_bypassing_cache_action, *m_stop_action);
+ m_reload_button->on_open_in_new_tab = [this](auto activate_tab) {
+ m_window->duplicate_tab(*this, activate_tab);
+ };
navigation_button_layout->addWidget(m_reload_button);
auto* location_edit_container = new QWidget(m_toolbar);