Skip to content

Truly independent web browser

UI/Qt: Restore pinned tabs at launch

sideshowbarkerc357ea3e3eea
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cpp
index e4f4164a8dd..bbb23671562 100644
--- a/UI/Qt/Application.cpp
+++ b/UI/Qt/Application.cpp
@@ -662,11 +662,6 @@ Optional<String> Application::system_font_family() const
BrowserWindow& Application::new_window(Vector<URL::URL> const& initial_urls, WindowConfiguration const& configuration, BrowserWindow::IsPopupWindow is_popup_window, WebView::IsPrivate is_private, Tab* parent_tab, Optional<WebView::CanonicalTraversable&> traversable, ShowWindow show_window, Optional<Web::HTML::PreparedNavigationDescriptor> initial_navigation)
{
auto* window = new BrowserWindow(initial_urls, is_popup_window, is_private, parent_tab, traversable);
- set_active_window(*window);
- QObject::connect(window, &QObject::destroyed, m_application.ptr(), [this, window] {
- if (m_active_window == window)
- m_active_window = nullptr;
- });
auto should_focus_location_editor = initial_urls.size() == 1 && initial_urls.first() == WebView::Application::settings().new_tab_page_url();
if (should_focus_location_editor) {
@@ -674,16 +669,7 @@ BrowserWindow& Application::new_window(Vector<URL::URL> const& initial_urls, Win
tab->set_url_is_hidden(true);
}
- window->set_window_rect(configuration.x, configuration.y, configuration.width, configuration.height);
- if (show_window == ShowWindow::Yes) {
- if (configuration.maximized == true)
- window->showMaximized();
- else
- window->show();
-
- window->activateWindow();
- window->raise();
- }
+ present_window(*window, configuration, show_window);
size_t initial_url_index = 0;
window->for_each_tab([&](Tab& tab) {
@@ -704,6 +690,64 @@ BrowserWindow& Application::new_window(Vector<URL::URL> const& initial_urls, Win
return *window;
}
+void Application::present_window(BrowserWindow& window, WindowConfiguration const& configuration, ShowWindow show_window)
+{
+ set_active_window(window);
+ QObject::connect(&window, &QObject::destroyed, m_application.ptr(), [this, window = &window] {
+ if (m_active_window == window)
+ m_active_window = nullptr;
+ });
+
+ window.set_window_rect(configuration.x, configuration.y, configuration.width, configuration.height);
+ if (show_window == ShowWindow::Yes) {
+ if (configuration.maximized == true)
+ window.showMaximized();
+ else
+ window.show();
+
+ window.activateWindow();
+ window.raise();
+ }
+}
+
+WebView::SessionLaunchPolicy Application::launch_restore_policy() const
+{
+ return WebView::SessionLaunchPolicy::PinnedOnly;
+}
+
+BrowserWindow* Application::restore_previous_session(WindowConfiguration const& frontmost_window_configuration)
+{
+ auto plan = WebView::Application::default_session().session_store->take_launch_plan();
+
+ BrowserWindow* frontmost_window = nullptr;
+ for (size_t i = 0; i < plan.size(); ++i) {
+ auto const& restored = plan[i];
+ // The plan lists the frontmost window last, and opening the windows in that order leaves it on top.
+ auto configuration = i == plan.size() - 1 ? frontmost_window_configuration : configuration_for_new_window();
+
+ auto* window = new BrowserWindow({}, BrowserWindow::IsPopupWindow::No, WebView::IsPrivate::No, nullptr, {}, restored.window_id);
+ window->restore_tabs(restored.tabs, restored.active_tab_index);
+ present_window(*window, configuration, ShowWindow::Yes);
+ frontmost_window = window;
+ }
+ return frontmost_window;
+}
+
+void Application::open_startup_tabs(BrowserWindow& window, Vector<URL::URL> const& urls)
+{
+ auto const& new_tab_page_url = WebView::Application::settings().new_tab_page_url();
+ for (size_t i = 0; i < urls.size(); ++i) {
+ auto& tab = window.new_tab_from_url(urls[i], i == 0 ? Web::HTML::ActivateTab::Yes : Web::HTML::ActivateTab::No, BrowserWindow::TabLocation::end());
+ if (urls.size() == 1 && urls[i] == new_tab_page_url) {
+ tab.set_url_is_hidden(true);
+ QTimer::singleShot(0, &window, [tab = QPointer<Tab>(&tab)] {
+ if (tab)
+ tab->focus_location_editor();
+ });
+ }
+ }
+}
+
BrowserWindow* Application::non_private_window_if_any() const
{
if (m_active_window && m_active_window->is_private() == WebView::IsPrivate::No)
diff --git a/UI/Qt/Application.h b/UI/Qt/Application.h
index fff89844ef7..d7552c7b3ba 100644
--- a/UI/Qt/Application.h
+++ b/UI/Qt/Application.h
@@ -53,6 +53,11 @@ public:
BrowserWindow& new_window(Vector<URL::URL> const& initial_urls, WindowConfiguration const& = {}, BrowserWindow::IsPopupWindow is_popup_window = BrowserWindow::IsPopupWindow::No, WebView::IsPrivate = WebView::IsPrivate::No, Tab* parent_tab = nullptr, Optional<WebView::CanonicalTraversable&> traversable = {}, ShowWindow = ShowWindow::Yes, Optional<Web::HTML::PreparedNavigationDescriptor> initial_navigation = {});
WindowConfiguration configuration_for_new_window() const;
+ // Opens the windows startup recovery kept open, and returns the frontmost one, if any.
+ BrowserWindow* restore_previous_session(WindowConfiguration const& frontmost_window_configuration);
+ void open_startup_tabs(BrowserWindow&, Vector<URL::URL> const&);
+ virtual WebView::SessionLaunchPolicy launch_restore_policy() const override;
+
void open_new_tab();
void open_new_window(WebView::IsPrivate);
void reopen_recently_closed_tab(BrowserWindow* target_window);
@@ -108,6 +113,7 @@ public:
QAction* quit_action();
private:
+ void present_window(BrowserWindow&, WindowConfiguration const&, ShowWindow);
explicit Application();
void ask_next_window_to_close_for_private_session_restart();
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 39d8dc858be..4547154096a 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -197,7 +197,7 @@ static QIcon const& app_icon()
return icon;
}
-BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window, WebView::IsPrivate is_private, Tab* parent_tab, Optional<WebView::CanonicalTraversable&> traversable)
+BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window, WebView::IsPrivate is_private, Tab* parent_tab, Optional<WebView::CanonicalTraversable&> traversable, Optional<WebView::SessionWindowId> restored_session_window_id)
: m_is_private(is_private)
, m_session(WebView::Application::session_for_new_view(is_private))
, m_tabs_container(new TabWidget(this))
@@ -205,7 +205,11 @@ BrowserWindow::BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow
{
auto const& browser_options = WebView::Application::browser_options();
- register_window_with_session_store();
+ // A restored window keeps the session row the store re-adopted for it.
+ if (restored_session_window_id.has_value())
+ m_session_window_id = restored_session_window_id;
+ else
+ register_window_with_session_store();
#if defined(AK_OS_MACOS)
setWindowFlag(Qt::ExpandedClientAreaHint);
@@ -543,6 +547,39 @@ void BrowserWindow::open_history_entry_in_new_tab(Tab& source_tab, int delta)
(void)tab.view().restore_session_history_from_snapshot(AK::move(history));
}
+void BrowserWindow::restore_tabs(Vector<WebView::RestoredSessionTab> const& tabs, i64 active_tab_index)
+{
+ if (tabs.is_empty())
+ return;
+
+ // The first tab a strip gets becomes its current tab, so the active tab goes in first, at what's then index 0, and
+ // every other tab goes in around it at its own index.
+ auto active_index = static_cast<size_t>(clamp<i64>(active_tab_index, 0, static_cast<i64>(tabs.size()) - 1));
+ create_restored_tab(tabs[active_index], 0);
+ for (size_t i = 0; i < tabs.size(); ++i) {
+ if (i != active_index)
+ create_restored_tab(tabs[i], static_cast<int>(i));
+ }
+}
+
+Tab& BrowserWindow::create_restored_tab(WebView::RestoredSessionTab const& restored, int index)
+{
+ auto* tab = new Tab(this);
+ // The tab already has a row, so initialize_tab() doesn't register a new one.
+ tab->view().set_session_tab_id(restored.tab_id);
+ tab->set_pinned(restored.pinned);
+
+ if (!m_current_tab)
+ set_current_tab(tab);
+ m_tabs_container->insert_tab(index, tab, "New Tab");
+ initialize_tab(tab);
+
+ auto history = m_session->session_store->load_tab_history(restored.tab_id);
+ if (!history.has_value() || tab->view().restore_session_history_from_snapshot(history.release_value()).is_error())
+ tab->navigate(restored.url);
+ return *tab;
+}
+
Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, Optional<WebView::CanonicalTraversable&> traversable)
{
cancel_window_close_preflight();
@@ -1318,6 +1355,8 @@ bool BrowserWindow::event(QEvent* event)
if (event->type() == QEvent::WindowActivate) {
Application::the().set_active_window(*this);
WebView::Application::the().update_actions_for_active_web_view();
+ if (m_session_window_id.has_value())
+ m_session->session_store->window_activated(*m_session_window_id);
QTimer::singleShot(0, this, [this] {
refresh_resize_cursor_at_current_position(true);
Application::the().crash_report_notifier().show_pending();
diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.h
index 435531ff3c6..b0984896fd2 100644
--- a/UI/Qt/BrowserWindow.h
+++ b/UI/Qt/BrowserWindow.h
@@ -139,7 +139,7 @@ public:
int m_index { 0 };
};
- BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window = IsPopupWindow::No, WebView::IsPrivate = WebView::IsPrivate::No, Tab* parent_tab = nullptr, Optional<WebView::CanonicalTraversable&> traversable = {});
+ BrowserWindow(Vector<URL::URL> const& initial_urls, IsPopupWindow is_popup_window = IsPopupWindow::No, WebView::IsPrivate = WebView::IsPrivate::No, Tab* parent_tab = nullptr, Optional<WebView::CanonicalTraversable&> traversable = {}, Optional<WebView::SessionWindowId> restored_session_window_id = {});
virtual ~BrowserWindow() override;
WebContentView& view() const { return m_current_tab->view(); }
@@ -162,6 +162,8 @@ 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);
+ // 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);
Tab* current_tab() const { return m_current_tab; }
Tab* activate_tab_with_url(URL::URL const&, URL::ExcludeFragment);
// Asks every tab's page if the window may close, without closing it, and reports the answer. Once the pages agree,
@@ -233,6 +235,7 @@ private:
virtual void config_variable_changed(WebView::ConfigVariableID) override;
Tab& create_new_tab(Web::HTML::ActivateTab, Optional<WebView::CanonicalTraversable&>);
+ Tab& create_restored_tab(WebView::RestoredSessionTab const&, int index);
void initialize_tab(Tab*);
void uninitialize_tab(Tab*);
void update_window_title(QString const&);
diff --git a/UI/Qt/main.cpp b/UI/Qt/main.cpp
index 1da90f9c47b..2a0f510b241 100644
--- a/UI/Qt/main.cpp
+++ b/UI/Qt/main.cpp
@@ -131,8 +131,15 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
app->new_window(urls, app->configuration_for_new_window());
};
- auto& window = app->new_window(browser_options.urls, app->configuration_for_new_window());
- window.setWindowTitle("Ladybird");
+ // The last run's pinned tabs come back first, and the usual startup tabs then open after them, in the
+ // frontmost restored window.
+ auto configuration = app->configuration_for_new_window();
+ if (auto* restored_window = app->restore_previous_session(configuration)) {
+ app->open_startup_tabs(*restored_window, browser_options.urls);
+ } else {
+ auto& window = app->new_window(browser_options.urls, configuration);
+ window.setWindowTitle("Ladybird");
+ }
app->offer_newest_pending_crash_report();
}