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 "ios/chrome/browser/ui/context_menu/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 #import "ios/chrome/browser/ui/context_menu/context_menu_holder.h" |
13 #include "ui/base/device_form_factor.h" | 13 #include "ui/base/device_form_factor.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 @interface ContextMenuController () { |
Eugene But (OOO till 7-30)
2016/05/12 22:27:48
Thank you for this cleanup. Could you please land
michaeldo
2016/05/12 23:32:42
yes, I will move to a separate CL and remove from
| |
18 @protocol ContextMenuControllerImpl<NSObject> | 18 // Underlying system alert. |
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:(ContextMenuHolder*)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 ContextMenuController by using UIAlertController. | |
35 @interface AlertController : NSObject<ContextMenuControllerImpl> { | |
36 base::WeakNSObject<UIAlertController> _alert; | 19 base::WeakNSObject<UIAlertController> _alert; |
37 } | 20 } |
38 // Redefined to readwrite. | 21 // Redefined to readwrite. |
39 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; | 22 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
40 @end | 23 @end |
41 | 24 |
42 // Displays a context menu. Implements Bridge pattern. | 25 // Displays a context menu. |
43 @implementation ContextMenuController { | 26 @implementation ContextMenuController |
44 base::scoped_nsprotocol<id<ContextMenuControllerImpl>> _impl; | 27 @synthesize visible = _visible; |
45 } | |
46 | |
47 - (BOOL)isVisible { | |
48 return [_impl isVisible]; | |
49 } | |
50 | |
51 - (instancetype)init { | |
52 self = [super init]; | |
53 if (self) { | |
54 _impl.reset([[AlertController alloc] init]); | |
55 } | |
56 return self; | |
57 } | |
58 | 28 |
59 - (void)dealloc { | 29 - (void)dealloc { |
60 [_impl dismissAnimated:NO completionHandler:nil]; | 30 [_alert dismissViewControllerAnimated:NO completion:nil]; |
61 [super dealloc]; | 31 [super dealloc]; |
62 } | 32 } |
63 | 33 |
64 - (void)showWithHolder:(ContextMenuHolder*)menuHolder | 34 - (void)showWithHolder:(ContextMenuHolder*)menuHolder |
65 atPoint:(CGPoint)point | 35 atPoint:(CGPoint)point |
66 inView:(UIView*)view { | 36 inView:(UIView*)view { |
67 DCHECK(menuHolder.itemCount); | 37 DCHECK(menuHolder.itemCount); |
68 // Check that the view is still visible on screen, otherwise just return and | 38 // Check that the view is still visible on screen, otherwise just return and |
69 // don't show the context menu. | 39 // don't show the context menu. |
70 if (![view window] && ![view isKindOfClass:[UIWindow class]]) | 40 if (![view window] && ![view isKindOfClass:[UIWindow class]]) |
71 return; | 41 return; |
72 [_impl showWithHolder:menuHolder atPoint:point inView:view]; | |
73 } | |
74 | 42 |
75 - (void)dismissAnimated:(BOOL)animated | |
76 completionHandler:(ProceduralBlock)completionHandler { | |
77 [_impl dismissAnimated:animated completionHandler:completionHandler]; | |
78 } | |
79 | |
80 @end | |
81 | |
82 @implementation AlertController | |
83 @synthesize visible = _visible; | |
84 | |
85 - (CGSize)sizeForTitleThatFitsMenuWithHolder:(ContextMenuHolder*)menuHolder | |
86 atPoint:(CGPoint)point | |
87 inView:(UIView*)view { | |
88 // Presenting and dismissing a dummy UIAlertController flushes a screen. | |
89 // As a workaround return an estimation of the space available depending | |
90 // on the device's type. | |
91 const CGFloat kAvailableWidth = 320; | |
92 const CGFloat kAvailableHeightTablet = 200; | |
93 const CGFloat kAvailableHeightPhone = 100; | |
94 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) { | |
95 return CGSizeMake(kAvailableWidth, kAvailableHeightTablet); | |
96 } | |
97 return CGSizeMake(kAvailableWidth, kAvailableHeightPhone); | |
98 } | |
99 | |
100 - (void)showWithHolder:(ContextMenuHolder*)menuHolder | |
101 atPoint:(CGPoint)point | |
102 inView:(UIView*)view { | |
103 UIAlertController* alert = [UIAlertController | 43 UIAlertController* alert = [UIAlertController |
104 alertControllerWithTitle:menuHolder.menuTitle | 44 alertControllerWithTitle:menuHolder.menuTitle |
105 message:nil | 45 message:nil |
106 preferredStyle:UIAlertControllerStyleActionSheet]; | 46 preferredStyle:UIAlertControllerStyleActionSheet]; |
107 alert.popoverPresentationController.sourceView = view; | 47 alert.popoverPresentationController.sourceView = view; |
108 alert.popoverPresentationController.sourceRect = | 48 alert.popoverPresentationController.sourceRect = |
109 CGRectMake(point.x, point.y, 1.0, 1.0); | 49 CGRectMake(point.x, point.y, 1.0, 1.0); |
110 | 50 |
111 // Add the actions. | 51 // Add the actions. |
112 base::WeakNSObject<AlertController> weakSelf(self); | 52 base::WeakNSObject<ContextMenuController> weakSelf(self); |
113 [menuHolder.itemTitles enumerateObjectsUsingBlock:^( | 53 [menuHolder.itemTitles enumerateObjectsUsingBlock:^( |
114 NSString* itemTitle, NSUInteger itemIndex, BOOL*) { | 54 NSString* itemTitle, NSUInteger itemIndex, BOOL*) { |
115 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) { | 55 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) { |
116 [menuHolder performActionAtIndex:itemIndex]; | 56 [menuHolder performActionAtIndex:itemIndex]; |
117 [weakSelf setVisible:NO]; | 57 [weakSelf setVisible:NO]; |
118 }; | 58 }; |
119 [alert addAction:[UIAlertAction actionWithTitle:itemTitle | 59 [alert addAction:[UIAlertAction actionWithTitle:itemTitle |
120 style:UIAlertActionStyleDefault | 60 style:UIAlertActionStyleDefault |
121 handler:actionHandler]]; | 61 handler:actionHandler]]; |
122 }]; | 62 }]; |
(...skipping 16 matching lines...) Expand all Loading... | |
139 self.visible = YES; | 79 self.visible = YES; |
140 _alert.reset(alert); | 80 _alert.reset(alert); |
141 } | 81 } |
142 | 82 |
143 - (void)dismissAnimated:(BOOL)animated | 83 - (void)dismissAnimated:(BOOL)animated |
144 completionHandler:(ProceduralBlock)completionHandler { | 84 completionHandler:(ProceduralBlock)completionHandler { |
145 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; | 85 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; |
146 } | 86 } |
147 | 87 |
148 @end | 88 @end |
OLD | NEW |