Skip to content

Conversation

@bleroux
Copy link
Contributor

@bleroux bleroux commented Oct 16, 2024

Description

This PR makes EditableText aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus).

Before this PR, on web and desktop, the whole content of a TextField was selected whenever a TextField gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed

Related Issue

Fixes When switching to another browser tab or window and then going back, all text on TextField is selected automatically.

Tests

Adds 1 test.

@flutter-dashboard
Copy link

It looks like this pull request may not have tests. Please make sure to add tests before merging. If you need an exemption, contact "@test-exemption-reviewer" in the #hackers channel in Discord (don't just cc them here, they won't see it!).

If you are not sure if you need tests, consider this rule of thumb: the purpose of a test is to make sure someone doesn't accidentally revert the fix. Ask yourself, is there anything in your PR that you feel it is important we not accidentally revert back to how it was before your fix?

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing. The test exemption team is a small volunteer group, so all reviewers should feel empowered to ask for tests, without delegating that responsibility entirely to the test exemption group.

@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. labels Oct 16, 2024
@bleroux bleroux requested a review from justinmc October 16, 2024 15:13
@bleroux bleroux marked this pull request as draft October 16, 2024 15:14
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.

I feel like this approach LGTM... It does seem like it should be possible to know the cause of a focus change, but without that functionality, this seems to be the best way to do it. @gspencergoog is this a reliable way to do this or is there a better way?

If Greg is onboard then this just needs a test and a comment somewhere explaining what this is for, maybe on the declaration of justResumed.

I checked out the code locally and confirmed that it works in all the situations it should:

Scenario Works?
Changing to another window and returning on desktop (Linux) ✔️
Changing tabs on Chrome ✔️
Changing to another window and returning to Chrome ✔️

@bleroux bleroux force-pushed the fix_textfield_select_all_after_application_is_resumed branch from 13a94f1 to 8c52729 Compare October 17, 2024 07:58
@bleroux bleroux marked this pull request as ready for review October 17, 2024 07:59
@bleroux bleroux changed the title [WIP] Fix TextField selects all content after the application is resumed Fix TextField selects all content after the application is resumed Oct 17, 2024
Copy link
Contributor

@gspencergoog gspencergoog left a comment

Choose a reason for hiding this comment

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

32384589-a60f0e74-c078-11e7-9bc1-e5b5287aea9d

This seems good to me. Transitioning to resumed can occur on app startup too (resumed is just the "running while focused" state of an app), but I think that not selecting the whole text in that case is probably the right behavior anyhow.

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 👍


expect(focusNode1.hasFocus, true);
expect(controller1.selection, collapsedAtEnd('Flutter!').selection);
}, 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.

Nit: Is TargetPlatformVariant.all() necessary? I think the behavior will be the same for all platform variants.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good point, I think it is important to keep it, otherwise this test will not fail if the fix is removed (the 'select all' behavior is only desktop and web so the logic I added to fix this issue is not used on Android).

@bleroux bleroux added the autosubmit Merge PR when tree becomes green via auto submit App label Oct 22, 2024
@auto-submit auto-submit bot merged commit 383a221 into flutter:master Oct 22, 2024
@bleroux bleroux deleted the fix_textfield_select_all_after_application_is_resumed branch October 22, 2024 04:58
@jacobsimionato
Copy link
Contributor

Reason for revert: Google3 roll failing - see b/375019489

@jacobsimionato jacobsimionato added the revert Autorevert PR (with "Reason for revert:" comment) label Oct 22, 2024
auto-submit bot pushed a commit that referenced this pull request Oct 22, 2024
@auto-submit auto-submit bot removed the revert Autorevert PR (with "Reason for revert:" comment) label Oct 22, 2024
auto-submit bot added a commit that referenced this pull request Oct 22, 2024
…esumed (#156968)" (#157378)

Reverts: #156968
Initiated by: jacobsimionato
Reason for reverting: Google3 roll failing - see b/375019489
Original PR Author: bleroux

Reviewed By: {justinmc, gspencergoog}

This change reverts the following previous change:
## Description

This PR makes `EditableText` aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus).

Before this PR, on web and desktop, the whole content of a `TextField` was selected whenever a `TextField` gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed 

## Related Issue

Fixes [When switching to another browser tab or window and then going back, all text on TextField is selected automatically](#156078).

## Tests

Adds 1 test.
@jacobsimionato
Copy link
Contributor

I'm sorry for the revert here! So what happened is some Google integration tests failed at this line:

assert(previousState == null || previousState == AppLifecycleState.hidden, 'Invalid state transition from $previousState to $state');

'package:flutter/src/widgets/app_lifecycle_listener.dart': Failed assertion: line 253 pos 16: 'previousState == null || previousState == AppLifecycleState.hidden': Invalid state transition from AppLifecycleState.resumed to AppLifecycleState.paused

The test logic is doing something like:

// Enter some text in text fields, click some buttons

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);

I imagine the test logic needs updating, but I'm not sure exactly what to do yet, hence the revert. Should I try to fix this by simulating a change to the 'hidden' state before 'paused' ?

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);

@gspencergoog
Copy link
Contributor

gspencergoog commented Oct 22, 2024

@jacobsimionato See this diagram for the allowed state transitions (or this one)

I think the test is doing an invalid state transition, and one that the engine wouldn't send. The engine implementations are coded to synthesize any intermediate state transitions if the platform jumps from one state to another so that the state machine is followed, and an application can count on getting all the states in the expected order.

So, yes, you should probably add that transition, but if the test is expected to start in a particular state, then you should generate any intermediate states too, so if, for instance, the test starts at resumed, it should generate these states to get to paused:

tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.inactive);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.hidden);
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);

@jacobsimionato
Copy link
Contributor

I've now updated the internal tests to do the valid state transitions as you suggested.

@bleroux could you reland the PR? Sorry to make you do it twice!

@bleroux
Copy link
Contributor Author

bleroux commented Oct 23, 2024

Could you reland the PR? Sorry to make you do it twice!

Hey @jacobsimionato 👋 !
No problem at all, especially because I just woke up (France time) and see that you already sorted everything out 😄🙏.

Here is the reland: #157399.
Can you approve it? That way I will be able to merge it as soon as the CI checks are ok.

auto-submit bot pushed a commit that referenced this pull request Oct 23, 2024
…sumed" (#157399)

## Description

Relands #156968 wich was reverted in #157378 

This PR makes `EditableText` aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus).

Before this PR, on web and desktop, the whole content of a `TextField` was selected whenever a `TextField` gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed 

## Related Issue

Fixes [When switching to another browser tab or window and then going back, all text on TextField is selected automatically](#156078).

## Tests

Adds 1 test.
M97Chahboun pushed a commit to M97Chahboun/flutter that referenced this pull request Oct 30, 2024
…esumed (flutter#156968)" (flutter#157378)

Reverts: flutter#156968
Initiated by: jacobsimionato
Reason for reverting: Google3 roll failing - see b/375019489
Original PR Author: bleroux

Reviewed By: {justinmc, gspencergoog}

This change reverts the following previous change:
## Description

This PR makes `EditableText` aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus).

Before this PR, on web and desktop, the whole content of a `TextField` was selected whenever a `TextField` gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed 

## Related Issue

Fixes [When switching to another browser tab or window and then going back, all text on TextField is selected automatically](flutter#156078).

## Tests

Adds 1 test.
M97Chahboun pushed a commit to M97Chahboun/flutter that referenced this pull request Oct 30, 2024
…sumed" (flutter#157399)

## Description

Relands flutter#156968 wich was reverted in flutter#157378 

This PR makes `EditableText` aware of the lifecycle 'resumed' state to let the current selection unchanged when the application is resumed (on web and desktop, 'resumed' means the Flutter app window regained focus).

Before this PR, on web and desktop, the whole content of a `TextField` was selected whenever a `TextField` gained focus. This is the correct behavior when tabbing between fields but it is not when a field regains focus after the application is resumed 

## Related Issue

Fixes [When switching to another browser tab or window and then going back, all text on TextField is selected automatically](flutter#156078).

## Tests

Adds 1 test.
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 12, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Feb 13, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Mar 7, 2025
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 framework flutter/packages/flutter repository. See also f: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[TextField] When switching to another browser tab or window and then going back, all text on TextField is selected automatically

4 participants