From 1007d29687f4ba0aa09d81223b28c758ed700be8 Mon Sep 17 00:00:00 2001 From: rustdesk Date: Wed, 23 Sep 2026 11:55:36 +0800 Subject: [PATCH] cursor: keep one native cursor per shape, and each tab its own A shape got a native cursor for every raster it was shown at, and all of them stayed until the session ended, so every zoom or DPR change added a native cursor for each shape in use. A shape now keeps the one at its current raster; the one it replaces is deleted the next time a cursor already registered is built, by when its successor has been activated. A raster returned to before that takes its cursor back instead of registering another under the same name. The native cursors of a window's engine are shared by its tabs, and the predefined cursors had one name in all of them, so a tab closing deleted the default and forbidden cursors the other tabs still showed. Names are now scoped to the tab's cursor model. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EZ49AbZJYfm8NTp5yDPMab --- flutter/lib/models/model.dart | 43 ++++++++++++++++++++- flutter/lib/native/custom_cursor.dart | 8 ++-- flutter/lib/web/custom_cursor.dart | 8 ++-- flutter/test/cursor_replay_test.dart | 54 +++++++++++++++++++++++++++ flutter/test/cursor_size_test.dart | 10 +++-- 5 files changed, 111 insertions(+), 12 deletions(-) diff --git a/flutter/lib/models/model.dart b/flutter/lib/models/model.dart index aab33e102..fae7a703a 100644 --- a/flutter/lib/models/model.dart +++ b/flutter/lib/models/model.dart @@ -3624,13 +3624,50 @@ class CursorModel with ChangeNotifier { final _restoring = {}; - /// Once its native cursor holds a raster, a shape keeps no pixels besides its ui.Image. - void registered(CursorData cache) { + // Tabs in a window share its engine's native cursors, predefined ones included. + static int _nextKeyScope = 0; + final int _keyScope = _nextKeyScope++; + // Each shape's one native cursor, and the ones replaced but maybe still on screen. + final _nativeKeys = {}; + final _replacedKeys = {}; + + String nativeKey(CursorData cache, double scale) => + '${_keyScope}_${cache.updateGetKey(scale)}'; + + /// Once its native cursor holds a raster, a shape keeps no pixels besides its ui.Image, and + /// no native cursor at another raster. + void registered(CursorData cache, String key) { + _useNativeKey(cache, key); if (identical(_cacheMap[cache.id], cache)) { cache.releasePixels(); } } + /// A raster returned to before its native cursor was deleted takes it back. + bool reviveNativeKey(CursorData cache, String key) { + if (!_replacedKeys.remove(key)) return false; + _cacheKeys.add(key); + _useNativeKey(cache, key); + return true; + } + + void _useNativeKey(CursorData cache, String key) { + final old = _nativeKeys[cache.id]; + _nativeKeys[cache.id] = key; + if (old != null && old != key && _cacheKeys.remove(old)) { + _replacedKeys.add(old); + } + } + + /// Called when a cursor already registered is built again: whatever replaced a cursor was + /// activated in an earlier frame, so the replaced ones are off screen. + void deleteReplacedKeys() { + for (final key in _replacedKeys) { + deleteCustomCursor(key); + } + _replacedKeys.clear(); + } + /// Reads a shape's pixels back from its ui.Image, for a raster its native cursors lack. void restorePixels(String id) { final shape = _images[id]; @@ -3760,6 +3797,8 @@ class CursorModel with ChangeNotifier { } _clearCache() { + deleteReplacedKeys(); + _nativeKeys.clear(); final keys = {...cachedKeys}; for (var k in keys) { debugPrint("deleting cursor with key $k"); diff --git a/flutter/lib/native/custom_cursor.dart b/flutter/lib/native/custom_cursor.dart index 06fd3523f..e15322b0c 100644 --- a/flutter/lib/native/custom_cursor.dart +++ b/flutter/lib/native/custom_cursor.dart @@ -18,8 +18,10 @@ MouseCursor buildCursorOfCache( if (cache == null) { return MouseCursor.defer; } else { - final key = cache.updateGetKey(scale); - if (!cursor.cachedKeys.contains(key)) { + final key = cursor.nativeKey(cache, scale); + if (cursor.cachedKeys.contains(key)) { + cursor.deleteReplacedKeys(); + } else if (!cursor.reviveNativeKey(cache, key)) { // data should be checked here, because it may be changed after `updateGetKey()` final data = cache.data; if (data == null) { @@ -49,7 +51,7 @@ MouseCursor buildCursorOfCache( ..hotX = cache.hotx ..hotY = cache.hoty); cursor.addKey(key); - cursor.registered(cache); + cursor.registered(cache, key); } return FlutterCustomMemoryImageCursor(key: key); } diff --git a/flutter/lib/web/custom_cursor.dart b/flutter/lib/web/custom_cursor.dart index 5c63b9c9e..ec05783cf 100644 --- a/flutter/lib/web/custom_cursor.dart +++ b/flutter/lib/web/custom_cursor.dart @@ -104,8 +104,10 @@ MouseCursor buildCursorOfCache( if (cache == null) { return MouseCursor.defer; } else { - final key = cache.updateGetKey(scale); - if (!cursor.cachedKeys.contains(key)) { + final key = cursor.nativeKey(cache, scale); + if (cursor.cachedKeys.contains(key)) { + cursor.deleteReplacedKeys(); + } else if (!cursor.reviveNativeKey(cache, key)) { // data should be checked here, because it may be changed after `updateGetKey()` final data = cache.data; if (data == null) { @@ -122,7 +124,7 @@ MouseCursor buildCursorOfCache( hotX: cache.hotx, hotY: cache.hoty)); cursor.addKey(key); - cursor.registered(cache); + cursor.registered(cache, key); } return FlutterCustomMemoryImageCursor(key: key); } diff --git a/flutter/test/cursor_replay_test.dart b/flutter/test/cursor_replay_test.dart index 22df20acb..b295334fe 100644 --- a/flutter/test/cursor_replay_test.dart +++ b/flutter/test/cursor_replay_test.dart @@ -46,9 +46,11 @@ void main() { ? SystemChannels.mouseCursor : const MethodChannel('flutter_custom_cursor'); final registered = []; + final deleted = []; late _FFI ffi; setUp(() { registered.clear(); + deleted.clear(); binding.defaultBinaryMessenger.setMockMethodCallHandler(channel, (call) async { final args = call.arguments as Map; @@ -56,6 +58,9 @@ void main() { registered.add(args['name'] as String); return args['name']; } + if (call.method.startsWith('deleteCustomCursor')) { + deleted.add(args['name'] as String); + } return null; }); ffi = _FFI(); @@ -155,6 +160,55 @@ void main() { reason: 'the replay goes in first-seen order and ends on this'); }); + test('a shape keeps one native cursor, the one at its raster', () async { + final cursor = ffi.cursorModel; + await _feed(ffi, '1', size: 32); + buildCursorOfCache(cursor, 1.0, cursor.cache); + await _settle(); + final first = registered.single; + + // Asks for its pixels back, registers the new raster, then is shown again. + for (var i = 0; i < 3; i++) { + buildCursorOfCache(cursor, 0.5, cursor.cache); + await _settle(); + } + expect(registered.length, 2); + expect(deleted, [first], + reason: 'the replaced one goes once its successor has been shown'); + expect(cursor.cachedKeys, {registered.last}); + }); + + test('a raster returned to before its cursor was dropped is kept', () async { + final cursor = ffi.cursorModel; + await _feed(ffi, '1', size: 32); + for (final scale in [1.0, 0.5, 0.5, 1.0, 1.0]) { + buildCursorOfCache(cursor, scale, cursor.cache); + await _settle(); + } + final shown = cursor.nativeKey(cursor.cache!, 1.0); + expect(buildCursorOfCache(cursor, 1.0, cursor.cache), + isNot(MouseCursor.defer)); + await _settle(); + expect(deleted, isNot(contains(shown)), reason: 'it is the one on screen'); + expect(cursor.cachedKeys, {shown}); + }); + + test('a tab closing does not delete the cursors of another tab', () async { + final other = _FFI(); + addTearDown(other.cursorModel.disposeImages); + while (preDefaultCursor.cache == null) { + await _settle(); + } + buildCursorOfCache(ffi.cursorModel, 1.0, preDefaultCursor.cache); + buildCursorOfCache(other.cursorModel, 1.0, preDefaultCursor.cache); + await _settle(); + expect(registered.toSet().length, 2, reason: 'each tab has its own'); + + ffi.cursorModel.clear(); + await _settle(); + expect(other.cursorModel.cachedKeys.single, isNot(isIn(deleted))); + }); + test('a shape is decoded from its pixels as they arrive', () async { await _feed(ffi, '5'); expect(ffi.cursorModel.cache?.id, '5'); diff --git a/flutter/test/cursor_size_test.dart b/flutter/test/cursor_size_test.dart index d881c9a24..5182047f0 100644 --- a/flutter/test/cursor_size_test.dart +++ b/flutter/test/cursor_size_test.dart @@ -257,10 +257,12 @@ Future _checkRasterTransitions( ]) { buildCursorOfCache(cursor, scale, cursor.cache); await Future.delayed(Duration.zero); - final key = cursor.cache.updateGetKey(scale); + final key = cursor.nativeKey(cursor.cache, scale); _expectSize( - registrations.singleWhere((args) => args['name'] == key), expected); + registrations.lastWhere((args) => args['name'] == key), expected); + expect(cursor.cachedKeys, {key}, reason: 'one native cursor per shape'); } + // A raster returned to before its cursor was deleted takes that cursor back. expect(registrations.length, 4); } @@ -359,8 +361,8 @@ Future _checkView(WidgetTester tester, (String, bool) mode, mode.$1 != kRemoteViewStyleOriginal ? (sourceSize * (Platform.isWindows ? dpr : 1.0)).ceil() : (size * scale * (Platform.isWindows ? dpr : 1.0)).ceil(); - final key = cursor.cache.updateGetKey(cursor.cache.scale); - _expectSize(registrations.singleWhere((v) => v['name'] == key), (w, w)); + final key = cursor.nativeKey(cursor.cache, cursor.cache.scale); + _expectSize(registrations.lastWhere((v) => v['name'] == key), (w, w)); } await tester.pumpWidget(const SizedBox.shrink()); }