Merge pull request #1472 from fufesou/flutter_cursors_cache

flutter_destkop: fix cursor cache scale
This commit is contained in:
RustDesk 2022-09-08 11:08:26 +08:00 committed by GitHub
commit e393c6aafe
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 21 deletions

View File

@ -539,7 +539,7 @@ class ImagePaint extends StatelessWidget {
Widget build(BuildContext context) { Widget build(BuildContext context) {
final m = Provider.of<ImageModel>(context); final m = Provider.of<ImageModel>(context);
var c = Provider.of<CanvasModel>(context); var c = Provider.of<CanvasModel>(context);
final cursor = Provider.of<CursorModel>(context);
final s = c.scale; final s = c.scale;
if (c.scrollStyle == ScrollStyle.scrollbar) { if (c.scrollStyle == ScrollStyle.scrollbar) {
final imageWidget = SizedBox( final imageWidget = SizedBox(
@ -568,18 +568,7 @@ class ImagePaint extends StatelessWidget {
cursor: (cursorOverImage.isTrue && keyboardEnabled.isTrue) cursor: (cursorOverImage.isTrue && keyboardEnabled.isTrue)
? (remoteCursorMoved.isTrue ? (remoteCursorMoved.isTrue
? SystemMouseCursors.none ? SystemMouseCursors.none
: (cursor.cacheLinux != null : _buildCustomCursorLinux(context, s))
? FlutterCustomMemoryImageCursor(
pixbuf: cursor.cacheLinux!.data,
key: cursor.cacheLinux!.key,
hotx: cursor.cacheLinux!.hotx,
hoty: cursor.cacheLinux!.hoty,
imageWidth:
(cursor.cacheLinux!.width * s).toInt(),
imageHeight:
(cursor.cacheLinux!.height * s).toInt(),
)
: MouseCursor.defer))
: MouseCursor.defer, : MouseCursor.defer,
onHover: (evt) { onHover: (evt) {
pos.value = evt.position; pos.value = evt.position;
@ -599,6 +588,25 @@ class ImagePaint extends StatelessWidget {
} }
} }
MouseCursor _buildCustomCursorLinux(BuildContext context, double scale) {
final cursor = Provider.of<CursorModel>(context);
final cacheLinux = cursor.cacheLinux;
if (cacheLinux == null) {
return MouseCursor.defer;
} else {
final key = cacheLinux.key(scale);
cursor.addKeyLinux(key);
return FlutterCustomMemoryImageCursor(
pixbuf: cacheLinux.data,
key: key,
hotx: cacheLinux.hotx,
hoty: cacheLinux.hoty,
imageWidth: (cacheLinux.width * scale).toInt(),
imageHeight: (cacheLinux.height * scale).toInt(),
);
}
}
Widget _buildCrossScrollbar(Widget child) { Widget _buildCrossScrollbar(Widget child) {
final physicsVertical = final physicsVertical =
cursorOverImage.value ? const NeverScrollableScrollPhysics() : null; cursorOverImage.value ? const NeverScrollableScrollPhysics() : null;

View File

@ -604,7 +604,6 @@ class CursorData {
final double hoty; final double hoty;
final int width; final int width;
final int height; final int height;
late String key;
CursorData({ CursorData({
required this.peerId, required this.peerId,
@ -614,10 +613,12 @@ class CursorData {
required this.hoty, required this.hoty,
required this.width, required this.width,
required this.height, required this.height,
}) { });
key =
'${peerId}_${id}_${(hotx * 10e6).round().toInt()}_${(hoty * 10e6).round().toInt()}_${width}_$height'; int _doubleToInt(double v) => (v * 10e6).round().toInt();
}
String key(double scale) =>
'${peerId}_${id}_${_doubleToInt(hotx)}_${_doubleToInt(hoty)}_${_doubleToInt(width * scale)}_${_doubleToInt(height * scale)}';
} }
class CursorModel with ChangeNotifier { class CursorModel with ChangeNotifier {
@ -625,6 +626,7 @@ class CursorModel with ChangeNotifier {
final _images = <int, Tuple3<ui.Image, double, double>>{}; final _images = <int, Tuple3<ui.Image, double, double>>{};
CursorData? _cacheLinux; CursorData? _cacheLinux;
final _cacheMapLinux = <int, CursorData>{}; final _cacheMapLinux = <int, CursorData>{};
final _cacheKeysLinux = <String>{};
double _x = -10000; double _x = -10000;
double _y = -10000; double _y = -10000;
double _hotx = 0; double _hotx = 0;
@ -649,8 +651,8 @@ class CursorModel with ChangeNotifier {
CursorModel(this.parent); CursorModel(this.parent);
List<String> get cachedKeysLinux => Set<String> get cachedKeysLinux => _cacheKeysLinux;
_cacheMapLinux.values.map((v) => v.key).toList(); addKeyLinux(String key) => _cacheKeysLinux.add(key);
// remote physical display coordinate // remote physical display coordinate
Rect getVisibleRect() { Rect getVisibleRect() {
@ -878,7 +880,7 @@ class CursorModel with ChangeNotifier {
} }
void _clearCacheLinux() { void _clearCacheLinux() {
final cachedKeys = [...cachedKeysLinux]; final cachedKeys = {...cachedKeysLinux};
for (var key in cachedKeys) { for (var key in cachedKeys) {
customCursorController.freeCache(key); customCursorController.freeCache(key);
} }