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] Fix pointer binding #14378

Merged
merged 31 commits into from
Dec 18, 2019
Merged

Conversation

dkwingsmt
Copy link
Contributor

@dkwingsmt dkwingsmt commented Dec 12, 2019

Overview

This PR refactors pointer_binding, fixes a few issues, and adds a number of tests for all three adapters.

Details

Rewrite adapters

A number of issues are fixed and tested in this PR, namely:

  • Right clicks that pop up the context menu will cause crashes.
  • Button changes during a click sequence will cause crashes.
  • Buttons other than primary or secondary are not recognized.
  • TouchAdapter does not work with multiple pointers.
  • Neither TouchAdapter nor PointerAdapter generates remove event for touch end, leading to memory leak.
  • Cancel events or remove events do not work, due to converters being too strict.
  • If a wheel event are seen as the first event, PointerAdapter will not properly work.

Some of the issues emerged after #14082, while some have existed since the first write.

This PR introduces the following non-trivial changes:

  • Expansion of coalesced events is only done during pointermove, as it's only available during this event according to specs.
  • The buttons bitfield is limited to the right-most 30 bits (_kButtonsMask).

This PR introduces the following minor improvements:

  • Reduces the duplicate code between PointerAdapter and MouseAdapter.
  • Removes unnecessary public symbols.
  • Fixes an error where domRenderer is read during its constructor.

Improve tests

This PR also creates a test environment to test the 3 adapters, and added a number of tests. With the help of "test contexts", we can test all applicable adapters without duplicate code.

@auto-assign auto-assign bot requested a review from stuartmorgan December 12, 2019 01:50
@dkwingsmt dkwingsmt requested review from yjbanov and mdebbar December 12, 2019 02:01
@dkwingsmt dkwingsmt added the platform-web Code specifically for the web engine label Dec 12, 2019

void handleDown({@required _PointerStateDataCallback dataCallback, @required int buttons}) {
// TODO(flutter_web): Remove this temporary fix for right click
// on web platform once context guesture is implemented.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: s/guesture/gesture

Copy link
Contributor Author

@dkwingsmt dkwingsmt Dec 12, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed. Also I didn't add this TODO, but only moved it (since there was a corresponding test), and I have no idea why it was there.

lib/web_ui/lib/src/engine/pointer_binding.dart Outdated Show resolved Hide resolved
lib/web_ui/lib/src/engine/pointer_binding.dart Outdated Show resolved Hide resolved
Comment on lines 288 to 289
_pressedButtons = 0;
dataCallback(change: ui.PointerChange.up, buttons: _pressedButtons);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same comment as above: did you mean to reverse the order of these two statements?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we want to reverse these two statements? We indeed want to dispatch an up event with "buttons: 0". Events should contain the current state of buttons of this pointer.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I'm misunderstading something here. I thought we tell Flutter which button are being released with this pointerup event. Is that not true?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flutter's PointerEvent class doesn't have a "button" property, but only a "buttons" property. https://github.com/flutter/flutter/blob/master/packages/flutter/lib/src/gestures/events.dart#L211
The "button" property (as only seen in HTML PointerEvent class) tells which button is changed, while the "buttons" property (as defined in HTML and Flutter) is a bitfield of which buttons are pressed after the event.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I meant buttons (and used "are" after it) but I misspelled it 😛

That's good to know, TIL. Is it documented anywhere that buttons specifies the buttons that are pressed after the event?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not really, and I'll add it when I got the opportunity.

) : super(callback, domRenderer, _pointerDataConverter);
) : super(callback, glassPaneElement, _pointerDataConverter);

final Map<int, _PointerStateManager> _pointerStates = <int, _PointerStateManager>{};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concern: this map will keep growing indefinitely since on touch devices, the browser gives us a new pointer id on every interaction.

return buttons & _kButtonsMask;
}

void handleDown({@required _PointerStateDataCallback dataCallback, @required int buttons}) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wish Dart had support for tuples. We would have avoided the dataCallback and just returned a Tuple<ui.PointerChange, int>.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can always create a class for that, but I think it's not as flexible as callbacks, since some handlers are stateful, and returning the list would be a little more complicated.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a second thought, I'll make it a list to address the issue mentioned in #14378 (comment)

lib/web_ui/lib/src/engine/pointer_binding.dart Outdated Show resolved Hide resolved
lib/web_ui/lib/src/engine/pointer_binding.dart Outdated Show resolved Hide resolved
final int len = touches.length;
for (int i = 0; i < len; i++) {
final html.Touch touch = touches[i];
for (html.Touch touch in event.changedTouches) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason we avoided for in here was to avoid the creation of unnecessary iterators.

Copy link
Contributor Author

@dkwingsmt dkwingsmt Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you worrying about performance? I think it's a premature optimization unless it's backed by a benchmark. In my opinion, loops in this file is of the same impact as loops in app files, so if app files do not care about the overhead of for in, so shouldn't we.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree it's premature optimization and it's not backed by a benchmark. This specific code path though, could be a gazillion times when the user is moving their fingers. And we know for sure that Dart generates sub-optimal JS code in some cases for for in. That's why we avoided it here.

You can keep the for in if you want. We are gonna have to tackle performance at some point anyway. Then, we would more concrete data to optimize against.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll leave it to you. Thanks for explaining.

engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
engine-flutter-autoroll added a commit to engine-flutter-autoroll/flutter that referenced this pull request Dec 19, 2019
chingjun pushed a commit to flutter/flutter that referenced this pull request Dec 19, 2019
* 988b8f1 Fix FontLoader does not remove the cache in web engine (flutter/engine#14536)

* 0aacac7 Roll src/third_party/skia 21df075cab00..e6a2ad81ab40 (1 commits) (flutter/engine#14552)

* 417dd7e Roll fuchsia/sdk/core/mac-amd64 from esDH2... to NHgyx... (flutter/engine#14554)

* f2e841d [Web] Fix pointer binding  (flutter/engine#14378)

* 105eb66 Roll src/third_party/skia e6a2ad81ab40..8fec4140f614 (17 commits) (flutter/engine#14557)

* 1ecfdcb [web] Calculate align offset for each paragraph line (LineMetrics.left) (flutter/engine#14537)

* dda3619 Roll src/third_party/dart 270966b16044..171059d27689 (19 commits) (flutter/engine#14558)

* 9c1bd8a Fixes Objective-C objects memory leaks (flutter/engine#14326)

* f2dbeb8 Reland Wire up Opacity on Fuchsia (flutter/engine#14559)

* 2f536e5 Roll fuchsia/sdk/core/linux-amd64 from jsuQq... to VdBKA... (flutter/engine#14560)

* 4312d37 Revert "[fuchsia] Add diagnostics directory to the set of remote dirs (#14470)" (flutter/engine#14566)

* 5c77ea1 Roll src/third_party/skia 8fec4140f614..9e0afb791ac2 (4 commits) (flutter/engine#14563)

* a09ff7c Roll src/third_party/dart 171059d27689..aa6709974dea (11 commits) (flutter/engine#14567)

* 0f90e65 Revert "[web] Calculate align offset for each paragraph line (LineMetrics.left) (#14537)" (flutter/engine#14569)
filmil pushed a commit to filmil/engine that referenced this pull request Mar 13, 2020
Refactors pointer_binding, fixes a few issues related to the pointer data converter in Web, and adds a number of tests for all three adapters.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes platform-web Code specifically for the web engine
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants