Skip to content

Add Translate to iOS selection context menu#169150

Closed
LouiseHsu wants to merge 18 commits intoflutter:masterfrom
LouiseHsu:translate-api
Closed

Add Translate to iOS selection context menu#169150
LouiseHsu wants to merge 18 commits intoflutter:masterfrom
LouiseHsu:translate-api

Conversation

@LouiseHsu
Copy link
Contributor

@LouiseHsu LouiseHsu commented May 20, 2025

Fixes #150392, part of #107578

This PR adds a "Translate" action to the ios selection context menu using this swiftui translate api announce during WWDC 2024.
Includes the ipad implementation as well.
This is the first swiftui api in the engine so fingers crossed I don't break everything. 👀 😨

Simulator.Screen.Recording.-.iPad.Air.11-inch.M3.-.2025-05-28.at.14.11.58.mp4
Simulator.Screen.Recording.-.iPhone.16.Plus.-.2025-05-28.at.14.09.13.mp4

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 platform-ios iOS applications specifically framework flutter/packages/flutter repository. See also f: labels. engine flutter/engine related. See also e: labels. f: material design flutter/packages/flutter/material repository. a: internationalization Supporting other languages or locales. (aka i18n) f: cupertino flutter/packages/flutter/cupertino repository team-ios Owned by iOS platform team labels May 20, 2025
@LouiseHsu LouiseHsu changed the title Translate api Add Translate to iOS selection context menu May 28, 2025
@LouiseHsu LouiseHsu marked this pull request as ready for review May 28, 2025 21:16
@LouiseHsu LouiseHsu requested a review from a team as a code owner May 28, 2025 21:16
@LouiseHsu LouiseHsu requested a review from justinmc May 28, 2025 21:16
@hellohuanlin
Copy link
Contributor

haven't reviewed PR but the video looks so nice! How did you get the popover size on iPad working?

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.

Super cool to see swift in the engine. Thanks for supporting both the system context menu and the Flutter context menu. The framework code looks great

I checked this branch out locally and did a bunch of QA on it. Everything was very close to native with a few nits:

  • While the translate menu is up, the selection and handles should be grey, but they remain blue on this branch.
    • This is definitely beyond the scope of this PR. I don't think we currently have an easy way to do this.
  • While the translate menu is up, the context menu should not be shown, but it is on this branch when using the Flutter-rendered context menu.
    • It works using SystemContextMenu.
    • Could you close the context menu on the framework side when the Translate button is tapped?
  • The "Replace with Translation" button is missing on this branch.
    • Any idea if it's possible to support this?

If you can confirm any of these but can't fix them in this PR, can you open an issue to track them? These are pretty minor; I think it's probably ok to ship this feature without fixes for any of them..

FlutterTextInputPlugin* _textInputPlugin = [self.engine textInputPlugin];
UITextRange* range = _textInputPlugin.textInputView.selectedTextRange;

// firstRectForRange cannot be used here as it's current implementation does
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" => "its"

/// * [LiveTextInputStatusNotifier], where the status of Live Text can be listened to.
liveTextInput,

/// A button that displays the translation screen for the current text selection
Copy link
Contributor

Choose a reason for hiding this comment

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

Period at the end.

Comment on lines +2897 to +2898
Future<void> translateForSelection(SelectionChangedCause cause) async {
assert(!widget.obscureText);
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we handle the case where this is called on a non-iOS platform? It's possible to call this directly without checking translateEnabled. Or what will happen if Translate.invoke is called on non-iOS?

Looks like we don't handle it in lookUpSelection etc. but maybe we should.

CGRect transformedLastRect = [(FlutterTextInputView*)_textInputPlugin.textInputView
localRectFromFrameworkTransform:lastRect];

// In case of RTL Language, get the minimum x coordinate too
Copy link
Contributor

Choose a reason for hiding this comment

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

Period at the end.

Comment on lines +410 to +413
@override
String toString() {
return 'IOSSystemContextMenuItemTranslate(title: $title)';
}
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 I made a mistake by adding these toString methods, I should have mixed in Diagnosticable and overridden debugFillProperties instead. Can you do that here, and I will open an issue for converting the rest of them?

Copy link
Contributor

Choose a reason for hiding this comment

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

@github-actions github-actions bot added the a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) label May 30, 2025
@flutter-dashboard
Copy link

CI had a failure that stopped further tests from running. We need to investigate to determine the root cause.

SHA at time of execution: 504065d.

Possible causes:

  • Configuration Changes: The .ci.yaml file might have been modified between the creation of this pull request and the start of this test run. This can lead to ci yaml validation errors.
  • Infrastructure Issues: Problems with the CI environment itself (e.g., quota) could have caused the failure.

A blank commit, or merging to head, will be required to resume running CI for this PR.

Error Details:

GitHub Error: You have exceeded a secondary rate limit. Please wait a few minutes before you try again. If you reach out to GitHub Support for help, please include the request ID 95A4:3D02A0:326CF:62CBC:683A151B.

Stack trace:

#0      GitHub.handleStatusCode (package:github/src/common/github.dart:486:5)
#1      GitHub.request (package:github/src/common/github.dart:422:7)
<asynchronous suspension>
#2      GitHub.requestJson (package:github/src/common/github.dart:323:22)
<asynchronous suspension>
#3      RetryOptions.retry (package:retry/retry.dart:131:16)
<asynchronous suspension>
#4      LuciBuildService.scheduleTryBuilds (package:cocoon_service/src/service/luci_build_service.dart:245:24)
<asynchronous suspension>
#5      Scheduler._runCiTestingStage (package:cocoon_service/src/service/scheduler.dart:1233:9)
<asynchronous suspension>
#6      Scheduler.proceedToCiTestingStage (package:cocoon_service/src/service/scheduler.dart:1294:7)
<asynchronous suspension>
#7      Scheduler._closeSuccessfulEngineBuildStage (package:cocoon_service/src/service/scheduler.dart:1105:5)
<asynchronous suspension>
#8      Scheduler.processCheckRunCompletion (package:cocoon_service/src/service/scheduler.dart:1038:9)
<asynchronous suspension>
#9      Scheduler.processCheckRun (package:cocoon_service/src/service/scheduler.dart:1378:9)
<asynchronous suspension>
#10     GithubWebhookSubscription.post (package:cocoon_service/src/request_handlers/github/webhook_subscription.dart:115:24)
<asynchronous suspension>
#11     RequestHandler.service (package:cocoon_service/src/request_handling/request_handler.dart:45:20)
<asynchronous suspension>
#12     SubscriptionHandler.service (package:cocoon_service/src/request_handling/subscription_handler.dart:140:5)
<asynchronous suspension>
#13     createServer.<anonymous closure> (package:cocoon_service/server.dart:317:7)
<asynchronous suspension>

@dkwingsmt dkwingsmt requested a review from justinmc June 4, 2025 18:45
@justinmc
Copy link
Contributor

justinmc commented Jun 5, 2025

I assume this is not ready for re-review yet but feel free to tag me whenever it is.

@LouiseHsu
Copy link
Contributor Author

I assume this is not ready for re-review yet but feel free to tag me whenever it is.

Sorry, Im getting some accessibility tree failures that will take some time to dig into, but Im occupied with something else at the moment. 😿 Im gonna put this back into draft to get it off your queue

@LouiseHsu LouiseHsu marked this pull request as draft June 5, 2025 21:33
@flutter-dashboard
Copy link

This pull request has been changed to a draft. The currently pending flutter-gold status will not be able to resolve until a new commit is pushed or the change is marked ready for review again.

For more guidance, visit Writing a golden file test for package:flutter.

Reviewers: Read the Tree Hygiene page and make sure this patch meets those guidelines before LGTMing.

@Piinks
Copy link
Contributor

Piinks commented Aug 5, 2025

Greetings from stale PR triage! 👋
Do you still have plans for this PR @LouiseHsu?

@justinmc justinmc removed their request for review September 17, 2025 22:34
@Piinks
Copy link
Contributor

Piinks commented Nov 17, 2025

Greetings once more from stale RP triage! Should we close this? It has many conflicts now and has not been updated since the last visit to triage.

@LouiseHsu
Copy link
Contributor Author

Greetings once more from stale RP triage! Should we close this? It has many conflicts now and has not been updated since the last visit to triage.

I will close it - Im planning on revisiting this next month but probably will open a new PR to avoid the 100000000 merge conflicts.

@LouiseHsu LouiseHsu closed this Nov 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

a: accessibility Accessibility, e.g. VoiceOver or TalkBack. (aka a11y) a: internationalization Supporting other languages or locales. (aka i18n) a: text input Entering text in a text field or keyboard related problems engine flutter/engine related. See also e: labels. f: cupertino flutter/packages/flutter/cupertino repository f: material design flutter/packages/flutter/material repository. framework flutter/packages/flutter repository. See also f: 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.

[iOS] Add Translate button to text fields' context menu

4 participants