OLD | NEW |
| (Empty) |
1 // Copyright 2016 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 // ====== New Architecture ===== | |
6 // = This code is only used in the new iOS Chrome architecture. = | |
7 // ============================================================================ | |
8 | |
9 #import "ios/chrome/browser/ui/tab_grid/tab_grid_view_controller.h" | |
10 | |
11 #include "base/mac/foundation_util.h" | |
12 #import "ios/chrome/browser/ui/actions/settings_actions.h" | |
13 #import "ios/chrome/browser/ui/actions/tab_grid_actions.h" | |
14 #import "ios/chrome/browser/ui/tab_grid/tab_grid_tab_cell.h" | |
15 #import "ios/chrome/browser/ui/commands/settings_commands.h" | |
16 #import "ios/chrome/browser/ui/commands/tab_commands.h" | |
17 #import "ios/chrome/browser/ui/commands/tab_grid_commands.h" | |
18 | |
19 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
20 #error "This file requires ARC support." | |
21 #endif | |
22 | |
23 namespace { | |
24 NSString* const kTabGridCellIdentifier = @"tabGridCell"; | |
25 const CGFloat kSpace = 20; | |
26 const CGFloat kTabSize = 150; | |
27 } | |
28 | |
29 @interface TabGridViewController ()<SettingsActions, | |
30 TabGridActions, | |
31 UICollectionViewDataSource, | |
32 UICollectionViewDelegate> | |
33 @property(nonatomic, weak) UICollectionView* grid; | |
34 @end | |
35 | |
36 @implementation TabGridViewController | |
37 | |
38 @synthesize dataSource = _dataSource; | |
39 @synthesize settingsCommandHandler = _settingsCommandHandler; | |
40 @synthesize tabGridCommandHandler = _tabGridCommandHandler; | |
41 @synthesize tabCommandHandler = _tabCommandHandler; | |
42 @synthesize grid = _grid; | |
43 | |
44 - (void)viewDidLoad { | |
45 // Placeholder dark grey stripe at the top of the view. | |
46 UIView* stripe = [[UIView alloc] initWithFrame:CGRectZero]; | |
47 stripe.translatesAutoresizingMaskIntoConstraints = NO; | |
48 stripe.backgroundColor = [UIColor darkGrayColor]; | |
49 [self.view addSubview:stripe]; | |
50 | |
51 // Placeholder settings button at in the stripe. | |
52 UIButton* settings = [UIButton buttonWithType:UIButtonTypeSystem]; | |
53 settings.translatesAutoresizingMaskIntoConstraints = NO; | |
54 [settings setTitle:@"⚙" forState:UIControlStateNormal]; | |
55 settings.titleLabel.font = [UIFont systemFontOfSize:24.0]; | |
56 [settings addTarget:nil | |
57 action:@selector(showSettings:) | |
58 forControlEvents:UIControlEventTouchUpInside]; | |
59 [stripe addSubview:settings]; | |
60 | |
61 [NSLayoutConstraint activateConstraints:@[ | |
62 [stripe.heightAnchor constraintEqualToConstant:64.0], | |
63 [stripe.widthAnchor constraintEqualToAnchor:self.view.widthAnchor], | |
64 [stripe.topAnchor constraintEqualToAnchor:self.view.topAnchor], | |
65 [stripe.centerXAnchor constraintEqualToAnchor:self.view.centerXAnchor], | |
66 [settings.leadingAnchor | |
67 constraintEqualToAnchor:stripe.layoutMarginsGuide.leadingAnchor], | |
68 [settings.centerYAnchor constraintEqualToAnchor:stripe.centerYAnchor] | |
69 ]]; | |
70 | |
71 UICollectionViewFlowLayout* layout = | |
72 [[UICollectionViewFlowLayout alloc] init]; | |
73 layout.minimumLineSpacing = kSpace; | |
74 layout.minimumInteritemSpacing = kSpace; | |
75 layout.sectionInset = UIEdgeInsetsMake(kSpace, kSpace, kSpace, kSpace); | |
76 layout.itemSize = CGSizeMake(kTabSize, kTabSize); | |
77 | |
78 UICollectionView* grid = [[UICollectionView alloc] initWithFrame:CGRectZero | |
79 collectionViewLayout:layout]; | |
80 grid.translatesAutoresizingMaskIntoConstraints = NO; | |
81 grid.backgroundColor = [UIColor blackColor]; | |
82 | |
83 [self.view addSubview:grid]; | |
84 self.grid = grid; | |
85 self.grid.dataSource = self; | |
86 self.grid.delegate = self; | |
87 [self.grid registerClass:[TabGridTabCell class] | |
88 forCellWithReuseIdentifier:kTabGridCellIdentifier]; | |
89 | |
90 [NSLayoutConstraint activateConstraints:@[ | |
91 [self.grid.topAnchor constraintEqualToAnchor:stripe.bottomAnchor], | |
92 [self.grid.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | |
93 [self.grid.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | |
94 [self.grid.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | |
95 ]]; | |
96 } | |
97 | |
98 - (void)viewWillAppear:(BOOL)animated { | |
99 [self.grid reloadData]; | |
100 } | |
101 | |
102 #pragma mark - UICollectionViewDataSource methods | |
103 | |
104 - (NSInteger)numberOfSectionsInCollectionView: | |
105 (UICollectionView*)collectionView { | |
106 return 1; | |
107 } | |
108 | |
109 - (NSInteger)collectionView:(UICollectionView*)collectionView | |
110 numberOfItemsInSection:(NSInteger)section { | |
111 return [self.dataSource numberOfTabsInTabGrid]; | |
112 } | |
113 | |
114 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | |
115 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { | |
116 TabGridTabCell* cell = | |
117 base::mac::ObjCCastStrict<TabGridTabCell>([collectionView | |
118 dequeueReusableCellWithReuseIdentifier:kTabGridCellIdentifier | |
119 forIndexPath:indexPath]); | |
120 cell.contentView.backgroundColor = [UIColor purpleColor]; | |
121 cell.selected = YES; | |
122 cell.label.text = [self.dataSource titleAtIndex:indexPath.item]; | |
123 return cell; | |
124 } | |
125 | |
126 #pragma mark - UICollectionViewDelegate methods | |
127 | |
128 - (void)collectionView:(UICollectionView*)collectionView | |
129 didSelectItemAtIndexPath:(NSIndexPath*)indexPath { | |
130 [self.tabCommandHandler showTabAtIndexPath:indexPath]; | |
131 } | |
132 | |
133 #pragma mark - ZoomTransitionDelegate methods | |
134 | |
135 - (CGRect)rectForZoomWithKey:(NSObject*)key inView:(UIView*)view { | |
136 NSIndexPath* cellPath = base::mac::ObjCCastStrict<NSIndexPath>(key); | |
137 if (!key) | |
138 return CGRectNull; | |
139 UICollectionViewCell* cell = [self.grid cellForItemAtIndexPath:cellPath]; | |
140 return [view convertRect:cell.bounds fromView:cell]; | |
141 } | |
142 | |
143 #pragma mark - SettingsActions | |
144 | |
145 - (void)showSettings:(id)sender { | |
146 [self.settingsCommandHandler showSettings]; | |
147 } | |
148 | |
149 #pragma mark - TabGridActions | |
150 | |
151 - (void)showTabGrid:(id)sender { | |
152 [self.tabGridCommandHandler showTabGrid]; | |
153 } | |
154 | |
155 @end | |
OLD | NEW |