Chromium Code Reviews| Index: ui/base/ios/cru_context_menu_controller.mm |
| diff --git a/ui/base/ios/cru_context_menu_controller.mm b/ui/base/ios/cru_context_menu_controller.mm |
| index f615c017b0619b0ac35bef929bf7d3609fb706bf..71d41ab68c7040ec726b226bd4bfe0f8eda0f19d 100644 |
| --- a/ui/base/ios/cru_context_menu_controller.mm |
| +++ b/ui/base/ios/cru_context_menu_controller.mm |
| @@ -6,34 +6,14 @@ |
| #include <algorithm> |
| -#include "base/ios/ios_util.h" |
| #include "base/ios/weak_nsobject.h" |
| #include "base/logging.h" |
| #import "base/mac/scoped_nsobject.h" |
| #include "ui/base/device_form_factor.h" |
| #import "ui/base/ios/cru_context_menu_holder.h" |
| #include "ui/base/l10n/l10n_util.h" |
| -#import "ui/gfx/ios/NSString+CrStringDrawing.h" |
| #include "ui/strings/grit/ui_strings.h" |
| -namespace { |
| - |
| -// Returns the screen's height in points. |
| -CGFloat GetScreenHeight() { |
| - DCHECK(!base::ios::IsRunningOnIOS8OrLater()); |
| - switch ([[UIApplication sharedApplication] statusBarOrientation]) { |
| - case UIInterfaceOrientationLandscapeLeft: |
| - case UIInterfaceOrientationLandscapeRight: |
| - return CGRectGetWidth([[UIScreen mainScreen] bounds]); |
| - case UIInterfaceOrientationPortraitUpsideDown: |
| - case UIInterfaceOrientationPortrait: |
| - case UIInterfaceOrientationUnknown: |
| - return CGRectGetHeight([[UIScreen mainScreen] bounds]); |
| - } |
| -} |
| - |
| -} // namespace |
| - |
| // Abstracts system implementation of popovers and action sheets. |
| @protocol CRUContextMenuControllerImpl<NSObject> |
| @@ -46,18 +26,7 @@ CGFloat GetScreenHeight() { |
| inView:(UIView*)view; |
| @end |
| -// Backs up CRUContextMenuController on iOS 7 by using UIActionSheet. |
| -@interface CRUActionSheetController |
| - : NSObject<CRUContextMenuControllerImpl, UIActionSheetDelegate> { |
| - // The action sheet used to display the UI. |
| - base::scoped_nsobject<UIActionSheet> _sheet; |
| - // Holds all the titles and actions for the menu. |
| - base::scoped_nsobject<CRUContextMenuHolder> _menuHolder; |
| -} |
| -@end |
| - |
| -// Backs up CRUContextMenuController on iOS 8 and higher by using |
| -// UIAlertController. |
| +// Backs up CRUContextMenuController by using UIAlertController. |
| @interface CRUAlertController : NSObject<CRUContextMenuControllerImpl> |
|
Eugene But (OOO till 7-30)
2016/02/17 15:06:24
There is no need for using a bridge pattern now an
|
| // Redefined to readwrite. |
| @property(nonatomic, readwrite, getter=isVisible) BOOL visible; |
| @@ -76,11 +45,7 @@ CGFloat GetScreenHeight() { |
| - (instancetype)init { |
| self = [super init]; |
| if (self) { |
| - if (base::ios::IsRunningOnIOS8OrLater()) { |
| - _impl.reset([[CRUAlertController alloc] init]); |
| - } else { |
| - _impl.reset([[CRUActionSheetController alloc] init]); |
| - } |
| + _impl.reset([[CRUAlertController alloc] init]); |
| } |
| return self; |
| } |
| @@ -98,132 +63,6 @@ CGFloat GetScreenHeight() { |
| @end |
| -#pragma mark - iOS 7 |
| - |
| -@implementation CRUActionSheetController |
| -@synthesize visible = _visible; |
| - |
| -- (void)dealloc { |
| - if (_visible) { |
| - // Context menu must be dismissed explicitly if it is still visible. |
| - NSUInteger cancelButtonIndex = [_menuHolder itemCount]; |
| - [_sheet dismissWithClickedButtonIndex:cancelButtonIndex animated:NO]; |
| - } |
| - [super dealloc]; |
| -} |
| - |
| -- (void)showWithHolder:(CRUContextMenuHolder*)menuHolder |
| - atPoint:(CGPoint)point |
| - inView:(UIView*)view { |
| - // If the content of UIActionSheet does not fit the screen then scrollbars |
| - // are added to the menu items area. If that's the case, elide the title to |
| - // avoid having scrollbars for menu items. |
| - CGSize spaceAvailableForTitle = |
| - [self sizeForTitleThatFitsMenuWithHolder:menuHolder |
| - atPoint:point |
| - inView:view]; |
| - NSString* menuTitle = menuHolder.menuTitle; |
| - if (menuTitle) { |
| - // Show at least one line of text, even if that means the action sheet's |
| - // items will need to scroll. |
| - const CGFloat kMinimumVerticalSpace = 21; |
| - spaceAvailableForTitle.height = |
| - std::max(kMinimumVerticalSpace, spaceAvailableForTitle.height); |
| - menuTitle = [menuTitle cr_stringByElidingToFitSize:spaceAvailableForTitle]; |
| - } |
| - |
| - // Present UIActionSheet. |
| - _sheet.reset( |
| - [self newActionSheetWithHolder:menuHolder title:menuTitle delegate:self]); |
| - [_sheet setCancelButtonIndex:menuHolder.itemCount]; |
| - [_sheet showFromRect:CGRectMake(point.x, point.y, 1.0, 1.0) |
| - inView:view |
| - animated:YES]; |
| - |
| - _menuHolder.reset([menuHolder retain]); |
| - _visible = YES; |
| -} |
| - |
| -#pragma mark Implementation |
| - |
| -// Returns an approximation of the free space available for the title of an |
| -// actionSheet filled with |menu| shown in |view| at |point|. |
| -- (CGSize)sizeForTitleThatFitsMenuWithHolder:(CRUContextMenuHolder*)menuHolder |
| - atPoint:(CGPoint)point |
| - inView:(UIView*)view { |
| - // Create a dummy UIActionSheet. |
| - base::scoped_nsobject<UIActionSheet> dummySheet( |
| - [self newActionSheetWithHolder:menuHolder title:nil delegate:nil]); |
| - // Temporarily add the dummy UIActionSheet to |view|. |
| - [dummySheet showFromRect:CGRectMake(point.x, point.y, 1.0, 1.0) |
| - inView:view |
| - animated:NO]; |
| - // On iPad the actionsheet is positioned under or over |point| (as opposed |
| - // to next to it) when the user clicks within approximately 200 points of |
| - // respectively the top or bottom edge. This reduces the amount of vertical |
| - // space available for the title, hence the large padding on ipad. |
| - const CGFloat kPaddingiPad = 200; |
| - const CGFloat kPaddingiPhone = 20; |
| - BOOL isIPad = ui::GetDeviceFormFactor() == ui::DEVICE_FORM_FACTOR_TABLET; |
| - const CGFloat padding = isIPad ? kPaddingiPad : kPaddingiPhone; |
| - // A title uses the full width of the actionsheet and all the vertical |
| - // space on the screen. |
| - CGSize result = CGSizeMake( |
| - CGRectGetWidth([dummySheet frame]), |
| - GetScreenHeight() - CGRectGetHeight([dummySheet frame]) - padding); |
| - [dummySheet dismissWithClickedButtonIndex:0 animated:NO]; |
| - return result; |
| -} |
| - |
| -// Returns an UIActionSheet. Callers responsible for releasing returned object. |
| -- (UIActionSheet*)newActionSheetWithHolder:(CRUContextMenuHolder*)menuHolder |
| - title:(NSString*)title |
| - delegate:(id<UIActionSheetDelegate>)delegate { |
| - UIActionSheet* sheet = [[UIActionSheet alloc] initWithTitle:title |
| - delegate:delegate |
| - cancelButtonTitle:nil |
| - destructiveButtonTitle:nil |
| - otherButtonTitles:nil]; |
| - |
| - for (NSString* itemTitle in menuHolder.itemTitles) { |
| - [sheet addButtonWithTitle:itemTitle]; |
| - } |
| - [sheet addButtonWithTitle:l10n_util::GetNSString(IDS_APP_CANCEL)]; |
| - return sheet; |
| -} |
| - |
| -#pragma mark UIActionSheetDelegate |
| - |
| -// Called when the action sheet is dismissed in the modal context menu sheet. |
| -// There is no way to dismiss the sheet without going through this method. Note |
| -// that on iPad this method is called with the index of an nonexistent cancel |
| -// button when the user taps outside the sheet. |
| -- (void)actionSheet:(UIActionSheet*)actionSheet |
| - didDismissWithButtonIndex:(NSInteger)buttonIndex { |
| - NSUInteger unsignedButtonIndex = buttonIndex; |
| - // Assumes "cancel" button is last in order. |
| - if (unsignedButtonIndex < [_menuHolder itemCount]) |
| - [_menuHolder performActionAtIndex:unsignedButtonIndex]; |
| - _menuHolder.reset(); |
| - _visible = NO; |
| -} |
| - |
| -// Called when the user chooses a button in the modal context menu sheet. Note |
| -// that on iPad this method is called with the index of an nonexistent cancel |
| -// button when the user taps outside the sheet. |
| -- (void)actionSheet:(UIActionSheet*)actionSheet |
| - clickedButtonAtIndex:(NSInteger)buttonIndex { |
| - // Some use cases (e.g. opening a new tab on handset) should not wait for the |
| - // action sheet to animate away before executing the action. |
| - if ([_menuHolder shouldDismissImmediatelyOnClickedAtIndex:buttonIndex]) { |
| - [_sheet dismissWithClickedButtonIndex:buttonIndex animated:NO]; |
| - } |
| -} |
| - |
| -@end |
| - |
| -#pragma mark - iOS8 and higher |
| - |
| @implementation CRUAlertController |
| @synthesize visible = _visible; |