Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[iOS] Hide keyboard on hot restart #167013

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 6 commits into from
Apr 22, 2025

Conversation

loic-sharma
Copy link
Member

@loic-sharma loic-sharma commented Apr 11, 2025

This hides the keyboard and text input context menu if you hot restart your iOS app.

Before After
Before.MP4
After.MP4

Part of #10713

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

@github-actions github-actions bot added platform-ios iOS applications specifically engine flutter/engine repository. See also e: labels. team-ios Owned by iOS platform team labels Apr 11, 2025
@loic-sharma loic-sharma marked this pull request as ready for review April 16, 2025 22:03
@loic-sharma loic-sharma requested review from a team and matanlurey as code owners April 16, 2025 22:03
// This file is modified during the test and needs to be restored at the end.
final String oldContents = mainFile.readAsStringSync();
const String forceKeyboardOn = 'const bool forceKeyboard = true;';
const String forceKeyboardOff = 'const bool forceKeyboard = false;';
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 just a raw string to be displayed? why does it look like a piece of code?

Copy link
Member Author

Choose a reason for hiding this comment

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

I added a comment to clarify:

// When the test starts, the app forces the keyboard to be visible.
// The test turns off this behavior by mutating the app's source code from
// `forceKeyboardOn` to `forceKeyboardOff`.
// See: //dev/integration_tests/keyboard_hot_restart/lib/main.dart

Let me know if you have follow-up questions!

Copy link
Contributor

Choose a reason for hiding this comment

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

wow changing the source code from the test, i've never seen this before. perhaps we can pass in an argument into the test instead.

Copy link
Member Author

Choose a reason for hiding this comment

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

A test argument wouldn't work as we need to change the app's behavior at the middle of the test: first it should make the keyboard visible, but after the hot restart it shouldn't. Let me know if you'd like to do a call to talk through 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.

This seems very brittle. Is this an existing practice?

Copy link
Member Author

@loic-sharma loic-sharma Apr 21, 2025

Choose a reason for hiding this comment

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

We have verify few tests that hot reload or hot restart an app. This appears to be an area of significant tech debt, but below are prior art I could find.

Hot mode test verifies that editing the framework's source code invalidates libraries:

flutterFrameworkSource.writeAsStringSync(
'${flutterFrameworkSource.readAsStringSync()}\n',
);
process.stdin.writeln('r');

Web dev mode test verifies that web hot reload works as expected:

appDartSource.writeAsStringSync(
appDartSource.readAsStringSync().replaceFirst(
"'Flutter Gallery'",
"'Updated Flutter Gallery'",
),
);
sw
..reset()
..start();
process.stdin.writeln('r');

@@ -0,0 +1,5 @@
# keyboard_hot_restart

An app used to verify that the keyboard is not visible after a hot restart.
Copy link
Contributor

Choose a reason for hiding this comment

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

bonus: maybe also check system context menu is dismissed?

Copy link
Contributor

Choose a reason for hiding this comment

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

The presence of the SystemContextMenu widget is probably enough to indicate it's showing or not, if you can check for that. If not, maybe you could print something from the TextField's contextMenuBuilder.

Copy link
Member Author

Choose a reason for hiding this comment

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

Ahhh I didn't realize I could check for the context menu on the framework side, thanks for the pointers!

Copy link
Member Author

Choose a reason for hiding this comment

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

Justin and I talked offline. Unfortunately, the presence of the SystemContextMenu widget and TextField's contextMenuBuilder are not enough to detect whether the native context menu is visible. We don't have a good way to verify this native context menu from this "outside" test.

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 but deferring to Huan for iOS expertise. Also I agree you should check for the presence of the system context menu in the test if you can.

Thanks for fixing this! It has annoyed me before. And thanks for writing a devicelab test for this.

@@ -0,0 +1,5 @@
# keyboard_hot_restart

An app used to verify that the keyboard is not visible after a hot restart.
Copy link
Contributor

Choose a reason for hiding this comment

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

The presence of the SystemContextMenu widget is probably enough to indicate it's showing or not, if you can check for that. If not, maybe you could print something from the TextField's contextMenuBuilder.

@@ -207,6 +207,7 @@ new PlatformMessageHandlerIos(task_runners.GetPlatformTaskRunner())) {}
}
[owner_controller_.platformViewsController reset];
[owner_controller_.restorationPlugin reset];
[owner_controller_.textInputPlugin reset];
Copy link
Contributor

Choose a reason for hiding this comment

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

This is all it took to fix it?

Copy link
Member Author

@loic-sharma loic-sharma Apr 18, 2025

Choose a reason for hiding this comment

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

Yup. The fix took 30 seconds, the test took 3 days 😅

Copy link
Contributor

Choose a reason for hiding this comment

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

Classic haha.

@@ -207,6 +207,7 @@ new PlatformMessageHandlerIos(task_runners.GetPlatformTaskRunner())) {}
}
[owner_controller_.platformViewsController reset];
[owner_controller_.restorationPlugin reset];
[owner_controller_.textInputPlugin reset];
Copy link
Contributor

Choose a reason for hiding this comment

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

just curious - the reset method of text input plugin was defined but never called before?

Copy link
Member Author

Choose a reason for hiding this comment

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

No reset is new, but it calls the existing hideTextInput method which is private and responds to the framework's TextInput.hide message.

Copy link
Contributor

@hellohuanlin hellohuanlin left a comment

Choose a reason for hiding this comment

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

Thank you for chatting offline about modifying the source file reasons.

@loic-sharma loic-sharma added the autosubmit Merge PR when tree becomes green via auto submit App label Apr 22, 2025
@auto-submit auto-submit bot added this pull request to the merge queue Apr 22, 2025
Merged via the queue into flutter:master with commit 08a59c2 Apr 22, 2025
179 checks passed
@flutter-dashboard flutter-dashboard bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Apr 22, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 23, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 1, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 2, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 3, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 4, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 5, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 6, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request May 7, 2025
github-merge-queue bot pushed a commit that referenced this pull request May 19, 2025
The keyboard hot restart test does these steps:

1. It launches an app that forces the keyboard to be visible
2. It checks that the keyboard is visible by examining the app's logs
3. It updates the app to no longer force the keyboard to be visible and
hot restarts the app
4. It checks that the keyboard is no longer visible by examining the
app's logs

Previously, the test app printed the keyboard status whenever the app
rebuilt. The test would move on as soon as it saw a log that indicates
the keyboard was in the expected state. However, since the app could log
the keyboard's visibility _before_ the Dart VM was available, hot
restart commands could be ignored (or at least, that's my theory from
the DeviceLab logs).

Now, the test app prints the keyboard status non-stop. The test now
waits until the Dart VM is available before checking if the logs
indicate the keyboard is open. Also, the test now waits until the hot
restart has completed before checking if the keyboard is closed. In
theory, this should ensure that hot restart does not happen until after
the Dart VM is available.

Follow-up to: #167013
Part of: #10713

## Pre-launch Checklist

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

If you need help, consider asking for advice on the #hackers-new channel
on [Discord].

<!-- Links -->
[Contributor Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#overview
[Tree Hygiene]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md
[test-exempt]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#tests
[Flutter Style Guide]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md
[Features we expect every widget to implement]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Style-guide-for-Flutter-repo.md#features-we-expect-every-widget-to-implement
[CLA]: https://cla.developers.google.com/
[flutter/tests]: https://github.com/flutter/tests
[breaking change policy]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Tree-hygiene.md#handling-breaking-changes
[Discord]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Chat.md
[Data Driven Fixes]:
https://github.com/flutter/flutter/blob/main/docs/contributing/Data-driven-Fixes.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine flutter/engine repository. See also e: labels. platform-ios iOS applications specifically team-ios Owned by iOS platform team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants