mirror of
https://github.com/rustdesk/rustdesk.git
synced 2026-09-11 23:11:01 +03:00
fix(flutter): scale the remote cursor with its image
This commit is contained in:
@@ -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,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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<CursorModel>(
|
||||
create: (_) => _CursorModel(image)),
|
||||
ChangeNotifierProvider<CanvasModel>(
|
||||
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));
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user