| OLD | NEW |
| (Empty) |
| 1 // Copyright 2012 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_controller.h" | |
| 6 | |
| 7 #include <algorithm> | |
| 8 | |
| 9 #include "base/ios/weak_nsobject.h" | |
| 10 #include "base/logging.h" | |
| 11 #import "base/mac/scoped_nsobject.h" | |
| 12 #import "ios/chrome/browser/ui/context_menu/context_menu_holder.h" | |
| 13 #include "ui/base/device_form_factor.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/strings/grit/ui_strings.h" | |
| 16 | |
| 17 @interface ContextMenuController () { | |
| 18 // Underlying system alert. | |
| 19 base::WeakNSObject<UIAlertController> _alert; | |
| 20 } | |
| 21 // Redefined to readwrite. | |
| 22 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | |
| 23 @end | |
| 24 | |
| 25 @implementation ContextMenuController | |
| 26 @synthesize visible = _visible; | |
| 27 | |
| 28 - (void)dealloc { | |
| 29 [_alert dismissViewControllerAnimated:NO completion:nil]; | |
| 30 [super dealloc]; | |
| 31 } | |
| 32 | |
| 33 - (void)showWithHolder:(ContextMenuHolder*)menuHolder | |
| 34 atPoint:(CGPoint)point | |
| 35 inView:(UIView*)view { | |
| 36 DCHECK(menuHolder.itemCount); | |
| 37 // Check that the view is still visible on screen, otherwise just return and | |
| 38 // don't show the context menu. | |
| 39 if (![view window] && ![view isKindOfClass:[UIWindow class]]) | |
| 40 return; | |
| 41 | |
| 42 UIAlertController* alert = [UIAlertController | |
| 43 alertControllerWithTitle:menuHolder.menuTitle | |
| 44 message:nil | |
| 45 preferredStyle:UIAlertControllerStyleActionSheet]; | |
| 46 alert.popoverPresentationController.sourceView = view; | |
| 47 alert.popoverPresentationController.sourceRect = | |
| 48 CGRectMake(point.x, point.y, 1.0, 1.0); | |
| 49 | |
| 50 // Add the actions. | |
| 51 base::WeakNSObject<ContextMenuController> weakSelf(self); | |
| 52 [menuHolder.itemTitles enumerateObjectsUsingBlock:^( | |
| 53 NSString* itemTitle, NSUInteger itemIndex, BOOL*) { | |
| 54 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) { | |
| 55 [menuHolder performActionAtIndex:itemIndex]; | |
| 56 [weakSelf setVisible:NO]; | |
| 57 }; | |
| 58 [alert addAction:[UIAlertAction actionWithTitle:itemTitle | |
| 59 style:UIAlertActionStyleDefault | |
| 60 handler:actionHandler]]; | |
| 61 }]; | |
| 62 | |
| 63 // Cancel button goes last, to match other browsers. | |
| 64 void (^cancelHandler)(UIAlertAction*) = ^(UIAlertAction* action) { | |
| 65 [weakSelf setVisible:NO]; | |
| 66 }; | |
| 67 UIAlertAction* cancel_action = | |
| 68 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) | |
| 69 style:UIAlertActionStyleCancel | |
| 70 handler:cancelHandler]; | |
| 71 [alert addAction:cancel_action]; | |
| 72 | |
| 73 // Present sheet/popover using controller that is added to view hierarchy. | |
| 74 UIViewController* topController = view.window.rootViewController; | |
| 75 while (topController.presentedViewController) | |
| 76 topController = topController.presentedViewController; | |
| 77 [topController presentViewController:alert animated:YES completion:nil]; | |
| 78 self.visible = YES; | |
| 79 _alert.reset(alert); | |
| 80 } | |
| 81 | |
| 82 - (void)dismissAnimated:(BOOL)animated | |
| 83 completionHandler:(ProceduralBlock)completionHandler { | |
| 84 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; | |
| 85 } | |
| 86 | |
| 87 @end | |
| OLD | NEW |