Skip to content

Truly independent web browser

UI/Qt: Morph the reload arrow into the stop icon

Andreas Kling8e74c6d6833a
diff --git a/UI/Qt/Icon.cpp b/UI/Qt/Icon.cpp
index 03e098ae088..a4fa0cfdfb8 100644
--- a/UI/Qt/Icon.cpp
+++ b/UI/Qt/Icon.cpp
@@ -11,6 +11,7 @@
#include <LibCore/Resource.h>
#include <UI/Qt/ChromeStyle.h>
#include <UI/Qt/Icon.h>
+#include <UI/Qt/Motion.h>
#include <UI/Qt/StringUtils.h>
#include <UI/Qt/Theme.h>
@@ -421,14 +422,6 @@ static QPainterPath const& chrome_icon_painter_path(ChromeIcon icon)
});
}
-static QPixmap create_transparent_icon_pixmap(QSize logical_size, qreal device_pixel_ratio)
-{
- QPixmap pixmap(physical_size_for_device_pixel_ratio(logical_size, device_pixel_ratio));
- pixmap.setDevicePixelRatio(device_pixel_ratio);
- pixmap.fill(Qt::transparent);
- return pixmap;
-}
-
static bool coordinates_match(qreal a, qreal b)
{
return abs(a - b) < 0.001;
@@ -606,17 +599,17 @@ static QTransform transform_for_diagonal_icon(QPainterPath const& path, qreal sc
return transform;
}
-// Puts the lines of an icon on the pixel grid of the size it is shown at, the way a font's hints do for its letters.
-static QPainterPath fit_icon_path_to_pixels(QPainterPath const& path, qreal scale, int stroke_width, int canvas_size)
+// Moves a part of an icon, or the whole of it, as fitting the icon to the pixels moves it.
+static QPainterPath fit_icon_part_to_pixels(QPainterPath const& icon_path, QPainterPath const& part, qreal scale, int stroke_width, int canvas_size)
{
- auto stems = find_icon_stems(path);
+ auto stems = find_icon_stems(icon_path);
if (stems.is_diagonal)
- return transform_for_diagonal_icon(path, scale, stroke_width, canvas_size).map(path);
+ return transform_for_diagonal_icon(icon_path, scale, stroke_width, canvas_size).map(part);
auto vertical = fit_stems_to_pixels(stems.vertical, scale, stroke_width, canvas_size);
auto horizontal = fit_stems_to_pixels(stems.horizontal, scale, stroke_width, canvas_size);
- QPainterPath fitted = path;
+ QPainterPath fitted = part;
for (int index = 0; index < fitted.elementCount(); ++index) {
auto const& element = fitted.elementAt(index);
fitted.setElementPositionAt(index, apply_fitted_positions(vertical, element.x * scale), apply_fitted_positions(horizontal, element.y * scale));
@@ -624,6 +617,12 @@ static QPainterPath fit_icon_path_to_pixels(QPainterPath const& path, qreal scal
return fitted;
}
+// Puts the lines of an icon on the pixel grid of the size it is shown at, the way a font's hints do for its letters.
+static QPainterPath fit_icon_path_to_pixels(QPainterPath const& path, qreal scale, int stroke_width, int canvas_size)
+{
+ return fit_icon_part_to_pixels(path, path, scale, stroke_width, canvas_size);
+}
+
static qreal chrome_icon_stroke_width(int logical_size)
{
if (logical_size > LARGEST_ICON_SIZE_WITH_FIXED_STROKE)
@@ -693,15 +692,23 @@ static QPainterPath icon_outline(QPainterPath const& path, int stroke_width)
}
// The icon is drawn from its vector at the size and device pixel ratio it is shown at, never scaled from a picture.
-static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& path, QColor color, qreal device_pixel_ratio, int logical_size)
+// Draws an icon from the outline of its lines, which it is given in pixels of the screen for the scale of the icon,
+// the width of its lines and the size of its canvas. A margin around the canvas leaves room for lines that move past
+// its edges.
+template<typename OutlineFunction>
+static QPixmap create_chrome_icon_pixmap(QColor color, qreal device_pixel_ratio, int logical_size, OutlineFunction outline_for, int margin_in_pixels = 0)
{
- auto pixmap = create_transparent_icon_pixmap({ logical_size, logical_size }, device_pixel_ratio);
+ auto canvas_size = physical_size_for_device_pixel_ratio({ logical_size, logical_size }, device_pixel_ratio);
+ QPixmap pixmap(canvas_size + QSize(2 * margin_in_pixels, 2 * margin_in_pixels));
+ pixmap.setDevicePixelRatio(device_pixel_ratio);
+ pixmap.fill(Qt::transparent);
QPainter painter(&pixmap);
painter.setRenderHint(QPainter::Antialiasing, true);
// NB: Everything below is measured in pixels of the screen.
painter.scale(1 / device_pixel_ratio, 1 / device_pixel_ratio);
+ painter.translate(margin_in_pixels, margin_in_pixels);
// An icon is drawn in one opaque color, then made as translucent as its color as a whole, so that where its lines
// cross they don't add up to a brighter spot.
@@ -712,13 +719,10 @@ static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& pa
// that leaves room inside an icon of this size.
auto stroke_width = max(1, static_cast<int>(floor((chrome_icon_stroke_width(logical_size) * device_pixel_ratio) + 0.25)));
auto scale = logical_size * device_pixel_ratio / CHROME_ICON_CANVAS_SIZE;
- auto fitted_path = fit_icon_path_to_pixels(path, scale, stroke_width, pixmap.width());
// Fill the combined outline once so antialiased edges do not accumulate where lines meet.
- auto outline = icon_outline(fitted_path, stroke_width);
+ QPainterPath outline = outline_for(scale, stroke_width, canvas_size.width());
outline.setFillRule(Qt::WindingFill);
- if (icon == ChromeIcon::StarFilled)
- outline.addPath(fitted_path);
painter.fillPath(outline.simplified(), opaque_color);
if (color.alpha() != 255) {
@@ -730,8 +734,19 @@ static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& pa
return pixmap;
}
+static QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QPainterPath const& path, QColor color, qreal device_pixel_ratio, int logical_size)
+{
+ return create_chrome_icon_pixmap(color, device_pixel_ratio, logical_size, [&](qreal scale, int stroke_width, int canvas_size) {
+ auto fitted_path = fit_icon_path_to_pixels(path, scale, stroke_width, canvas_size);
+ auto outline = icon_outline(fitted_path, stroke_width);
+ if (icon == ChromeIcon::StarFilled)
+ outline.addPath(fitted_path);
+ return outline;
+ });
+}
+
// Draws a chrome icon from its vector for each size and device pixel ratio it is asked for.
-class ChromeIconEngine final : public QIconEngine {
+class ChromeIconEngine : public QIconEngine {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -777,7 +792,7 @@ public:
if (entry.side == side && entry.scale == scale && entry.mode == mode)
return entry.pixmap;
}
- auto pixmap = create_chrome_icon_pixmap(m_icon, m_path, color_for_mode(mode), scale, side);
+ auto pixmap = create_icon_pixmap(color_for_mode(mode), scale, side);
m_pixmaps.append({ side, scale, mode, pixmap });
return pixmap;
}
@@ -791,6 +806,14 @@ public:
painter->drawPixmap(position, pixmap);
}
+protected:
+ virtual QPixmap create_icon_pixmap(QColor color, qreal device_pixel_ratio, int logical_size) const
+ {
+ return create_chrome_icon_pixmap(m_icon, m_path, color, device_pixel_ratio, logical_size);
+ }
+
+ Colors const& colors() const { return m_colors; }
+
private:
QColor color_for_mode(QIcon::Mode mode) const
{
@@ -826,7 +849,7 @@ QPixmap create_chrome_icon_pixmap(ChromeIcon icon, QColor color, qreal device_pi
return create_chrome_icon_pixmap(icon, chrome_icon_painter_path(icon), color, device_pixel_ratio, CHROME_ICON_CANVAS_SIZE);
}
-static QIcon create_chrome_icon(ChromeIcon icon, QPainterPath const& path, QPalette const& palette, ChromeStyle::WindowActivity activity, int logical_size)
+static ChromeIconEngine::Colors chrome_icon_colors(ChromeIcon icon, QPalette const& palette, ChromeStyle::WindowActivity activity)
{
auto const& chrome = Theme::chrome(palette);
@@ -854,7 +877,12 @@ static QIcon create_chrome_icon(ChromeIcon icon, QPainterPath const& path, QPale
if (activity == ChromeStyle::WindowActivity::Inactive)
normal = chrome.muted;
- return QIcon(new ChromeIconEngine(icon, path, { normal, chrome.icon, chrome.disabled, chrome.icon }, logical_size));
+ return { normal, chrome.icon, chrome.disabled, chrome.icon };
+}
+
+static QIcon create_chrome_icon(ChromeIcon icon, QPainterPath const& path, QPalette const& palette, ChromeStyle::WindowActivity activity, int logical_size)
+{
+ return QIcon(new ChromeIconEngine(icon, path, chrome_icon_colors(icon, palette, activity), logical_size));
}
QIcon create_chrome_icon(ChromeIcon icon, QPalette const& palette, ChromeStyle::WindowActivity activity, int logical_size)
@@ -889,6 +917,168 @@ QIcon create_site_settings_icon(QPalette const& palette, qreal knob_travel)
return create_chrome_icon(ChromeIcon::SiteSettings, path, palette, ChromeStyle::WindowActivity::Active, CHROME_ICON_CANVAS_SIZE);
}
+// What is left of a path of one line or curve after another once the given part of its length is cut from its start.
+static QPainterPath cut_from_start_of_path(QPainterPath const& path, qreal cut_part)
+{
+ auto cut_length = path.length() * cut_part;
+ QPainterPath result;
+ QPointF current = path.elementAt(0);
+ qreal length_so_far = 0;
+ for (int index = 1; index < path.elementCount(); ++index) {
+ auto const& element = path.elementAt(index);
+ QPainterPath segment;
+ segment.moveTo(current);
+ if (element.isCurveTo()) {
+ QPointF control1 = element;
+ QPointF control2 = path.elementAt(index + 1);
+ QPointF end = path.elementAt(index + 2);
+ segment.cubicTo(control1, control2, end);
+ auto segment_length = segment.length();
+ if (length_so_far + segment_length > cut_length) {
+ if (result.elementCount() == 0) {
+ // NB: Split the curve where the cut falls, keeping its second part.
+ auto t = segment.percentAtLength(cut_length - length_so_far);
+ auto lerp = [](QPointF a, QPointF b, qreal t) { return a + ((b - a) * t); };
+ auto p01 = lerp(current, control1, t);
+ auto p12 = lerp(control1, control2, t);
+ auto p23 = lerp(control2, end, t);
+ auto p012 = lerp(p01, p12, t);
+ auto p123 = lerp(p12, p23, t);
+ result.moveTo(lerp(p012, p123, t));
+ result.cubicTo(p123, p23, end);
+ } else {
+ result.cubicTo(control1, control2, end);
+ }
+ }
+ length_so_far += segment_length;
+ current = end;
+ index += 2;
+ continue;
+ }
+ QPointF end = element;
+ segment.lineTo(end);
+ auto segment_length = segment.length();
+ if (length_so_far + segment_length > cut_length) {
+ if (result.elementCount() == 0)
+ result.moveTo(current + ((end - current) * ((cut_length - length_so_far) / segment_length)));
+ result.lineTo(end);
+ }
+ length_so_far += segment_length;
+ current = end;
+ }
+ return result;
+}
+
+// The reload arrow turns a quarter turn clockwise while its arc reels itself into the arrowhead, whose legs then pull
+// into their corner. The cross draws itself from there, one line after the other: the first from the bottom right,
+// where the arrowhead ended up, and the second from the top right.
+class ReloadToStopIconEngine final : public ChromeIconEngine {
+public:
+ AK_ALLOC_WITH_KMALLOC;
+
+ ReloadToStopIconEngine(Colors colors, int logical_size, qreal progress)
+ : ChromeIconEngine(ChromeIcon::Reload, {}, AK::move(colors), logical_size)
+ , m_logical_size(logical_size)
+ , m_progress(progress)
+ {
+ build_paths();
+ }
+
+ virtual QIconEngine* clone() const override { return new ReloadToStopIconEngine(colors(), m_logical_size, m_progress); }
+
+ // NB: The pixmap reaches past the icon by its margin on each side.
+ virtual void paint(QPainter* painter, QRect const& rect, QIcon::Mode mode, QIcon::State state) override
+ {
+ auto side = min(rect.width(), rect.height());
+ auto device_pixel_ratio = painter->device() ? painter->device()->devicePixelRatio() : 1;
+ auto pixmap = scaledPixmap(QSize(side, side), mode, state, device_pixel_ratio);
+ auto margin = margin_in_pixels(device_pixel_ratio) / device_pixel_ratio;
+ auto position = QPointF(rect.topLeft() + QPoint((rect.width() - side) / 2, (rect.height() - side) / 2)) - QPointF(margin, margin);
+ painter->drawPixmap(position, pixmap);
+ }
+
+private:
+ void build_paths()
+ {
+ static auto const arc = parse_svg_path("M13.41 9a5.5 5.5 0 0 1-5.41 4.5a5.5 5.5 0 1 1 3.89-9.39L13.5 5.5"sv);
+ static constexpr QPointF ARROWHEAD_CORNER { 13.5, 5.5 };
+ static constexpr QPointF ARROWHEAD_LEG_ENDS[] { { 13.5, 2 }, { 10, 5.5 } };
+ static auto const exit_easing = Motion::exit_easing();
+ static auto const enter_easing = Motion::enter_easing();
+
+ auto part = [&](qreal start, qreal end) { return clamp((m_progress - start) / (end - start), 0.0, 1.0); };
+ auto reeled_in = exit_easing.valueForProgress(part(0, 0.45));
+ auto legs_pulled_in = exit_easing.valueForProgress(part(0.325, 0.45));
+ auto first_line_drawn = enter_easing.valueForProgress(part(0.425, 0.8));
+ auto second_line_drawn = enter_easing.valueForProgress(part(0.55, 1));
+
+ QPainterPath arrow;
+ if (reeled_in < 1)
+ arrow = cut_from_start_of_path(arc, reeled_in);
+ if (legs_pulled_in < 1) {
+ for (auto leg_end : ARROWHEAD_LEG_ENDS) {
+ arrow.moveTo(ARROWHEAD_CORNER);
+ arrow.lineTo(ARROWHEAD_CORNER + ((leg_end - ARROWHEAD_CORNER) * (1 - legs_pulled_in)));
+ }
+ }
+ m_arrow = arrow;
+ m_arrow_turn_degrees = 90 * reeled_in;
+
+ // NB: A line drawn only a sliver of its length would show as a dot.
+ static constexpr qreal SHORTEST_DRAWN_PART = 0.02;
+ auto add_line = [&](QPointF from, QPointF to, qreal drawn) {
+ if (drawn <= SHORTEST_DRAWN_PART)
+ return;
+ m_cross.moveTo(from);
+ m_cross.lineTo(from + ((to - from) * drawn));
+ };
+ add_line({ 11.5, 11.5 }, { 3.5, 3.5 }, first_line_drawn);
+ add_line({ 11.5, 3.5 }, { 3.5, 11.5 }, second_line_drawn);
+ }
+
+ // The arrowhead swings past the corners of the icon as the arrow turns.
+ static int margin_in_pixels(qreal device_pixel_ratio)
+ {
+ static constexpr qreal MARGIN = 2;
+ return static_cast<int>(ceil(MARGIN * device_pixel_ratio));
+ }
+
+ // The arrow is fitted to the pixels as the reload icon is, and the cross as the stop icon is, so that the morph
+ // starts and ends just where the two icons take over. The arrow turns after it is fitted, so that its lines move
+ // smoothly rather than jump from one pixel to the next.
+ virtual QPixmap create_icon_pixmap(QColor color, qreal device_pixel_ratio, int logical_size) const override
+ {
+ static constexpr QPointF ARROW_TURN_CENTER { 8, 8 };
+ auto outline_for = [&](qreal scale, int stroke_width, int canvas_size) {
+ auto const& reload_path = chrome_icon_painter_path(ChromeIcon::Reload);
+ QPainterPath turn_center;
+ turn_center.moveTo(ARROW_TURN_CENTER);
+ auto fitted_turn_center = QPointF(fit_icon_part_to_pixels(reload_path, turn_center, scale, stroke_width, canvas_size).elementAt(0));
+ QTransform turn;
+ turn.translate(fitted_turn_center.x(), fitted_turn_center.y());
+ turn.rotate(m_arrow_turn_degrees);
+ turn.translate(-fitted_turn_center.x(), -fitted_turn_center.y());
+ auto outline = icon_outline(turn.map(fit_icon_part_to_pixels(reload_path, m_arrow, scale, stroke_width, canvas_size)), stroke_width);
+
+ auto const& stop_path = chrome_icon_painter_path(ChromeIcon::Stop);
+ outline.addPath(icon_outline(fit_icon_part_to_pixels(stop_path, m_cross, scale, stroke_width, canvas_size), stroke_width));
+ return outline;
+ };
+ return create_chrome_icon_pixmap(color, device_pixel_ratio, logical_size, outline_for, margin_in_pixels(device_pixel_ratio));
+ }
+
+ int m_logical_size { CHROME_ICON_CANVAS_SIZE };
+ qreal m_progress { 0 };
+ QPainterPath m_arrow;
+ qreal m_arrow_turn_degrees { 0 };
+ QPainterPath m_cross;
+};
+
+QIcon create_reload_to_stop_icon(QPalette const& palette, ChromeStyle::WindowActivity activity, qreal progress, int logical_size)
+{
+ return QIcon(new ReloadToStopIconEngine(chrome_icon_colors(ChromeIcon::Reload, palette, activity), logical_size, progress));
+}
+
#if !defined(AK_OS_MACOS)
// NB: Pages are only translated on macOS, where the system draws the icon; see TranslateIconMac.mm.
QIcon create_translate_icon(QPalette const& palette, bool)
diff --git a/UI/Qt/Icon.h b/UI/Qt/Icon.h
index fa8ee54f100..e465ac5de36 100644
--- a/UI/Qt/Icon.h
+++ b/UI/Qt/Icon.h
@@ -80,6 +80,8 @@ QIcon favicon_from_png(ReadonlyBytes, int logical_size);
QIcon create_chrome_icon(ChromeIcon, QPalette const&, ChromeStyle::WindowActivity = ChromeStyle::WindowActivity::Active, int logical_size = CHROME_ICON_CANVAS_SIZE);
// The site settings icon with its two knobs moved toward each other's place, by a travel from 0 to 1.
QIcon create_site_settings_icon(QPalette const&, qreal knob_travel);
+// The reload icon partway through turning into the stop icon, by a progress from 0 to 1.
+QIcon create_reload_to_stop_icon(QPalette const&, ChromeStyle::WindowActivity, qreal progress, int logical_size);
// The translate button's icon, in the accent color once the page is translated.
QIcon create_translate_icon(QPalette const&, bool page_is_translated);
// The icon in one color, whatever its state.
diff --git a/UI/Qt/WindowToolbar.cpp b/UI/Qt/WindowToolbar.cpp
index a8cbdfcee78..8da79306e2f 100644
--- a/UI/Qt/WindowToolbar.cpp
+++ b/UI/Qt/WindowToolbar.cpp
@@ -233,9 +233,11 @@ private:
};
// The reload button becomes a stop button while the page loads. So that one click does not do what the user did not aim
-// at, a stop button under the pointer stays until the pointer leaves or a short delay passes, and a click on stop within
-// the double-click interval of a reload click is ignored, so a double-click does not stop the reload it started. The
-// reload action stays attached to the button while it shows stop, so its shortcuts still work during a load.
+// at, a stop button under the pointer stays until the pointer leaves or a short delay passes, and a click within the
+// double-click interval of a click on the other action is ignored, so a double-click does not stop the reload it
+// started or reload the page it stopped. The
+// reload action stays attached to the button while it shows stop, so its shortcuts still work during a load. Like the
+// back and forward buttons, it moves only when clicked: a load started or stopped otherwise changes its icon at once.
class ReloadButton final : public ToolbarButton {
public:
AK_ALLOC_WITH_KMALLOC;
@@ -243,12 +245,25 @@ public:
explicit ReloadButton(QWidget* parent)
: ToolbarButton(parent)
{
- m_icon_fade.setStartValue(0.0);
- m_icon_fade.setEndValue(1.0);
- QObject::connect(&m_icon_fade, &QVariantAnimation::valueChanged, this, [this] { update(); });
+ QObject::connect(&m_morph, &QVariantAnimation::valueChanged, this, [this](QVariant const& value) {
+ m_morph_progress = value.toReal();
+ update();
+ });
+
+ // NB: A coarse timer can fire early, and show the stop icon for a load that ends just inside the delay.
+ m_morph_delay_timer.setSingleShot(true);
+ m_morph_delay_timer.setTimerType(Qt::PreciseTimer);
+ m_morph_delay_timer.setInterval(MORPH_DELAY_MS);
+ QObject::connect(&m_morph_delay_timer, &QTimer::timeout, this, [this] {
+ show_stop_mode();
+ if (m_load_was_started_by_click)
+ morph_to(1);
+ else
+ show_morph_at(1);
+ });
- m_double_click_timer.setSingleShot(true);
- m_double_click_timer.setInterval(QApplication::doubleClickInterval());
+ m_last_click_timer.setSingleShot(true);
+ m_last_click_timer.setInterval(QApplication::doubleClickInterval());
m_stop_to_reload_timer.setSingleShot(true);
m_stop_to_reload_timer.setInterval(STOP_TO_RELOAD_DELAY_MS);
@@ -256,11 +271,6 @@ public:
change_mode(m_intended_mode, ForceChange::Yes);
});
- QObject::connect(this, &QToolButton::pressed, this, [this] {
- if (m_visible_mode == Mode::Reload)
- m_double_click_timer.start();
- });
-
// NB: A stop click that arrives after the load ended has nothing to stop, so show the reload button right away.
QObject::connect(this, &QToolButton::clicked, this, [this] {
if (m_visible_mode == Mode::Stop)
@@ -283,7 +293,11 @@ public:
// Another tab's state is shown as it stands, without the motion of a change.
void show_loading_at_once(bool is_loading)
{
- m_icon_fade.stop();
+ m_morph_delay_timer.stop();
+ show_morph_at(is_loading ? 1 : 0);
+ m_last_click_timer.stop();
+ m_reload_click_is_pending = false;
+ m_stop_was_clicked = false;
m_stop_to_reload_timer.stop();
m_intended_mode = m_visible_mode = is_loading ? Mode::Stop : Mode::Reload;
setDefaultAction(is_loading ? m_stop_action : m_reload_action);
@@ -306,9 +320,22 @@ protected:
// any other click there does.
virtual void nextCheckState() override
{
- if (m_visible_mode == Mode::Stop && m_double_click_timer.isActive())
+ // NB: Only a click that reaches an action counts, so a press released outside the button does not animate it.
+ // During the morph delay the button is still the reload button, but it is about to be the stop button, so
+ // the second click of a double-click on reload is ignored there too.
+ // A click that opens a new tab acts on no load here, so it neither counts nor is ignored.
+ auto opens_in_new_tab = click_opens_in_new_tab();
+ auto clicked_mode = m_visible_mode == Mode::Stop || m_morph_delay_timer.isActive() ? Mode::Stop : Mode::Reload;
+ if (!opens_in_new_tab && m_last_click_timer.isActive() && clicked_mode != m_last_clicked_mode)
return;
- if (m_visible_mode == Mode::Reload && !click_opens_in_new_tab() && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {
+ if (m_visible_mode == Mode::Stop)
+ m_stop_was_clicked = true;
+ if (!opens_in_new_tab) {
+ m_last_clicked_mode = m_visible_mode;
+ m_last_click_timer.start();
+ m_reload_click_is_pending = m_visible_mode == Mode::Reload;
+ }
+ if (m_visible_mode == Mode::Reload && !opens_in_new_tab && QApplication::keyboardModifiers().testFlag(Qt::ShiftModifier)) {
m_reload_bypassing_cache_action->trigger();
return;
}
@@ -317,13 +344,8 @@ protected:
private:
static constexpr int STOP_TO_RELOAD_DELAY_MS = 1000;
- static constexpr int ARROW_TURN_DURATION_MS = 340;
- static constexpr int TURN_CROSS_FADE_DELAY_MS = 200;
- static constexpr int TURN_CROSS_FADE_DURATION_MS = 140;
- static constexpr int STOP_TURN_DELAY_MS = 180;
- static constexpr int STOP_TURN_DURATION_MS = 200;
- static constexpr int RELOAD_TO_STOP_DURATION_MS = STOP_TURN_DELAY_MS + STOP_TURN_DURATION_MS;
- static constexpr int ICON_CROSS_FADE_DURATION_MS = 160;
+ static constexpr int MORPH_DURATION_MS = 400;
+ static constexpr int MORPH_DELAY_MS = 100;
enum class Mode {
Reload,
@@ -337,22 +359,40 @@ private:
void change_mode(Mode mode, ForceChange force_change)
{
+ // NB: A stop click belongs to the load it stopped, not to one that starts while the stop button is held.
+ if (mode == Mode::Stop && m_intended_mode == Mode::Reload)
+ m_stop_was_clicked = false;
m_intended_mode = mode;
- auto change_is_safe = mode == Mode::Stop || m_visible_mode != Mode::Stop;
+ // NB: The stop button is held under the pointer only once it shows stop; a morph toward it still turns back.
+ auto change_is_safe = mode == Mode::Stop || m_visible_mode != Mode::Stop || m_morph.state() == QAbstractAnimation::Running;
if (force_change == ForceChange::Yes || !underMouse() || change_is_safe) {
m_stop_to_reload_timer.stop();
- if (m_visible_mode != mode && defaultAction() && !SystemAppearance::prefers_reduced_motion()) {
- m_previous_icon = defaultAction()->icon();
- // NB: The arrow turns into the stop icon only as the reload button is clicked. A load started otherwise
- // cross-fades it, as does the end of a load.
- m_turns_icon = mode == Mode::Stop && m_double_click_timer.isActive();
- m_icon_fade.stop();
- m_icon_fade.setDuration(m_turns_icon ? RELOAD_TO_STOP_DURATION_MS : ICON_CROSS_FADE_DURATION_MS);
- m_icon_fade.start();
+ // A load that ends within a moment never turns the button into the stop button: it stays the reload button,
+ // action and all, until the delay passes. One that starts while the arrow is still turning back turns it
+ // around at once. A morph under way turns back rather than jumping, whatever changed the load.
+ if (mode == Mode::Stop) {
+ if (m_visible_mode == Mode::Stop || m_morph_delay_timer.isActive())
+ return;
+ // NB: A reload click animates the one load it started, not another that starts within the same interval.
+ m_load_was_started_by_click = m_reload_click_is_pending && m_last_click_timer.isActive();
+ m_reload_click_is_pending = false;
+ if (m_morph_progress > 0) {
+ show_stop_mode();
+ morph_to(1);
+ } else {
+ m_morph_delay_timer.start();
+ }
+ return;
}
- m_visible_mode = mode;
- setDefaultAction(mode == Mode::Stop ? m_stop_action : m_reload_action);
+ m_morph_delay_timer.stop();
+ if (m_stop_was_clicked || m_morph.state() == QAbstractAnimation::Running)
+ morph_to(0);
+ else
+ show_morph_at(0);
+ m_stop_was_clicked = false;
+ m_visible_mode = Mode::Reload;
+ setDefaultAction(m_reload_action);
return;
}
@@ -360,51 +400,74 @@ private:
m_stop_to_reload_timer.start();
}
- // As a click on it starts a load, the reload arrow turns half a turn, and over its second half cross-fades into the
- // stop icon, which turns into place. Otherwise, as when the load ends, the two icons only cross-fade.
+ void show_stop_mode()
+ {
+ m_stop_was_clicked = false;
+ m_visible_mode = Mode::Stop;
+ setDefaultAction(m_stop_action);
+ }
+
+ // The arrow morphs into the stop icon as a load starts, and back as it ends. A change partway through turns back
+ // from where the morph is, at the same pace.
+ void morph_to(qreal target)
+ {
+ if (SystemAppearance::prefers_reduced_motion() || !isVisible()) {
+ show_morph_at(target);
+ return;
+ }
+ auto duration = qRound(MORPH_DURATION_MS * abs(target - m_morph_progress));
+ if (duration <= 0) {
+ show_morph_at(target);
+ return;
+ }
+ m_morph.stop();
+ m_morph.setStartValue(m_morph_progress);
+ m_morph.setEndValue(target);
+ m_morph.setDuration(duration);
+ m_morph.start();
+ }
+
+ void show_morph_at(qreal progress)
+ {
+ m_morph.stop();
+ m_morph_progress = progress;
+ update();
+ }
+
virtual void paintEvent(QPaintEvent* event) override
{
- if (m_icon_fade.state() != QAbstractAnimation::Running) {
+ auto shows_action_icon = m_morph.state() != QAbstractAnimation::Running
+ && m_morph_progress == (m_visible_mode == Mode::Stop ? 1 : 0);
+ if (shows_action_icon) {
ToolbarButton::paintEvent(event);
return;
}
QPainter painter(this);
- painter.setRenderHint(QPainter::SmoothPixmapTransform, true);
ChromeStyle::paint_control_frame(painter, *this);
- auto elapsed = m_icon_fade.currentValue().toReal() * m_icon_fade.duration();
- auto part = [&](qreal delay, qreal duration) { return clamp((elapsed - delay) / duration, 0.0, 1.0); };
- auto center = QRectF(icon_rect()).center();
- auto paint_turned = [&](QIcon const& icon, qreal opacity, qreal degrees) {
- painter.save();
- painter.setOpacity(opacity);
- painter.translate(center);
- painter.rotate(degrees);
- painter.translate(-center);
- icon.paint(&painter, icon_rect(), Qt::AlignCenter, icon_mode());
- painter.restore();
- };
- if (!m_turns_icon) {
- auto fade = part(0, ICON_CROSS_FADE_DURATION_MS);
- paint_turned(m_previous_icon, 1 - fade, 0);
- paint_turned(icon(), fade, 0);
- return;
- }
- static auto const easing = Motion::enter_easing();
- auto fade = part(TURN_CROSS_FADE_DELAY_MS, TURN_CROSS_FADE_DURATION_MS);
- paint_turned(m_previous_icon, 1 - fade, 180 * easing.valueForProgress(part(0, ARROW_TURN_DURATION_MS)));
- paint_turned(icon(), fade, -90 * (1 - easing.valueForProgress(part(STOP_TURN_DELAY_MS, STOP_TURN_DURATION_MS))));
+ QIcon icon;
+ if (m_morph_progress <= 0)
+ icon = m_reload_action->icon();
+ else if (m_morph_progress >= 1)
+ icon = m_stop_action->icon();
+ else
+ icon = create_reload_to_stop_icon(Theme::chrome_palette(*this), ChromeStyle::window_activity(*this), m_morph_progress, iconSize().width());
+ icon.paint(&painter, icon_rect(), Qt::AlignCenter, icon_mode());
}
QAction* m_reload_action { nullptr };
QAction* m_reload_bypassing_cache_action { nullptr };
QAction* m_stop_action { nullptr };
- QIcon m_previous_icon;
- QVariantAnimation m_icon_fade;
- bool m_turns_icon { false };
+ QVariantAnimation m_morph;
+ qreal m_morph_progress { 0 };
+ QTimer m_morph_delay_timer;
+ bool m_load_was_started_by_click { false };
+ bool m_stop_was_clicked { false };
Mode m_visible_mode { Mode::Reload };
Mode m_intended_mode { Mode::Reload };
- QTimer m_double_click_timer;
+ QTimer m_last_click_timer;
+ Mode m_last_clicked_mode { Mode::Reload };
+ bool m_reload_click_is_pending { false };
QTimer m_stop_to_reload_timer;
};
@@ -1732,6 +1795,7 @@ void WindowToolbar::bind(Tab* tab)
// A tab that hasn't loaded has no page yet, so it is shown once it has one.
if (!tab || !tab->is_loaded()) {
show_location_edit(nullptr);
+ m_reload_button->show_loading_at_once(false);
if (tab)
QObject::connect(tab, &Tab::loaded, this, [this, tab] { show_tab(*tab); }, Qt::SingleShotConnection);
return;