Skip to content

Truly independent web browser

LibGfx+LibWeb+UI/Qt: Show the zoom-in and zoom-out cursors

sideshowbarker2a2126b1cc70
diff --git a/Libraries/LibGfx/Cursor.h b/Libraries/LibGfx/Cursor.h
index 41110eca7ec..4a4ab094666 100644
--- a/Libraries/LibGfx/Cursor.h
+++ b/Libraries/LibGfx/Cursor.h
@@ -35,7 +35,8 @@ enum class StandardCursor {
Wait,
Disallowed,
Eyedropper,
- Zoom,
+ ZoomIn,
+ ZoomOut,
};
struct ImageCursor {
@@ -90,8 +91,10 @@ constexpr StringView standard_cursor_to_string(StandardCursor cursor)
return "Disallowed"sv;
case StandardCursor::Eyedropper:
return "Eyedropper"sv;
- case StandardCursor::Zoom:
- return "Zoom"sv;
+ case StandardCursor::ZoomIn:
+ return "ZoomIn"sv;
+ case StandardCursor::ZoomOut:
+ return "ZoomOut"sv;
}
VERIFY_NOT_REACHED();
}
diff --git a/Libraries/LibWeb/Page/PageCursor.cpp b/Libraries/LibWeb/Page/PageCursor.cpp
index 7b17fce5bb0..4d5f44652bc 100644
--- a/Libraries/LibWeb/Page/PageCursor.cpp
+++ b/Libraries/LibWeb/Page/PageCursor.cpp
@@ -81,8 +81,9 @@ Gfx::Cursor css_to_gfx_cursor(CSS::CursorPredefined css_cursor)
case CSS::CursorPredefined::NwseResize:
return Gfx::StandardCursor::ResizeDiagonalTLBR;
case CSS::CursorPredefined::ZoomIn:
+ return Gfx::StandardCursor::ZoomIn;
case CSS::CursorPredefined::ZoomOut:
- return Gfx::StandardCursor::Zoom;
+ return Gfx::StandardCursor::ZoomOut;
case CSS::CursorPredefined::Default:
return Gfx::StandardCursor::Arrow;
case CSS::CursorPredefined::ContextMenu:
diff --git a/Tests/LibWeb/Text/expected/css/cursor-zoom-in-and-zoom-out.txt b/Tests/LibWeb/Text/expected/css/cursor-zoom-in-and-zoom-out.txt
new file mode 100644
index 00000000000..9c8dad8f672
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/cursor-zoom-in-and-zoom-out.txt
@@ -0,0 +1,2 @@
+cursor over #zoom-in: ZoomIn
+cursor over #zoom-out: ZoomOut
diff --git a/Tests/LibWeb/Text/input/css/cursor-zoom-in-and-zoom-out.html b/Tests/LibWeb/Text/input/css/cursor-zoom-in-and-zoom-out.html
new file mode 100644
index 00000000000..74e8e15487a
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/cursor-zoom-in-and-zoom-out.html
@@ -0,0 +1,24 @@
+<!doctype html>
+<script src="../include.js"></script>
+<style>
+ body { margin: 0; }
+ div { height: 40px; }
+</style>
+<div id="zoom-in" style="cursor: zoom-in"></div>
+<div id="zoom-out" style="cursor: zoom-out"></div>
+<script>
+ function nextRenderingUpdate() {
+ return new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
+ }
+
+ // zoom-in and zoom-out are two distinct cursors.
+ asyncTest(async done => {
+ for (const element of [document.getElementById("zoom-in"), document.getElementById("zoom-out")]) {
+ const rect = element.getBoundingClientRect();
+ internals.mouseMove(rect.left + 20, rect.top + 20);
+ await nextRenderingUpdate();
+ println(`cursor over #${element.id}: ${internals.currentCursor()}`);
+ }
+ done();
+ });
+</script>
diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cpp
index 88731a57a5e..5149956fdb1 100644
--- a/UI/Qt/WebContentView.cpp
+++ b/UI/Qt/WebContentView.cpp
@@ -2360,6 +2360,41 @@ Optional<RemoteView::ScreenRects> WebContentView::screen_rects() const
return screen_rects_from_qt();
}
+// Qt has no zoom cursors, so draw a magnifier with a plus or a minus in its lens, with the hotspot at the lens's center.
+static QCursor make_zoom_cursor(bool zoom_in, qreal device_pixel_ratio)
+{
+ static constexpr int size = 24;
+ static constexpr QPointF lens_center { 9.5, 9.5 };
+ static constexpr qreal lens_radius = 6.5;
+
+ QPixmap pixmap { qRound(size * device_pixel_ratio), qRound(size * device_pixel_ratio) };
+ pixmap.setDevicePixelRatio(device_pixel_ratio);
+ pixmap.fill(Qt::transparent);
+
+ QPainter painter { &pixmap };
+ painter.setRenderHint(QPainter::Antialiasing);
+ auto draw_shape = [&](QColor const& color, qreal width) {
+ painter.setPen(QPen { color, width, Qt::SolidLine, Qt::RoundCap });
+ painter.setBrush(Qt::NoBrush);
+ painter.drawEllipse(lens_center, lens_radius, lens_radius);
+ painter.setPen(QPen { color, width + 1.5, Qt::SolidLine, Qt::RoundCap });
+ painter.drawLine(QPointF { 14.5, 14.5 }, QPointF { 21, 21 });
+ painter.setPen(QPen { color, width, Qt::SolidLine, Qt::RoundCap });
+ painter.drawLine(QPointF { 6.5, 9.5 }, QPointF { 12.5, 9.5 });
+ if (zoom_in)
+ painter.drawLine(QPointF { 9.5, 6.5 }, QPointF { 9.5, 12.5 });
+ };
+ // NB: A white outline under the black shape keeps the cursor visible on dark and light backgrounds alike.
+ draw_shape(Qt::white, 3.5);
+ painter.setBrush(QColor { 255, 255, 255, 160 });
+ painter.setPen(Qt::NoPen);
+ painter.drawEllipse(lens_center, lens_radius - 1, lens_radius - 1);
+ draw_shape(Qt::black, 1.5);
+ painter.end();
+
+ return QCursor { pixmap, qRound(lens_center.x()), qRound(lens_center.y()) };
+}
+
void WebContentView::update_cursor(Gfx::Cursor cursor)
{
cursor.visit([this](Gfx::StandardCursor standard_cursor) {
@@ -2418,9 +2453,14 @@ void WebContentView::update_cursor(Gfx::Cursor cursor)
case Gfx::StandardCursor::Disallowed:
setCursor(Qt::ForbiddenCursor);
break;
+ case Gfx::StandardCursor::ZoomIn:
+ setCursor(make_zoom_cursor(true, devicePixelRatioF()));
+ break;
+ case Gfx::StandardCursor::ZoomOut:
+ setCursor(make_zoom_cursor(false, devicePixelRatioF()));
+ break;
case Gfx::StandardCursor::Eyedropper:
- case Gfx::StandardCursor::Zoom:
- // FIXME: No corresponding Qt cursors, default to Arrow
+ // FIXME: No corresponding Qt cursor, default to Arrow
default:
setCursor(Qt::ArrowCursor);
break;