Truly independent web browser
UI/Qt: Swirl a purple gradient around loading tabs
Andreas Kling58e38371c28f
diff --git a/UI/Qt/BrowserWindow.cpp b/UI/Qt/BrowserWindow.cppindex eae14adda8e..d102fad8b52 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_changed, this, [this] {+ m_tabs_container->tab_bar()->update_loading_swirl_animation();+ });QObject::connect(tab, &Tab::loading_finished, this, [this, tab] {m_tabs_container->tab_bar()->play_load_sheen(*tab);});diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cppindex 863af5a1113..d520f9ac570 100644--- a/UI/Qt/ChromeStyle.cpp+++ b/UI/Qt/ChromeStyle.cpp@@ -10,9 +10,11 @@#include <UI/Qt/StringUtils.h>#include <QAbstractButton>+#include <QElapsedTimer>#include <QGuiApplication>#include <QImage>#include <QPainter>+#include <QPainterPath>#include <QPalette>#include <QStyleHints>@@ -1588,4 +1590,43 @@ void paint_circular_control_frame(QPainter& painter, QAbstractButton const& buttpaint_circular_control_frame(painter, button.rect(), button.devicePixelRatioF(), background, border);}+// A purple gradient with two soft bright spots that moves steadily along a border, to show that something is loading.+// Every swirl follows the same clock, so the swirls on screen move in step.+void paint_loading_swirl(QPainter& painter, QPainterPath const& border)+{+ static QElapsedTimer clock;+ if (!clock.isValid())+ clock.start();+ auto phase = static_cast<qreal>(clock.elapsed() % LOADING_SWIRL_PERIOD_MS) / LOADING_SWIRL_PERIOD_MS;++ // The border is drawn in short segments, each colored by where it sits along the border.+ static constexpr qreal segment_length = 2.0;+ static constexpr qreal bright_spots = 2.0;+ // NB: Without caching, every pointAtPercent() call below measures the whole path again.+ auto path = border;+ path.setCachingEnabled(true);+ auto segments = max(1, static_cast<int>(ceil(path.length() / segment_length)));+ auto color = chrome_accent();++ painter.save();+ painter.setRenderHint(QPainter::Antialiasing, true);+ painter.setBrush(Qt::NoBrush);++ // NB: pointAtPercent() measures along the path's length, so the swirl moves just as fast around the curves of a+ // border as along its straight edges.+ auto from_point = path.pointAtPercent(0);+ for (int segment = 0; segment < segments; ++segment) {+ auto from = static_cast<qreal>(segment) / segments;+ auto to = static_cast<qreal>(segment + 1) / segments;+ auto to_point = path.pointAtPercent(to);+ auto wave = 0.5 + (0.5 * cos(2.0 * M_PI * bright_spots * (((from + to) / 2.0) - phase)));+ color.setAlphaF(0.2 + (0.8 * wave * wave));+ painter.setPen(QPen(color, 1.5, Qt::SolidLine, Qt::FlatCap));+ painter.drawLine(from_point, to_point);+ from_point = to_point;+ }++ painter.restore();+}+}diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.hindex 38c2ffd307b..84d6104611e 100644--- a/UI/Qt/ChromeStyle.h+++ b/UI/Qt/ChromeStyle.h@@ -11,11 +11,13 @@class QAbstractButton;class QPainter;+class QPainterPath;class QPalette;namespace Ladybird::ChromeStyle {constexpr inline int LOCATION_EDIT_CORNER_RADIUS = 16;+constexpr inline int LOADING_SWIRL_PERIOD_MS = 2000;bool is_dark(QPalette const&);QColor mix(QColor const& from, QColor const& to, double amount);@@ -59,4 +61,6 @@ enum class CircularControlFrameStyle {};void paint_circular_control_frame(QPainter&, QAbstractButton const&, CircularControlFrameStyle = CircularControlFrameStyle::InteractionOnly);+void paint_loading_swirl(QPainter&, QPainterPath const& border);+}diff --git a/UI/Qt/Tab.cpp b/UI/Qt/Tab.cppindex adfc5a7ea12..98f0a06ca20 100644--- a/UI/Qt/Tab.cpp+++ b/UI/Qt/Tab.cpp@@ -1401,6 +1401,7 @@ void Tab::set_loading(bool is_loading)m_is_loading = is_loading;m_loading_animation_frame = 0;+ emit loading_changed();if (m_is_loading)m_loading_animation_timer->start();diff --git a/UI/Qt/Tab.h b/UI/Qt/Tab.hindex e52496bdc27..c1fa7574b47 100644--- a/UI/Qt/Tab.h+++ b/UI/Qt/Tab.h@@ -102,6 +102,7 @@ public:QIcon const& favicon() const { return m_favicon; }QIcon tab_icon() const;+ bool is_loading() const { return m_is_loading; }QString title() const;QMenu* context_menu() const { return m_context_menu; }@@ -129,6 +130,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_changed();void loading_finished();private:diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cppindex d7d1e35be03..59f3945b060 100644--- a/UI/Qt/TabBar.cpp+++ b/UI/Qt/TabBar.cpp@@ -546,6 +546,25 @@ TabBar::TabBar(TabWidget* tab_widget)m_tab_preview_popup = new TabPreviewPopup(this);+ m_loading_swirl_animation = new QVariantAnimation(this);+ m_loading_swirl_animation->setDuration(ChromeStyle::LOADING_SWIRL_PERIOD_MS);+ m_loading_swirl_animation->setStartValue(0.0);+ m_loading_swirl_animation->setEndValue(1.0);+ m_loading_swirl_animation->setLoopCount(-1);+ // NB: Each frame repaints only the loading tabs, and checks again, so the swirl also stops when the last loading tab+ // is closed.+ connect(m_loading_swirl_animation, &QVariantAnimation::valueChanged, this, [this] {+ auto any_tab_loading = false;+ for (int index = 0; index < count(); ++index) {+ if (!is_tab_loading(index))+ continue;+ any_tab_loading = true;+ update(visual_tab_rect(index));+ }+ if (!any_tab_loading || !isVisible())+ update_loading_swirl_animation();+ });+m_hover_animation = new QVariantAnimation(this);m_hover_animation->setDuration(120);m_hover_animation->setEasingCurve(QEasingCurve::OutCubic);@@ -692,6 +711,10 @@ void TabBar::tabLayoutChange()bool TabBar::event(QEvent* event){+ // The swirl stops while the tab bar is hidden, so it has to start again for tabs still loading when it is shown.+ if (event->type() == QEvent::Show)+ update_loading_swirl_animation();+if (event->type() == QEvent::ToolTip) {auto* help_event = static_cast<QHelpEvent*>(event);auto hovered_tab = tab_index_at(help_event->pos());@@ -762,6 +785,8 @@ void TabBar::paintEvent(QPaintEvent*)painter.drawPath(tab_path);}+ if (is_tab_loading(index))+ ChromeStyle::paint_loading_swirl(painter, tab_path);if (auto progress = load_sheen_progress(index); progress.has_value())paint_load_sheen(painter, palette(), tab_path, shape_rect, *progress);@@ -1216,6 +1241,29 @@ void TabBar::play_load_sheen(Tab& tab)animation->start();}+bool TabBar::is_tab_loading(int index) const+{+ if (!m_tab_widget || index < 0 || index >= count())+ return false;+ auto* tab = m_tab_widget->tab(index);+ return tab && tab->is_loading();+}++// The swirl only needs repainting while a tab is loading and the tab bar can be seen.+void TabBar::update_loading_swirl_animation()+{+ auto any_tab_loading = false;+ for (int index = 0; index < count() && !any_tab_loading; ++index)+ any_tab_loading = is_tab_loading(index);++ auto should_run = any_tab_loading && isVisible();+ if (should_run && m_loading_swirl_animation->state() != QAbstractAnimation::Running)+ m_loading_swirl_animation->start();+ else if (!should_run && m_loading_swirl_animation->state() != QAbstractAnimation::Stopped)+ m_loading_swirl_animation->stop();+ update();+}+Optional<qreal> TabBar::load_sheen_progress(int index) const{if (m_load_sheens.is_empty() || !m_tab_widget)@@ -1801,6 +1849,9 @@ void TabWidget::insert_tab(int index, Tab* widget, QString const& label)update_toolbar_placement();update_tab_layout();update_tab_button_visibility();++ // A tab moved here from another window can already be loading.+ m_tab_bar->update_loading_swirl_animation();}void TabWidget::remove_tab(int index)diff --git a/UI/Qt/TabBar.h b/UI/Qt/TabBar.hindex c25bbeafd87..85a7d8f0fe3 100644--- a/UI/Qt/TabBar.h+++ b/UI/Qt/TabBar.h@@ -67,6 +67,7 @@ public:void recreate_icons();void play_load_sheen(Tab&);+ void update_loading_swirl_animation();private:virtual QSize sizeHint() const override;@@ -113,6 +114,7 @@ private:void set_hovered_tab_index(int);Optional<qreal> load_sheen_progress(int index) const;+ bool is_tab_loading(int index) const;void ensure_tab_visible(int index);void update_tab_button_geometry();@@ -133,6 +135,7 @@ private:QVariantAnimation* animation { nullptr };};Vector<LoadSheen> m_load_sheens;+ QVariantAnimation* m_loading_swirl_animation { nullptr };QIcon m_fallback_tab_icon;QPointer<Tab> m_pressed_tab;QPoint m_position_in_selected_tab_while_dragging;