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 // Collection view of tabs. |
19 @property(nonatomic, readwrite) UICollectionView* tabs; | 20 @property(nonatomic, readwrite) UICollectionView* tabs; |
| 21 // The model backing the collection view. |
20 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; | 22 @property(nonatomic, readwrite) NSMutableArray<TabCollectionItem*>* items; |
| 23 // Selected index of tab collection. |
21 @property(nonatomic, assign) int selectedIndex; | 24 @property(nonatomic, assign) int selectedIndex; |
22 @end | 25 @end |
23 | 26 |
24 @implementation TabCollectionViewController | 27 @implementation TabCollectionViewController |
25 @synthesize tabs = _tabs; | 28 @synthesize tabs = _tabs; |
26 @synthesize items = _items; | 29 @synthesize items = _items; |
27 @synthesize selectedIndex = _selectedIndex; | 30 @synthesize selectedIndex = _selectedIndex; |
28 | 31 |
29 #pragma mark - UIViewController | 32 #pragma mark - UIViewController |
30 | 33 |
(...skipping 12 matching lines...) Expand all Loading... |
43 [self.tabs registerClass:[TabCollectionTabCell class] | 46 [self.tabs registerClass:[TabCollectionTabCell class] |
44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; | 47 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; |
45 | 48 |
46 [NSLayoutConstraint activateConstraints:@[ | 49 [NSLayoutConstraint activateConstraints:@[ |
47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 50 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 51 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 52 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 53 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
51 ]]; | 54 ]]; |
52 | 55 |
53 [self selectItemAtIndex:self.selectedIndex]; | 56 [self.tabs |
| 57 selectItemAtIndexPath:[NSIndexPath indexPathForItem:self.selectedIndex |
| 58 inSection:0] |
| 59 animated:NO |
| 60 scrollPosition:UICollectionViewScrollPositionNone]; |
54 } | 61 } |
55 | 62 |
56 - (UIStatusBarStyle)preferredStatusBarStyle { | 63 - (UIStatusBarStyle)preferredStatusBarStyle { |
57 return UIStatusBarStyleLightContent; | 64 return UIStatusBarStyleLightContent; |
58 } | 65 } |
59 | 66 |
| 67 #pragma mark - Setters |
| 68 |
| 69 - (void)setSelectedIndex:(int)selectedIndex { |
| 70 [self.tabs selectItemAtIndexPath:[NSIndexPath indexPathForItem:selectedIndex |
| 71 inSection:0] |
| 72 animated:YES |
| 73 scrollPosition:UICollectionViewScrollPositionNone]; |
| 74 _selectedIndex = selectedIndex; |
| 75 } |
| 76 |
60 #pragma mark - Required subclass override | 77 #pragma mark - Required subclass override |
61 | 78 |
62 - (UICollectionViewLayout*)collectionViewLayout { | 79 - (UICollectionViewLayout*)collectionViewLayout { |
63 NOTREACHED() << "You must override " | 80 NOTREACHED() << "You must override " |
64 << base::SysNSStringToUTF8(NSStringFromSelector(_cmd)) | 81 << base::SysNSStringToUTF8(NSStringFromSelector(_cmd)) |
65 << " in a subclass."; | 82 << " in a subclass."; |
66 return nil; | 83 return nil; |
67 } | 84 } |
68 | 85 |
69 - (void)showTabAtIndex:(int)index { | 86 - (void)showTabAtIndex:(int)index { |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
131 | 148 |
132 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { | 149 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { |
133 NSInteger item = [[self.tabs indexPathForCell:cell] item]; | 150 NSInteger item = [[self.tabs indexPathForCell:cell] item]; |
134 DCHECK_LE(item, INT_MAX); | 151 DCHECK_LE(item, INT_MAX); |
135 int index = static_cast<int>(item); | 152 int index = static_cast<int>(item); |
136 [self closeTabAtIndex:index]; | 153 [self closeTabAtIndex:index]; |
137 } | 154 } |
138 | 155 |
139 #pragma mark - TabCollectionConsumer methods | 156 #pragma mark - TabCollectionConsumer methods |
140 | 157 |
141 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { | 158 - (void)insertItem:(TabCollectionItem*)item |
| 159 atIndex:(int)index |
| 160 selectedIndex:(int)selectedIndex { |
| 161 DCHECK_GE(index, 0); |
142 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); | 162 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); |
143 [self.items insertObject:item atIndex:index]; | 163 [self.items insertObject:item atIndex:index]; |
144 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 164 [self.tabs insertItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index |
| 165 inSection:0] ]]; |
| 166 self.selectedIndex = selectedIndex; |
145 } | 167 } |
146 | 168 |
147 - (void)deleteItemAtIndex:(int)index { | 169 - (void)deleteItemAtIndex:(int)index selectedIndex:(int)selectedIndex { |
| 170 DCHECK_GE(index, 0); |
148 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | 171 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
149 [self.items removeObjectAtIndex:index]; | 172 [self.items removeObjectAtIndex:index]; |
150 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 173 [self.tabs deleteItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index |
| 174 inSection:0] ]]; |
| 175 self.selectedIndex = selectedIndex; |
151 } | 176 } |
152 | 177 |
153 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { | 178 - (void)moveItemFromIndex:(int)fromIndex |
| 179 toIndex:(int)toIndex |
| 180 selectedIndex:(int)selectedIndex { |
154 TabCollectionItem* item = self.items[fromIndex]; | 181 TabCollectionItem* item = self.items[fromIndex]; |
155 [self.items removeObjectAtIndex:fromIndex]; | 182 [self.items removeObjectAtIndex:fromIndex]; |
156 [self.items insertObject:item atIndex:toIndex]; | 183 [self.items insertObject:item atIndex:toIndex]; |
157 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] | 184 [self.tabs |
158 toIndexPath:[self indexPathForIndex:toIndex]]; | 185 moveItemAtIndexPath:[NSIndexPath indexPathForItem:fromIndex inSection:0] |
| 186 toIndexPath:[NSIndexPath indexPathForItem:toIndex inSection:0]]; |
| 187 self.selectedIndex = selectedIndex; |
159 } | 188 } |
160 | 189 |
161 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { | 190 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { |
162 [self.items removeObjectAtIndex:index]; | 191 DCHECK_GE(index, 0); |
163 [self.items insertObject:item atIndex:index]; | 192 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
| 193 self.items[index] = item; |
| 194 TabCollectionTabCell* cell = base::mac::ObjCCastStrict<TabCollectionTabCell>( |
| 195 [self.tabs cellForItemAtIndexPath:[NSIndexPath indexPathForItem:index |
| 196 inSection:0]]); |
| 197 [cell setAppearanceForTabTitle:self.items[index].title |
| 198 favicon:nil |
| 199 cellSize:CGSizeZero]; |
164 } | 200 } |
165 | 201 |
166 - (void)selectItemAtIndex:(int)index { | 202 - (void)populateItems:(NSArray<TabCollectionItem*>*)items |
167 self.selectedIndex = index; | 203 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]; | 204 self.items = [items mutableCopy]; |
175 [self.tabs reloadData]; | 205 [self.tabs reloadItemsAtIndexPaths:[self.tabs indexPathsForVisibleItems]]; |
176 } | 206 self.selectedIndex = selectedIndex; |
177 | |
178 #pragma mark - Private | |
179 | |
180 - (NSIndexPath*)indexPathForIndex:(int)index { | |
181 return [NSIndexPath indexPathForItem:index inSection:0]; | |
182 } | 207 } |
183 | 208 |
184 @end | 209 @end |
OLD | NEW |