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" |
18 #include "ios/web/public/navigation_item.h" | 19 #include "ios/web/public/navigation_item.h" |
19 #import "ui/gfx/ios/NSString+CrStringDrawing.h" | 20 #import "ui/gfx/ios/NSString+CrStringDrawing.h" |
20 #include "ui/gfx/ios/uikit_util.h" | 21 #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 const CGFloat kTabHistoryMinWidth = 250.0; | 28 static const CGFloat kTabHistoryMinWidth = 250.0; |
29 const CGFloat kTabHistoryMaxWidthLandscapePhone = 350.0; | 29 static 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 const CGFloat kCellTextXCoordinate = 60.0; | 31 static const CGFloat kCellTextXCoordinate = 60.0; |
32 // The corner radius for the popover container view. | |
33 const CGFloat kPopoverCornerRadius = 2.0; | |
34 // Inset for the shadows of the contained views. | 32 // Inset for the shadows of the contained views. |
35 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { | 33 NS_INLINE UIEdgeInsets TabHistoryPopupMenuInsets() { |
36 return UIEdgeInsetsMakeDirected(9, 11, 12, 11); | 34 return UIEdgeInsetsMakeDirected(9, 11, 12, 11); |
37 } | 35 } |
38 | 36 |
39 // Layout for popup: shift twelve pixels towards the leading edge. | 37 // Layout for popup: shift twelve pixels towards the leading edge. |
40 LayoutOffset kHistoryPopupLeadingOffset = -12; | 38 LayoutOffset kHistoryPopupLeadingOffset = -12; |
41 CGFloat kHistoryPopupYOffset = 3; | 39 CGFloat kHistoryPopupYOffset = 3; |
42 // Occupation of the Tools Container as a percentage of the parent height. | 40 // Occupation of the Tools Container as a percentage of the parent height. |
43 static const CGFloat kHeightPercentage = 0.85; | 41 static const CGFloat kHeightPercentage = 0.85; |
44 } // anonymous namespace | 42 } // anonymous namespace |
45 | 43 |
46 @interface TabHistoryPopupController () | 44 @interface TabHistoryPopupController () { |
| 45 // TableViewController for the table displaying tab history entries. |
| 46 TabHistoryViewController* tabHistoryTableViewController_; |
47 | 47 |
48 // The UITableViewController displaying Tab history items. | 48 // Container view of the history entries table. |
| 49 UIView* tabHistoryTableViewContainer_; |
| 50 } |
| 51 |
| 52 // Determines the width for the popup depending on the device, orientation, and |
| 53 // CRWSessionEntrys to display. |
| 54 - (CGFloat)calculatePopupWidth:(NSArray*)entries; |
| 55 |
49 @property(nonatomic, strong) | 56 @property(nonatomic, strong) |
50 TabHistoryViewController* tabHistoryTableViewController; | 57 TabHistoryViewController* tabHistoryTableViewController; |
51 // The container view that displays |tabHistoryTableViewController|. | |
52 @property(nonatomic, strong) UIView* tabHistoryTableViewContainer; | 58 @property(nonatomic, strong) UIView* tabHistoryTableViewContainer; |
53 | 59 |
54 // Determines the width for the popup depending on the device, orientation, and | |
55 // number of NavigationItems to display. | |
56 + (CGFloat)popupWidthForItems:(const web::NavigationItemList)items; | |
57 | |
58 @end | 60 @end |
59 | 61 |
60 @implementation TabHistoryPopupController | 62 @implementation TabHistoryPopupController |
61 | 63 |
62 @synthesize tabHistoryTableViewController = _tabHistoryTableViewController; | 64 @synthesize tabHistoryTableViewController = tabHistoryTableViewController_; |
63 @synthesize tabHistoryTableViewContainer = _tabHistoryTableViewContainer; | 65 @synthesize tabHistoryTableViewContainer = tabHistoryTableViewContainer_; |
64 | 66 |
65 - (id)initWithOrigin:(CGPoint)origin | 67 - (id)initWithOrigin:(CGPoint)origin |
66 parentView:(UIView*)parent | 68 parentView:(UIView*)parent |
67 items:(const web::NavigationItemList&)items { | 69 entries:(NSArray*)entries { |
68 DCHECK(parent); | 70 DCHECK(parent); |
69 if ((self = [super initWithParentView:parent])) { | 71 self = [super initWithParentView:parent]; |
70 // Create the table view controller. | 72 if (self) { |
71 _tabHistoryTableViewController = | 73 tabHistoryTableViewController_ = [[TabHistoryViewController alloc] init]; |
72 [[TabHistoryViewController alloc] initWithItems:items]; | 74 [tabHistoryTableViewController_ setSessionEntries:entries]; |
73 | 75 |
74 // Set up the container view. | 76 UICollectionView* collectionView = |
75 _tabHistoryTableViewContainer = [[UIView alloc] initWithFrame:CGRectZero]; | 77 [tabHistoryTableViewController_ collectionView]; |
76 _tabHistoryTableViewContainer.layer.cornerRadius = kPopoverCornerRadius; | 78 [collectionView setAccessibilityIdentifier:@"Tab History"]; |
77 _tabHistoryTableViewContainer.layer.masksToBounds = YES; | |
78 [_tabHistoryTableViewContainer | |
79 addSubview:_tabHistoryTableViewController.view]; | |
80 | 79 |
81 // Calculate the optimal popup size. | 80 tabHistoryTableViewContainer_ = [[UIView alloc] initWithFrame:CGRectZero]; |
| 81 [tabHistoryTableViewContainer_ layer].cornerRadius = 2; |
| 82 [tabHistoryTableViewContainer_ layer].masksToBounds = YES; |
| 83 [tabHistoryTableViewContainer_ addSubview:collectionView]; |
| 84 |
82 LayoutOffset originOffset = | 85 LayoutOffset originOffset = |
83 kHistoryPopupLeadingOffset - TabHistoryPopupMenuInsets().left; | 86 kHistoryPopupLeadingOffset - TabHistoryPopupMenuInsets().left; |
84 CGPoint newOrigin = CGPointLayoutOffset(origin, originOffset); | 87 CGPoint newOrigin = CGPointLayoutOffset(origin, originOffset); |
85 newOrigin.y += kHistoryPopupYOffset; | 88 newOrigin.y += kHistoryPopupYOffset; |
| 89 |
86 CGFloat availableHeight = | 90 CGFloat availableHeight = |
87 (CGRectGetHeight([parent bounds]) - origin.y) * kHeightPercentage; | 91 (CGRectGetHeight([parent bounds]) - origin.y) * kHeightPercentage; |
88 CGFloat optimalHeight = | 92 CGFloat optimalHeight = |
89 [_tabHistoryTableViewController optimalHeight:availableHeight]; | 93 [tabHistoryTableViewController_ optimalHeight:availableHeight]; |
90 CGFloat popupWidth = [[self class] popupWidthForItems:items]; | 94 CGFloat popupWidth = [self calculatePopupWidth:entries]; |
91 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) | 95 [self setOptimalSize:CGSizeMake(popupWidth, optimalHeight) |
92 atOrigin:newOrigin]; | 96 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]; |
93 } | 109 } |
94 return self; | 110 return self; |
95 } | 111 } |
96 | 112 |
97 - (void)dealloc { | |
98 [_tabHistoryTableViewContainer removeFromSuperview]; | |
99 } | |
100 | |
101 #pragma mark - PopupMenuController | |
102 | |
103 - (void)fadeInPopupFromSource:(CGPoint)source | 113 - (void)fadeInPopupFromSource:(CGPoint)source |
104 toDestination:(CGPoint)destination { | 114 toDestination:(CGPoint)destination { |
105 // Add the container view to the popup view and resize. | 115 [tabHistoryTableViewContainer_ setAlpha:0]; |
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]; | |
115 [UIView animateWithDuration:ios::material::kDuration1 | 116 [UIView animateWithDuration:ios::material::kDuration1 |
116 animations:^{ | 117 animations:^{ |
117 [_tabHistoryTableViewContainer setAlpha:1]; | 118 [tabHistoryTableViewContainer_ setAlpha:1]; |
118 }]; | 119 }]; |
119 [super fadeInPopupFromSource:source toDestination:destination]; | 120 [super fadeInPopupFromSource:source toDestination:destination]; |
120 } | 121 } |
121 | 122 |
122 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { | 123 - (void)dismissAnimatedWithCompletion:(void (^)(void))completion { |
123 [_tabHistoryTableViewContainer setAlpha:0]; | 124 [tabHistoryTableViewContainer_ setAlpha:0]; |
124 [super dismissAnimatedWithCompletion:completion]; | 125 [super dismissAnimatedWithCompletion:completion]; |
125 } | 126 } |
126 | 127 |
127 #pragma mark - | 128 - (CGFloat)calculatePopupWidth:(NSArray*)entries { |
128 | |
129 + (CGFloat)popupWidthForItems:(const web::NavigationItemList)items { | |
130 CGFloat maxWidth; | 129 CGFloat maxWidth; |
131 | 130 |
132 // Determine the maximum width for the device and orientation. | 131 // Determine the maximum width for the device and orientation. |
133 | 132 |
134 if (!IsIPadIdiom()) { | 133 if (!IsIPadIdiom()) { |
135 UIInterfaceOrientation orientation = | 134 UIInterfaceOrientation orientation = |
136 [[UIApplication sharedApplication] statusBarOrientation]; | 135 [[UIApplication sharedApplication] statusBarOrientation]; |
137 // Phone in portrait has constant width. | 136 // Phone in portrait has constant width. |
138 if (UIInterfaceOrientationIsPortrait(orientation)) | 137 if (UIInterfaceOrientationIsPortrait(orientation)) |
139 return kTabHistoryMinWidth; | 138 return kTabHistoryMinWidth; |
140 maxWidth = kTabHistoryMaxWidthLandscapePhone; | 139 maxWidth = kTabHistoryMaxWidthLandscapePhone; |
141 } else { | 140 } else { |
142 // On iPad use 85% of the available width. | 141 // On iPad use 85% of the available width. |
143 maxWidth = ui::AlignValueToUpperPixel( | 142 maxWidth = ui::AlignValueToUpperPixel( |
144 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); | 143 [UIApplication sharedApplication].keyWindow.frame.size.width * .85); |
145 } | 144 } |
146 // Increase the width to fit the text to display but don't exceed maxWidth. | 145 // Increase the width to fit the text to display but don't exceed maxWidth. |
147 CGFloat cellWidth = kTabHistoryMinWidth; | 146 CGFloat cellWidth = kTabHistoryMinWidth; |
148 UIFont* font = [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16]; | 147 UIFont* font = [[MDFRobotoFontLoader sharedInstance] regularFontOfSize:16]; |
149 for (web::NavigationItem* item : items) { | 148 for (CRWSessionEntry* sessionEntry in entries) { |
| 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 items. | 158 // checking the rest of the entries. |
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 |
167 @end | 171 @end |
OLD | NEW |