fix(flutter): scale the remote cursor with its image

This commit is contained in:
fufesou
2026-09-11 13:51:24 +08:00
parent 72ea38ca4f
commit d202a2fba4
3 changed files with 70 additions and 31 deletions

View File

@@ -1413,26 +1413,25 @@ class CursorPaint extends StatelessWidget {
} }
} }
double x = m.x * c.scale + cx - hotx; final image = m.image ?? preDefaultCursor.image;
double y = m.y * c.scale + cy - hoty; final nativePixels = isWindows ? MediaQuery.devicePixelRatioOf(context) : 1.0;
double scale = 1.0; double scale = c.scale;
final isViewOriginal = c.viewStyle.style == kRemoteViewStyleOriginal; if (image != null && scale * nativePixels != 1.0) {
if (zoomCursor.value || isViewOriginal) { final sx = kMinCursorSize / (image.width * nativePixels);
x = m.x - hotx + cx / c.scale; final sy = kMinCursorSize / (image.height * nativePixels);
y = m.y - hoty + cy / c.scale; final minimumScale = sx > sy ? sx : sy;
scale = c.scale; if (scale < minimumScale) scale = minimumScale;
} 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 x = (m.x * c.scale + cx) / scale - hotx;
final y = (m.y * c.scale + cy) / scale - hoty;
return CustomPaint( return CustomPaint(
painter: ImagePainter( painter: ImagePainter(
image: m.image ?? preDefaultCursor.image, image: image,
x: x, x: x,
y: y, y: y,
scale: scale, scale: scale,
useIntegerPosition: false,
), ),
); );
} }

View File

@@ -96,12 +96,14 @@ class ImagePainter extends CustomPainter {
required this.x, required this.x,
required this.y, required this.y,
required this.scale, required this.scale,
this.useIntegerPosition = true,
}); });
ui.Image? image; ui.Image? image;
double x; double x;
double y; double y;
double scale; double scale;
final bool useIntegerPosition;
@override @override
void paint(Canvas canvas, Size size) { void paint(Canvas canvas, Size size) {
@@ -122,8 +124,10 @@ class ImagePainter extends CustomPainter {
if (isWeb) { if (isWeb) {
paint.filterQuality = FilterQuality.high; paint.filterQuality = FilterQuality.high;
} }
canvas.drawImage( final position = useIntegerPosition
image!, Offset(x.toInt().toDouble(), y.toInt().toDouble()), paint); ? Offset(x.toInt().toDouble(), y.toInt().toDouble())
: Offset(x, y);
canvas.drawImage(image!, position, paint);
} }
@override @override

View File

@@ -11,9 +11,8 @@ import 'package:get/get.dart';
import 'package:provider/provider.dart'; import 'package:provider/provider.dart';
const _hotspot = Offset(4, 9); const _hotspot = Offset(4, 9);
const _remotePosition = Offset(100, 80); const _remotePosition = Offset(100.25, 80.75);
const _canvasOffset = Offset(15, 10); const _canvasOffset = Offset(15.125, 10.25);
const _canvasScale = 0.5;
const _viewport = Size(200, 160); const _viewport = Size(200, 160);
class _CursorModel extends ChangeNotifier implements CursorModel { class _CursorModel extends ChangeNotifier implements CursorModel {
@@ -35,7 +34,7 @@ class _CursorModel extends ChangeNotifier implements CursorModel {
} }
class _CanvasModel extends ChangeNotifier implements CanvasModel { class _CanvasModel extends ChangeNotifier implements CanvasModel {
_CanvasModel(String style) _CanvasModel(String style, this.scale)
: viewStyle = ViewStyle( : viewStyle = ViewStyle(
style: style, style: style,
width: _viewport.width, width: _viewport.width,
@@ -51,7 +50,7 @@ class _CanvasModel extends ChangeNotifier implements CanvasModel {
@override @override
double get y => _canvasOffset.dy; double get y => _canvasOffset.dy;
@override @override
double get scale => _canvasScale; final double scale;
@override @override
ScrollStyle get scrollStyle => ScrollStyle.scrollauto; ScrollStyle get scrollStyle => ScrollStyle.scrollauto;
@@ -59,17 +58,48 @@ class _CanvasModel extends ChangeNotifier implements CanvasModel {
dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 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() { void main() {
for (final (style, zoom, dpr, scale) in [ for (final (style, zoom, dpr, source, canvasScale, scale) in [
(kRemoteViewStyleAdaptive, false, 2.0, 0.5), (kRemoteViewStyleAdaptive, false, 2.0, (48, 64), 0.375, 0.375),
(kRemoteViewStyleAdaptive, false, 3.0, 1 / 3), (kRemoteViewStyleAdaptive, false, 3.0, (48, 64), 0.25, 0.25),
(kRemoteViewStyleAdaptive, true, 3.0, _canvasScale), (kRemoteViewStyleAdaptive, true, 3.0, (48, 64), 0.375, 0.375),
(kRemoteViewStyleOriginal, false, 2.0, _canvasScale), (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 { (tester) async {
final image = final image = (await tester.runAsync(
(await tester.runAsync(() => createTestImage(width: 9, height: 18)))!; () => createTestImage(width: source.$1, height: source.$2)))!;
addTearDown(image.dispose); addTearDown(image.dispose);
await tester.pumpWidget(MediaQuery( await tester.pumpWidget(MediaQuery(
data: MediaQueryData(devicePixelRatio: dpr), data: MediaQueryData(devicePixelRatio: dpr),
@@ -78,7 +108,7 @@ void main() {
ChangeNotifierProvider<CursorModel>( ChangeNotifierProvider<CursorModel>(
create: (_) => _CursorModel(image)), create: (_) => _CursorModel(image)),
ChangeNotifierProvider<CanvasModel>( ChangeNotifierProvider<CanvasModel>(
create: (_) => _CanvasModel(style)), create: (_) => _CanvasModel(style, canvasScale)),
], ],
child: CursorPaint(id: 'cursor-test', zoomCursor: zoom.obs), child: CursorPaint(id: 'cursor-test', zoomCursor: zoom.obs),
), ),
@@ -90,7 +120,13 @@ void main() {
expect(painter.image, same(image)); expect(painter.image, same(image));
expect(painter.scale, scale); expect(painter.scale, scale);
expect((Offset(painter.x, painter.y) + _hotspot) * scale, expect((Offset(painter.x, painter.y) + _hotspot) * scale,
_remotePosition * _canvasScale + _canvasOffset); _remotePosition * canvasScale + _canvasOffset);
}, skip: Platform.isWindows && style == kRemoteViewStyleAdaptive && !zoom); 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));
});
} }
} }