Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[web] Fixes drag scrolling in embedded mode. #53647

Merged
merged 3 commits into from
Jun 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/web_ui/lib/src/engine/dom.dart
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,9 @@ extension DomEventExtension on DomEvent {
external JSString get _type;
String get type => _type.toDart;

external JSBoolean? get _cancelable;
bool get cancelable => _cancelable?.toDart ?? true;

external JSVoid preventDefault();
external JSVoid stopPropagation();

Expand Down Expand Up @@ -720,6 +723,8 @@ extension DomElementExtension on DomElement {
removeChild(firstChild!);
}
}

external void setPointerCapture(num? pointerId);
}

@JS()
Expand Down
36 changes: 26 additions & 10 deletions lib/web_ui/lib/src/engine/pointer_binding.dart
Original file line number Diff line number Diff line change
Expand Up @@ -432,8 +432,10 @@ class PointerSupportDetector {
String toString() => 'pointers:$hasPointerEvents';
}

class _Listener {
_Listener._({
/// Encapsulates a DomEvent registration so it can be easily unregistered later.
@visibleForTesting
class Listener {
Listener._({
required this.event,
required this.target,
required this.handler,
Expand All @@ -448,7 +450,7 @@ class _Listener {
/// associated with this event. If `passive` is false, the browser will wait
/// for the handler to finish execution before performing the respective
/// action.
factory _Listener.register({
factory Listener.register({
required String event,
required DomEventTarget target,
required DartDomEventListener handler,
Expand All @@ -465,12 +467,12 @@ class _Listener {
target.addEventListenerWithOptions(event, jsHandler, eventOptions);
}

final _Listener listener = _Listener._(
final Listener listener = Listener._(
event: event,
target: target,
handler: jsHandler,
);
target.addEventListener(event, jsHandler);

return listener;
}

Expand All @@ -496,12 +498,12 @@ abstract class _BaseAdapter {
PointerDataConverter get _pointerDataConverter => _owner._pointerDataConverter;
KeyboardConverter? get _keyboardConverter => _owner._keyboardConverter;

final List<_Listener> _listeners = <_Listener>[];
final List<Listener> _listeners = <Listener>[];
DomWheelEvent? _lastWheelEvent;
bool _lastWheelEventWasTrackpad = false;
bool _lastWheelEventAllowedDefault = false;

DomEventTarget get _viewTarget => _view.dom.rootElement;
DomElement get _viewTarget => _view.dom.rootElement;
DomEventTarget get _globalTarget => _view.embeddingStrategy.globalEventTarget;

/// Each subclass is expected to override this method to attach its own event
Expand All @@ -510,7 +512,7 @@ abstract class _BaseAdapter {

/// Cleans up all event listeners attached by this adapter.
void dispose() {
for (final _Listener listener in _listeners) {
for (final Listener listener in _listeners) {
listener.unregister();
}
_listeners.clear();
Expand Down Expand Up @@ -546,7 +548,7 @@ abstract class _BaseAdapter {
handler(event);
}
}
_listeners.add(_Listener.register(
_listeners.add(Listener.register(
event: eventName,
target: target,
handler: loggedHandler,
Expand Down Expand Up @@ -719,7 +721,7 @@ mixin _WheelEventListenerMixin on _BaseAdapter {
}

void _addWheelEventListener(DartDomEventListener handler) {
_listeners.add(_Listener.register(
_listeners.add(Listener.register(
event: 'wheel',
target: _viewTarget,
handler: handler,
Expand Down Expand Up @@ -966,6 +968,20 @@ class _PointerAdapter extends _BaseAdapter with _WheelEventListenerMixin {

@override
void setup() {
// Prevents the browser auto-canceling pointer events.
// Register into `_listener` directly so the event isn't forwarded to semantics.
_listeners.add(Listener.register(
event: 'touchstart',
target: _viewTarget,
handler: (DomEvent event) {
// This is one of the ways I've seen this done. PixiJS does a similar thing.
// ThreeJS seems to subscribe move/leave in the pointerdown handler instead?
if (event.cancelable) {
event.preventDefault();
}
},
));

_addPointerEventListener(_viewTarget, 'pointerdown', (DomPointerEvent event) {
final int device = _getPointerId(event);
final List<ui.PointerData> pointerData = <ui.PointerData>[];
Expand Down
66 changes: 66 additions & 0 deletions lib/web_ui/test/engine/pointer_binding_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,18 @@ void testMain() {
},
);

test('prevents default on touchstart events', () async {
final event = createDomEvent('Event', 'touchstart');

rootElement.dispatchEvent(event);

expect(
event.defaultPrevented,
isTrue,
reason: 'touchstart events should be prevented so pointer events are not cancelled later.',
);
});

test(
'can receive pointer events on the app root',
() {
Expand Down Expand Up @@ -2670,6 +2682,60 @@ void testMain() {
);
});

group('Listener', () {
late DomElement eventTarget;
late DomEvent expected;
late bool handled;

setUp(() {
eventTarget = createDomElement('div');
expected = createDomEvent('Event', 'custom-event');
handled = false;
});

test('listeners can be registered', () {
Listener.register(
event: 'custom-event',
target: eventTarget,
handler: (event) {
expect(event, expected);
handled = true;
},
);

// Trigger the event...
eventTarget.dispatchEvent(expected);
expect(handled, isTrue);
});

test('listeners can be unregistered', () {
final Listener listener = Listener.register(
event: 'custom-event',
target: eventTarget,
handler: (event) {
handled = true;
},
);
listener.unregister();

eventTarget.dispatchEvent(expected);
expect(handled, isFalse);
});

test('listeners are registered only once', () {
int timesHandled = 0;
Listener.register(
event: 'custom-event',
target: eventTarget,
handler: (event) {
timesHandled++;
},
);
eventTarget.dispatchEvent(expected);
expect(timesHandled, 1, reason: 'The handler ran multiple times for a single event.');
});
});

group('ClickDebouncer', () {
_testClickDebouncer(getBinding: () => instance);
});
Expand Down
Loading