Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 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 | |
| 5 #import "ios/chrome/browser/ui/context_menu/context_menu_wrangler.h" | |
| 6 | |
| 7 #include "base/ios/weak_nsobject.h" | |
| 8 #include "base/strings/sys_string_conversions.h" | |
| 9 #import "ios/web/public/web_state/context_menu_params.h" | |
| 10 #include "ui/base/l10n/l10n_util.h" | |
| 11 #include "ui/strings/grit/ui_strings.h" | |
| 12 | |
| 13 @interface ContextMenuWrangler () { | |
| 14 // Underlying system alert. | |
| 15 base::WeakNSObject<UIAlertController> _alert; | |
| 16 // Parameters that define what is shown in the context menu. | |
| 17 web::ContextMenuParams _params; | |
| 18 } | |
| 19 // Redefined to readwrite. | |
| 20 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | |
| 21 // Lazy initializer to create the |_alert|. | |
| 22 @property(nonatomic, readonly) UIAlertController* alert; | |
|
Eugene But (OOO till 7-30)
2016/05/12 22:27:48
NIT: s/alert/alertController
michaeldo
2016/05/12 23:32:43
Done.
| |
| 23 // Called when the alert is dismissed to perform cleanup. | |
| 24 - (void)alertDismissed; | |
| 25 @end | |
| 26 | |
| 27 // Displays a context menu. | |
| 28 @implementation ContextMenuWrangler | |
| 29 @synthesize visible = _visible; | |
| 30 | |
| 31 - (instancetype)initWithContextMenuParams: | |
| 32 (const web::ContextMenuParams&)params { | |
| 33 self = [super init]; | |
| 34 if (self) { | |
| 35 _params = params; | |
| 36 } | |
| 37 return self; | |
| 38 } | |
| 39 | |
| 40 - (void)dealloc { | |
| 41 [_alert dismissViewControllerAnimated:NO completion:nil]; | |
| 42 [super dealloc]; | |
| 43 } | |
| 44 | |
| 45 - (UIAlertController*)alert { | |
| 46 if (!_alert) { | |
| 47 UIAlertController* alert = [UIAlertController | |
| 48 alertControllerWithTitle:_params.menu_title | |
| 49 message:nil | |
| 50 preferredStyle:UIAlertControllerStyleActionSheet]; | |
| 51 alert.popoverPresentationController.sourceView = _params.view; | |
| 52 alert.popoverPresentationController.sourceRect = | |
| 53 CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0); | |
| 54 _alert.reset([alert retain]); | |
| 55 } | |
| 56 return _alert; | |
| 57 } | |
| 58 | |
| 59 - (void)appendItemWithTitle:(NSString*)title | |
| 60 action:(ProceduralBlock)actionBlock { | |
| 61 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
| 62 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) { | |
| 63 [weakSelf alertDismissed]; | |
| 64 actionBlock(); | |
| 65 }; | |
| 66 [self.alert addAction:[UIAlertAction actionWithTitle:title | |
| 67 style:UIAlertActionStyleDefault | |
| 68 handler:actionHandler]]; | |
| 69 } | |
| 70 | |
| 71 - (void)present { | |
| 72 // Check that the view is still visible on screen, otherwise just return and | |
| 73 // don't show the context menu. | |
| 74 if (![_params.view window] && | |
| 75 ![_params.view isKindOfClass:[UIWindow class]]) { | |
| 76 _alert.reset(); | |
| 77 return; | |
| 78 } | |
| 79 | |
| 80 base::WeakNSObject<ContextMenuWrangler> weakSelf(self); | |
|
Eugene But (OOO till 7-30)
2016/05/12 22:27:48
This should be a part of alert creation, not prese
michaeldo
2016/05/12 23:32:43
I thought it would not end up at the end of the li
| |
| 81 UIAlertAction* cancel_action = | |
| 82 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) | |
| 83 style:UIAlertActionStyleCancel | |
| 84 handler:^(UIAlertAction*) { | |
| 85 [weakSelf alertDismissed]; | |
| 86 }]; | |
| 87 [self.alert addAction:cancel_action]; | |
| 88 | |
| 89 // Present sheet/popover using controller that is added to view hierarchy. | |
| 90 UIViewController* topController = [_params.view window].rootViewController; | |
| 91 while (topController.presentedViewController) | |
| 92 topController = topController.presentedViewController; | |
| 93 [topController presentViewController:_alert animated:YES completion:nil]; | |
| 94 self.visible = YES; | |
| 95 } | |
| 96 | |
| 97 - (void)dismissAnimated:(BOOL)animated | |
| 98 completionHandler:(ProceduralBlock)completionHandler { | |
| 99 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; | |
| 100 [self setVisible:NO]; | |
| 101 _alert.reset(); | |
| 102 } | |
| 103 | |
| 104 - (void)alertDismissed { | |
| 105 [self setVisible:NO]; | |
| 106 _alert.reset(); | |
| 107 } | |
| 108 | |
| 109 @end | |
| OLD | NEW |