Skip to content

[iOS] Hide keyboard on hot restart - #167013

Merged
auto-submit[bot] merged 6 commits into
flutter:masterfrom
loic-sharma:ios_hide_keyboard
Apr 22, 2025
Merged

auto-submit[bot] merged 6 commits into
flutter:masterfrom
loic-sharma:ios_hide_keyboard

Conversation

@loic-sharma

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

Copy link
Copy Markdown
Member

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 related. See also e: labels. team-ios Owned by iOS platform team labels Apr 11, 2025
Comment thread engine/src/flutter/shell/platform/darwin/ios/platform_view_ios.mm
@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
Comment thread dev/devicelab/lib/tasks/keyboard_hot_restart_test.dart Outdated
// 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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

@loic-sharma loic-sharma Apr 21, 2025 •

Copy link
Copy Markdown
Member Author

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');

Comment thread dev/devicelab/lib/tasks/keyboard_hot_restart_test.dart
Comment thread dev/devicelab/lib/tasks/keyboard_hot_restart_test.dart Outdated
@@ -0,0 +1,5 @@
# keyboard_hot_restart

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

Comment thread dev/integration_tests/keyboard_hot_restart/lib/main.dart Outdated

@justinmc justinmc left a comment

Copy link
Copy Markdown
Contributor

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.

Comment thread dev/devicelab/lib/tasks/keyboard_hot_restart_test.dart Outdated
@@ -0,0 +1,5 @@
# keyboard_hot_restart

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

Copy link
Copy Markdown
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.

}
[owner_controller_.platformViewsController reset];
[owner_controller_.restorationPlugin reset];
[owner_controller_.textInputPlugin reset];

Copy link
Copy Markdown
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?

@loic-sharma loic-sharma Apr 18, 2025 •

Copy link
Copy Markdown
Member Author

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Classic haha.

}
[owner_controller_.platformViewsController reset];
[owner_controller_.restorationPlugin reset];
[owner_controller_.textInputPlugin reset];

Copy link
Copy Markdown
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
Copy Markdown
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.

@hellohuanlin hellohuanlin left a comment

Copy link
Copy Markdown
Contributor

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
@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 Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 28, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 29, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 30, 2025
engine-flutter-autoroll added a commit to engine-flutter-autoroll/packages that referenced this pull request Apr 30, 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 1, 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 1, 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 1, 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
@loic-sharma loic-sharma mentioned this pull request May 8, 2025
9 tasks done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

engine flutter/engine related. 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