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

Side by Side Diff: ios/chrome/browser/ui/history/tab_history_popup_controller.mm

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

Powered by Google App Engine
This is Rietveld 408576698