OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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/clean/chrome/browser/ui/tab_collection/tab_collection_view_controll er.h" | 5 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_view_controll er.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/mac/foundation_util.h" | 8 #include "base/mac/foundation_util.h" |
9 #include "base/strings/sys_string_conversions.h" | 9 #include "base/strings/sys_string_conversions.h" |
10 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" | 10 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_item.h" |
11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_tab_cell.h" | 11 #import "ios/clean/chrome/browser/ui/tab_collection/tab_collection_tab_cell.h" |
12 | 12 |
13 #if !defined(__has_feature) || !__has_feature(objc_arc) | 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
14 #error "This file requires ARC support." | 14 #error "This file requires ARC support." |
15 #endif | 15 #endif |
16 | 16 |
17 @interface TabCollectionViewController ()<UICollectionViewDelegate, | 17 @interface TabCollectionViewController ()<UICollectionViewDelegate, |
18 SessionCellDelegate> | 18 SessionCellDelegate> |
19 @property(nonatomic, readwrite) UICollectionView* tabs; | 19 @property(nonatomic, readwrite) UICollectionView* tabs; |
20 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; | 20 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; |
21 @property(nonatomic, assign) int selectedIndex; | |
22 @end | 21 @end |
23 | 22 |
24 @implementation TabCollectionViewController | 23 @implementation TabCollectionViewController |
25 @synthesize tabs = _tabs; | 24 @synthesize tabs = _tabs; |
26 @synthesize items = _items; | 25 @synthesize items = _items; |
27 @synthesize selectedIndex = _selectedIndex; | 26 @synthesize selectedIndex = _selectedIndex; |
28 | 27 |
29 #pragma mark - UIViewController | 28 #pragma mark - UIViewController |
30 | 29 |
31 - (void)viewDidLoad { | 30 - (void)viewDidLoad { |
(...skipping 11 matching lines...) Expand all Loading... | |
43 [self.tabs registerClass:[TabCollectionTabCell class] | 42 [self.tabs registerClass:[TabCollectionTabCell class] |
44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; | 43 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; |
45 | 44 |
46 [NSLayoutConstraint activateConstraints:@[ | 45 [NSLayoutConstraint activateConstraints:@[ |
47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 46 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 47 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 48 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 49 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
51 ]]; | 50 ]]; |
52 | 51 |
53 [self selectItemAtIndex:self.selectedIndex]; | 52 [self.tabs |
53 selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedIndex | |
54 inSection:0] | |
55 animated:NO | |
56 scrollPosition:UICollectionViewScrollPositionNone]; | |
54 } | 57 } |
55 | 58 |
56 - (UIStatusBarStyle)preferredStatusBarStyle { | 59 - (UIStatusBarStyle)preferredStatusBarStyle { |
57 return UIStatusBarStyleLightContent; | 60 return UIStatusBarStyleLightContent; |
58 } | 61 } |
59 | 62 |
63 #pragma mark - Setters | |
64 | |
65 - (void)setSelectedIndex:(int)selectedIndex { | |
66 [self.tabs selectItemAtIndexPath:[NSIndexPath indexPathForItem:selectedIndex | |
marq (ping after 24h)
2017/06/02 11:31:00
Do you also need to deselect the previous selectio
edchin
2017/06/02 17:24:59
Deselect happens automatically.
| |
67 inSection:0] | |
68 animated:YES | |
69 scrollPosition:UICollectionViewScrollPositionNone]; | |
70 _selectedIndex = selectedIndex; | |
71 } | |
72 | |
60 #pragma mark - Required subclass override | 73 #pragma mark - Required subclass override |
61 | 74 |
62 - (UICollectionViewLayout*)collectionViewLayout { | 75 - (UICollectionViewLayout*)collectionViewLayout { |
63 NOTREACHED() << "You must override " | 76 NOTREACHED() << "You must override " |
64 << base::SysNSStringToUTF8(NSStringFromSelector(_cmd)) | 77 << base::SysNSStringToUTF8(NSStringFromSelector(_cmd)) |
65 << " in a subclass."; | 78 << " in a subclass."; |
66 return nil; | 79 return nil; |
67 } | 80 } |
68 | 81 |
69 - (void)showTabAtIndex:(int)index { | 82 - (void)showTabAtIndex:(int)index { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
131 | 144 |
132 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { | 145 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { |
133 NSInteger item = [[self.tabs indexPathForCell:cell] item]; | 146 NSInteger item = [[self.tabs indexPathForCell:cell] item]; |
134 DCHECK_LE(item, INT_MAX); | 147 DCHECK_LE(item, INT_MAX); |
135 int index = static_cast<int>(item); | 148 int index = static_cast<int>(item); |
136 [self closeTabAtIndex:index]; | 149 [self closeTabAtIndex:index]; |
137 } | 150 } |
138 | 151 |
139 #pragma mark - TabCollectionConsumer methods | 152 #pragma mark - TabCollectionConsumer methods |
140 | 153 |
141 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { | 154 - (void)insertItem:(TabCollectionItem*)item |
155 atIndex:(int)index | |
156 selectedIndex:(int)selectedIndex { | |
157 DCHECK_GE(index, 0); | |
142 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); | 158 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); |
143 [self.items insertObject:item atIndex:index]; | 159 [self.items insertObject:item atIndex:index]; |
144 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 160 [self.tabs insertItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index |
161 inSection:0] ]]; | |
162 self.selectedIndex = selectedIndex; | |
145 } | 163 } |
146 | 164 |
147 - (void)deleteItemAtIndex:(int)index { | 165 - (void)deleteItemAtIndex:(int)index selectedIndex:(int)selectedIndex { |
166 DCHECK_GE(index, 0); | |
148 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | 167 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
149 [self.items removeObjectAtIndex:index]; | 168 [self.items removeObjectAtIndex:index]; |
150 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 169 [self.tabs deleteItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index |
170 inSection:0] ]]; | |
171 self.selectedIndex = selectedIndex; | |
151 } | 172 } |
152 | 173 |
153 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { | 174 - (void)moveItemFromIndex:(int)fromIndex |
175 toIndex:(int)toIndex | |
176 selectedIndex:(int)selectedIndex { | |
154 TabCollectionItem* item = self.items[fromIndex]; | 177 TabCollectionItem* item = self.items[fromIndex]; |
155 [self.items removeObjectAtIndex:fromIndex]; | 178 [self.items removeObjectAtIndex:fromIndex]; |
156 [self.items insertObject:item atIndex:toIndex]; | 179 [self.items insertObject:item atIndex:toIndex]; |
157 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] | 180 [self.tabs |
158 toIndexPath:[self indexPathForIndex:toIndex]]; | 181 moveItemAtIndexPath:[NSIndexPath indexPathForItem:fromIndex inSection:0] |
182 toIndexPath:[NSIndexPath indexPathForItem:toIndex inSection:0]]; | |
183 self.selectedIndex = selectedIndex; | |
159 } | 184 } |
160 | 185 |
161 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { | 186 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { |
162 [self.items removeObjectAtIndex:index]; | 187 DCHECK_GE(index, 0); |
163 [self.items insertObject:item atIndex:index]; | 188 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
189 self.items[index] = item; | |
190 TabCollectionTabCell* cell = base::mac::ObjCCastStrict<TabCollectionTabCell>( | |
191 [self.tabs cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index | |
192 inSection:0]]); | |
193 [cell setAppearanceForTabTitle:self.items[index].title | |
194 favicon:nil | |
195 cellSize:CGSizeZero]; | |
164 } | 196 } |
165 | 197 |
166 - (void)selectItemAtIndex:(int)index { | 198 - (void)populateItems:(NSArray<TabCollectionItem*>*)items |
167 self.selectedIndex = index; | 199 selectedIndex:(int)selectedIndex { |
168 [self.tabs selectItemAtIndexPath:[self indexPathForIndex:index] | |
169 animated:YES | |
170 scrollPosition:UITableViewScrollPositionNone]; | |
171 } | |
172 | |
173 - (void)populateItems:(NSArray<TabCollectionItem*>*)items { | |
174 self.items = [items mutableCopy]; | 200 self.items = [items mutableCopy]; |
175 [self.tabs reloadData]; | 201 [self.tabs reloadItemsAtIndexPaths:[self.tabs indexPathsForVisibleItems]]; |
176 } | 202 self.selectedIndex = selectedIndex; |
177 | |
178 #pragma mark - Private | |
179 | |
180 - (NSIndexPath*)indexPathForIndex:(int)index { | |
181 return [NSIndexPath indexPathForItem:index inSection:0]; | |
182 } | 203 } |
183 | 204 |
184 @end | 205 @end |
OLD | NEW |