2024-03-29 10:52:32 +08:00
|
|
|
import 'package:flutter_custom_cursor/cursor_manager.dart'
|
|
|
|
|
as custom_cursor_manager;
|
|
|
|
|
import 'package:flutter_custom_cursor/flutter_custom_cursor.dart';
|
|
|
|
|
|
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
2026-09-18 12:43:07 +08:00
|
|
|
import 'package:flutter_hbb/common.dart' show isLinux;
|
2024-03-29 10:52:32 +08:00
|
|
|
import 'package:flutter_hbb/models/model.dart';
|
2026-09-18 12:43:07 +08:00
|
|
|
import 'package:image/image.dart' as img;
|
2024-03-29 10:52:32 +08:00
|
|
|
|
|
|
|
|
deleteCustomCursor(String key) =>
|
|
|
|
|
custom_cursor_manager.CursorManager.instance.deleteCursor(key);
|
2024-08-27 23:58:04 +08:00
|
|
|
resetSystemCursor() {}
|
2024-03-29 10:52:32 +08:00
|
|
|
|
|
|
|
|
MouseCursor buildCursorOfCache(
|
|
|
|
|
CursorModel cursor, double scale, CursorData? cache) {
|
|
|
|
|
if (cache == null) {
|
2026-09-23 23:02:53 +08:00
|
|
|
return _shownCursor(cursor);
|
2024-03-29 10:52:32 +08:00
|
|
|
} else {
|
2026-09-23 11:55:36 +08:00
|
|
|
final key = cursor.nativeKey(cache, scale);
|
2026-09-24 02:46:53 +08:00
|
|
|
if (!cursor.cachedKeys.contains(key)) {
|
2024-03-29 10:52:32 +08:00
|
|
|
// data should be checked here, because it may be changed after `updateGetKey()`
|
|
|
|
|
final data = cache.data;
|
|
|
|
|
if (data == null) {
|
2026-09-23 11:43:54 +08:00
|
|
|
cursor.restorePixels(cache.id);
|
2026-09-23 23:02:53 +08:00
|
|
|
return _shownCursor(cursor);
|
2024-03-29 10:52:32 +08:00
|
|
|
}
|
2026-09-18 12:43:07 +08:00
|
|
|
// Square canvases avoid clipping or stray edge pixels on Linux.
|
|
|
|
|
final width = isLinux && cache.rasterWidth < cache.rasterHeight
|
|
|
|
|
? cache.rasterHeight
|
|
|
|
|
: cache.rasterWidth;
|
|
|
|
|
final height = isLinux ? width : cache.rasterHeight;
|
2024-03-29 10:52:32 +08:00
|
|
|
debugPrint(
|
|
|
|
|
"Register custom cursor with key $key (${cache.hotx},${cache.hoty})");
|
|
|
|
|
// [Safety]
|
|
|
|
|
// It's ok to call async registerCursor in current synchronous context,
|
|
|
|
|
// because activating the cursor is also an async call and will always
|
|
|
|
|
// be executed after this.
|
|
|
|
|
custom_cursor_manager.CursorManager.instance
|
|
|
|
|
.registerCursor(custom_cursor_manager.CursorData()
|
|
|
|
|
..name = key
|
2026-09-18 12:43:07 +08:00
|
|
|
..buffer =
|
|
|
|
|
width == cache.rasterWidth && height == cache.rasterHeight
|
|
|
|
|
? data
|
|
|
|
|
: _padCursor(data, width)
|
|
|
|
|
..width = width
|
|
|
|
|
..height = height
|
2024-03-29 10:52:32 +08:00
|
|
|
..hotX = cache.hotx
|
|
|
|
|
..hotY = cache.hoty);
|
|
|
|
|
cursor.addKey(key);
|
2026-09-23 11:55:36 +08:00
|
|
|
cursor.registered(cache, key);
|
2024-03-29 10:52:32 +08:00
|
|
|
}
|
2026-09-25 10:11:54 +08:00
|
|
|
cursor.shown(cache, key);
|
2024-03-29 10:52:32 +08:00
|
|
|
return FlutterCustomMemoryImageCursor(key: key);
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-09-18 12:43:07 +08:00
|
|
|
|
2026-09-25 10:11:54 +08:00
|
|
|
// The last shape's cursor stays while the one in use is made, rather than the system one.
|
2026-09-23 23:02:53 +08:00
|
|
|
MouseCursor _shownCursor(CursorModel cursor) {
|
|
|
|
|
final key = cursor.shownKey;
|
|
|
|
|
return key == null
|
|
|
|
|
? MouseCursor.defer
|
|
|
|
|
: FlutterCustomMemoryImageCursor(key: key);
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-18 12:43:07 +08:00
|
|
|
Uint8List _padCursor(Uint8List data, int size) {
|
|
|
|
|
final bitmap = img.decodePng(data);
|
|
|
|
|
if (bitmap == null) {
|
|
|
|
|
throw const FormatException('Invalid native cursor PNG');
|
|
|
|
|
}
|
|
|
|
|
final padded = img.copyExpandCanvas(bitmap,
|
|
|
|
|
newWidth: size,
|
|
|
|
|
newHeight: size,
|
|
|
|
|
position: img.ExpandCanvasPosition.topLeft,
|
|
|
|
|
toImage: img.Image(width: size, height: size, numChannels: 4));
|
|
|
|
|
return Uint8List.fromList(img.encodePng(padded));
|
|
|
|
|
}
|