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