Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(57)

Side by Side Diff: ios/chrome/browser/ui/context_menu/context_menu_controller.mm

Issue 1891863004: Copy contents of ui/base/ios into ios/chrome/browser/ui/context_menu (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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. 36 // Weak underlying UIAlertController.
sdefresne 2016/04/18 08:47:29 nit: This comment is redundant with "base::WeakNSO
Jackie Quinn 2016/04/19 14:24:27 Done.
37 base::WeakNSObject<UIAlertController> _alert; 37 base::WeakNSObject<UIAlertController> _alert;
38 } 38 }
39 // Redefined to readwrite. 39 // Redefined to readwrite.
40 @property(nonatomic, readwrite, getter=isVisible) BOOL visible; 40 @property(nonatomic, readwrite, getter=isVisible) BOOL visible;
41 @end 41 @end
42 42
43 // Displays a context menu. Implements Bridge pattern. 43 // Displays a context menu. Implements Bridge pattern.
44 @implementation CRUContextMenuController { 44 @implementation ContextMenuController {
45 // Implementation specific for iOS version. 45 // Implementation specific for iOS version.
sdefresne 2016/04/18 08:47:29 nit: This comment is redundant, this file is in a
Jackie Quinn 2016/04/19 14:24:27 Done.
46 base::scoped_nsprotocol<id<CRUContextMenuControllerImpl>> _impl; 46 base::scoped_nsprotocol<id<ContextMenuControllerImpl>> _impl;
47 } 47 }
48 48
49 - (BOOL)isVisible { 49 - (BOOL)isVisible {
50 return [_impl isVisible]; 50 return [_impl isVisible];
51 } 51 }
52 52
53 - (instancetype)init { 53 - (instancetype)init {
54 self = [super init]; 54 self = [super init];
55 if (self) { 55 if (self) {
56 _impl.reset([[CRUAlertController alloc] init]); 56 _impl.reset([[AlertController alloc] init]);
57 } 57 }
58 return self; 58 return self;
59 } 59 }
60 60
61 - (void)dealloc { 61 - (void)dealloc {
62 [_impl dismissAnimated:NO completionHandler:nil]; 62 [_impl dismissAnimated:NO completionHandler:nil];
63 [super dealloc]; 63 [super dealloc];
64 } 64 }
65 65
66 - (void)showWithHolder:(CRUContextMenuHolder*)menuHolder 66 - (void)showWithHolder:(ContextMenuHolder*)menuHolder
67 atPoint:(CGPoint)point 67 atPoint:(CGPoint)point
68 inView:(UIView*)view { 68 inView:(UIView*)view {
69 DCHECK(menuHolder.itemCount); 69 DCHECK(menuHolder.itemCount);
70 // Check that the view is still visible on screen, otherwise just return and 70 // Check that the view is still visible on screen, otherwise just return and
71 // don't show the context menu. 71 // don't show the context menu.
72 if (![view window] && ![view isKindOfClass:[UIWindow class]]) 72 if (![view window] && ![view isKindOfClass:[UIWindow class]])
73 return; 73 return;
74 [_impl showWithHolder:menuHolder atPoint:point inView:view]; 74 [_impl showWithHolder:menuHolder atPoint:point inView:view];
75 } 75 }
76 76
77 - (void)dismissAnimated:(BOOL)animated 77 - (void)dismissAnimated:(BOOL)animated
78 completionHandler:(ProceduralBlock)completionHandler { 78 completionHandler:(ProceduralBlock)completionHandler {
79 [_impl dismissAnimated:animated completionHandler:completionHandler]; 79 [_impl dismissAnimated:animated completionHandler:completionHandler];
80 } 80 }
81 81
82 @end 82 @end
83 83
84 @implementation CRUAlertController 84 @implementation AlertController
85 @synthesize visible = _visible; 85 @synthesize visible = _visible;
86 86
87 - (CGSize)sizeForTitleThatFitsMenuWithHolder:(CRUContextMenuHolder*)menuHolder 87 - (CGSize)sizeForTitleThatFitsMenuWithHolder:(ContextMenuHolder*)menuHolder
88 atPoint:(CGPoint)point 88 atPoint:(CGPoint)point
89 inView:(UIView*)view { 89 inView:(UIView*)view {
90 // Presenting and dismissing a dummy UIAlertController flushes a screen. 90 // Presenting and dismissing a dummy UIAlertController flushes a screen.
91 // As a workaround return an estimation of the space available depending 91 // As a workaround return an estimation of the space available depending
92 // on the device's type. 92 // on the device's type.
93 const CGFloat kAvailableWidth = 320; 93 const CGFloat kAvailableWidth = 320;
94 const CGFloat kAvailableHeightTablet = 200; 94 const CGFloat kAvailableHeightTablet = 200;
95 const CGFloat kAvailableHeightPhone = 100; 95 const CGFloat kAvailableHeightPhone = 100;
96 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) { 96 if (ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET) {
97 return CGSizeMake(kAvailableWidth, kAvailableHeightTablet); 97 return CGSizeMake(kAvailableWidth, kAvailableHeightTablet);
98 } 98 }
99 return CGSizeMake(kAvailableWidth, kAvailableHeightPhone); 99 return CGSizeMake(kAvailableWidth, kAvailableHeightPhone);
100 } 100 }
101 101
102 - (void)showWithHolder:(CRUContextMenuHolder*)menuHolder 102 - (void)showWithHolder:(ContextMenuHolder*)menuHolder
103 atPoint:(CGPoint)point 103 atPoint:(CGPoint)point
104 inView:(UIView*)view { 104 inView:(UIView*)view {
105 UIAlertController* alert = [UIAlertController 105 UIAlertController* alert = [UIAlertController
106 alertControllerWithTitle:menuHolder.menuTitle 106 alertControllerWithTitle:menuHolder.menuTitle
107 message:nil 107 message:nil
108 preferredStyle:UIAlertControllerStyleActionSheet]; 108 preferredStyle:UIAlertControllerStyleActionSheet];
109 alert.popoverPresentationController.sourceView = view; 109 alert.popoverPresentationController.sourceView = view;
110 alert.popoverPresentationController.sourceRect = 110 alert.popoverPresentationController.sourceRect =
111 CGRectMake(point.x, point.y, 1.0, 1.0); 111 CGRectMake(point.x, point.y, 1.0, 1.0);
112 112
113 // Add the actions. 113 // Add the actions.
114 base::WeakNSObject<CRUAlertController> weakSelf(self); 114 base::WeakNSObject<AlertController> weakSelf(self);
115 [menuHolder.itemTitles enumerateObjectsUsingBlock:^(NSString* itemTitle, 115 [menuHolder.itemTitles enumerateObjectsUsingBlock:^(
116 NSUInteger itemIndex, 116 NSString* itemTitle, NSUInteger itemIndex, BOOL*) {
117 BOOL*) {
118 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) { 117 void (^actionHandler)(UIAlertAction*) = ^(UIAlertAction* action) {
119 [menuHolder performActionAtIndex:itemIndex]; 118 [menuHolder performActionAtIndex:itemIndex];
120 [weakSelf setVisible:NO]; 119 [weakSelf setVisible:NO];
121 }; 120 };
122 [alert addAction:[UIAlertAction actionWithTitle:itemTitle 121 [alert addAction:[UIAlertAction actionWithTitle:itemTitle
123 style:UIAlertActionStyleDefault 122 style:UIAlertActionStyleDefault
124 handler:actionHandler]]; 123 handler:actionHandler]];
125 }]; 124 }];
126 125
127 // Cancel button goes last, to match other browsers. 126 // Cancel button goes last, to match other browsers.
(...skipping 14 matching lines...) Expand all
142 self.visible = YES; 141 self.visible = YES;
143 _alert.reset(alert); 142 _alert.reset(alert);
144 } 143 }
145 144
146 - (void)dismissAnimated:(BOOL)animated 145 - (void)dismissAnimated:(BOOL)animated
147 completionHandler:(ProceduralBlock)completionHandler { 146 completionHandler:(ProceduralBlock)completionHandler {
148 [_alert dismissViewControllerAnimated:animated completion:completionHandler]; 147 [_alert dismissViewControllerAnimated:animated completion:completionHandler];
149 } 148 }
150 149
151 @end 150 @end
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698