Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

Commit f3a38f0

Browse files
authored
System mouse cursor: Web (#17718)
Adds system mouse cursor to the web engine.
1 parent 4be3a03 commit f3a38f0

File tree

4 files changed

+58
-0
lines changed

4 files changed

+58
-0
lines changed

ci/licenses_golden/licenses_flutter

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ FILE: ../../../flutter/lib/web_ui/lib/src/engine/history.dart
443443
FILE: ../../../flutter/lib/web_ui/lib/src/engine/houdini_canvas.dart
444444
FILE: ../../../flutter/lib/web_ui/lib/src/engine/html_image_codec.dart
445445
FILE: ../../../flutter/lib/web_ui/lib/src/engine/keyboard.dart
446+
FILE: ../../../flutter/lib/web_ui/lib/src/engine/mouse_cursor.dart
446447
FILE: ../../../flutter/lib/web_ui/lib/src/engine/onscreen_logging.dart
447448
FILE: ../../../flutter/lib/web_ui/lib/src/engine/path_to_svg.dart
448449
FILE: ../../../flutter/lib/web_ui/lib/src/engine/picture.dart

lib/web_ui/lib/src/engine.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ part 'engine/history.dart';
6161
part 'engine/houdini_canvas.dart';
6262
part 'engine/html_image_codec.dart';
6363
part 'engine/keyboard.dart';
64+
part 'engine/mouse_cursor.dart';
6465
part 'engine/onscreen_logging.dart';
6566
part 'engine/path_to_svg.dart';
6667
part 'engine/picture.dart';
@@ -216,6 +217,7 @@ void webOnlyInitializeEngine() {
216217
};
217218

218219
Keyboard.initialize();
220+
MouseCursor.initialize();
219221
}
220222

221223
class _NullTreeSanitizer implements html.NodeTreeSanitizer {
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// @dart = 2.6
6+
part of engine;
7+
8+
/// Provides mouse cursor bindings, such as the `flutter/mousecursor` channel.
9+
class MouseCursor {
10+
/// Initializes the [MouseCursor] singleton.
11+
///
12+
/// Use the [instance] getter to get the singleton after calling this method.
13+
static void initialize() {
14+
_instance ??= MouseCursor._();
15+
}
16+
17+
/// The [MouseCursor] singleton.
18+
static MouseCursor get instance => _instance;
19+
static MouseCursor _instance;
20+
21+
MouseCursor._() {}
22+
23+
// The kind values must be kept in sync with flutter's
24+
// rendering/mouse_cursor.dart
25+
static const Map<String, String> _kindToCssValueMap = <String, String>{
26+
'none': 'none',
27+
'basic': 'default',
28+
'click': 'pointer',
29+
'text': 'text',
30+
'forbidden': 'not-allowed',
31+
'grab': 'grab',
32+
'grabbing': 'grabbing',
33+
};
34+
static String _mapKindToCssValue(String kind) {
35+
return _kindToCssValueMap[kind] ?? 'default';
36+
}
37+
38+
void activateSystemCursor(String kind) {
39+
domRenderer.setElementStyle(
40+
domRenderer.glassPaneElement,
41+
'cursor',
42+
_mapKindToCssValue(kind),
43+
);
44+
}
45+
}

lib/web_ui/lib/src/engine/window.dart

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,16 @@ class EngineWindow extends ui.Window {
536536
textEditing.channel.handleTextInput(data, callback);
537537
return;
538538

539+
case 'flutter/mousecursor':
540+
const MethodCodec codec = StandardMethodCodec();
541+
final MethodCall decoded = codec.decodeMethodCall(data);
542+
final Map<dynamic, dynamic> arguments = decoded.arguments;
543+
switch (decoded.method) {
544+
case 'activateSystemCursor':
545+
MouseCursor.instance.activateSystemCursor(arguments['kind']);
546+
}
547+
return;
548+
539549
case 'flutter/web_test_e2e':
540550
const MethodCodec codec = JSONMethodCodec();
541551
_replyToPlatformMessage(

0 commit comments

Comments
 (0)