Skip to content

Truly independent web browser

UI/Qt: Only show a button's hover frame while the pointer is over it

Andreas Klingc05aa47226c6
diff --git a/UI/Qt/ChromeStyle.cpp b/UI/Qt/ChromeStyle.cpp
index 2d68bd67e61..b9ab710bdc8 100644
--- a/UI/Qt/ChromeStyle.cpp
+++ b/UI/Qt/ChromeStyle.cpp
@@ -10,6 +10,7 @@
#include <UI/Qt/StringUtils.h>
#include <QAbstractButton>
+#include <QCursor>
#include <QElapsedTimer>
#include <QFont>
#include <QGuiApplication>
@@ -1704,15 +1705,18 @@ static void paint_circular_control_frame(QPainter& painter, QRect const& rect, q
}
}
-void paint_circular_control_frame(QPainter& painter, QAbstractButton const& button, CircularControlFrameStyle frame_style)
+Optional<ControlFrameColors> circular_control_frame_colors(QAbstractButton const& button, CircularControlFrameStyle frame_style)
{
if (!button.isEnabled())
- return;
+ return {};
auto pressed = button.isDown() || button.isChecked();
- auto hovered = button.underMouse();
+ // NB: Qt keeps a button hovered until the pointer moves, even after a relayout has moved the button away from the
+ // pointer, as when a button is clicked and its toolbar or sidebar changes around it. So also check that the
+ // pointer is actually over the button.
+ auto hovered = button.underMouse() && button.rect().contains(button.mapFromGlobal(QCursor::pos()));
if (!hovered && !pressed && frame_style == CircularControlFrameStyle::InteractionOnly)
- return;
+ return {};
auto const& palette = button.window()->palette();
auto background = frame_style == CircularControlFrameStyle::ActiveTabOverlay
@@ -1739,7 +1743,13 @@ void paint_circular_control_frame(QPainter& painter, QAbstractButton const& butt
border = chrome_control_border(palette);
}
- paint_circular_control_frame(painter, button.rect(), button.devicePixelRatioF(), background, border);
+ return ControlFrameColors { background, border };
+}
+
+void paint_circular_control_frame(QPainter& painter, QAbstractButton const& button, CircularControlFrameStyle frame_style)
+{
+ if (auto colors = circular_control_frame_colors(button, frame_style); colors.has_value())
+ paint_circular_control_frame(painter, button.rect(), button.devicePixelRatioF(), colors->background, colors->border);
}
// A purple gradient with two soft bright spots that moves steadily along a border, to show that something is loading.
diff --git a/UI/Qt/ChromeStyle.h b/UI/Qt/ChromeStyle.h
index f776be1123d..8088c248958 100644
--- a/UI/Qt/ChromeStyle.h
+++ b/UI/Qt/ChromeStyle.h
@@ -6,6 +6,7 @@
#pragma once
+#include <AK/Optional.h>
#include <QColor>
#include <QString>
@@ -65,6 +66,12 @@ enum class CircularControlFrameStyle {
InteractionOnly,
ActiveTabOverlay,
};
+struct ControlFrameColors {
+ QColor background;
+ QColor border;
+};
+// The colors of a button's frame in the button's current state, or nothing while it shows no frame.
+Optional<ControlFrameColors> circular_control_frame_colors(QAbstractButton const&, CircularControlFrameStyle = CircularControlFrameStyle::InteractionOnly);
void paint_circular_control_frame(QPainter&, QAbstractButton const&, CircularControlFrameStyle = CircularControlFrameStyle::InteractionOnly);
void paint_loading_swirl(QPainter&, QPainterPath const& border);
diff --git a/UI/Qt/TabBar.cpp b/UI/Qt/TabBar.cpp
index 3572919e5b3..c4155dd30c5 100644
--- a/UI/Qt/TabBar.cpp
+++ b/UI/Qt/TabBar.cpp
@@ -426,15 +426,14 @@ private:
// NB: The horizontal new tab button's shape is square, so this pill is a circle.
QPainterPath tab_path;
tab_path.addRoundedRect(shape_rect, shape_rect.height() / 2.0, shape_rect.height() / 2.0);
- auto hovered = underMouse();
-
- if (isDown()) {
- painter.setBrush(ChromeStyle::chrome_surface_pressed(palette()));
- painter.setPen(QPen(ChromeStyle::chrome_control_border(palette()), 1));
- painter.drawPath(tab_path);
- } else if (hovered) {
- painter.setBrush(tab_hover_surface(palette(), 1.0));
- painter.setPen(Qt::NoPen);
+ // NB: Hovered and pressed, the button looks like the toolbar's buttons, though in the shape of a tab.
+ auto hovered = underMouse() && rect().contains(mapFromGlobal(QCursor::pos()));
+ if (auto colors = ChromeStyle::circular_control_frame_colors(*this); colors.has_value()) {
+ // NB: Hovered but not pressed, it lights up like the tabs beside it.
+ if (hovered && !isDown())
+ colors->background = tab_hover_surface(palette(), 1.0);
+ painter.setBrush(colors->background);
+ painter.setPen(colors->border.alpha() > 0 ? QPen(colors->border, 1) : QPen(Qt::NoPen));
painter.drawPath(tab_path);
}