From 19040f3eef5ac7b024afd707f9b054e791b6e2b4 Mon Sep 17 00:00:00 2001 From: fufesou Date: Mon, 14 Sep 2026 00:00:25 +0800 Subject: [PATCH] fix: preserve unzoomed cursor size for hosts without density --- flutter/lib/desktop/pages/remote_page.dart | 11 +- .../test/cursor_density_validation_test.dart | 102 ++++++++++++++---- flutter/test/cursor_dpi_policy_test.dart | 7 +- 3 files changed, 88 insertions(+), 32 deletions(-) diff --git a/flutter/lib/desktop/pages/remote_page.dart b/flutter/lib/desktop/pages/remote_page.dart index 45b0e74da..1d6a41d95 100644 --- a/flutter/lib/desktop/pages/remote_page.dart +++ b/flutter/lib/desktop/pages/remote_page.dart @@ -1129,8 +1129,9 @@ class _ImagePaintState extends State { // buildCursorOfCache() converts it back to logical scale for the plugin. return (isWindows ? dpr : 1.0) / peerDpr; } - // Density metadata is optional. Keep the legacy path for hosts that - // omit it so capture-backend upgrades are not a client prerequisite. + // Without density, keep the legacy unzoomed scale of 1. Dividing by + // controller DPR would also trigger the legacy short-edge minimum, + // enlarging thin artwork that previously needed no resizing. final imageScale = isViewScaled() && zoomCursor.isTrue ? _cursorImageScale(widget.ffi, cursor, useLocalPointer: true) : s; @@ -1143,12 +1144,6 @@ class _ImagePaintState extends State { } else { if (zoomCursor.value || isViewOriginal()) { cursorScale = imageScale; - } else if (!isWeb) { - // NSCursor and GdkCursor treat the bitmap size as logical - // pixels, so an unzoomed cursor must be shrunk by the DPR to - // keep 1 remote px == 1 physical px, the size Original view - // already renders it at. - cursorScale = 1.0 / dpr; } } return cursorScale; diff --git a/flutter/test/cursor_density_validation_test.dart b/flutter/test/cursor_density_validation_test.dart index bc3c662ec..27e5edcf9 100644 --- a/flutter/test/cursor_density_validation_test.dart +++ b/flutter/test/cursor_density_validation_test.dart @@ -101,6 +101,7 @@ void main() { }); tearDown(() => binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, null)); + _legacySizingTests(registrations); for (final style in [kRemoteViewStyleAdaptive, kRemoteViewStyleCustom]) { for (final density in ['0', '1', '2', '1e-300', '0.001']) { testWidgets( @@ -111,6 +112,28 @@ void main() { } } +void _legacySizingTests(List> registrations) { + for (final style in [kRemoteViewStyleAdaptive, kRemoteViewStyleCustom]) { + for (final density in [null, '0']) { + for (final (width, height, dpr) in [ + (4, 32, 1.0), + (4, 32, 2.0), + (9, 18, 1.0), + (9, 18, 2.0), + (32, 32, 1.0), + (32, 32, 2.0), + ]) { + testWidgets( + 'legacy $style density=$density ${width}x$height DPR=$dpr', + (tester) => tester.runAsync(() => checkDensity( + tester, (style, density), registrations, + sourceSize: Size(width.toDouble(), height.toDouble()), + dpr: dpr))); + } + } + } +} + void _rasterBoundsTests() { for (final (width, height, scale, legacy, rasterScale) in [ (32, 32, 1e300, false, 1.0), @@ -146,13 +169,14 @@ void _rasterBoundsTests() { } } -Future checkDensity(WidgetTester tester, (String, String) input, - List> registrations) async { +Future checkDensity(WidgetTester tester, (String, String?) input, + List> registrations, + {Size sourceSize = const Size(32, 32), double dpr = 1}) async { final (style, density) = input; final id = '$style-$density'; - tester.view.devicePixelRatio = 1; + tester.view.devicePixelRatio = dpr; addTearDown(tester.view.resetDevicePixelRatio); - final canvas = _Canvas(1, style: style, scale: 0.5); + final canvas = _Canvas(dpr, style: style, scale: 0.5); final ffi = _FFI(canvas)..ffiModel.pi.platform = kPeerPlatformMacOS; final cursor = CursorModel(WeakReference(ffi))..id = id; addTearDown(() async { @@ -164,20 +188,35 @@ Future checkDensity(WidgetTester tester, (String, String) input, cursor.dispose(); canvas.dispose(); }); - await cursor.updateCursorData(_cursorEvent(id, density)); + await cursor.updateCursorData(_cursorEvent(id, density, size: sourceSize)); final rejected = density == '1e-300' || density == '0.001'; if (rejected) { expect(cursor.cache, isNull, reason: 'Reject density before publishing a cursor'); await cursor.updateCursorData(_cursorEvent(id, '2')); } - expect(cursor.cache!.pixelRatio, rejected ? 2 : double.parse(density)); + expect(cursor.cache!.pixelRatio, rejected ? 2 : double.parse(density ?? '0')); + await _paintCursor(tester, ffi, cursor); + expect(tester.takeException(), isNull); + for (final key in cursor.cachedKeys) { + await CursorManager.instance.ensureCursorRegistered(key); + } + expect(registrations, hasLength(1)); + await tester.pumpWidget(const SizedBox.shrink()); + if (density == null || density == '0') { + expect(cursor.cache!.scale, 1.0); + _expectLegacyRaster(registrations.single, sourceSize, dpr); + } +} + +Future _paintCursor( + WidgetTester tester, _FFI ffi, CursorModel cursor) async { await tester.pumpWidget(MediaQuery( - data: const MediaQueryData(devicePixelRatio: 1), + data: MediaQueryData(devicePixelRatio: ffi.canvasModel.devicePixelRatio), child: MultiProvider( providers: [ ChangeNotifierProvider(create: (_) => _Image()), - ChangeNotifierProvider.value(value: canvas), + ChangeNotifierProvider.value(value: ffi.canvasModel), ChangeNotifierProvider.value(value: cursor), ], child: ImagePaint( @@ -188,20 +227,41 @@ Future checkDensity(WidgetTester tester, (String, String) input, keyboardEnabled: true.obs, remoteCursorMoved: false.obs)), )); - expect(tester.takeException(), isNull); - for (final key in cursor.cachedKeys) { - await CursorManager.instance.ensureCursorRegistered(key); - } - expect(registrations, hasLength(1)); - await tester.pumpWidget(const SizedBox.shrink()); } -Map _cursorEvent(String id, String density) => { +void _expectLegacyRaster(Map args, Size size, double dpr) { + // Base unzoomed sizing is logical on macOS/GTK, physical on Windows. + final rasterScale = common.isWindows ? 1.0 : dpr; + final width = (size.width * rasterScale).toInt(); + final height = (size.height * rasterScale).toInt(); + final bufferWidth = common.isLinux ? size.longestSide * rasterScale : width; + expect((args['width'], args['height']), (bufferWidth, height)); + expect((args['hotX'], args['hotY']), + ((size.width ~/ 2) * rasterScale, (size.height ~/ 2) * rasterScale)); + expect(args['imagePixelRatio'], dpr); + final bytes = args['buffer'] as Uint8List; + final decoded = common.isWindows + ? img.Image.fromBytes( + width: width, + height: height, + bytes: bytes.buffer, + bytesOffset: bytes.offsetInBytes, + order: img.ChannelOrder.bgra) + : img.decodePng(bytes)!; + expect((decoded.width, decoded.height), (bufferWidth, height)); + expect(decoded.getPixel(width - 1, height - 1).a, 255); + if (bufferWidth > width) expect(decoded.getPixel(width, 0).a, 0); +} + +Map _cursorEvent(String id, String? density, + {Size size = const Size(32, 32)}) => + { 'id': id, - 'width': '32', - 'height': '32', - 'hotx': '8', - 'hoty': '12', - 'scale': density, - 'colors': jsonEncode(List.filled(32 * 32 * 4, 255)), + 'width': '${size.width.toInt()}', + 'height': '${size.height.toInt()}', + 'hotx': '${size.width ~/ 2}', + 'hoty': '${size.height ~/ 2}', + if (density != null) 'scale': density, + 'colors': jsonEncode( + List.filled((size.width * size.height * 4).toInt(), 255)), }; diff --git a/flutter/test/cursor_dpi_policy_test.dart b/flutter/test/cursor_dpi_policy_test.dart index 08673ba82..cc8d8272c 100644 --- a/flutter/test/cursor_dpi_policy_test.dart +++ b/flutter/test/cursor_dpi_policy_test.dart @@ -151,8 +151,8 @@ void main() { }); } for (final testCase in [ - (kRemoteViewStyleAdaptive, false, 0, 0.25, windows ? 1.0 : 4 / 3), - (kRemoteViewStyleCustom, false, 0, 0.25, windows ? 1.0 : 4 / 3), + (kRemoteViewStyleAdaptive, false, 0, 0.25, 1.0), + (kRemoteViewStyleCustom, false, 0, 0.25, 1.0), (kRemoteViewStyleAdaptive, false, 1, 0.25, windows ? 2.0 : 1.0), (kRemoteViewStyleAdaptive, false, 2, 0.25, windows ? 1.0 : 0.5), (kRemoteViewStyleCustom, false, 2, 0.25, windows ? 1.0 : 0.5), @@ -231,7 +231,8 @@ Future _checkPolicy( if (revoke) { final args = registrations.last; expect(args['name'], contains('_${kPreForbiddenCursorId}_')); - expect((args['width'], args['height']), (32, 32)); + final rasterSize = 32 * (Platform.isWindows ? 1 : dpr); + expect((args['width'], args['height']), (rasterSize, rasterSize)); expect((args['hotX'], args['hotY']), (0.0, 0.0)); } if (style == kRemoteViewStyleAdaptive && !zoom && density > 0) {