From 1bf9700da661b6f0a0c4575e306eeac9ae2f82ca Mon Sep 17 00:00:00 2001 From: fufesou Date: Wed, 7 Sep 2022 02:14:52 -0700 Subject: [PATCH] flutter_desktop: show cursor Signed-off-by: fufesou --- flutter/lib/desktop/pages/remote_page.dart | 20 +++++---- flutter/lib/models/model.dart | 48 +++++++++++++--------- 2 files changed, 41 insertions(+), 27 deletions(-) diff --git a/flutter/lib/desktop/pages/remote_page.dart b/flutter/lib/desktop/pages/remote_page.dart index 3a34b44ef..90c729abe 100644 --- a/flutter/lib/desktop/pages/remote_page.dart +++ b/flutter/lib/desktop/pages/remote_page.dart @@ -1,5 +1,6 @@ import 'dart:async'; import 'dart:io'; +import 'dart:typed_data'; import 'dart:ui' as ui; import 'package:flutter/gestures.dart'; @@ -407,7 +408,7 @@ class _RemotePageState extends State ]; paints.add(Obx(() => Visibility( - visible: _keyboardEnabled.isTrue || _showRemoteCursor.isTrue, + visible: _showRemoteCursor.isTrue && _remoteCursorMoved.isTrue, child: CursorPaint( id: widget.id, )))); @@ -493,7 +494,7 @@ class ImagePaint extends StatelessWidget { painter: ImagePainter(image: m.image, x: 0, y: 0, scale: s), )); - Rx pos = Rx(Offset(0.0, 0.0)); + Rx pos = Rx(const Offset(0.0, 0.0)); return Center( child: NotificationListener( onNotification: (notification) { @@ -512,12 +513,15 @@ class ImagePaint extends StatelessWidget { cursor: (cursorOverImage.isTrue && keyboardEnabled.isTrue) ? (remoteCursorMoved.isTrue ? SystemMouseCursors.none - : FlutterCustomMemoryImageCursor( - pixbuf: cursor.rgba!, - hotx: cursor.hotx, - hoty: cursor.hoty, - imageWidth: (cursor.image!.width * s).toInt(), - imageHeight: (cursor.image!.height * s).toInt())) + : (cursor.pngData != null + ? FlutterCustomMemoryImageCursor( + pixbuf: cursor.pngData!, + hotx: cursor.hotx, + hoty: cursor.hoty, + imageWidth: (cursor.image!.width * s).toInt(), + imageHeight: (cursor.image!.height * s).toInt(), + ) + : MouseCursor.defer)) : MouseCursor.defer, onHover: (evt) { pos.value = evt.position; diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index 51c0b4225..9f9899553 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -4,6 +4,7 @@ import 'dart:io'; import 'dart:math'; import 'dart:typed_data'; import 'dart:ui' as ui; +import 'dart:ui'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -595,8 +596,9 @@ class CanvasModel with ChangeNotifier { class CursorModel with ChangeNotifier { ui.Image? _image; - Uint8List? _rgba; - final _images = >{}; + final _images = >{}; + Uint8List? _pngData; + final _pngs = {}; double _x = -10000; double _y = -10000; double _hotx = 0; @@ -607,7 +609,7 @@ class CursorModel with ChangeNotifier { WeakReference parent; ui.Image? get image => _image; - Uint8List? get rgba => _rgba; + Uint8List? get pngData => _pngData; double get x => _x - _displayOriginX; @@ -757,26 +759,34 @@ class CursorModel with ChangeNotifier { var pid = parent.target?.id; ui.decodeImageFromPixels(rgba, width, height, ui.PixelFormat.rgba8888, (image) { - if (parent.target?.id != pid) return; - _image = image; - _rgba = rgba; - _images[id] = Tuple4(rgba, image, _hotx, _hoty); - try { - // my throw exception, because the listener maybe already dispose - notifyListeners(); - } catch (e) { - debugPrint('notify cursor: $e'); - } + () async { + if (parent.target?.id != pid) return; + _image = image; + _images[id] = Tuple3(image, _hotx, _hoty); + final data = await image.toByteData(format: ImageByteFormat.png); + if (data != null) { + _pngData = data.buffer.asUint8List(); + } else { + _pngData = null; + } + _pngs[id] = _pngData; + try { + // my throw exception, because the listener maybe already dispose + notifyListeners(); + } catch (e) { + debugPrint('notify cursor: $e'); + } + }(); }); } void updateCursorId(Map evt) { + _pngData = _pngs[int.parse(evt['id'])]; final tmp = _images[int.parse(evt['id'])]; if (tmp != null) { - _rgba = tmp.item1; - _image = tmp.item2; - _hotx = tmp.item3; - _hoty = tmp.item4; + _image = tmp.item1; + _hotx = tmp.item2; + _hoty = tmp.item3; notifyListeners(); } } @@ -786,7 +796,7 @@ class CursorModel with ChangeNotifier { _x = double.parse(evt['x']); _y = double.parse(evt['y']); try { - RemoteCursorMovedState.find(id).value = false; + RemoteCursorMovedState.find(id).value = true; } catch (e) { // } @@ -1011,7 +1021,7 @@ class FFI { Peer.fromJson(s[0] as String, s[1] as Map)) .toList(); } catch (e) { - print('peers(): $e'); + debugPrint('peers(): $e'); } return []; }