Skip to content

Truly independent web browser

UI/Qt: Sweep a sheen across a tab when it finishes loading

Andreas Kling5cf9011c8a5b
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cpp
index 15c411d7c80..eae14adda8e 100644
--- a/UI/Qt/BrowserWindow.cpp
+++ b/UI/Qt/BrowserWindow.cpp
@@ -570,6 +570,9 @@ void BrowserWindow::initialize_tab(Tab* tab)
QObject::connect(tab, &Tab::title_changed, this, &BrowserWindow::tab_title_changed);
QObject::connect(tab, &Tab::favicon_changed, this, &BrowserWindow::tab_favicon_changed);
QObject::connect(tab, &Tab::audio_play_state_changed, this, &BrowserWindow::tab_audio_play_state_changed);
+ QObject::connect(tab, &Tab::loading_finished, this, [this, tab] {
+ m_tabs_container->tab_bar()->play_load_sheen(*tab);
+ });
QObject::connect(&tab->view(), &WebContentView::urls_dropped, this, [this](auto& urls) {
VERIFY(urls.size());
diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cpp
index 1450f27b3e7..adfc5a7ea12 100644
--- a/UI/Qt/Tab.cpp
+++ b/UI/Qt/Tab.cpp
@@ -1408,6 +1408,11 @@ void Tab::set_loading(bool is_loading)
m_loading_animation_timer->stop();
update_tab_icon();
+
+ // Only mark the end of loads that finished, rather than being stopped. The browser's own about: pages are not
+ // marked.
+ if (!m_is_loading && view().last_load_finished() && view().url().scheme() != "about"sv)
+ emit loading_finished();
}
void Tab::update_tab_icon()
diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.h
index 27dd99bdb29..e52496bdc27 100644
--- a/UI/Qt/Tab.h
+++ b/UI/Qt/Tab.h
@@ -129,6 +129,7 @@ signals:
void title_changed(int id, QString const&);
void favicon_changed(int id, QIcon const&);
void audio_play_state_changed(int id, Web::HTML::AudioPlayState);
+ void loading_finished();
private:
void location_edit_return_pressed(String, Optional<URL::URL>, WebView::OmniboxDestinationKind);
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index b96443bd6a7..d7d1e35be03 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -95,6 +95,8 @@ static constexpr int TAB_PREVIEW_SHADOW_MARGIN = 12;
static constexpr int TAB_PREVIEW_CARD_GAP = 6;
static constexpr int TAB_BUTTON_SIZE = 22;
static constexpr int TAB_BUTTON_ICON_SIZE = 14;
+static constexpr qreal LOAD_SHEEN_FADE_PROGRESS = 0.15;
+static constexpr int LOAD_SHEEN_DURATION_MS = 400;
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;
@@ -205,6 +207,31 @@ static QRect tab_card_shape_rect(QRect const& rect)
return rect.adjusted(TAB_CARD_SHAPE_HORIZONTAL_INSET, TAB_CARD_SHAPE_VERTICAL_INSET, -TAB_CARD_SHAPE_HORIZONTAL_INSET, -TAB_CARD_SHAPE_VERTICAL_INSET);
}
+// When a tab finishes loading, a soft band of light sweeps across its pill from left to right.
+static void paint_load_sheen(QPainter& painter, QPalette const& palette, QPainterPath const& tab_path, QRectF const& shape_rect, qreal progress)
+{
+ // The bright middle of the band travels from the left edge of the tab to its right edge, so the sweep is seen to
+ // start at the very left. It fades out at the right edge rather than leaving the tab.
+ auto band_width = shape_rect.height() * 2.5;
+ auto center_x = shape_rect.left() + (shape_rect.width() * progress);
+ auto strength = min(1.0, (1.0 - progress) / LOAD_SHEEN_FADE_PROGRESS);
+
+ // NB: The gradient runs from the top left to the bottom right of the band, which slants the band like "/".
+ QLinearGradient sheen(QPointF(center_x - (band_width / 2.0), shape_rect.top()), QPointF(center_x + (band_width / 2.0), shape_rect.bottom()));
+ auto peak = ChromeStyle::is_dark(palette) ? QColor(255, 255, 255, 46) : QColor(0, 0, 0, 14);
+ peak.setAlphaF(peak.alphaF() * strength);
+ auto clear = peak;
+ clear.setAlpha(0);
+ sheen.setColorAt(0.0, clear);
+ sheen.setColorAt(0.5, peak);
+ sheen.setColorAt(1.0, clear);
+
+ painter.save();
+ painter.setClipPath(tab_path);
+ painter.fillRect(shape_rect, sheen);
+ painter.restore();
+}
+
static QColor tab_hover_surface(QPalette const& palette, qreal hover_progress)
{
auto dark = ChromeStyle::is_dark(palette);
@@ -735,6 +762,9 @@ void TabBar::paintEvent(QPaintEvent*)
painter.drawPath(tab_path);
}
+ if (auto progress = load_sheen_progress(index); progress.has_value())
+ paint_load_sheen(painter, palette(), tab_path, shape_rect, *progress);
+
auto icon = tabIcon(index);
if (icon.isNull())
icon = m_fallback_tab_icon;
@@ -1162,6 +1192,45 @@ void TabBar::start_hover_animation(int tab_index, qreal target_progress)
m_hover_animation->start();
}
+void TabBar::play_load_sheen(Tab& tab)
+{
+ m_load_sheens.remove_all_matching([&](auto const& sheen) {
+ if (sheen.tab != &tab)
+ return false;
+ sheen.animation->deleteLater();
+ return true;
+ });
+
+ auto* animation = new QVariantAnimation(this);
+ animation->setDuration(LOAD_SHEEN_DURATION_MS);
+ animation->setEasingCurve(QEasingCurve::InOutSine);
+ animation->setStartValue(0.0);
+ animation->setEndValue(1.0);
+ connect(animation, &QVariantAnimation::valueChanged, this, [this] { update(); });
+ connect(animation, &QVariantAnimation::finished, this, [this, animation] {
+ m_load_sheens.remove_first_matching([&](auto const& sheen) { return sheen.animation == animation; });
+ animation->deleteLater();
+ update();
+ });
+ m_load_sheens.append({ &tab, animation });
+ animation->start();
+}
+
+Optional<qreal> TabBar::load_sheen_progress(int index) const
+{
+ if (m_load_sheens.is_empty() || !m_tab_widget)
+ return {};
+ // NB: A sheen whose tab was closed has a null tab, which must not match a missing one here.
+ auto* tab = m_tab_widget->tab(index);
+ if (!tab)
+ return {};
+ for (auto const& sheen : m_load_sheens) {
+ if (sheen.tab == tab)
+ return sheen.animation->currentValue().toReal();
+ }
+ return {};
+}
+
void TabBar::schedule_tab_preview(int index)
{
if (!m_tab_preview_timer || !m_tab_preview_popup)
diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.h
index 518b84c33e5..c25bbeafd87 100644
--- a/UI/Qt/TabBar.h
+++ b/UI/Qt/TabBar.h
@@ -8,7 +8,9 @@
#pragma once
+#include <AK/Optional.h>
#include <AK/TypeCasts.h>
+#include <AK/Vector.h>
#include <AK/kmalloc.h>
#include <LibWebView/Settings.h>
#include <UI/Qt/Tab.h>
@@ -64,6 +66,8 @@ public:
void refresh_tab_layout();
void recreate_icons();
+ void play_load_sheen(Tab&);
+
private:
virtual QSize sizeHint() const override;
virtual QSize minimumSizeHint() const override;
@@ -108,6 +112,7 @@ private:
void set_vertical_scroll_offset(int);
void set_hovered_tab_index(int);
+ Optional<qreal> load_sheen_progress(int index) const;
void ensure_tab_visible(int index);
void update_tab_button_geometry();
@@ -122,6 +127,12 @@ private:
qreal m_hover_progress { 0.0 };
int m_drop_indicator_index { -1 };
QVariantAnimation* m_hover_animation { nullptr };
+
+ struct LoadSheen {
+ QPointer<Tab> tab;
+ QVariantAnimation* animation { nullptr };
+ };
+ Vector<LoadSheen> m_load_sheens;
QIcon m_fallback_tab_icon;
QPointer<Tab> m_pressed_tab;
QPoint m_position_in_selected_tab_while_dragging;