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_coordinator.h" |
| 6 |
| 7 #include "base/ios/weak_nsobject.h" |
| 8 #include "base/mac/scoped_nsobject.h" |
| 9 #include "base/strings/sys_string_conversions.h" |
| 10 #import "ios/web/public/web_state/context_menu_params.h" |
| 11 #include "ui/base/l10n/l10n_util.h" |
| 12 #include "ui/strings/grit/ui_strings.h" |
| 13 |
| 14 @interface ContextMenuCoordinator () { |
| 15 // Underlying system alert. |
| 16 base::scoped_nsobject<UIAlertController> _alertController; |
| 17 // View controller which will be used to present the |_alertController|. |
| 18 base::WeakNSObject<UIViewController> _presentingViewController; |
| 19 // Parameters that define what is shown in the context menu. |
| 20 web::ContextMenuParams _params; |
| 21 } |
| 22 // Redefined to readwrite. |
| 23 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
| 24 // Lazy initializer to create the |_alert|. |
| 25 @property(nonatomic, readonly) UIAlertController* alertController; |
| 26 // Called when the alert is dismissed to perform cleanup. |
| 27 - (void)alertDismissed; |
| 28 @end |
| 29 |
| 30 @implementation ContextMenuCoordinator |
| 31 @synthesize visible = _visible; |
| 32 |
| 33 - (instancetype)initWithViewController:(UIViewController*)viewController |
| 34 params:(const web::ContextMenuParams&)params { |
| 35 self = [super init]; |
| 36 if (self) { |
| 37 _params = params; |
| 38 _presentingViewController.reset(viewController); |
| 39 } |
| 40 return self; |
| 41 } |
| 42 |
| 43 #pragma mark - Object Lifecycle |
| 44 - (void)dealloc { |
| 45 [self stop]; |
| 46 [super dealloc]; |
| 47 } |
| 48 |
| 49 #pragma mark - Public Methods |
| 50 - (void)addItemWithTitle:(NSString*)title action:(ProceduralBlock)actionBlock { |
| 51 base::WeakNSObject<ContextMenuCoordinator> weakSelf(self); |
| 52 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction*) { |
| 53 [weakSelf alertDismissed]; |
| 54 actionBlock(); |
| 55 }; |
| 56 [self.alertController |
| 57 addAction:[UIAlertAction actionWithTitle:title |
| 58 style:UIAlertActionStyleDefault |
| 59 handler:actionHandler]]; |
| 60 } |
| 61 |
| 62 - (void)start { |
| 63 // Check that the view is still visible on screen, otherwise just return and |
| 64 // don't show the context menu. |
| 65 if (![_params.view window] && |
| 66 ![_params.view isKindOfClass:[UIWindow class]]) { |
| 67 return; |
| 68 } |
| 69 |
| 70 [_presentingViewController presentViewController:self.alertController |
| 71 animated:YES |
| 72 completion:nil]; |
| 73 self.visible = YES; |
| 74 } |
| 75 |
| 76 - (void)stop { |
| 77 [_alertController dismissViewControllerAnimated:NO completion:nil]; |
| 78 [self setVisible:NO]; |
| 79 _alertController.reset(); |
| 80 } |
| 81 |
| 82 #pragma mark - Property Implementation |
| 83 - (UIAlertController*)alertController { |
| 84 if (!_alertController) { |
| 85 DCHECK([_params.view isDescendantOfView:[_presentingViewController view]]); |
| 86 UIAlertController* alert = [UIAlertController |
| 87 alertControllerWithTitle:_params.menu_title |
| 88 message:nil |
| 89 preferredStyle:UIAlertControllerStyleActionSheet]; |
| 90 alert.popoverPresentationController.sourceView = _params.view; |
| 91 alert.popoverPresentationController.sourceRect = |
| 92 CGRectMake(_params.location.x, _params.location.y, 1.0, 1.0); |
| 93 |
| 94 base::WeakNSObject<ContextMenuCoordinator> weakSelf(self); |
| 95 UIAlertAction* cancelAction = |
| 96 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) |
| 97 style:UIAlertActionStyleCancel |
| 98 handler:^(UIAlertAction*) { |
| 99 [weakSelf alertDismissed]; |
| 100 }]; |
| 101 [alert addAction:cancelAction]; |
| 102 |
| 103 _alertController.reset([alert retain]); |
| 104 } |
| 105 return _alertController; |
| 106 } |
| 107 |
| 108 #pragma mark - Private Methods |
| 109 - (void)alertDismissed { |
| 110 self.visible = NO; |
| 111 _alertController.reset(); |
| 112 } |
| 113 |
| 114 @end |
OLD | NEW |