Truly independent web browser
UI/Qt: Defer tab updates while opening multiple tabs
Callum Law21c9bc73e5c1
diff --git a/UI/Qt/Application.cpp b/UI/Qt/Application.cppindex 916e7127c50..9a2ab6ccd1f 100644--- a/UI/Qt/Application.cpp+++ b/UI/Qt/Application.cpp@@ -2439,6 +2439,7 @@ void Application::open_urls_in_new_tabs(ReadonlySpan<URL::URL> urls) const}auto& window = active_window();+ auto deferred_tab_updates = window.tab_widget().defer_tab_updates();auto* previous_tab = window.current_tab();for (auto const& url : urls) {auto location = previous_tabdiff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex 8c236ddbfb9..fb11f67ec9a 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -936,6 +936,9 @@ void TabBar::lock_tab_widths()void TabBar::refresh_tab_layout(){+ if (m_tab_widget && m_tab_widget->tab_updates_deferred())+ return;+// NB: Qt lays the tabs out only as the bar resizes, but their size can change while the bar's does not, such as when// the sidebar collapses to the width its tab bar had in the open layout.QResizeEvent resize_event(size(), size());@@ -1075,6 +1078,8 @@ void TabBar::tabRemoved(int index)void TabBar::tabLayoutChange(){QTabBar::tabLayoutChange();+ if (m_tab_widget && m_tab_widget->tab_updates_deferred())+ return;update_layout_animation();set_scroll_offset(m_scroll_offset);update_tab_button_geometry();@@ -2542,6 +2547,9 @@ qreal TabBar::loading_ring_opacity(int index) const// The ring only needs repainting while a tab is loading and the tab bar can be seen.void TabBar::update_loading_ring_animation(){+ if (m_tab_widget && m_tab_widget->tab_updates_deferred())+ return;+auto any_tab_loading = false;for (int index = 0; index < count() && !any_tab_loading; ++index)any_tab_loading = loading_ring_opacity(index) > 0;@@ -2556,6 +2564,9 @@ void TabBar::update_loading_ring_animation()void TabBar::update_microphone_indicators(){+ if (m_tab_widget && m_tab_widget->tab_updates_deferred())+ return;+HashTable<Tab const*> capturing_tabs;for (int index = 0; index < count(); ++index) {if (auto* tab = m_tab_widget ? m_tab_widget->tab(index) : nullptr; tab && tab->is_loaded() && tab->view().tab_indicator_visible(WebView::TabIndicator::UsingMicrophone))@@ -2819,6 +2830,9 @@ void TabBar::set_hovered_tab_index(int index)void TabBar::update_tab_button_geometry(){+ if (m_tab_widget && m_tab_widget->tab_updates_deferred())+ return;+auto prepare_button = [&](QWidget* button, TabBarButton::Placement placement) {if (!button)return;@@ -3383,6 +3397,23 @@ void TabWidget::add_tab(Tab* widget, QString const& label)insert_tab(m_tab_bar->count(), widget, label);}+ScopeGuard<Function<void()>> TabWidget::defer_tab_updates()+{+ VERIFY(!m_tab_updates_deferred);+ m_tab_updates_deferred = true;+ auto layout_was_enabled = m_stacked_widget->layout()->isEnabled();+ m_stacked_widget->layout()->setEnabled(false);+ return ScopeGuard<Function<void()>> { [this, layout_was_enabled] {+ m_stacked_widget->layout()->setEnabled(layout_was_enabled);+ m_tab_updates_deferred = false;+ update_tab_button_visibility();+ update_tab_layout();+ place_toolbar_over_page_column();+ m_tab_bar->update_microphone_indicators();+ m_tab_bar->update_loading_ring_animation();+ } };+}+// Sizing the hidden page's view alone doesn't hold: The page itself still has its placeholder geometry, and the next// pass of the page's own layout pulls the view back to it. So, give the whole page the current page's geometry, laid// out now — and only then push the view's viewport.@@ -3426,7 +3457,8 @@ void TabWidget::insert_tab(int index, Tab* widget, QString const& label)m_stacked_widget->insertWidget(index, widget);m_tab_bar->insertTab(index, label);- place_toolbar_over_page_column();+ if (!m_tab_updates_deferred)+ place_toolbar_over_page_column();update_tab_layout();update_tab_button_visibility();@@ -5270,6 +5302,9 @@ void TabWidget::update_vertical_tabs_button_layout()void TabWidget::update_tab_layout(){+ if (m_tab_updates_deferred)+ return;+if (m_tab_bar->tab_layout() != TabLayout::Horizontal) {auto reserved_width = vertical_tabs_layout_width();auto side_bar_width = current_vertical_tabs_width();@@ -5314,6 +5349,9 @@ void TabWidget::update_tab_layout()void TabWidget::update_tab_button_visibility(){+ if (m_tab_updates_deferred)+ return;+if (m_tab_bar->tab_layout() == TabLayout::VerticalCollapsed) {for (int index = 0; index < m_tab_bar->count(); ++index) {if (auto* button = m_tab_bar->tabButton(index, QTabBar::RightSide))diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex b525ca92d7f..b1a86d93d40 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -9,9 +9,11 @@#pragma once#include <AK/Array.h>+#include <AK/Function.h>#include <AK/HashMap.h>#include <AK/HashTable.h>#include <AK/Optional.h>+#include <AK/ScopeGuard.h>#include <AK/TypeCasts.h>#include <AK/Vector.h>#include <AK/kmalloc.h>@@ -311,6 +313,8 @@ public:void add_tab(Tab* widget, QString const& label);void insert_tab(int index, Tab* widget, QString const& label);+ ScopeGuard<Function<void()>> defer_tab_updates();+ bool tab_updates_deferred() const { return m_tab_updates_deferred; }void remove_tab(int index);Tab* take_tab(int index);// Another tab stands in for the one at the index, which keeps its place in the tab bar rather than closing.@@ -517,6 +521,7 @@ private:QWidget* m_toolbar_row_backdrop { nullptr };bool m_is_resizing_vertical_tabs { false };bool m_is_updating_chrome_style { false };+ bool m_tab_updates_deferred { false };int m_vertical_tabs_expanded_width { 0 };int m_vertical_tabs_resize_start_global_x { 0 };int m_vertical_tabs_resize_start_width { 0 };