| 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/history/tab_history_popup_controller.h" | 5 #import "ios/chrome/browser/ui/history/tab_history_popup_controller.h" |
| 6 | 6 |
| 7 #import <QuartzCore/QuartzCore.h> | 7 #import <QuartzCore/QuartzCore.h> |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/mac/bundle_locations.h" | 10 #include "base/mac/bundle_locations.h" |
| 11 #include "base/strings/sys_string_conversions.h" | 11 #include "base/strings/sys_string_conversions.h" |
| 12 #import "ios/chrome/browser/ui/history/tab_history_view_controller.h" | 12 #import "ios/chrome/browser/ui/history/tab_history_view_controller.h" |
| 13 #import "ios/chrome/browser/ui/popup_menu/popup_menu_view.h" | 13 #import "ios/chrome/browser/ui/popup_menu/popup_menu_view.h" |
| 14 #include "ios/chrome/browser/ui/rtl_geometry.h" | 14 #include "ios/chrome/browser/ui/rtl_geometry.h" |
| 15 #include "ios/chrome/browser/ui/ui_util.h" | 15 #include "ios/chrome/browser/ui/ui_util.h" |
| 16 #import "ios/chrome/common/material_timing.h" | 16 #import "ios/chrome/common/material_timing.h" |
| 17 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" | 17 #import "ios/third_party/material_roboto_font_loader_ios/src/src/MaterialRobotoF
ontLoader.h" |
| 18 #import "ios/web/navigation/crw_session_entry.h" | |
| 19 #include "ios/web/public/navigation_item.h" | 18 #include "ios/web/public/navigation_item.h" |
| 20 #import "ui/gfx/ios/NSString+CrStringDrawing.h" | 19 #import "ui/gfx/ios/NSString+CrStringDrawing.h" |
| 21 #include "ui/gfx/ios/uikit_util.h" | 20 #include "ui/gfx/ios/uikit_util.h" |
| 21 #include "url/gurl.h" |
| 22 | 22 |
| 23 #if !defined(__has_feature) || !__has_feature(objc_arc) | 23 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 24 #error "This file requires ARC support." | 24 #error "This file requires ARC support." |
| 25 #endif | 25 #endif |
| 26 | 26 |
| 27 namespace { | 27 namespace { |
| 28 static const CGFloat kTabHistoryMinWidth = 250.0; | 28 const CGFloat kTabHistoryMinWidth = 250.0; |
| 29 static const CGFloat kTabHistoryMaxWidthLandscapePhone = 350.0; | 29 const CGFloat kTabHistoryMaxWidthLandscapePhone = 350.0; |
| 30 // x coordinate for the textLabel in a default table cell with an image. | 30 // x coordinate for the textLabel in a default table cell with an image. |
| 31 static const CGFloat kCellTextXCoordinate = 60.0; | 31 const CGFloat kCellTextXCoordinate = 60.0; |
| 32 // The corner radius for the popover container view. |
| 33 const CGFloat kPopoverCornerRadius = 2.0; |
| 32 // Inset for the shadows of the contained views. | 34 // Inset for the shadows of the contained views. |
| 33 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { | 35 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { |
| 34 return UIEdgeInsetsMakeDirected(9, 11, 12, 11); | 36 return UIEdgeInsetsMakeDirected(9, 11, 12, 11); |
| 35 } | 37 } |
| 36 | 38 |
| 37 // Layout for popup: shift twelve pixels towards the leading edge. | 39 // Layout for popup: shift twelve pixels towards the leading edge. |
| 38 LayoutOffset kHistoryPopupLeadingOffset = -12; | 40 LayoutOffset kHistoryPopupLeadingOffset = -12; |
| 39 CGFloat kHistoryPopupYOffset = 3; | 41 CGFloat kHistoryPopupYOffset = 3; |
| 40 // Occupation of the Tools Container as a percentage of the parent height. | 42 // Occupation of the Tools Container as a percentage of the parent height. |
| 41 static const CGFloat kHeightPercentage = 0.85; | 43 static const CGFloat kHeightPercentage = 0.85; |
| 42 } // anonymous namespace | 44 } // anonymous namespace |
| 43 | 45 |
| 44 @interface TabHistoryPopupController () { | 46 @interface TabHistoryPopupController () |
| 45 // TableViewController for the table displaying tab history entries. | |
| 46 TabHistoryViewController* tabHistoryTableViewController_; | |
| 47 | 47 |
| 48 // Container view of the history entries table. | 48 // The UITableViewController displaying Tab history items. |
| 49 UIView* tabHistoryTableViewContainer_; | 49 @property(nonatomic, strong) |
| 50 } | 50 TabHistoryViewController* tabHistoryTableViewController; |
| 51 // The container view that displays |tabHistoryTableViewController|. |
| 52 @property(nonatomic, strong) UIView* tabHistoryTableViewContainer; |
| 51 | 53 |
| 52 // Determines the width for the popup depending on the device, orientation, and | 54 // Determines the width for the popup depending on the device, orientation, and |
| 53 // CRWSessionEntrys to display. | 55 // number of NavigationItems to display. |
| 54 - (CGFloat)calculatePopupWidth:(NSArray*)entries; | 56 + (CGFloat)popupWidthForItems:(const web::NavigationItemList)items; |
| 55 | |
| 56 @property(nonatomic, strong) | |
| 57 TabHistoryViewController* tabHistoryTableViewController; | |
| 58 @property(nonatomic, strong) UIView* tabHistoryTableViewContainer; | |
| 59 | 57 |
| 60 @end | 58 @end |
| 61 | 59 |
| 62 @implementation TabHistoryPopupController | 60 @implementation TabHistoryPopupController |
| 63 | 61 |
| 64 @synthesize tabHistoryTableViewController = tabHistoryTableViewController_; | 62 @synthesize tabHistoryTableViewController = _tabHistoryTableViewController; |
| 65 @synthesize tabHistoryTableViewContainer = tabHistoryTableViewContainer_; | 63 @synthesize tabHistoryTableViewContainer = _tabHistoryTableViewContainer; |
| 66 | 64 |
| 67 - (id)initWithOrigin:(CGPoint)origin | 65 - (id)initWithOrigin:(CGPoint)origin |
| 68 parentView:(UIView*)parent | 66 parentView:(UIView*)parent |
| 69 entries:(NSArray*)entries { | 67 items:(const web::NavigationItemList&)items { |
| 70 DCHECK(parent); | 68 DCHECK(parent); |
| 71 self = [super initWithParentView:parent]; | 69 if ((self = [super initWithParentView:parent])) { |
| 72 if (self) { | 70 // Create the table view controller. |
| 73 tabHistoryTableViewController_ = [[TabHistoryViewController alloc] init]; | 71 _tabHistoryTableViewController = |
| 74 [tabHistoryTableViewController_ setSessionEntries:entries]; | 72 [[TabHistoryViewController alloc] initWithItems:items]; |
| 75 | 73 |
| 76 UICollectionView* collectionView = | 74 // Set up the container view. |
| 77 [tabHistoryTableViewController_ collectionView]; | 75 _tabHistoryTableViewContainer = [[UIView alloc] initWithFrame:CGRectZero]; |
| 78 [collectionView setAccessibilityIdentifier:@"Tab History"]; | 76 _tabHistoryTableViewContainer.layer.cornerRadius = kPopoverCornerRadius; |
| 77 _tabHistoryTableViewContainer.layer.masksToBounds = YES; |
| 78 [_tabHistoryTableViewContainer |
| 79 addSubview:_tabHistoryTableViewController.view]; |
| 79 | 80 |
| 80 tabHistoryTableViewContainer_ = [[UIView alloc] initWithFrame:CGRectZero]; | 81 // Calculate the optimal popup size. |
| 81 [tabHistoryTableViewContainer_ layer].cornerRadius = 2; | |
| 82 [tabHistoryTableViewContainer_ layer].masksToBounds = YES; | |
| 83 [tabHistoryTableViewContainer_ addSubview:collectionView]; | |
| 84 | |
| 85 LayoutOffset originOffset = | 82 LayoutOffset originOffset = |
| 86 kHistoryPopupLeadingOffset - TabHistoryPopupMenuInsets().left; | 83 kHistoryPopupLeadingOffset - TabHistoryPopupMenuInsets().left; |
| 87 CGPoint newOrigin = CGPointLayoutOffset(origin, originOffset); | 84 CGPoint newOrigin = CGPointLayoutOffset(origin, originOffset); |
| 88 newOrigin.y += kHistoryPopupYOffset; | 85 newOrigin.y += kHistoryPopupYOffset; |
| 89 | |
| 90 CGFloat availableHeight = | 86 CGFloat availableHeight = |
| 91 (CGRectGetHeight([parent bounds]) - origin.y) * kHeightPercentage; | 87 (CGRectGetHeight([parent bounds]) - origin.y) * kHeightPercentage; |
| 92 CGFloat optimalHeight = | 88 CGFloat optimalHeight = |
| 93 [tabHistoryTableViewController_ optimalHeight:availableHeight]; | 89 [_tabHistoryTableViewController optimalHeight:availableHeight]; |
| 94 CGFloat popupWidth = [self calculatePopupWidth:entries]; | 90 CGFloat popupWidth = [[self class] popupWidthForItems:items]; |
| 95 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) | 91 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) |
| 96 atOrigin:newOrigin]; | 92 atOrigin:newOrigin]; |
| 97 | |
| 98 CGRect bounds = [[self popupContainer] bounds]; | |
| 99 CGRect frame = UIEdgeInsetsInsetRect(bounds, TabHistoryPopupMenuInsets()); | |
| 100 | |
| 101 [tabHistoryTableViewContainer_ setFrame:frame]; | |
| 102 [collectionView setFrame:[tabHistoryTableViewContainer_ bounds]]; | |
| 103 | |
| 104 [[self popupContainer] addSubview:tabHistoryTableViewContainer_]; | |
| 105 CGRect containerFrame = [[self popupContainer] frame]; | |
| 106 CGPoint destination = CGPointMake(CGRectGetLeadingEdge(containerFrame), | |
| 107 CGRectGetMinY(containerFrame)); | |
| 108 [self fadeInPopupFromSource:origin toDestination:destination]; | |
| 109 } | 93 } |
| 110 return self; | 94 return self; |
| 111 } | 95 } |
| 112 | 96 |
| 97 - (void)dealloc { |
| 98 [_tabHistoryTableViewContainer removeFromSuperview]; |
| 99 } |
| 100 |
| 101 #pragma mark - PopupMenuController |
| 102 |
| 113 - (void)fadeInPopupFromSource:(CGPoint)source | 103 - (void)fadeInPopupFromSource:(CGPoint)source |
| 114 toDestination:(CGPoint)destination { | 104 toDestination:(CGPoint)destination { |
| 115 [tabHistoryTableViewContainer_ setAlpha:0]; | 105 // Add the container view to the popup view and resize. |
| 106 if (!_tabHistoryTableViewContainer.superview) |
| 107 [self.popupContainer addSubview:_tabHistoryTableViewContainer]; |
| 108 _tabHistoryTableViewContainer.frame = UIEdgeInsetsInsetRect( |
| 109 self.popupContainer.bounds, TabHistoryPopupMenuInsets()); |
| 110 _tabHistoryTableViewController.view.frame = |
| 111 _tabHistoryTableViewContainer.bounds; |
| 112 |
| 113 // Set up the animation. |
| 114 [_tabHistoryTableViewContainer setAlpha:0]; |
| 116 [UIView animateWithDuration:ios::material::kDuration1 | 115 [UIView animateWithDuration:ios::material::kDuration1 |
| 117 animations:^{ | 116 animations:^{ |
| 118 [tabHistoryTableViewContainer_ setAlpha:1]; | 117 [_tabHistoryTableViewContainer setAlpha:1]; |
| 119 }]; | 118 }]; |
| 120 [super fadeInPopupFromSource:source toDestination:destination]; | 119 [super fadeInPopupFromSource:source toDestination:destination]; |
| 121 } | 120 } |
| 122 | 121 |
| 123 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { | 122 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { |
| 124 [tabHistoryTableViewContainer_ setAlpha:0]; | 123 [_tabHistoryTableViewContainer setAlpha:0]; |
| 125 [super dismissAnimatedWithCompletion:completion]; | 124 [super dismissAnimatedWithCompletion:completion]; |
| 126 } | 125 } |
| 127 | 126 |
| 128 - (CGFloat)calculatePopupWidth:(NSArray*)entries { | 127 #pragma mark - |
| 128 |
| 129 + (CGFloat)popupWidthForItems:(const web::NavigationItemList)items { |
| 129 CGFloat maxWidth; | 130 CGFloat maxWidth; |
| 130 | 131 |
| 131 // Determine the maximum width for the device and orientation. | 132 // Determine the maximum width for the device and orientation. |
| 132 | 133 |
| 133 if (!IsIPadIdiom()) { | 134 if (!IsIPadIdiom()) { |
| 134 UIInterfaceOrientation orientation = | 135 UIInterfaceOrientation orientation = |
| 135 [[UIApplication sharedApplication] statusBarOrientation]; | 136 [[UIApplication sharedApplication] statusBarOrientation]; |
| 136 // Phone in portrait has constant width. | 137 // Phone in portrait has constant width. |
| 137 if (UIInterfaceOrientationIsPortrait(orientation)) | 138 if (UIInterfaceOrientationIsPortrait(orientation)) |
| 138 return kTabHistoryMinWidth; | 139 return kTabHistoryMinWidth; |
| 139 maxWidth = kTabHistoryMaxWidthLandscapePhone; | 140 maxWidth = kTabHistoryMaxWidthLandscapePhone; |
| 140 } else { | 141 } else { |
| 141 // On iPad use 85% of the available width. | 142 // On iPad use 85% of the available width. |
| 142 maxWidth = ui::AlignValueToUpperPixel( | 143 maxWidth = ui::AlignValueToUpperPixel( |
| 143 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); | 144 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); |
| 144 } | 145 } |
| 145 // Increase the width to fit the text to display but don't exceed maxWidth. | 146 // Increase the width to fit the text to display but don't exceed maxWidth. |
| 146 CGFloat cellWidth = kTabHistoryMinWidth; | 147 CGFloat cellWidth = kTabHistoryMinWidth; |
| 147 UIFont* font = [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16]; | 148 UIFont* font = [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16]; |
| 148 for (CRWSessionEntry* sessionEntry in entries) { | 149 for (web::NavigationItem* item : items) { |
| 149 web::NavigationItem* item = sessionEntry.navigationItem; | |
| 150 // TODO(rohitrao): Can this be replaced with GetTitleForDisplay()? | 150 // TODO(rohitrao): Can this be replaced with GetTitleForDisplay()? |
| 151 NSString* cellText = item->GetTitle().empty() | 151 NSString* cellText = item->GetTitle().empty() |
| 152 ? base::SysUTF8ToNSString(item->GetURL().spec()) | 152 ? base::SysUTF8ToNSString(item->GetURL().spec()) |
| 153 : base::SysUTF16ToNSString(item->GetTitle()); | 153 : base::SysUTF16ToNSString(item->GetTitle()); |
| 154 CGFloat contentWidth = [cellText cr_pixelAlignedSizeWithFont:font].width + | 154 CGFloat contentWidth = [cellText cr_pixelAlignedSizeWithFont:font].width + |
| 155 kCellTextXCoordinate; | 155 kCellTextXCoordinate; |
| 156 | 156 |
| 157 // If contentWidth is larger than maxWidth, return maxWidth instead of | 157 // If contentWidth is larger than maxWidth, return maxWidth instead of |
| 158 // checking the rest of the entries. | 158 // checking the rest of the items. |
| 159 if (contentWidth > maxWidth) | 159 if (contentWidth > maxWidth) |
| 160 return maxWidth; | 160 return maxWidth; |
| 161 if (contentWidth > cellWidth) | 161 if (contentWidth > cellWidth) |
| 162 cellWidth = contentWidth; | 162 cellWidth = contentWidth; |
| 163 } | 163 } |
| 164 return cellWidth; | 164 return cellWidth; |
| 165 } | 165 } |
| 166 | 166 |
| 167 - (void)dealloc { | |
| 168 [tabHistoryTableViewContainer_ removeFromSuperview]; | |
| 169 } | |
| 170 | |
| 171 @end | 167 @end |
| OLD | NEW |