custom_cursor.dart 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import 'package:flutter_custom_cursor/cursor_manager.dart'
  2. as custom_cursor_manager;
  3. import 'package:flutter_custom_cursor/flutter_custom_cursor.dart';
  4. import 'package:flutter/foundation.dart';
  5. import 'package:flutter/services.dart';
  6. import 'package:flutter_hbb/models/model.dart';
  7. deleteCustomCursor(String key) =>
  8. custom_cursor_manager.CursorManager.instance.deleteCursor(key);
  9. resetSystemCursor() {}
  10. MouseCursor buildCursorOfCache(
  11. CursorModel cursor, double scale, CursorData? cache) {
  12. if (cache == null) {
  13. return MouseCursor.defer;
  14. } else {
  15. final key = cache.updateGetKey(scale);
  16. if (!cursor.cachedKeys.contains(key)) {
  17. // data should be checked here, because it may be changed after `updateGetKey()`
  18. final data = cache.data;
  19. if (data == null) {
  20. return MouseCursor.defer;
  21. }
  22. debugPrint(
  23. "Register custom cursor with key $key (${cache.hotx},${cache.hoty})");
  24. // [Safety]
  25. // It's ok to call async registerCursor in current synchronous context,
  26. // because activating the cursor is also an async call and will always
  27. // be executed after this.
  28. custom_cursor_manager.CursorManager.instance
  29. .registerCursor(custom_cursor_manager.CursorData()
  30. ..name = key
  31. ..buffer = data
  32. ..width = (cache.width * cache.scale).toInt()
  33. ..height = (cache.height * cache.scale).toInt()
  34. ..hotX = cache.hotx
  35. ..hotY = cache.hoty);
  36. cursor.addKey(key);
  37. }
  38. return FlutterCustomMemoryImageCursor(key: key);
  39. }
  40. }