这是indexloc提供的服务,不要输入任何密码
Skip to content

fix: SelectableText should handle focus changes #155771

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

Merged
merged 4 commits into from
Sep 27, 2024

Conversation

Renzo-Olivares
Copy link
Contributor

This change updates the behavior of SelectableText, to clear its selection when it loses focus and the application is currently running. This fixes the behavior where you may have multiple active highlights if you have SelectableText along with other "selectable" widgets such as TextField, or Text widgets under a SelectionArea.

If the application is in the background, for example when another window is focused, the selection should be retained so when a user returns to the application it is still there.

This change also updates the behavior of selection on macOS, single tap up, previously it was selecting the word edge closest to the tapped position, the correct behavior on native is to select the precise position. This was causing onSelectionChanged to be called twice, once for tap down (sets the precise tapped position, handled by logic in TextSelectionGestureDetector), and a second time for single tap up (moves the cursor to closest word edge, handled by logic in _SelectableTextSelectionGestureDetectorBuilder). This type of selection inconsistency is related to this issue #129726, I plan to look into this further in a separate PR.

Fixes #117573
Fixes #103725

Pre-launch Checklist

  • 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].
  • I followed the [breaking change policy] and added [Data Driven Fixes] where supported.
  • All existing and new tests are passing.

@github-actions github-actions bot added a: text input Entering text in a text field or keyboard related problems framework flutter/packages/flutter repository. See also f: labels. f: material design flutter/packages/flutter/material repository. labels Sep 26, 2024
@Renzo-Olivares Renzo-Olivares force-pushed the selectabletext-focus-fix branch from aa001c5 to a6dc254 Compare September 26, 2024 18:27
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 👍

It seems a little weird that we have to check the lifecycle state when the focus changes, but if that's how Flutter's focus works then I guess that's what you have to do.

Comment on lines +634 to +640
// We should only clear the selection when this SelectableText loses
// focus while the application is currently running. It is possible
// that the application is not currently running, for example on desktop
// platforms, clicking on a different window switches the focus to
// the new window causing the Flutter application to go inactive. In this
// case we want to retain the selection so it remains when we return to
// the Flutter application.
Copy link
Contributor

Choose a reason for hiding this comment

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

So @gspencergoog when the application is backgrounded, any focused FocusNode loses focus? Just making sure that's how it works and is intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

For more context on this, as of #142930, the FocusManager responds to app lifecycle changes.

Copy link
Contributor

Choose a reason for hiding this comment

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

This looks correct to me then, thanks for the context 👍

await tester.pumpAndSettle();
expect(controllerA.selection, TextRange.empty);
expect(controllerB.selection, const TextSelection.collapsed(offset: 0));
}, variant: TargetPlatformVariant.all());
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 worth adding .all here instead of just omitting it? You do have a switch on the platform above, but maybe I think it doesn't affect this particular test.

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 okay with either, but I usually lean towards .all if possible, and not platform specific, for more test coverage. What do you think?

Comment on lines -3268 to +3403
variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }),
variant: const TargetPlatformVariant(<TargetPlatform>{ TargetPlatform.iOS, TargetPlatform.macOS }),
Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch

@justinmc
Copy link
Contributor

I think you need to merge master into this branch to fix the Google test failure.

@Renzo-Olivares Renzo-Olivares force-pushed the selectabletext-focus-fix branch from a6dc254 to 6ea29fa Compare September 26, 2024 20:50
@Renzo-Olivares Renzo-Olivares added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 26, 2024
@auto-submit auto-submit bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Sep 26, 2024
Copy link
Contributor

auto-submit bot commented Sep 26, 2024

auto label is removed for flutter/flutter/155771, due to - The status or check suite Windows build_tests_2_7 has failed. Please fix the issues identified (or deflake) before re-applying this label.

@Renzo-Olivares Renzo-Olivares added the autosubmit Merge PR when tree becomes green via auto submit App label Sep 26, 2024
@Renzo-Olivares Renzo-Olivares force-pushed the selectabletext-focus-fix branch from 020592c to be2d9d3 Compare September 26, 2024 23:16
@auto-submit auto-submit bot merged commit 799cf16 into flutter:master Sep 27, 2024
73 checks passed
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 27, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 27, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 27, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 28, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Sep 28, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 1, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 2, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 3, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 4, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 4, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 4, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 5, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 5, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 6, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 6, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Oct 7, 2024
auto-submit bot pushed a commit to flutter/packages that referenced this pull request Oct 7, 2024
Manual roll requested by stuartmorgan@google.com

flutter/flutter@fa402c8...ead6b0d

2024-09-27 kustermann@google.com Remove left-over traces of "link-dry-run" - which isn't used anywhere in flutter (flutter/flutter#155820)
2024-09-27 engine-flutter-autoroll@skia.org Roll Flutter Engine from e57b440ec4ee to 7c603de2dca7 (5 revisions) (flutter/flutter#155811)
2024-09-27 bruno.leroux@gmail.com Fix DropdownMenu rendered behind AppBar (flutter/flutter#155539)
2024-09-27 engine-flutter-autoroll@skia.org Roll Flutter Engine from 53517772a5b0 to e57b440ec4ee (8 revisions) (flutter/flutter#155799)
2024-09-27 ditman@gmail.com Throw StateError when implicitView is null on `wrapWithDefaultView`. (flutter/flutter#155734)
2024-09-27 34871572+gmackall@users.noreply.github.com Roll packages manually (flutter/flutter#155786)
2024-09-27 rmolivares@renzo-olivares.dev fix: SelectableText should handle focus changes (flutter/flutter#155771)
2024-09-27 34871572+gmackall@users.noreply.github.com Use flutter from in same repo (not path) in `generate_gradle_lockfiles.dart` (again) (flutter/flutter#155794)
2024-09-26 34871572+gmackall@users.noreply.github.com Use flutter from in same repo (not path) in `generate_gradle_lockfiles.dart` (flutter/flutter#155790)
2024-09-26 rmolivares@renzo-olivares.dev `RenderParagraph` should invalidate its `_SelectableFragment`s cached rects on window size updates (flutter/flutter#155719)
2024-09-26 zeqinjie@qq.com Fix broken text field with set hint and min and max lines(#153183) (flutter/flutter#153235)
2024-09-26 engine-flutter-autoroll@skia.org Roll Flutter Engine from 9e6133e8d906 to 53517772a5b0 (1 revision) (flutter/flutter#155772)
2024-09-26 ian@hixie.ch Fix line-wrapping in `flutter create` error message. (flutter/flutter#150325)
2024-09-26 christopherfujino@gmail.com remove fujino from CODEOWNERS (flutter/flutter#155369)

If this roll has caused a breakage, revert this CL and stop the roller
using the controls here:
https://autoroll.skia.org/r/flutter-packages
Please CC camillesimon@google.com,stuartmorgan@google.com on the revert to ensure that a human
is aware of the problem.

To file a bug in Packages: https://github.com/flutter/flutter/issues/new/choose

To report a problem with the AutoRoller itself, please file a bug:
https://issues.skia.org/issues/new?component=1389291&template=1850622

Documentation for the AutoRoller is here:
https://skia.googlesource.com/buildbot/+doc/main/autoroll/README.md
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 11, 2024
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Dec 12, 2024
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: 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.

SelectableText doesn't follow focus logic SelectableText Widget Web & Desktop Allows Multiple Highlights
2 participants