shell_translation_delegate.m 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #import "ios/web_view/shell/shell_translation_delegate.h"
  5. #import <UIKit/UIKit.h>
  6. #if !defined(__has_feature) || !__has_feature(objc_arc)
  7. #error "This file requires ARC support."
  8. #endif
  9. @interface ShellTranslationDelegate ()
  10. // Action Sheet to prompt user whether or not the page should be translated.
  11. @property(nonatomic, strong) UIAlertController* beforeTranslateActionSheet;
  12. @end
  13. @implementation ShellTranslationDelegate
  14. @synthesize beforeTranslateActionSheet = _beforeTranslateActionSheet;
  15. - (void)dealloc {
  16. [_beforeTranslateActionSheet dismissViewControllerAnimated:YES
  17. completion:nil];
  18. }
  19. #pragma mark - CWVTranslationDelegate methods
  20. - (void)translationController:(CWVTranslationController*)controller
  21. canOfferTranslationFromLanguage:(CWVTranslationLanguage*)pageLanguage
  22. toLanguage:(CWVTranslationLanguage*)userLanguage {
  23. NSLog(@"%@:%@:%@", NSStringFromSelector(_cmd), pageLanguage, userLanguage);
  24. __weak ShellTranslationDelegate* weakSelf = self;
  25. self.beforeTranslateActionSheet = [UIAlertController
  26. alertControllerWithTitle:nil
  27. message:@"Pick Translate Action"
  28. preferredStyle:UIAlertControllerStyleActionSheet];
  29. _beforeTranslateActionSheet.popoverPresentationController.sourceView =
  30. [self anyKeyWindow];
  31. CGRect bounds = [self anyKeyWindow].bounds;
  32. _beforeTranslateActionSheet.popoverPresentationController.sourceRect =
  33. CGRectMake(CGRectGetWidth(bounds) / 2, 60, 1, 1);
  34. UIAlertAction* cancelAction =
  35. [UIAlertAction actionWithTitle:@"Nope."
  36. style:UIAlertActionStyleCancel
  37. handler:^(UIAlertAction* action) {
  38. weakSelf.beforeTranslateActionSheet = nil;
  39. }];
  40. [_beforeTranslateActionSheet addAction:cancelAction];
  41. NSString* translateTitle = [NSString
  42. stringWithFormat:@"Translate to %@", userLanguage.localizedName];
  43. UIAlertAction* translateAction = [UIAlertAction
  44. actionWithTitle:translateTitle
  45. style:UIAlertActionStyleDefault
  46. handler:^(UIAlertAction* action) {
  47. weakSelf.beforeTranslateActionSheet = nil;
  48. if (!weakSelf) {
  49. return;
  50. }
  51. CWVTranslationLanguage* source = pageLanguage;
  52. CWVTranslationLanguage* target = userLanguage;
  53. [controller translatePageFromLanguage:source
  54. toLanguage:target
  55. userInitiated:YES];
  56. }];
  57. [_beforeTranslateActionSheet addAction:translateAction];
  58. UIAlertAction* alwaysTranslateAction = [UIAlertAction
  59. actionWithTitle:@"Always Translate"
  60. style:UIAlertActionStyleDefault
  61. handler:^(UIAlertAction* action) {
  62. weakSelf.beforeTranslateActionSheet = nil;
  63. if (!weakSelf) {
  64. return;
  65. }
  66. CWVTranslationPolicy* policy = [CWVTranslationPolicy
  67. translationPolicyAutoTranslateToLanguage:userLanguage];
  68. [controller setTranslationPolicy:policy
  69. forPageLanguage:pageLanguage];
  70. }];
  71. [_beforeTranslateActionSheet addAction:alwaysTranslateAction];
  72. UIAlertAction* neverTranslateAction = [UIAlertAction
  73. actionWithTitle:@"Never Translate"
  74. style:UIAlertActionStyleDefault
  75. handler:^(UIAlertAction* action) {
  76. weakSelf.beforeTranslateActionSheet = nil;
  77. if (!weakSelf) {
  78. return;
  79. }
  80. CWVTranslationPolicy* policy =
  81. [CWVTranslationPolicy translationPolicyNever];
  82. [controller setTranslationPolicy:policy
  83. forPageLanguage:pageLanguage];
  84. }];
  85. [_beforeTranslateActionSheet addAction:neverTranslateAction];
  86. [[self anyKeyWindow].rootViewController
  87. presentViewController:_beforeTranslateActionSheet
  88. animated:YES
  89. completion:nil];
  90. }
  91. - (void)translationController:(CWVTranslationController*)controller
  92. didStartTranslationFromLanguage:(CWVTranslationLanguage*)sourceLanguage
  93. toLanguage:(CWVTranslationLanguage*)targetLanguage
  94. userInitiated:(BOOL)userInitiated {
  95. NSLog(@"%@:%@:%@:%@", NSStringFromSelector(_cmd), sourceLanguage,
  96. targetLanguage, @(userInitiated));
  97. }
  98. - (void)translationController:(CWVTranslationController*)controller
  99. didFinishTranslationFromLanguage:(CWVTranslationLanguage*)sourceLanguage
  100. toLanguage:(CWVTranslationLanguage*)targetLanguage
  101. error:(nullable NSError*)error {
  102. NSLog(@"%@:%@:%@:%@", NSStringFromSelector(_cmd), sourceLanguage,
  103. targetLanguage, error);
  104. }
  105. #pragma mark - Private
  106. - (UIWindow*)anyKeyWindow {
  107. NSArray<UIWindow*>* windows = [UIApplication sharedApplication].windows;
  108. for (UIWindow* window in windows) {
  109. if (window.isKeyWindow)
  110. return window;
  111. }
  112. return nil;
  113. }
  114. @end