Skip to content

Conversation

@Renzo-Olivares
Copy link
Contributor

@Renzo-Olivares Renzo-Olivares commented Jan 24, 2023

This PR adds the following triple tap/click gestures to select text in a TextField to match the native platform behavior.

  • Triple tap/click to select a paragraph in multi-line text fields. (Windows/Android/Fuchsia/iOS/macOS)
  • Triple tap/click to select a line in multi-line text fields. (Linux)
  • Triple tap/click to select all text in single-line text fields. (All)
  • Triple click + drag to select text in paragraph blocks. (Windows/Android/Fuchsia/iOS/macOS)
  • Triple click + drag to select text in line blocks. (Linux)

Fixes #63576

  • I read the [Contributor Guide] and followed the process outlined there for submitting PRs.
  • I read the [Tree Hygiene] wiki page, which explains my responsibilities.
  • I read and followed the [Flutter Style Guide], including [Features we expect every widget to implement].
  • I signed the [CLA].
  • I listed at least one issue that this PR fixes in the description above.
  • I updated/added relevant documentation (doc comments with ///).
  • I added new tests to check the change I am making, or this PR is [test-exempt].
  • All existing and new tests are passing.

@flutter-dashboard flutter-dashboard bot added a: text input Entering text in a text field or keyboard related problems f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels. labels Jan 24, 2023
@Renzo-Olivares Renzo-Olivares force-pushed the triple-tap-select branch 3 times, most recently from 1b4d81e to f4615e1 Compare January 25, 2023 00:52
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps remove the doc comment reference to [int] as it's a generic type? Can follow the style of TextBoundary (or even CharacterBoundary which only has class level doc comments...).

Copy link
Contributor

Choose a reason for hiding this comment

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

I guess these magic numbers could be avoided with constants (also needed in isLineTerminator).

Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above on constants.

Copy link
Contributor

Choose a reason for hiding this comment

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

Looking at TextPainter, the list of what's considered a newline is different: no 0xD on its own, plus 0x2028/0x2029.

  static bool _isNewline(int codePoint) {
    switch (codePoint) {
      case 0x000A:
      case 0x0085:
      case 0x000B:
      case 0x000C:
      case 0x2028:
      case 0x2029:
        return true;
      default:
        return false;
    }
  }

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Renzo-Olivares
Copy link
Contributor Author

@tgucio sorry this wasn't ready for review. This depends on another PR so some files are not related to this one. Appreciate the feedback nonetheless :)

@tgucio
Copy link
Contributor

tgucio commented Jan 25, 2023

@Renzo-Olivares this is a very welcome PR so I thought I'd share review comments early but it looks like I missed the "Draft" status :).

@Renzo-Olivares Renzo-Olivares force-pushed the triple-tap-select branch 7 times, most recently from e17f147 to 962fb81 Compare February 1, 2023 09:20
@Renzo-Olivares Renzo-Olivares force-pushed the triple-tap-select branch 3 times, most recently from 0a2a3f6 to 123f26a Compare February 4, 2023 00:26
@Renzo-Olivares Renzo-Olivares marked this pull request as ready for review February 4, 2023 00:26
@Renzo-Olivares
Copy link
Contributor Author

Renzo-Olivares commented Feb 4, 2023

This is ready for an initial review with a caveat: I'm still addressing an edge case where the CupertinoTextSelectionControls selection handles blocks the TextSelectionGestureDetector below it. This will happen when the tap begins at a word edge, on double tap the selection handles will pop up and block the gesture detector below it, so the triple tap is never registered.

@Renzo-Olivares Renzo-Olivares changed the title Add TextField triple tap gestures Add TextField triple tap/click gestures Feb 4, 2023
@Renzo-Olivares Renzo-Olivares requested a review from tgucio February 4, 2023 01:08
Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM with nits 👍

The main thing is just a TODO that I commented on, not sure if you plan to do that in this PR or a new one.

It's really great that this PR is so straightforward with the addition of your paragraph boundary work and refactoring. Before that it was such a mess trying to support triple tap!

Copy link
Contributor

Choose a reason for hiding this comment

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

Why do iOS and Mac fall through to the same behavior as Windows if the comments above seem to describe their behavior differently?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

To me it looked like iOS/macOS/Windows increased their tap count infinitely but they handled taps past 3 differently.

iOS and macOS don't handle anything past a third tap. The selection is retained at the paragraph that was selected on the triple tap.

Windows alternates between double click and triple click when going past the third tap. Alternating between selecting the word or paragraph at the position.

Let me know if I can clarify this further.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's ok as-is. I looked through the code to see how consecutiveTapCount is used and it makes sense that these all return null but later have different behavior.

Copy link
Contributor Author

@Renzo-Olivares Renzo-Olivares Feb 10, 2023

Choose a reason for hiding this comment

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

I think it makes more sense now after introducing an effectiveConsecutiveTapCount.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I guess some tests that previously tapped very soon after a double tap might behave differently now. We should watch out for this breaking Google tests or anything else. Though it looks like Google tests are currently passing 👍

Copy link
Contributor Author

@Renzo-Olivares Renzo-Olivares Feb 6, 2023

Choose a reason for hiding this comment

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

Yeah, how it worked previously is that the double tap timer on TextSelectionGestureDetector would reset the count to 0 after every double tap for every platform. So some test behavior changed a bit, but google tests look good so far.

Copy link
Contributor

Choose a reason for hiding this comment

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

Is your plan to do this in another PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm hoping that I can fix it in this one. But if the fix becomes overly complex then I'll probably make it into another PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Just wanted to draw attention to this again in case you forgot about it.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bummer that we have to duplicate all of these tests for Cupertino and Material, but I guess they won't work on EditableText because of the code changes in text_selection.dart.

Someday we need to make it easier to share these kinds of tests!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed, it would definitely help with velocity if we only had to write these tests once.

Copy link
Contributor

Choose a reason for hiding this comment

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

It's nice that this test still works (with minor changes) and makes a lot of sense with the addition of triple tap.

@Renzo-Olivares Renzo-Olivares force-pushed the triple-tap-select branch 2 times, most recently from bc68b75 to 004fbf0 Compare February 7, 2023 00:31
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: maybe it's easier to write the if clauses, if the local variable is for "effective tap count".

Copy link
Contributor

Choose a reason for hiding this comment

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

or maybe move the logic to the static method you created later in this file that turns the raw tap count to the effective tap count.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry i'm not sure I follow, can you elaborate? The static method I created later in the file is meant to tell the gesture recognizer what its ceiling is for the consecutive tap count before it resets it to 0.

Copy link
Contributor

Choose a reason for hiding this comment

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

is it possible to repurpose _getDefaultMaxConsecutiveTap so it takes the raw consecutive tap count and returns the "effective" tap count? For instance for windows the conversion would look like input < 2 ? input : 2 + input % 2 and for other platforms it's max(input, 3).

Copy link
Contributor Author

@Renzo-Olivares Renzo-Olivares Feb 9, 2023

Choose a reason for hiding this comment

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

In this scenario if we are repurposing _getDefaultMaxConsecutiveTap are we then setting the ceiling for consecutive tap count on the recognizer to null for all platforms?

In that case I think we can do this.

  static int _getEffectiveConsecutiveTapCount(int rawCount) {
    switch(defaultTargetPlatform) {
      case TargetPlatform.android:
      case TargetPlatform.fuchsia:
      case TargetPlatform.linux:
        return rawCount <= 3 ? rawCount : 0 + (rawCount % 3 == 0 ? 3 : rawCount % 3);
      case TargetPlatform.iOS:
      case TargetPlatform.macOS:
        return math.min(rawCount, 3);
      case TargetPlatform.windows:
        return rawCount < 2 ? rawCount : 2 + rawCount % 2;
    }
  }

Definitely makes the if cases much better! Thanks for the code and suggestion!

Copy link
Contributor

@justinmc justinmc left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@Renzo-Olivares Renzo-Olivares added the autosubmit Merge PR when tree becomes green via auto submit App label Mar 27, 2023
@auto-submit auto-submit bot merged commit 04f6acd into flutter:master Mar 27, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 28, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 10, 2023
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 10, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: text input Entering text in a text field or keyboard related problems autosubmit Merge PR when tree becomes green via auto submit App f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Support selecting all text on triple click

4 participants