forked from firka/flutter
[web] Set cursor property on body (flutter/engine#49098)
This is a temporary workaround to get mouse cursor working in multi-view (until https://github.com/flutter/flutter/issues/140226 is resolved).
This commit is contained in:
@@ -4,6 +4,8 @@
|
||||
|
||||
import '../dom.dart';
|
||||
|
||||
const String _kDefaultCursor = 'default';
|
||||
|
||||
/// Controls the mouse cursor in the given [element].
|
||||
class MouseCursor {
|
||||
MouseCursor(this.element);
|
||||
@@ -17,7 +19,7 @@ class MouseCursor {
|
||||
static const Map<String, String> _kindToCssValueMap = <String, String>{
|
||||
'alias': 'alias',
|
||||
'allScroll': 'all-scroll',
|
||||
'basic': 'default',
|
||||
'basic': _kDefaultCursor,
|
||||
'cell': 'cell',
|
||||
'click': 'pointer',
|
||||
'contextMenu': 'context-menu',
|
||||
@@ -53,10 +55,18 @@ class MouseCursor {
|
||||
};
|
||||
|
||||
static String _mapKindToCssValue(String? kind) {
|
||||
return _kindToCssValueMap[kind] ?? 'default';
|
||||
return _kindToCssValueMap[kind] ?? _kDefaultCursor;
|
||||
}
|
||||
|
||||
void activateSystemCursor(String? kind) {
|
||||
element.style.cursor = _mapKindToCssValue(kind);
|
||||
final String cssValue = _mapKindToCssValue(kind);
|
||||
// TODO(mdebbar): This should be set on the element, not the body. In order
|
||||
// to do that, we need the framework to send us the view ID.
|
||||
// https://github.com/flutter/flutter/issues/140226
|
||||
if (cssValue == _kDefaultCursor) {
|
||||
domDocument.body!.style.removeProperty('cursor');
|
||||
} else {
|
||||
domDocument.body!.style.cursor = cssValue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -600,9 +600,10 @@ class EnginePlatformDispatcher extends ui.PlatformDispatcher {
|
||||
decoded.arguments as Map<dynamic, dynamic>;
|
||||
switch (decoded.method) {
|
||||
case 'activateSystemCursor':
|
||||
// TODO(mdebbar): This needs a view ID from the framework.
|
||||
// https://github.com/flutter/flutter/issues/137289
|
||||
implicitView?.mouseCursor
|
||||
// TODO(mdebbar): Once the framework starts sending us a viewId, we
|
||||
// should use it to grab the correct view.
|
||||
// https://github.com/flutter/flutter/issues/140226
|
||||
views.firstOrNull?.mouseCursor
|
||||
.activateSystemCursor(arguments.tryString('kind'));
|
||||
}
|
||||
return;
|
||||
|
||||
@@ -16,25 +16,38 @@ void testMain() {
|
||||
final DomElement rootViewElement = createDomElement('div');
|
||||
final MouseCursor mouseCursor = MouseCursor(rootViewElement);
|
||||
|
||||
// TODO(mdebbar): This should be `rootViewElement`.
|
||||
// https://github.com/flutter/flutter/issues/140226
|
||||
final DomElement cursorTarget = domDocument.body!;
|
||||
|
||||
mouseCursor.activateSystemCursor('alias');
|
||||
expect(rootViewElement.style.cursor, 'alias');
|
||||
expect(cursorTarget.style.cursor, 'alias');
|
||||
|
||||
mouseCursor.activateSystemCursor('move');
|
||||
expect(rootViewElement.style.cursor, 'move');
|
||||
expect(cursorTarget.style.cursor, 'move');
|
||||
|
||||
mouseCursor.activateSystemCursor('precise');
|
||||
expect(rootViewElement.style.cursor, 'crosshair');
|
||||
expect(cursorTarget.style.cursor, 'crosshair');
|
||||
|
||||
mouseCursor.activateSystemCursor('resizeDownRight');
|
||||
expect(rootViewElement.style.cursor, 'se-resize');
|
||||
expect(cursorTarget.style.cursor, 'se-resize');
|
||||
|
||||
mouseCursor.activateSystemCursor('basic');
|
||||
expect(cursorTarget.style.cursor, isEmpty);
|
||||
});
|
||||
|
||||
test('handles unknown cursor type', () {
|
||||
final DomElement rootViewElement = createDomElement('div');
|
||||
final MouseCursor mouseCursor = MouseCursor(rootViewElement);
|
||||
|
||||
// TODO(mdebbar): This should be `rootViewElement`.
|
||||
// https://github.com/flutter/flutter/issues/140226
|
||||
final DomElement cursorTarget = domDocument.body!;
|
||||
|
||||
mouseCursor.activateSystemCursor('unknown');
|
||||
expect(rootViewElement.style.cursor, 'default');
|
||||
expect(cursorTarget.style.cursor, isEmpty);
|
||||
|
||||
mouseCursor.activateSystemCursor(null);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user