Truly independent web browser
LibWebView+UI/Qt: Load a restored tab only once it’s selected
sideshowbarker4681de5ef1b2
diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex 4dbd1be97fc..fcdc09247d8 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -485,7 +485,7 @@ private:};void create_bookmark_menu_items(Optional<MenuData> = {});- Vector<BookmarkItem::Bookmark> bookmarks_for_all_tabs_in_current_window() const;+ virtual Vector<BookmarkItem::Bookmark> bookmarks_for_all_tabs_in_current_window() const;virtual Vector<DevTools::TabDescription> tab_list() const override;virtual Vector<DevTools::CSSProperty> css_property_list() const override;diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 4aed99d00ae..7ea3bc21c3b 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -1173,15 +1173,37 @@ Optional<WebView::ViewImplementation&> Application::active_web_view() constreturn {};}-Vector<WebView::ViewImplementation&> Application::active_window_web_views() const+Vector<WebView::BookmarkItem::Bookmark> Application::bookmarks_for_all_tabs_in_current_window() const{if (!m_active_window)return {};- Vector<WebView::ViewImplementation&> web_views;- m_active_window->for_each_tab([&](Tab& tab) { web_views.append(tab.view()); });+ // Every tab gets a bookmark, so this walks the tabs directly: for_each_tab() skips the ones that haven't loaded.+ Vector<WebView::BookmarkItem::Bookmark> bookmarks;+ for (int i = 0; i < m_active_window->tab_count(); ++i) {+ auto& tab = *m_active_window->tab_at(i);+ if (tab.is_loaded()) {+ auto const& view = tab.view();+ bookmarks.append(WebView::BookmarkItem::Bookmark {+ .url = view.url(),+ .title = view.title().is_empty() ? Optional<String> {} : view.title().to_utf8(),+ .favicon_hash = view.favicon_hash(),+ });+ continue;+ }- return web_views;+ // A tab that hasn't loaded shows the title browsing history has for its page, or else its URL, which isn't a+ // title worth keeping. The bookmark store fills in the favicon on a later visit.+ auto url = tab.url();+ auto title = tab.title();+ auto has_title = !title.isEmpty() && title != qstring_from_ak_string(url.serialize());+ bookmarks.append(WebView::BookmarkItem::Bookmark {+ .url = url,+ .title = has_title ? ak_string_from_qstring(title) : Optional<String> {},+ .favicon_hash = {},+ });+ }+ return bookmarks;}Optional<WebView::ViewImplementation&> Application::activate_tab_with_url(URL::URL const& url, URL::ExcludeFragment exclude_fragment) constdiff --git a/UI/Qt/Application.h b/UI/Qt/Application.hindex 902e8db53e4..066dd42a8bb 100644--- a/UI/Qt/Application.h+++ b/UI/Qt/Application.h@@ -138,7 +138,7 @@ private:virtual OwnPtr<WebView::PictureInPictureWindow> create_picture_in_picture_window(WebView::WebContentPage& requesting_page, WebView::CanonicalTraversable&, Gfx::IntSize video_size) override;virtual Optional<WebView::ViewImplementation&> active_web_view() const override;- virtual Vector<WebView::ViewImplementation&> active_window_web_views() const override;+ virtual Vector<WebView::BookmarkItem::Bookmark> bookmarks_for_all_tabs_in_current_window() const override;virtual Optional<WebView::ViewImplementation&> activate_tab_with_url(URL::URL const&, URL::ExcludeFragment) const override;virtual Optional<WebView::ViewImplementation&> open_blank_new_tab(Web::HTML::ActivateTab) const override;diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex ad0592fcb8d..346a8f5d9ea 100644--- a/UI/Qt/BrowserWindow.cpp+++ b/UI/Qt/BrowserWindow.cpp@@ -13,6 +13,7 @@#include <AK/StdLibExtras.h>#include <AK/TypeCasts.h>#include <LibWebView/Application.h>+#include <LibWebView/HistoryStore.h>#include <LibWebView/SessionStore.h>#include <LibWebView/URL.h>#include <UI/Qt/Application.h>@@ -527,11 +528,16 @@ void BrowserWindow::open_new_tab()void BrowserWindow::duplicate_tab(Tab& source_tab){- auto history = source_tab.view().session_history_snapshot();- auto source_url = source_tab.view().url();- auto& duplicate = create_new_tab(Web::HTML::ActivateTab::Yes, TabLocation::after_tab(source_tab));+ // A tab that hasn't loaded keeps its history in the session store.+ Optional<WebView::SessionHistorySnapshot> history;+ if (source_tab.is_loaded())+ 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();- if (duplicate.view().restore_session_history_from_snapshot(AK::move(history)).is_error())+ auto& duplicate = create_new_tab(Web::HTML::ActivateTab::Yes, 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);}@@ -555,18 +561,35 @@ void BrowserWindow::restore_tabs(Vector<WebView::RestoredSessionTab> const& tabs// 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);+ create_restored_tab(tabs[active_index], 0, false);for (size_t i = 0; i < tabs.size(); ++i) {if (i != active_index)- create_restored_tab(tabs[i], static_cast<int>(i));+ create_restored_tab(tabs[i], static_cast<int>(i), !tabs[i].pinned);}}-Tab& BrowserWindow::create_restored_tab(WebView::RestoredSessionTab const& restored, int index)+Tab& BrowserWindow::create_restored_tab(WebView::RestoredSessionTab const& restored, int index, bool defer){+ if (defer) {+ // Until the tab loads, it shows what browsing history knows about its page.+ auto entry = m_session->history_store->entry_for_url(restored.url);+ DeferredTab deferred_tab {+ .session_tab_id = restored.tab_id,+ .url = restored.url,+ .title = entry.has_value() && entry->title.has_value() ? qstring_from_ak_string(*entry->title) : qstring_from_ak_string(restored.url.serialize()),+ .favicon = entry.has_value() && entry->favicon_png.has_value() ? icon_from_png(entry->favicon_png->bytes(), TAB_ICON_SIZE) : QIcon {},+ };+ auto* tab = new Tab(this, AK::move(deferred_tab));+ tab->set_pinned(restored.pinned);+ m_tabs_container->insert_tab(index, tab, tab->title());+ initialize_tab(tab);+ tab_title_changed(m_tabs_container->index_of(tab), tab->title());+ return *tab;+ }+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_session_tab_id(restored.tab_id);tab->set_pinned(restored.pinned);if (!m_current_tab)@@ -656,7 +679,7 @@ Tab& BrowserWindow::create_new_tab(Web::HTML::ActivateTab activate_tab, TabLocatvoid BrowserWindow::initialize_tab(Tab* tab){- if (m_session_window_id.has_value() && !tab->view().session_tab_id().has_value()) {+ if (m_session_window_id.has_value() && !tab->session_tab_id().has_value()) {auto insertion_index = tab_index(tab);WebView::SessionStore::TabOpened opened {.window_id = m_session_window_id,@@ -668,7 +691,7 @@ void BrowserWindow::initialize_tab(Tab* tab)if (session_tab_id.is_error())dbgln("Unable to register the new tab with the session store: {}", session_tab_id.error());else- tab->view().set_session_tab_id(session_tab_id.value());+ tab->set_session_tab_id(session_tab_id.value());}QObject::connect(tab, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);@@ -681,6 +704,16 @@ void BrowserWindow::initialize_tab(Tab* tab)m_tabs_container->tab_bar()->play_load_sheen(*tab);});+ if (tab->is_loaded())+ initialize_tab_view(tab);+ else+ QObject::connect(tab, &Tab::loaded, this, [this, tab] { initialize_tab_view(tab); }, Qt::SingleShotConnection);++ initialize_tab_buttons(tab);+}++void BrowserWindow::initialize_tab_view(Tab* tab)+{QObject::connect(&tab->view(), &WebContentView::urls_dropped, this, [this](auto& urls) {VERIFY(urls.size());m_current_tab->view().load_from_user_input(ak_url_from_qurl(urls[0]));@@ -709,14 +742,13 @@ void BrowserWindow::initialize_tab(Tab* tab)auto& new_tab = create_new_tab(activate_tab, traversable);return new_tab.view().handle();};-- initialize_tab_buttons(tab);}void BrowserWindow::uninitialize_tab(Tab* tab){QObject::disconnect(tab, nullptr, this, nullptr);- QObject::disconnect(&tab->view(), nullptr, this, nullptr);+ if (tab->is_loaded())+ QObject::disconnect(&tab->view(), nullptr, this, nullptr);}void BrowserWindow::adopt_tab(Tab& tab, int index)@@ -728,10 +760,11 @@ void BrowserWindow::adopt_tab(Tab& tab, int index)tab.set_window(*this);m_tabs_container->insert_tab(index, &tab, "New Tab");- tab.view().finish_window_move();+ if (tab.is_loaded())+ tab.view().finish_window_move();if (!m_session_window_id.has_value())register_window_with_session_store();- auto moved_tab_id = tab.view().session_tab_id();+ auto moved_tab_id = tab.session_tab_id();initialize_tab(&tab);tab_title_changed(index, tab.title());@@ -748,12 +781,14 @@ void BrowserWindow::adopt_tab(Tab& tab, int index)// The source window's reported order no longer includes this tab; it must leave// tracking rather than stay mapped there.m_session->session_store->tab_detached(*moved_tab_id);- tab.view().clear_session_tab_id();+ tab.clear_session_tab_id();}}- tab.view().set_device_pixel_ratio(m_screen_observer->device_pixel_ratio());- tab.view().set_display_metadata(m_screen_observer->display_id(), m_screen_observer->refresh_rate());+ if (tab.is_loaded()) {+ tab.view().set_device_pixel_ratio(m_screen_observer->device_pixel_ratio());+ tab.view().set_display_metadata(m_screen_observer->display_id(), m_screen_observer->refresh_rate());+ }m_tabs_container->set_current_tab(&tab);}@@ -777,7 +812,8 @@ void BrowserWindow::move_tab_to_window(int index, BrowserWindow& target_window,cancel_window_close_preflight();auto* tab = m_tabs_container->tab(index);- tab->view().prepare_for_window_move();+ if (tab->is_loaded())+ tab->view().prepare_for_window_move();uninitialize_tab(tab);m_tabs_container->take_tab(index);if (m_current_tab == tab)@@ -820,6 +856,10 @@ void BrowserWindow::set_current_tab(Tab* tab)if (tab == m_current_tab)return;+ // A restored tab loads only once it's selected.+ if (tab)+ tab->ensure_loaded();+if (m_current_tab)m_current_tab->view().set_system_visibility_state(Web::HTML::VisibilityState::Hidden);@@ -828,7 +868,7 @@ void BrowserWindow::set_current_tab(Tab* tab)if (m_current_tab) {m_current_tab->view().set_system_visibility_state(Web::HTML::VisibilityState::Visible);- if (auto session_tab_id = m_current_tab->view().session_tab_id(); session_tab_id.has_value())+ if (auto session_tab_id = m_current_tab->session_tab_id(); session_tab_id.has_value())m_session->session_store->active_tab_changed(*session_tab_id);}@@ -844,7 +884,7 @@ Tab* BrowserWindow::activate_tab_with_url(URL::URL const& url, URL::ExcludeFragm{for (int index = 0; index < m_tabs_container->count(); ++index) {auto* tab = m_tabs_container->tab(index);- if (tab && tab->view().url().equals(url, exclude_fragment)) {+ if (tab && tab->url().equals(url, exclude_fragment)) {m_tabs_container->set_current_index(index);return tab;}@@ -863,7 +903,7 @@ bool BrowserWindow::definitely_close_tab(int index)auto* tab = m_tabs_container->tab(index);m_tabs_container->remove_tab(index);- if (auto session_tab_id = tab->view().session_tab_id(); session_tab_id.has_value()) {+ if (auto session_tab_id = tab->session_tab_id(); session_tab_id.has_value()) {WebView::SessionStore::TabClosed closed {.tab_id = *session_tab_id,.closed_at = UnixDateTime::now(),@@ -938,7 +978,7 @@ void BrowserWindow::unpin_tab(Tab& tab)void BrowserWindow::pinned_state_changed(Tab& tab){// A move already reported the new order, through the tab bar's tabMoved signal.- if (auto session_tab_id = tab.view().session_tab_id(); session_tab_id.has_value()) {+ if (auto session_tab_id = tab.session_tab_id(); session_tab_id.has_value()) {if (auto result = m_session->session_store->tab_pinned_changed(*session_tab_id, tab.is_pinned()); result.is_error())dbgln("Unable to record the tab's pin in the session store: {}", result.error());}@@ -968,7 +1008,7 @@ void BrowserWindow::sync_session_tab_order()Vector<WebView::SessionTabId> tab_order;tab_order.ensure_capacity(m_tabs_container->count());for (int i = 0; i < m_tabs_container->count(); ++i) {- if (auto session_tab_id = m_tabs_container->tab(i)->view().session_tab_id(); session_tab_id.has_value())+ if (auto session_tab_id = m_tabs_container->tab(i)->session_tab_id(); session_tab_id.has_value())tab_order.append(*session_tab_id);}diff --git a/UI/Qt/BrowserWindow.h b/UI/Qt/BrowserWindow.hindex 6feb5151b4a..3d7d58b835b 100644--- a/UI/Qt/BrowserWindow.h+++ b/UI/Qt/BrowserWindow.h@@ -185,8 +185,12 @@ public:template<typename Callback>void for_each_tab(Callback&& callback){- for (int i = 0; i < m_tabs_container->count(); ++i)- callback(*m_tabs_container->tab(i));+ for (int i = 0; i < m_tabs_container->count(); ++i) {+ // A tab that hasn't loaded has no view or toolbar to update, and it picks up the window's toolbar and+ // tab-bar state when it loads.+ if (auto* tab = m_tabs_container->tab(i); tab->is_loaded())+ callback(*tab);+ }}void update_tabs_display();@@ -236,8 +240,9 @@ 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);+ Tab& create_restored_tab(WebView::RestoredSessionTab const&, int index, bool defer);void initialize_tab(Tab*);+ void initialize_tab_view(Tab*);void uninitialize_tab(Tab*);void update_window_title(QString const&);void register_window_with_session_store();diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex d7466779cd4..d441c4d40a4 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -658,32 +658,57 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers: QWidget(window), m_window(window){- auto& application = WebView::Application::the();+ create_tab_shell();+ initialize(traversable);+}++Tab::Tab(BrowserWindow* window, DeferredTab deferred_tab)+ : QWidget(window)+ , m_window(window)+ , m_title(deferred_tab.title)+ , m_favicon(deferred_tab.favicon)+ , m_session_tab_id(deferred_tab.session_tab_id)+ , m_deferred_tab(AK::move(deferred_tab))+{+ create_tab_shell();+}+void Tab::create_tab_shell()+{auto* tab_layout = new QBoxLayout(QBoxLayout::Direction::TopToBottom, this);tab_layout->setSpacing(0);tab_layout->setContentsMargins(0, 0, 0, 0);+ // TabWidget keeps every tab's toolbar container in a stack beside its pages, so even a tab that hasn't loaded needs+ // one. initialize() fills it.+ m_toolbar_container = new QWidget(this);+ m_toolbar_container->setObjectName("LadybirdToolbarContainer");+ m_toolbar_container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);++ create_tab_context_menu();+}++void Tab::initialize(Optional<WebView::CanonicalTraversable&> traversable)+{+ auto& application = WebView::Application::the();+ auto* tab_layout = static_cast<QBoxLayout*>(layout());+auto view_initial_state = WebContentViewInitialState {- .is_private = window->is_private(),- .maximum_frames_per_second = window->refresh_rate(),- .display_id = window->display_id(),+ .is_private = m_window->is_private(),+ .maximum_frames_per_second = m_window->refresh_rate(),+ .display_id = m_window->display_id(),.owner_view = nullptr,};m_view = new WebContentView(this, traversable, AK::move(view_initial_state));// The window reports its geometry to its tabs as it is moved and resized, so a tab opened later has to be told// where the window stands now.- m_view->set_window_position({ window->x(), window->y() });- m_view->set_window_size({ window->width(), window->height() });+ m_view->set_window_position({ m_window->x(), m_window->y() });+ m_view->set_window_size({ m_window->width(), m_window->height() });m_javascript_dialog = new JavaScriptDialog(m_view);m_find_in_page = new FindInPageWidget(this, m_view);m_find_in_page->setVisible(false);- m_toolbar_container = new QWidget(this);- m_toolbar_container->setObjectName("LadybirdToolbarContainer");- m_toolbar_container->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Fixed);-m_toolbar = new QWidget(this);m_toolbar->setObjectName("LadybirdNavigationToolbar");m_toolbar->setFixedHeight(browser_chrome_layout_policy().toolbar_height);@@ -762,7 +787,7 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> travers#ifdef AK_OS_MACOSm_view_layout = new QHBoxLayout;m_view_layout->addWidget(m_view);- update_web_view_frame(true, {});+ update_web_view_frame(m_web_view_chrome_visible, m_web_view_sidebar_position);tab_layout->addLayout(m_view_layout);#elsetab_layout->addWidget(m_view);@@ -1341,6 +1366,11 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversview().on_screen_wake_lock_state_changed = [this](auto wake_lock_state) {set_screen_wake_lock_state(wake_lock_state);};+}++void Tab::create_tab_context_menu()+{+ auto& application = WebView::Application::the();auto* duplicate_tab_action = new QAction("&Duplicate Tab", this);QObject::connect(duplicate_tab_action, &QAction::triggered, this, [this]() {@@ -1380,6 +1410,10 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversauto* close_tabs_to_right_action = new QAction("Close Tabs to R&ight", this);QObject::connect(close_tabs_to_right_action, &QAction::triggered, this, [this]() {+ // Closing the current tab selects a neighbor, which may be a tab about to close too, and selecting a tab that+ // hasn't loaded loads it. So, this tab, which stays open, becomes the current tab first.+ if (auto* current = m_window->current_tab(); current && m_window->tab_index(current) > tab_index() && !current->is_pinned())+ m_window->activate_tab(tab_index());for (auto i = m_window->tab_count() - 1; i > tab_index(); i--) {if (!m_window->tab_at(i)->is_pinned())m_window->request_to_close_tab(i);@@ -1388,6 +1422,9 @@ Tab::Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversauto* close_other_tabs_action = new QAction("Cl&ose Other Tabs", this);QObject::connect(close_other_tabs_action, &QAction::triggered, this, [this]() {+ // As in Close Tabs to Right, this tab becomes the current tab first if the current tab is among those closing.+ if (auto* current = m_window->current_tab(); current && current != this && !current->is_pinned())+ m_window->activate_tab(tab_index());for (auto i = m_window->tab_count() - 1; i >= 0; i--) {if (i == tab_index() || m_window->tab_at(i)->is_pinned())continue;@@ -1416,11 +1453,75 @@ Tab::~Tab() = default;void Tab::park_location_editing(){+ // A tab that hasn't loaded has no omnibox yet, so it has no editing session to park.+ if (!is_loaded())+ return;m_location_edit->park_editing_session();}+void Tab::ensure_loaded()+{+ if (is_loaded())+ return;++ // Until initialize() has built the view, url() reads the deferred tab's URL, so the deferred tab stays until then.+ auto deferred_url = m_deferred_tab->url;+ // initialize() hides the toolbar's window controls, which overwrites the visibility TabWidget last asked for.+ auto toolbar_window_controls_visible = m_toolbar_window_controls_visible;+ initialize({});+ m_deferred_tab.clear();++ // TabWidget pushed these while the tab had no toolbar to apply them to.+ set_vertical_tabs_enabled(m_vertical_tabs_enabled);+ recreate_toolbar_icons();+ update_vertical_tabs_toolbar_button_placement();+ set_toolbar_window_controls_visible(toolbar_window_controls_visible);++ if (m_session_tab_id.has_value())+ view().set_session_tab_id(*m_session_tab_id);+ emit loaded();++ Optional<WebView::SessionHistorySnapshot> history;+ if (m_session_tab_id.has_value())+ history = m_window->session().session_store->load_tab_history(*m_session_tab_id);+ if (!history.has_value() || view().restore_session_history_from_snapshot(history.release_value()).is_error())+ navigate(deferred_url);+}++URL::URL Tab::url() const+{+ if (!is_loaded())+ return m_deferred_tab->url;+ return view().url();+}++Optional<WebView::SessionTabId> Tab::session_tab_id() const+{+ // A loaded tab's view holds the ID it reports its state under.+ if (is_loaded())+ return view().session_tab_id();+ return m_session_tab_id;+}++void Tab::set_session_tab_id(WebView::SessionTabId session_tab_id)+{+ m_session_tab_id = session_tab_id;+ if (is_loaded())+ view().set_session_tab_id(session_tab_id);+}++void Tab::clear_session_tab_id()+{+ m_session_tab_id.clear();+ if (is_loaded())+ view().clear_session_tab_id();+}+void Tab::focus_location_editor(){+ if (!is_loaded())+ return;+#if defined(AK_OS_MACOS)make_appkit_window_first_responder(*m_location_edit);#endif@@ -1434,6 +1535,11 @@ void Tab::set_window(BrowserWindow& window)if (&window == m_window)return;+ if (!is_loaded()) {+ m_window = &window;+ return;+ }+QObject::disconnect(&m_window->hamburger_menu(), nullptr, m_hamburger_button, nullptr);m_window = &window;@@ -1455,6 +1561,9 @@ void Tab::set_pinned(bool pinned)void Tab::set_vertical_tabs_enabled(bool enabled){m_vertical_tabs_enabled = enabled;+ if (!is_loaded())+ return;+m_toolbar->setProperty(WINDOW_DRAG_REGION_PROPERTY, true);if (m_sidebar_toggle_navigation_spacer)m_sidebar_toggle_navigation_spacer->changeSize(enabled ? TOOLBAR_SIDEBAR_TOGGLE_NAVIGATION_GAP : 0, 0, QSizePolicy::Fixed, QSizePolicy::Minimum);@@ -1469,6 +1578,9 @@ void Tab::set_vertical_tabs_position(WebView::VerticalTabsPosition position)return;m_vertical_tabs_position = position;+ if (!is_loaded())+ return;+recreate_toolbar_icons();update_vertical_tabs_toolbar_button_placement();m_toolbar->layout()->invalidate();@@ -1477,6 +1589,11 @@ void Tab::set_vertical_tabs_position(WebView::VerticalTabsPosition position)#ifdef AK_OS_MACOSvoid Tab::update_web_view_frame(bool chrome_visible, Optional<WebView::VerticalTabsPosition> sidebar_position){+ m_web_view_chrome_visible = chrome_visible;+ m_web_view_sidebar_position = sidebar_position;+ if (!is_loaded())+ return;+// With the browser chrome hidden, the page fills the window edge to edge.if (!chrome_visible) {m_view_layout->setContentsMargins(0, 0, 0, 0);@@ -1514,6 +1631,10 @@ void Tab::set_toolbar_container_in_tab_layout(bool in_tab_layout)void Tab::set_toolbar_window_controls_visible(bool visible){+ m_toolbar_window_controls_visible = visible;+ if (!is_loaded())+ return;+auto const has_native_window_controls = use_native_macos_window_controls() && visible;auto const has_custom_window_controls = !use_native_macos_window_controls() && visible;@@ -1595,7 +1716,7 @@ QIcon Tab::tab_icon() constQString Tab::title() const{auto title = m_fixed_title.value_or(m_title);- if (!WebView::Application::settings().config_variable_as_bool(WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle))+ if (!is_loaded() || !WebView::Application::settings().config_variable_as_bool(WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle))return title;return QString("%1 [%2]").arg(title).arg(view().web_content_pid());@@ -1644,6 +1765,9 @@ void Tab::update_tab_title()void Tab::tab_settings_changed(){+ if (!is_loaded())+ return;+auto const& tab_settings = WebView::Application::settings().tab_settings();m_vertical_tabs_enabled = tab_settings.vertical_tabs_enabled;m_vertical_tabs_position = tab_settings.vertical_tabs_position;@@ -1657,6 +1781,9 @@ void Tab::tab_settings_changed()void Tab::config_variable_changed(WebView::ConfigVariableID variable){+ if (!is_loaded())+ return;+if (variable == WebView::ConfigVariableID::ShowTabPerformanceMonitor)m_performance_monitor->setVisible(WebView::Application::settings().config_variable_as_bool(variable));if (variable == WebView::ConfigVariableID::ShowWebContentProcessIDInTabTitle)@@ -1665,6 +1792,9 @@ void Tab::config_variable_changed(WebView::ConfigVariableID variable)void Tab::content_blocker_count_visibility_changed(){+ if (!is_loaded())+ return;+m_content_blocking_button->set_show_count_badge(WebView::Application::settings().show_content_blocker_count());}@@ -1742,6 +1872,9 @@ void Tab::resizeEvent(QResizeEvent* event)QWidget::resizeEvent(event);if (isVisible())did_get_real_size();+ if (!is_loaded())+ return;+if (m_hover_label->isVisible())update_hover_label();}@@ -1749,6 +1882,8 @@ void Tab::resizeEvent(QResizeEvent* event)void Tab::hideEvent(QHideEvent* event){QWidget::hideEvent(event);+ if (!is_loaded())+ return;set_bookmarks_bar_hover_expanded(false);m_hover_label->hide();@@ -1782,7 +1917,7 @@ void Tab::hide_hover_label()bool Tab::event(QEvent* event){- if (event->type() == QEvent::PaletteChange) {+ if (event->type() == QEvent::PaletteChange && is_loaded()) {update_chrome_style();recreate_toolbar_icons();if (m_is_loading)@@ -1962,6 +2097,9 @@ void Tab::position_downloads_popover()void Tab::download_added(WebView::FileDownloader::Download const& download){+ if (!is_loaded())+ return;+// NB: A window lists the downloads of its own browsing session only.if (download.is_private != m_window->is_private())return;@@ -1975,6 +2113,9 @@ void Tab::download_added(WebView::FileDownloader::Download const& download)void Tab::download_updated(WebView::FileDownloader::Download const& download){+ if (!is_loaded())+ return;+if (download.is_private != m_window->is_private())return;update_downloads_button();@@ -1983,6 +2124,9 @@ void Tab::download_updated(WebView::FileDownloader::Download const& download)void Tab::download_removed(u64, WebView::IsPrivate is_private){+ if (!is_loaded())+ return;+if (is_private != m_window->is_private())return;update_downloads_button();@@ -2139,6 +2283,9 @@ void Tab::update_bookmarks_bar_visibility()bool Tab::has_bookmarks_bar_in_page() const{+ // A tab that hasn't loaded has no bookmarks bar yet.+ if (!is_loaded())+ return false;return m_bookmarks_bar_container->parentWidget() == this && !m_bookmarks_bar_container->isHidden();}@@ -2247,6 +2394,13 @@ void Tab::update_bookmarks_bar_geometry()void Tab::request_close(){m_window->cancel_window_close_preflight();++ // A tab that hasn't loaded has no page to ask, so it closes at once.+ if (!is_loaded()) {+ m_window->definitely_close_tab(tab_index());+ return;+ }+m_suppress_javascript_dialogs_until_navigation = true;m_preserve_close_retry_state_on_dialog_dismissal = m_already_requested_close;m_javascript_dialog->dismiss();@@ -2274,6 +2428,11 @@ void Tab::request_close()void Tab::request_window_close_preflight(Function<void(bool)> on_complete){+ if (!is_loaded()) {+ on_complete(true);+ return;+ }+m_suppress_javascript_dialogs_until_navigation = true;if (m_javascript_dialog->type() != JavaScriptDialog::Type::BeforeUnload)@@ -2296,6 +2455,9 @@ void Tab::request_window_close_preflight(Function<void(bool)> on_complete)void Tab::cancel_window_close_preflight(){+ if (!is_loaded())+ return;+if (m_javascript_dialog->type() == JavaScriptDialog::Type::BeforeUnload)m_javascript_dialog->dismiss();m_already_requested_close = false;@@ -2304,6 +2466,9 @@ void Tab::cancel_window_close_preflight()Function<void()> Tab::prepare_for_window_close(){+ if (!is_loaded())+ return [] { };+m_window_is_closing = true;m_suppress_javascript_dialogs_until_navigation = true;m_javascript_dialog->dismiss();diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex ca97b85e6a2..19f2565a5c1 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -18,6 +18,7 @@#include <LibWebCommon/Page/PageId.h>#include <LibWebCommon/Page/ScreenWakeLockState.h>#include <LibWebView/FileDownloader.h>+#include <LibWebView/SessionStore.h>#include <LibWebView/Settings.h>#include <UI/Qt/BookmarksBar.h>#include <UI/Qt/FindInPageWidget.h>@@ -72,6 +73,14 @@ signals:void mouse_entered(QEnterEvent*);};+// What a restored tab shows until it's selected: It has no view, and so no WebContent process, until then.+struct DeferredTab {+ WebView::SessionTabId session_tab_id { 0 };+ URL::URL url;+ QString title;+ QIcon favicon;+};+class Tab final: public QWidget, public WebView::FileDownloaderObserver@@ -82,10 +91,29 @@ public:AK_ALLOC_WITH_KMALLOC;Tab(BrowserWindow* window, Optional<WebView::CanonicalTraversable&> traversable = {});+ Tab(BrowserWindow* window, DeferredTab);virtual ~Tab() override;- WebContentView& view() { return *m_view; }- WebContentView const& view() const { return *m_view; }+ WebContentView& view()+ {+ VERIFY(m_view);+ return *m_view;+ }+ WebContentView const& view() const+ {+ VERIFY(m_view);+ return *m_view;+ }++ bool is_loaded() const { return m_view != nullptr; }+ // Builds the tab's view and toolbar, and restores its history, if it hasn't loaded yet.+ void ensure_loaded();++ URL::URL url() const;++ Optional<WebView::SessionTabId> session_tab_id() const;+ void set_session_tab_id(WebView::SessionTabId);+ void clear_session_tab_id();// A page that has never had a real size must be sized while hidden, so that it doesn't load into Qt's placeholder// geometry. Once it has had one, it keeps its size while hidden and takes the window's when it is shown again.@@ -143,7 +171,7 @@ public:public slots:void focus_location_editor();void park_location_editing();- LocationEdit* location_edit_with_parked_editing() const { return m_location_edit->has_parked_editing_session() ? m_location_edit : nullptr; }+ LocationEdit* location_edit_with_parked_editing() const { return is_loaded() && m_location_edit->has_parked_editing_session() ? m_location_edit : nullptr; }signals:void title_changed(int id, QString const&);@@ -151,8 +179,13 @@ signals:void audio_play_state_changed(int id, Web::HTML::AudioPlayState);void loading_changed();void loading_finished();+ void loaded();private:+ void create_tab_shell();+ void create_tab_context_menu();+ void initialize(Optional<WebView::CanonicalTraversable&>);+void location_edit_return_pressed(String, Optional<URL::URL>, WebView::OmniboxDestinationKind);#ifdef AK_OS_MACOSvirtual void paintEvent(QPaintEvent*) override;@@ -245,6 +278,9 @@ private:WebView::VerticalTabsPosition m_vertical_tabs_position { WebView::VerticalTabsPosition::Left };#ifdef AK_OS_MACOSQHBoxLayout* m_view_layout { nullptr };+ // The web view's frame as TabWidget last asked for it, which a tab that hasn't loaded gets once it loads.+ bool m_web_view_chrome_visible { true };+ Optional<WebView::VerticalTabsPosition> m_web_view_sidebar_position;#endifint m_loading_animation_frame { 0 };@@ -284,6 +320,10 @@ private:bool m_preserve_close_retry_state_on_dialog_dismissal { false };bool m_window_is_closing { false };bool m_is_pinned { false };++ bool m_toolbar_window_controls_visible { false };+ Optional<WebView::SessionTabId> m_session_tab_id;+ Optional<DeferredTab> m_deferred_tab;};}diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex cc703d3857e..2f3a3f0a2ae 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -107,7 +107,6 @@ static constexpr qreal LOAD_SHEEN_FADE_PROGRESS = 0.15;static constexpr int LOAD_SHEEN_DURATION_MS = 400;static constexpr int DROP_PREVIEW_DURATION_MS = 150;static constexpr int TAB_TITLE_FADE_WIDTH = 24;-static constexpr int TAB_ICON_SIZE = 16;static constexpr int COLLAPSED_VERTICAL_TAB_BUTTON_SIZE = 16;static constexpr int COLLAPSED_VERTICAL_TAB_ICON_SIZE = 12;static constexpr auto COLLAPSED_VERTICAL_TAB_BUTTON_PROPERTY = "collapsedVerticalTabButton";@@ -502,7 +501,7 @@ public:outer_layout->addWidget(m_card);}- void set_preview(QPalette const& palette, QString title, QString url, QPixmap const& thumbnail)+ void set_preview(QPalette const& palette, QString title, QString url, Optional<QPixmap const&> thumbnail){update_chrome_style(palette);@@ -512,7 +511,9 @@ public:QFontMetrics title_metrics(m_title_label->font());QFontMetrics url_metrics(m_url_label->font());- m_thumbnail->set_pixmap(thumbnail);+ m_thumbnail->setVisible(thumbnail.has_value());+ if (thumbnail.has_value())+ m_thumbnail->set_pixmap(*thumbnail);m_title_label->setText(title_metrics.elidedText(title, Qt::ElideRight, TAB_PREVIEW_THUMBNAIL_WIDTH));m_url_label->setText(url_metrics.elidedText(url, Qt::ElideMiddle, TAB_PREVIEW_THUMBNAIL_WIDTH));adjustSize();@@ -811,7 +812,7 @@ bool TabBar::event(QEvent* event)auto hovered_tab = tab_index_at(help_event->pos());if (hovered_tab >= 0 && hovered_tab != currentIndex()) {if (auto* tab = m_tab_widget ? m_tab_widget->tab(hovered_tab) : nullptr) {- if (tab->view().tab_preview_pixmap({ TAB_PREVIEW_THUMBNAIL_WIDTH, TAB_PREVIEW_THUMBNAIL_HEIGHT }).has_value()) {+ if (!tab->is_loaded() || tab->view().tab_preview_pixmap({ TAB_PREVIEW_THUMBNAIL_WIDTH, TAB_PREVIEW_THUMBNAIL_HEIGHT }).has_value()) {event->accept();return true;}@@ -1670,6 +1671,15 @@ void TabBar::show_tab_preview()return;}+ // A tab that hasn't loaded has no page to show, so its preview is its title and URL.+ if (!tab->is_loaded()) {+ m_tab_preview_popup->set_preview(palette(), tab->title(), qstring_from_ak_string(tab->url().serialize()), {});+ m_tab_preview_popup->move(tab_preview_position_for(index, m_tab_preview_popup->sizeHint()));+ m_tab_preview_popup->show();+ m_tab_preview_popup->raise();+ return;+ }+if (m_previewed_tab != tab) {m_previewed_tab = tab;m_tab_preview_paint_connection = connect(&tab->view(), &WebContentView::ready_to_paint, this, &TabBar::show_tab_preview);@@ -2332,13 +2342,22 @@ static void size_page_like(Tab& page, Tab const& current)void TabWidget::insert_tab(int index, Tab* widget, QString const& label){- // A page inserted behind the current one stays at Qt's default geometry until it's selected, and a load can start- // into it long before that. Size its view like the current page's view first — as Chrome does for a new background- // tab (TabStripModel::AddWebContents) — so the page's first layout sees the real viewport.- if (auto* current = as_if<Tab>(m_stacked_widget->currentWidget()); current && current != widget)- size_page_like(*widget, *current);- widget->view().installEventFilter(this);- widget->bookmarks_bar().installEventFilter(this);+ if (widget->is_loaded()) {+ // A page inserted behind the current one stays at Qt's default geometry until it's selected, and a load can+ // start into it long before that. Size its view like the current page's view first, so the page's first layout+ // sees the real viewport.+ if (auto* current = as_if<Tab>(m_stacked_widget->currentWidget()); current && current != widget && current->is_loaded())+ size_page_like(*widget, *current);+ widget->view().installEventFilter(this);+ widget->bookmarks_bar().installEventFilter(this);+ } else {+ // A tab that hasn't loaded gets the filters for its view and bookmarks bar once it has them.+ auto install_event_filters = [this, widget] {+ widget->view().installEventFilter(this);+ widget->bookmarks_bar().installEventFilter(this);+ };+ connect(widget, &Tab::loaded, this, install_event_filters, Qt::SingleShotConnection);+ }m_stacked_widget->insertWidget(index, widget);m_tab_bar->insertTab(index, label);@@ -2371,8 +2390,11 @@ Tab* TabWidget::take_tab(int index)return nullptr;m_stacked_widget->removeWidget(widget);- as<Tab>(widget)->view().removeEventFilter(this);- as<Tab>(widget)->bookmarks_bar().removeEventFilter(this);+ if (as<Tab>(widget)->is_loaded()) {+ as<Tab>(widget)->view().removeEventFilter(this);+ as<Tab>(widget)->bookmarks_bar().removeEventFilter(this);+ }+ disconnect(as<Tab>(widget), &Tab::loaded, this, nullptr);auto* toolbar = as<Tab>(widget)->toolbar_container();if (m_toolbar_container->indexOf(toolbar) != -1)m_toolbar_container->removeWidget(toolbar);@@ -2545,7 +2567,7 @@ bool TabWidget::eventFilter(QObject* watched, QEvent* event)defer_update_vertical_tabs_hover_expanded();if (event->type() == QEvent::Resize) {- if (auto* current = as_if<Tab>(m_stacked_widget->currentWidget()); current && watched == ¤t->view())+ if (auto* current = as_if<Tab>(m_stacked_widget->currentWidget()); current && current->is_loaded() && watched == ¤t->view())size_hidden_pages_like_the_current_one();}@@ -2672,16 +2694,17 @@ void TabWidget::resizeEvent(QResizeEvent* event)// The stacked layout sizes only the current page, so the tabs behind it keep stale geometry: Qt's default 100x30 for// a tab that was created before the window was first shown — which is every tab but the first on a command line, and// every restored one. Their pages may already be loading. So, whenever the current page's view gets its size, then size-// every hidden page's view the same, until it has had a real size.+// every hidden page's view the same, until it has had a real size. A tab that hasn't loaded is skipped: It has no view+// yet, and it loads only once it's the current page, which the stacked layout sizes.// NB: This hangs off the view's own resize event, not this widget's: At the window's first show, this widget is laid// out before the current page's child layout has run. So at that point the current view still has its placeholder size.void TabWidget::size_hidden_pages_like_the_current_one(){auto* current = as_if<Tab>(m_stacked_widget->currentWidget());- if (!current)+ if (!current || !current->is_loaded())return;for (int i = 0; i < m_stacked_widget->count(); ++i) {- if (auto* page = as_if<Tab>(m_stacked_widget->widget(i)); page && page != current)+ if (auto* page = as_if<Tab>(m_stacked_widget->widget(i)); page && page != current && page->is_loaded())size_page_like(*page, *current);}}diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex beb56aba3c2..afe3c7e2eaa 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -46,6 +46,8 @@ namespace Ladybird {class TabPreviewPopup;class TabWidget;+static constexpr int TAB_ICON_SIZE = 16;+enum class TabLayout : u8 {Horizontal,VerticalCollapsed,