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_data_source.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; |
| 21 @property(nonatomic, assign) int selectedIndex; |
20 @end | 22 @end |
21 | 23 |
22 @implementation TabCollectionViewController | 24 @implementation TabCollectionViewController |
23 @synthesize tabs = _tabs; | 25 @synthesize tabs = _tabs; |
24 @synthesize dataSource = _dataSource; | 26 @synthesize items = _items; |
| 27 @synthesize selectedIndex = _selectedIndex; |
25 | 28 |
26 #pragma mark - UIViewController | 29 #pragma mark - UIViewController |
27 | 30 |
28 - (void)viewDidLoad { | 31 - (void)viewDidLoad { |
29 [super viewDidLoad]; | 32 [super viewDidLoad]; |
30 UICollectionView* tabs = | 33 UICollectionView* tabs = |
31 [[UICollectionView alloc] initWithFrame:CGRectZero | 34 [[UICollectionView alloc] initWithFrame:CGRectZero |
32 collectionViewLayout:[self collectionViewLayout]]; | 35 collectionViewLayout:[self collectionViewLayout]]; |
33 tabs.translatesAutoresizingMaskIntoConstraints = NO; | 36 tabs.translatesAutoresizingMaskIntoConstraints = NO; |
34 tabs.backgroundColor = [UIColor blackColor]; | 37 tabs.backgroundColor = [UIColor blackColor]; |
35 | 38 |
36 [self.view addSubview:tabs]; | 39 [self.view addSubview:tabs]; |
37 self.tabs = tabs; | 40 self.tabs = tabs; |
38 self.tabs.dataSource = self; | 41 self.tabs.dataSource = self; |
39 self.tabs.delegate = self; | 42 self.tabs.delegate = self; |
40 [self.tabs registerClass:[TabCollectionTabCell class] | 43 [self.tabs registerClass:[TabCollectionTabCell class] |
41 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; | 44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; |
42 | 45 |
43 [NSLayoutConstraint activateConstraints:@[ | 46 [NSLayoutConstraint activateConstraints:@[ |
44 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
45 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
46 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
47 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
48 ]]; | 51 ]]; |
| 52 |
| 53 [self selectItemAtIndex:self.selectedIndex]; |
49 } | 54 } |
50 | 55 |
51 - (UIStatusBarStyle)preferredStatusBarStyle { | 56 - (UIStatusBarStyle)preferredStatusBarStyle { |
52 return UIStatusBarStyleLightContent; | 57 return UIStatusBarStyleLightContent; |
53 } | 58 } |
54 | 59 |
55 #pragma mark - Required subclass override | 60 #pragma mark - Required subclass override |
56 | 61 |
57 - (UICollectionViewLayout*)collectionViewLayout { | 62 - (UICollectionViewLayout*)collectionViewLayout { |
58 NOTREACHED() << "You must override " | 63 NOTREACHED() << "You must override " |
(...skipping 16 matching lines...) Expand all Loading... |
75 | 80 |
76 #pragma mark - UICollectionViewDataSource methods | 81 #pragma mark - UICollectionViewDataSource methods |
77 | 82 |
78 - (NSInteger)numberOfSectionsInCollectionView: | 83 - (NSInteger)numberOfSectionsInCollectionView: |
79 (UICollectionView*)collectionView { | 84 (UICollectionView*)collectionView { |
80 return 1; | 85 return 1; |
81 } | 86 } |
82 | 87 |
83 - (NSInteger)collectionView:(UICollectionView*)collectionView | 88 - (NSInteger)collectionView:(UICollectionView*)collectionView |
84 numberOfItemsInSection:(NSInteger)section { | 89 numberOfItemsInSection:(NSInteger)section { |
85 return [self.dataSource numberOfTabs]; | 90 return static_cast<NSInteger>(self.items.count); |
86 } | 91 } |
87 | 92 |
88 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView | 93 - (UICollectionViewCell*)collectionView:(UICollectionView*)collectionView |
89 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { | 94 cellForItemAtIndexPath:(nonnull NSIndexPath*)indexPath { |
90 TabCollectionTabCell* cell = | 95 TabCollectionTabCell* cell = |
91 base::mac::ObjCCastStrict<TabCollectionTabCell>([collectionView | 96 base::mac::ObjCCastStrict<TabCollectionTabCell>([collectionView |
92 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell | 97 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell |
93 identifier] | 98 identifier] |
94 forIndexPath:indexPath]); | 99 forIndexPath:indexPath]); |
95 cell.delegate = self; | 100 cell.delegate = self; |
96 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; | 101 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; |
97 DCHECK_LE(indexPath.item, INT_MAX); | 102 DCHECK_LE(indexPath.item, INT_MAX); |
98 int item = static_cast<int>(indexPath.item); | 103 int index = static_cast<int>(indexPath.item); |
99 [cell setAppearanceForTabTitle:[self.dataSource titleAtIndex:item] | 104 [cell setAppearanceForTabTitle:self.items[index].title |
100 favicon:nil | 105 favicon:nil |
101 cellSize:CGSizeZero]; | 106 cellSize:CGSizeZero]; |
102 [cell setSelected:(indexPath.item == [self.dataSource indexOfActiveTab])]; | |
103 return cell; | 107 return cell; |
104 } | 108 } |
105 | 109 |
106 #pragma mark - UICollectionViewDelegate methods | 110 #pragma mark - UICollectionViewDelegate methods |
107 | 111 |
108 - (BOOL)collectionView:(UICollectionView*)collectionView | 112 - (BOOL)collectionView:(UICollectionView*)collectionView |
109 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 113 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
110 // Prevent user selection of items. | 114 // Prevent user selection of items. |
111 return NO; | 115 return NO; |
112 } | 116 } |
(...skipping 14 matching lines...) Expand all Loading... |
127 | 131 |
128 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { | 132 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { |
129 NSInteger item = [[self.tabs indexPathForCell:cell] item]; | 133 NSInteger item = [[self.tabs indexPathForCell:cell] item]; |
130 DCHECK_LE(item, INT_MAX); | 134 DCHECK_LE(item, INT_MAX); |
131 int index = static_cast<int>(item); | 135 int index = static_cast<int>(item); |
132 [self closeTabAtIndex:index]; | 136 [self closeTabAtIndex:index]; |
133 } | 137 } |
134 | 138 |
135 #pragma mark - TabCollectionConsumer methods | 139 #pragma mark - TabCollectionConsumer methods |
136 | 140 |
137 - (void)insertItemAtIndex:(int)index { | 141 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { |
138 [self.tabs insertItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index | 142 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); |
139 inSection:0] ]]; | 143 [self.items insertObject:item atIndex:index]; |
| 144 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
140 } | 145 } |
141 | 146 |
142 - (void)deleteItemAtIndex:(int)index { | 147 - (void)deleteItemAtIndex:(int)index { |
143 [self.tabs deleteItemsAtIndexPaths:@[ [NSIndexPath indexPathForItem:index | 148 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
144 inSection:0] ]]; | 149 [self.items removeObjectAtIndex:index]; |
| 150 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
145 } | 151 } |
146 | 152 |
147 - (void)reloadItemsAtIndexes:(NSIndexSet*)indexes { | 153 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { |
148 NSMutableArray<NSIndexPath*>* indexPaths = [[NSMutableArray alloc] init]; | 154 TabCollectionItem* item = self.items[fromIndex]; |
149 [indexes enumerateIndexesUsingBlock:^(NSUInteger index, BOOL* _Nonnull stop) { | 155 [self.items removeObjectAtIndex:fromIndex]; |
150 [indexPaths addObject:[NSIndexPath indexPathForItem:index inSection:0]]; | 156 [self.items insertObject:item atIndex:toIndex]; |
151 }]; | 157 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] |
152 [self.tabs reloadItemsAtIndexPaths:indexPaths]; | 158 toIndexPath:[self indexPathForIndex:toIndex]]; |
| 159 } |
| 160 |
| 161 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { |
| 162 [self.items removeObjectAtIndex:index]; |
| 163 [self.items insertObject:item atIndex:index]; |
| 164 } |
| 165 |
| 166 - (void)selectItemAtIndex:(int)index { |
| 167 self.selectedIndex = index; |
| 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]; |
| 175 [self.tabs reloadData]; |
| 176 } |
| 177 |
| 178 #pragma mark - Private |
| 179 |
| 180 - (NSIndexPath*)indexPathForIndex:(int)index { |
| 181 return [NSIndexPath indexPathForItem:index inSection:0]; |
153 } | 182 } |
154 | 183 |
155 @end | 184 @end |
OLD | NEW |