From d202a2fba46ff33b9e91cff1ed5991e144d6124c Mon Sep 17 00:00:00 2001 From: fufesou Date: Fri, 11 Sep 2026 13:51:24 +0800 Subject: [PATCH] fix(flutter): scale the remote cursor with its image --- flutter/lib/desktop/pages/remote_page.dart | 25 ++++---- flutter/lib/utils/image.dart | 8 ++- flutter/test/cursor_paint_scale_test.dart | 68 +++++++++++++++++----- 3 files changed, 70 insertions(+), 31 deletions(-) diff --git a/flutter/lib/desktop/pages/remote_page.dart b/flutter/lib/desktop/pages/remote_page.dart index f726cdae8..b988638d2 100644 --- a/flutter/lib/desktop/pages/remote_page.dart +++ b/flutter/lib/desktop/pages/remote_page.dart @@ -1413,26 +1413,25 @@ class CursorPaint extends StatelessWidget { } } - double x = m.x * c.scale + cx - hotx; - double y = m.y * c.scale + cy - hoty; - double scale = 1.0; - final isViewOriginal = c.viewStyle.style == kRemoteViewStyleOriginal; - if (zoomCursor.value || isViewOriginal) { - x = m.x - hotx + cx / c.scale; - y = m.y - hoty + cy / c.scale; - scale = c.scale; - } else if (isLinux || isMacOS) { - scale = 1.0 / MediaQuery.devicePixelRatioOf(context); - x = (m.x * c.scale + cx) / scale - hotx; - y = (m.y * c.scale + cy) / scale - hoty; + final image = m.image ?? preDefaultCursor.image; + final nativePixels = isWindows ? MediaQuery.devicePixelRatioOf(context) : 1.0; + double scale = c.scale; + if (image != null && scale * nativePixels != 1.0) { + final sx = kMinCursorSize / (image.width * nativePixels); + final sy = kMinCursorSize / (image.height * nativePixels); + final minimumScale = sx > sy ? sx : sy; + if (scale < minimumScale) scale = minimumScale; } + final x = (m.x * c.scale + cx) / scale - hotx; + final y = (m.y * c.scale + cy) / scale - hoty; return CustomPaint( painter: ImagePainter( - image: m.image ?? preDefaultCursor.image, + image: image, x: x, y: y, scale: scale, + useIntegerPosition: false, ), ); } diff --git a/flutter/lib/utils/image.dart b/flutter/lib/utils/image.dart index d5042e61a..4e2fe1e74 100644 --- a/flutter/lib/utils/image.dart +++ b/flutter/lib/utils/image.dart @@ -96,12 +96,14 @@ class ImagePainter extends CustomPainter { required this.x, required this.y, required this.scale, + this.useIntegerPosition = true, }); ui.Image? image; double x; double y; double scale; + final bool useIntegerPosition; @override void paint(Canvas canvas, Size size) { @@ -122,8 +124,10 @@ class ImagePainter extends CustomPainter { if (isWeb) { paint.filterQuality = FilterQuality.high; } - canvas.drawImage( - image!, Offset(x.toInt().toDouble(), y.toInt().toDouble()), paint); + final position = useIntegerPosition + ? Offset(x.toInt().toDouble(), y.toInt().toDouble()) + : Offset(x, y); + canvas.drawImage(image!, position, paint); } @override diff --git a/flutter/test/cursor_paint_scale_test.dart b/flutter/test/cursor_paint_scale_test.dart index 5de3f5808..3e902113b 100644 --- a/flutter/test/cursor_paint_scale_test.dart +++ b/flutter/test/cursor_paint_scale_test.dart @@ -11,9 +11,8 @@ import 'package:get/get.dart'; import 'package:provider/provider.dart'; const _hotspot = Offset(4, 9); -const _remotePosition = Offset(100, 80); -const _canvasOffset = Offset(15, 10); -const _canvasScale = 0.5; +const _remotePosition = Offset(100.25, 80.75); +const _canvasOffset = Offset(15.125, 10.25); const _viewport = Size(200, 160); class _CursorModel extends ChangeNotifier implements CursorModel { @@ -35,7 +34,7 @@ class _CursorModel extends ChangeNotifier implements CursorModel { } class _CanvasModel extends ChangeNotifier implements CanvasModel { - _CanvasModel(String style) + _CanvasModel(String style, this.scale) : viewStyle = ViewStyle( style: style, width: _viewport.width, @@ -51,7 +50,7 @@ class _CanvasModel extends ChangeNotifier implements CanvasModel { @override double get y => _canvasOffset.dy; @override - double get scale => _canvasScale; + final double scale; @override ScrollStyle get scrollStyle => ScrollStyle.scrollauto; @@ -59,17 +58,48 @@ class _CanvasModel extends ChangeNotifier implements CanvasModel { dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); } +class _Canvas extends Fake implements Canvas { + double factor = 1; + Offset? position; + + @override + void scale(double sx, [double? sy]) => factor *= sx; + + @override + void drawImage(ui.Image image, Offset offset, Paint paint) { + position = offset * factor; + } +} + void main() { - for (final (style, zoom, dpr, scale) in [ - (kRemoteViewStyleAdaptive, false, 2.0, 0.5), - (kRemoteViewStyleAdaptive, false, 3.0, 1 / 3), - (kRemoteViewStyleAdaptive, true, 3.0, _canvasScale), - (kRemoteViewStyleOriginal, false, 2.0, _canvasScale), + for (final (style, zoom, dpr, source, canvasScale, scale) in [ + (kRemoteViewStyleAdaptive, false, 2.0, (48, 64), 0.375, 0.375), + (kRemoteViewStyleAdaptive, false, 3.0, (48, 64), 0.25, 0.25), + (kRemoteViewStyleAdaptive, true, 3.0, (48, 64), 0.375, 0.375), + (kRemoteViewStyleOriginal, false, 2.0, (48, 64), 0.5, 0.5), + (kRemoteViewStyleOriginal, true, 2.0, (48, 64), 0.5, 0.5), + ( + kRemoteViewStyleAdaptive, + false, + 2.0, + (9, 18), + 0.1, + Platform.isWindows ? 2 / 3 : 4 / 3 + ), + ( + kRemoteViewStyleAdaptive, + true, + 2.0, + (9, 18), + 0.1, + Platform.isWindows ? 2 / 3 : 4 / 3 + ), ]) { - testWidgets('$style zoom=$zoom dpr=$dpr keeps cursor scale and hotspot', + testWidgets( + '$style zoom=$zoom dpr=$dpr source=$source keeps remote geometry', (tester) async { - final image = - (await tester.runAsync(() => createTestImage(width: 9, height: 18)))!; + final image = (await tester.runAsync( + () => createTestImage(width: source.$1, height: source.$2)))!; addTearDown(image.dispose); await tester.pumpWidget(MediaQuery( data: MediaQueryData(devicePixelRatio: dpr), @@ -78,7 +108,7 @@ void main() { ChangeNotifierProvider( create: (_) => _CursorModel(image)), ChangeNotifierProvider( - create: (_) => _CanvasModel(style)), + create: (_) => _CanvasModel(style, canvasScale)), ], child: CursorPaint(id: 'cursor-test', zoomCursor: zoom.obs), ), @@ -90,7 +120,13 @@ void main() { expect(painter.image, same(image)); expect(painter.scale, scale); expect((Offset(painter.x, painter.y) + _hotspot) * scale, - _remotePosition * _canvasScale + _canvasOffset); - }, skip: Platform.isWindows && style == kRemoteViewStyleAdaptive && !zoom); + _remotePosition * canvasScale + _canvasOffset); + final canvas = _Canvas(); + painter.paint(canvas, _viewport); + final position = canvas.position! + _hotspot * canvas.factor; + final target = _remotePosition * canvasScale + _canvasOffset; + expect(position.dx, closeTo(target.dx, 1e-9)); + expect(position.dy, closeTo(target.dy, 1e-9)); + }); } }