| 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 "ui/base/ios/cru_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 #include "ui/base/device_form_factor.h" | |
| 13 #import "ui/base/ios/cru_context_menu_holder.h" | |
| 14 #include "ui/base/l10n/l10n_util.h" | |
| 15 #include "ui/strings/grit/ui_strings.h" | |
| 16 | |
| 17 // Abstracts system implementation of popovers and action sheets. | |
| 18 @protocol CRUContextMenuControllerImpl<NSObject> | |
| 19 | |
| 20 // Whether the context menu is visible. | |
| 21 @property(nonatomic, readonly, getter=isVisible) BOOL visible; | |
| 22 | |
| 23 // Displays a context menu. | |
| 24 - (void)showWithHolder:(CRUContextMenuHolder*)menuHolder | |
| 25 atPoint:(CGPoint)localPoint | |
| 26 inView:(UIView*)view; | |
| 27 | |
| 28 // Dismisses displayed context menu. | |
| 29 - (void)dismissAnimated:(BOOL)animated | |
| 30 completionHandler:(ProceduralBlock)completionHandler; | |
| 31 | |
| 32 @end | |
| 33 | |
| 34 // Backs up CRUContextMenuController by using UIAlertController. | |
| 35 @interface CRUAlertController : NSObject<CRUContextMenuControllerImpl> { | |
| 36 // Weak underlying UIAlertController. | |
| 37 base::WeakNSObject<UIAlertController> _alert; | |
| 38 } | |
| 39 // Redefined to readwrite. | |
| 40 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | |
| 41 @end | |
| 42 | |
| 43 // Displays a context menu. Implements Bridge pattern. | |
| 44 @implementation CRUContextMenuController { | |
| 45 // Implementation specific for iOS version. | |
| 46 base::scoped_nsprotocol<id<CRUContextMenuControllerImpl>> _impl; | |
| 47 } | |
| 48 | |
| 49 - (BOOL)isVisible { | |
| 50 return [_impl isVisible]; | |
| 51 } | |
| 52 | |
| 53 - (instancetype)init { | |
| 54 self = [super init]; | |
| 55 if (self) { | |
| 56 _impl.reset([[CRUAlertController alloc] init]); | |
| 57 } | |
| 58 return self; | |
| 59 } | |
| 60 | |
| 61 - (void)dealloc { | |
| 62 [_impl dismissAnimated:NO completionHandler:nil]; | |
| 63 [super dealloc]; | |
| 64 } | |
| 65 | |
| 66 - (void)showWithHolder:(CRUContextMenuHolder*)menuHolder | |
| 67 atPoint:(CGPoint)point | |
| 68 inView:(UIView*)view { | |
| 69 DCHECK(menuHolder.itemCount); | |
| 70 // Check that the view is still visible on screen, otherwise just return and | |
| 71 // don't show the context menu. | |
| 72 if (![view window] && ![view isKindOfClass:[UIWindow class]]) | |
| 73 return; | |
| 74 [_impl showWithHolder:menuHolder atPoint:point inView:view]; | |
| 75 } | |
| 76 | |
| 77 - (void)dismissAnimated:(BOOL)animated | |
| 78 completionHandler:(ProceduralBlock)completionHandler { | |
| 79 [_impl dismissAnimated:animated completionHandler:completionHandler]; | |
| 80 } | |
| 81 | |
| 82 @end | |
| 83 | |
| 84 @implementation CRUAlertController | |
| 85 @synthesize visible = _visible; | |
| 86 | |
| 87 - (CGSize)sizeForTitleThatFitsMenuWithHolder:(CRUContextMenuHolder*)menuHolder | |
| 88 atPoint:(CGPoint)point | |
| 89 inView:(UIView*)view { | |
| 90 // Presenting and dismissing a dummy UIAlertController flushes a screen. | |
| 91 // As a workaround return an estimation of the space available depending | |
| 92 // on the device's type. | |
| 93 const CGFloat kAvailableWidth = 320; | |
| 94 const CGFloat kAvailableHeightTablet = 200; | |
| 95 const CGFloat kAvailableHeightPhone = 100; | |
| 96 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) { | |
| 97 return CGSizeMake(kAvailableWidth, kAvailableHeightTablet); | |
| 98 } | |
| 99 return CGSizeMake(kAvailableWidth, kAvailableHeightPhone); | |
| 100 } | |
| 101 | |
| 102 - (void)showWithHolder:(CRUContextMenuHolder*)menuHolder | |
| 103 atPoint:(CGPoint)point | |
| 104 inView:(UIView*)view { | |
| 105 UIAlertController* alert = [UIAlertController | |
| 106 alertControllerWithTitle:menuHolder.menuTitle | |
| 107 message:nil | |
| 108 preferredStyle:UIAlertControllerStyleActionSheet]; | |
| 109 alert.popoverPresentationController.sourceView = view; | |
| 110 alert.popoverPresentationController.sourceRect = | |
| 111 CGRectMake(point.x, point.y, 1.0, 1.0); | |
| 112 | |
| 113 // Add the actions. | |
| 114 base::WeakNSObject<CRUAlertController> weakSelf(self); | |
| 115 [menuHolder.itemTitles enumerateObjectsUsingBlock:^(NSString* itemTitle, | |
| 116 NSUInteger itemIndex, | |
| 117 BOOL*) { | |
| 118 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) { | |
| 119 [menuHolder performActionAtIndex:itemIndex]; | |
| 120 [weakSelf setVisible:NO]; | |
| 121 }; | |
| 122 [alert addAction:[UIAlertAction actionWithTitle:itemTitle | |
| 123 style:UIAlertActionStyleDefault | |
| 124 handler:actionHandler]]; | |
| 125 }]; | |
| 126 | |
| 127 // Cancel button goes last, to match other browsers. | |
| 128 void (^cancelHandler)(UIAlertAction*) = ^(UIAlertAction* action) { | |
| 129 [weakSelf setVisible:NO]; | |
| 130 }; | |
| 131 UIAlertAction* cancel_action = | |
| 132 [UIAlertAction actionWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL) | |
| 133 style:UIAlertActionStyleCancel | |
| 134 handler:cancelHandler]; | |
| 135 [alert addAction:cancel_action]; | |
| 136 | |
| 137 // Present sheet/popover using controller that is added to view hierarchy. | |
| 138 UIViewController* topController = view.window.rootViewController; | |
| 139 while (topController.presentedViewController) | |
| 140 topController = topController.presentedViewController; | |
| 141 [topController presentViewController:alert animated:YES completion:nil]; | |
| 142 self.visible = YES; | |
| 143 _alert.reset(alert); | |
| 144 } | |
| 145 | |
| 146 - (void)dismissAnimated:(BOOL)animated | |
| 147 completionHandler:(ProceduralBlock)completionHandler { | |
| 148 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; | |
| 149 } | |
| 150 | |
| 151 @end | |
| OLD | NEW |