cursor: never fall back to the default cursor on a switch

A switch to a shape without its image, or without a native cursor at
its raster, showed the default arrow until the core gave the shape back
and it decoded: the painted cursor on every switch back, the native one
after an eviction or while a new shape decoded.

- The shape shown before stays, image, hotspot and native cursor, until
  the one in use is ready.
- Where the cursor is painted, on mobile or with the remote cursor
  shown, the images of the 64 shapes used last are kept, like the native
  cursors, so an animation does not decode a frame per turn. Elsewhere
  only the shape in use keeps one.
- A shape that finishes decoding after the peer moved on is kept with
  them instead of dropped, so a fast animation fills them at all.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab
This commit is contained in:
rustdesk
2026-09-23 23:02:53 +08:00
co-authored by Claude Opus 5.5
parent 86800194cb
commit d677bbebf6
5 changed files with 182 additions and 32 deletions
+54 -23
View File
@@ -3175,9 +3175,10 @@ class CursorModel with ChangeNotifier {
get lastIsBlocked => _lastIsBlocked;
/// Asks the core for the shape in use when it has to be painted and was let go.
/// The image of the shape in use, or the one shown before until it is back: a switch never
/// falls back to the default cursor. Asks the core for the shape in use when it was let go.
ui.Image? get image {
if (_image == null && _cache != null) {
if (_images[_id] == null && _cacheMap.containsKey(_id)) {
restorePixels(_id);
}
return _image;
@@ -3512,7 +3513,7 @@ class CursorModel with ChangeNotifier {
_images.clear();
}
/// Whether the shape decoded; it is kept only while it is the one in use.
/// Whether the shape decoded; its image is kept with the ones used last, see [_imageLimit].
Future<bool> updateCursorData(String id, int hotxInt, int hotyInt, int width,
int height, Uint8List rgba) async {
final generation = _generation;
@@ -3530,8 +3531,12 @@ class CursorModel with ChangeNotifier {
image.dispose();
return false;
}
_images[id]?.item1.dispose();
final old = _images.remove(id);
_images[id] = Tuple3(image, hotx, hoty);
if (old != null) {
if (identical(old.item1, _image)) _image = image;
old.item1.dispose();
}
// Update last cursor data.
// Do not use the previous `image` and `id`, because `_id` may be changed.
@@ -3641,15 +3646,42 @@ class CursorModel with ChangeNotifier {
_evictNativeKeys();
}
/// Native cursors of the peer's shapes kept, the ones used last; the core rebuilds the
/// others. An animated cursor is a shape per frame, 18 for the Windows busy cursor and 23
/// for KDE's, and a cycle longer than this limit would rebuild every frame.
static const kMaxNativeCursors = 64;
/// The peer's shapes used last that keep a native cursor, and a painted image; the core
/// rebuilds the others. An animated cursor is a shape per frame, 18 for the Windows busy
/// cursor and 23 for KDE's, and a cycle longer than this limit would rebuild every frame.
static const kRecentShapes = 64;
/// The native cursor shown last, shown on while the shape in use is made.
String? shownKey;
/// Whether the desktop paints the peer's cursor over the remote image.
@protected
bool get showsRemoteCursor {
final tag = ShowRemoteCursorState.tag(parent.target?.id ?? '');
return Get.isRegistered<RxBool>(tag: tag) && Get.find<RxBool>(tag: tag).value;
}
// Painted images kept: mobile always paints the cursor, a desktop only when it shows the
// remote cursor, and otherwise the native cursors show the shapes.
int get _imageLimit => isMobile || showsRemoteCursor ? kRecentShapes : 1;
// `_images` is in order of use. The image painted is kept, whoever's it is.
void _evictImages() {
var excess = _images.length - _imageLimit;
_images.removeWhere((id, shape) {
if (excess <= 0 || id == _id || identical(shape.item1, _image)) {
return false;
}
excess--;
shape.item1.dispose();
return true;
});
}
// `_nativeKeys` is in order of use. The predefined cursors are not the peer's and stay.
void _evictNativeKeys() {
var excess = _nativeKeys.keys.where(_cacheMap.containsKey).length -
kMaxNativeCursors;
kRecentShapes;
_nativeKeys.removeWhere((id, key) {
if (excess <= 0 || id == _id || !_cacheMap.containsKey(id)) return false;
excess--;
@@ -3691,16 +3723,16 @@ class CursorModel with ChangeNotifier {
() async {
try {
final shape = await fetchCursorShape(id);
if (generation != _generation || id != _id) {
if (generation != _generation) {
return;
} else if (shape == null) {
_unavailable = id;
if (id == _id) _unavailable = id;
debugPrint('Cursor $id is not kept by the core');
} else {
// Decoded even if the peer moved on: an animation comes back to it.
final decoded = await updateCursorData(id, shape.hotx, shape.hoty,
shape.width, shape.height, shape.colors);
// A shape switched away from while it decoded is let go, not lost.
if (!decoded && generation == _generation) {
if (!decoded && generation == _generation && id == _id) {
// It did not decode; painting must not ask for it on every frame.
_unavailable = id;
}
@@ -3716,7 +3748,9 @@ class CursorModel with ChangeNotifier {
bool _updateCurData() {
final previous = _cache;
_cache = _cacheMap[_id];
final cache = _cacheMap[_id];
// A shape not decoded yet leaves the one shown before in place until it is.
if (cache != null) _cache = cache;
if (previous != null && !identical(previous, _cache)) {
previous.releasePixels();
}
@@ -3725,25 +3759,21 @@ class CursorModel with ChangeNotifier {
_nativeKeys[_id] = key;
}
_evictNativeKeys();
final tmp = _images[_id];
_image = tmp?.item1;
final tmp = _images.remove(_id);
if (tmp != null) {
_images[_id] = tmp;
_image = tmp.item1;
_hotx = tmp.item2;
_hoty = tmp.item3;
}
// Only the shape in use keeps an image; the core has the others.
_images.removeWhere((id, shape) {
if (id == _id) return false;
shape.item1.dispose();
return true;
});
_evictImages();
try {
// may throw exception, because the listener maybe already dispose
notifyListeners();
} catch (e) {
debugPrint('WARNING: updateCursorId $_id, without notifyListeners(). $e');
}
return tmp != null || _cache != null;
return tmp != null || cache != null;
}
updateCursorId(Map<String, dynamic> evt) {
@@ -3814,6 +3844,7 @@ class CursorModel with ChangeNotifier {
deleteCustomCursor(k);
}
_cacheKeys.clear();
shownKey = null;
resetSystemCursor();
}
+11 -2
View File
@@ -16,7 +16,7 @@ resetSystemCursor() {}
MouseCursor buildCursorOfCache(
CursorModel cursor, double scale, CursorData? cache) {
if (cache == null) {
return MouseCursor.defer;
return _shownCursor(cursor);
} else {
final key = cursor.nativeKey(cache, scale);
if (cursor.cachedKeys.contains(key)) {
@@ -26,7 +26,7 @@ MouseCursor buildCursorOfCache(
final data = cache.data;
if (data == null) {
cursor.restorePixels(cache.id);
return MouseCursor.defer;
return _shownCursor(cursor);
}
cursor.deleteReplacedKeys();
// Square canvases avoid clipping or stray edge pixels on Linux.
@@ -54,10 +54,19 @@ MouseCursor buildCursorOfCache(
cursor.addKey(key);
cursor.registered(cache, key);
}
cursor.shownKey = key;
return FlutterCustomMemoryImageCursor(key: key);
}
}
// The cursor shown last stays while the one in use is made, rather than the system one.
MouseCursor _shownCursor(CursorModel cursor) {
final key = cursor.shownKey;
return key == null
? MouseCursor.defer
: FlutterCustomMemoryImageCursor(key: key);
}
Uint8List _padCursor(Uint8List data, int size) {
final bitmap = img.decodePng(data);
if (bitmap == null) {
+11 -2
View File
@@ -102,7 +102,7 @@ resetSystemCursor() => CursorManager.instance.resetSystemCursor();
MouseCursor buildCursorOfCache(
model.CursorModel cursor, double scale, model.CursorData? cache) {
if (cache == null) {
return MouseCursor.defer;
return _shownCursor(cursor);
} else {
final key = cursor.nativeKey(cache, scale);
if (cursor.cachedKeys.contains(key)) {
@@ -112,7 +112,7 @@ MouseCursor buildCursorOfCache(
final data = cache.data;
if (data == null) {
cursor.restorePixels(cache.id);
return MouseCursor.defer;
return _shownCursor(cursor);
}
cursor.deleteReplacedKeys();
debugPrint(
@@ -127,6 +127,15 @@ MouseCursor buildCursorOfCache(
cursor.addKey(key);
cursor.registered(cache, key);
}
cursor.shownKey = key;
return FlutterCustomMemoryImageCursor(key: key);
}
}
// The cursor shown last stays while the one in use is made, rather than the system one.
MouseCursor _shownCursor(model.CursorModel cursor) {
final key = cursor.shownKey;
return key == null
? MouseCursor.defer
: FlutterCustomMemoryImageCursor(key: key);
}
+104 -5
View File
@@ -1,6 +1,7 @@
import 'dart:io';
import 'package:flutter/services.dart';
import 'package:flutter_custom_cursor/flutter_custom_cursor.dart';
import 'package:flutter_hbb/common.dart';
import 'package:flutter_hbb/generated_bridge.dart' show CursorShape;
import 'package:flutter_hbb/models/model.dart';
@@ -18,8 +19,15 @@ class _Cursor extends CursorModel {
fetched.add(id);
return core[id];
}
bool showRemoteCursor = false;
@override
bool get showsRemoteCursor => showRemoteCursor;
}
String? _key(MouseCursor cursor) =>
cursor is FlutterCustomMemoryImageCursor ? cursor.key : null;
class _FFI extends Fake implements FFI {
_FFI() {
ffiModel = FfiModel(WeakReference(this));
@@ -129,7 +137,7 @@ void main() {
test('the shapes used last keep their native cursors, the others go',
() async {
const max = CursorModel.kMaxNativeCursors;
const max = CursorModel.kRecentShapes;
final cursor = ffi.cursorModel;
Future<void> show() async {
buildCursorOfCache(cursor, 1.0, cursor.cache);
@@ -155,15 +163,19 @@ void main() {
_select(ffi, '2');
await show();
expect(ffi.cursor.fetched, isEmpty, reason: 'shape 2 kept its cursor');
final shown = cursor.nativeKey(cursor.cache!, 1.0);
_select(ffi, '1');
expect(buildCursorOfCache(cursor, 1.0, cursor.cache), MouseCursor.defer);
expect(_key(buildCursorOfCache(cursor, 1.0, cursor.cache)), shown,
reason: 'the cursor shown stays while shape 1 is made again');
await _settle();
expect(ffi.cursor.fetched, ['1'], reason: 'rebuilt from the core');
expect(_key(buildCursorOfCache(cursor, 1.0, cursor.cache)),
cursor.nativeKey(cursor.cache!, 1.0));
});
test('shapes each shown once leave no more native cursors than the limit',
() async {
const max = CursorModel.kMaxNativeCursors;
const max = CursorModel.kRecentShapes;
final cursor = ffi.cursorModel;
for (var i = 0; i < max + 4; i++) {
await _feed(ffi, '$i');
@@ -207,11 +219,12 @@ void main() {
expect(deleted, isEmpty);
});
test('a shape painted again is decoded again from the core', () async {
test('a shape painted again is decoded again, the last one shown meanwhile',
() async {
await _feed(ffi, '1', size: 16);
await _feed(ffi, '2');
_select(ffi, '1');
expect(ffi.cursorModel.image, isNull);
expect(ffi.cursorModel.image?.width, 8, reason: 'shape 2 until 1 is back');
await _settle();
expect(ffi.cursor.fetched, ['1']);
expect(ffi.cursorModel.image?.width, 16);
@@ -390,4 +403,90 @@ void main() {
final p = ffi.cursorModel.cache!.image.getPixel(0, 0);
expect([p.r, p.g, p.b, p.a], [3, 4, 5, 6]);
});
test('a shape still decoding leaves the one shown before in place', () async {
final cursor = ffi.cursorModel;
await _feed(ffi, '1');
final shown = _key(buildCursorOfCache(cursor, 1.0, cursor.cache));
await _settle();
_select(ffi, '2'); // its shape has not decoded yet
expect(cursor.cache?.id, '1');
expect(_key(buildCursorOfCache(cursor, 1.0, cursor.cache)), shown);
expect(cursor.image, isNotNull);
await _feed(ffi, '2');
expect(cursor.cache?.id, '2');
});
group('a painted cursor', () {
void painted(String how) {
if (how == 'mobile') {
final was = isMobile;
isMobile = true;
addTearDown(() => isMobile = was);
} else {
ffi.cursor.showRemoteCursor = true;
}
}
for (final how in ['mobile', 'remote cursor shown']) {
test('keeps the images of two animations and the everyday set ($how)',
() async {
painted(how);
final cursor = ffi.cursorModel;
final shapes = [
for (var i = 0; i < 12; i++) 's$i',
for (var i = 0; i < 23; i++) 'wait$i',
for (var i = 0; i < 23; i++) 'progress$i',
];
for (var i = 0; i < shapes.length; i++) {
await _feed(ffi, shapes[i], seed: i);
}
for (final id in shapes) {
_select(ffi, id);
expect(cursor.shapeIds.last, id, reason: 'its image is at hand');
}
await _settle();
expect(ffi.cursor.fetched, isEmpty);
});
}
test('keeps the images used last, and decodes the others again', () async {
painted('mobile');
const max = CursorModel.kRecentShapes;
final cursor = ffi.cursorModel;
for (var i = 0; i <= max; i++) {
await _feed(ffi, '$i');
}
expect(cursor.shapeIds.length, max);
_select(ffi, '$max');
_select(ffi, '1');
await _settle();
expect(ffi.cursor.fetched, isEmpty, reason: 'shape 1 was used lately');
_select(ffi, '0');
expect(cursor.image, isNotNull, reason: 'shape 1 until 0 is back');
await _settle();
expect(ffi.cursor.fetched, ['0']);
expect(cursor.shapeIds.length, max);
});
test('keeps a shape that finished decoding after the peer moved on',
() async {
painted('mobile');
const max = CursorModel.kRecentShapes;
final cursor = ffi.cursorModel;
for (var i = 0; i <= max; i++) {
await _feed(ffi, '$i');
}
_select(ffi, '0');
cursor.image; // let go, so it is asked for
await Future<void>.delayed(Duration.zero);
_select(ffi, '1');
await _settle();
_select(ffi, '0');
cursor.image;
await _settle();
expect(ffi.cursor.fetched, ['0'], reason: 'the late one was kept');
expect(cursor.shapeIds.last, '0');
});
});
}
+2
View File
@@ -78,6 +78,8 @@ class _FFI extends Fake implements FFI {
final ffiModel = _Peer();
@override
final inputModel = _Input();
@override
var id = '';
}
class _Cursor extends CursorModel {