Chromium Code Reviews| 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" |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 42 self.tabs.delegate = self; | 42 self.tabs.delegate = self; |
| 43 [self.tabs registerClass:[TabCollectionTabCell class] | 43 [self.tabs registerClass:[TabCollectionTabCell class] |
| 44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; | 44 forCellWithReuseIdentifier:[TabCollectionTabCell identifier]]; |
| 45 | 45 |
| 46 [NSLayoutConstraint activateConstraints:@[ | 46 [NSLayoutConstraint activateConstraints:@[ |
| 47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], | 47 [self.tabs.topAnchor constraintEqualToAnchor:self.view.topAnchor], |
| 48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], | 48 [self.tabs.bottomAnchor constraintEqualToAnchor:self.view.bottomAnchor], |
| 49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], | 49 [self.tabs.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor], |
| 50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], | 50 [self.tabs.trailingAnchor constraintEqualToAnchor:self.view.trailingAnchor], |
| 51 ]]; | 51 ]]; |
| 52 | |
| 53 [self selectItemAtIndex:self.selectedIndex]; | |
| 54 } | 52 } |
| 55 | 53 |
| 56 - (UIStatusBarStyle)preferredStatusBarStyle { | 54 - (UIStatusBarStyle)preferredStatusBarStyle { |
| 57 return UIStatusBarStyleLightContent; | 55 return UIStatusBarStyleLightContent; |
| 58 } | 56 } |
| 59 | 57 |
| 60 #pragma mark - Required subclass override | 58 #pragma mark - Required subclass override |
| 61 | 59 |
| 62 - (UICollectionViewLayout*)collectionViewLayout { | 60 - (UICollectionViewLayout*)collectionViewLayout { |
| 63 NOTREACHED() << "You must override " | 61 NOTREACHED() << "You must override " |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 97 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell | 95 dequeueReusableCellWithReuseIdentifier:[TabCollectionTabCell |
| 98 identifier] | 96 identifier] |
| 99 forIndexPath:indexPath]); | 97 forIndexPath:indexPath]); |
| 100 cell.delegate = self; | 98 cell.delegate = self; |
| 101 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; | 99 [cell setSessionType:TabSwitcherSessionType::REGULAR_SESSION]; |
| 102 DCHECK_LE(indexPath.item, INT_MAX); | 100 DCHECK_LE(indexPath.item, INT_MAX); |
| 103 int index = static_cast<int>(indexPath.item); | 101 int index = static_cast<int>(indexPath.item); |
| 104 [cell setAppearanceForTabTitle:self.items[index].title | 102 [cell setAppearanceForTabTitle:self.items[index].title |
| 105 favicon:nil | 103 favicon:nil |
| 106 cellSize:CGSizeZero]; | 104 cellSize:CGSizeZero]; |
| 105 [cell setSelected:(index == self.selectedIndex)]; | |
| 107 return cell; | 106 return cell; |
| 108 } | 107 } |
| 109 | 108 |
| 110 #pragma mark - UICollectionViewDelegate methods | 109 #pragma mark - UICollectionViewDelegate methods |
| 111 | 110 |
| 112 - (BOOL)collectionView:(UICollectionView*)collectionView | 111 - (BOOL)collectionView:(UICollectionView*)collectionView |
| 113 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { | 112 shouldSelectItemAtIndexPath:(NSIndexPath*)indexPath { |
| 114 // Prevent user selection of items. | 113 // Prevent user selection of items. |
| 115 return NO; | 114 return NO; |
| 116 } | 115 } |
| (...skipping 14 matching lines...) Expand all Loading... | |
| 131 | 130 |
| 132 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { | 131 - (void)deleteButtonPressedForCell:(UICollectionViewCell*)cell { |
| 133 NSInteger item = [[self.tabs indexPathForCell:cell] item]; | 132 NSInteger item = [[self.tabs indexPathForCell:cell] item]; |
| 134 DCHECK_LE(item, INT_MAX); | 133 DCHECK_LE(item, INT_MAX); |
| 135 int index = static_cast<int>(item); | 134 int index = static_cast<int>(item); |
| 136 [self closeTabAtIndex:index]; | 135 [self closeTabAtIndex:index]; |
| 137 } | 136 } |
| 138 | 137 |
| 139 #pragma mark - TabCollectionConsumer methods | 138 #pragma mark - TabCollectionConsumer methods |
| 140 | 139 |
| 141 - (void)insertItem:(TabCollectionItem*)item atIndex:(int)index { | 140 - (void)insertItem:(TabCollectionItem*)item |
| 141 atIndex:(int)index | |
| 142 selectedIndex:(int)selectedIndex { | |
| 143 DCHECK_GE(index, 0); | |
| 142 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); | 144 DCHECK_LE(static_cast<NSUInteger>(index), self.items.count); |
| 143 [self.items insertObject:item atIndex:index]; | 145 [self.items insertObject:item atIndex:index]; |
| 144 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 146 [self.tabs insertItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
| 147 [self selectItemAtIndex:selectedIndex]; | |
|
marq (ping after 24h)
2017/05/30 11:09:43
I think I don't like the view controller calling c
edchin
2017/06/01 23:52:28
Good idea. Done.
| |
| 145 } | 148 } |
| 146 | 149 |
| 147 - (void)deleteItemAtIndex:(int)index { | 150 - (void)deleteItemAtIndex:(int)index selectedIndex:(int)selectedIndex { |
| 151 DCHECK_GE(index, 0); | |
| 148 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | 152 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
| 149 [self.items removeObjectAtIndex:index]; | 153 [self.items removeObjectAtIndex:index]; |
| 150 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | 154 [self.tabs deleteItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; |
| 155 [self selectItemAtIndex:selectedIndex]; | |
| 151 } | 156 } |
| 152 | 157 |
| 153 - (void)moveItemFromIndex:(int)fromIndex toIndex:(int)toIndex { | 158 - (void)moveItemFromIndex:(int)fromIndex |
| 159 toIndex:(int)toIndex | |
| 160 selectedIndex:(int)selectedIndex { | |
| 154 TabCollectionItem* item = self.items[fromIndex]; | 161 TabCollectionItem* item = self.items[fromIndex]; |
| 155 [self.items removeObjectAtIndex:fromIndex]; | 162 [self.items removeObjectAtIndex:fromIndex]; |
| 156 [self.items insertObject:item atIndex:toIndex]; | 163 [self.items insertObject:item atIndex:toIndex]; |
| 157 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] | 164 [self.tabs moveItemAtIndexPath:[self indexPathForIndex:fromIndex] |
| 158 toIndexPath:[self indexPathForIndex:toIndex]]; | 165 toIndexPath:[self indexPathForIndex:toIndex]]; |
| 166 [self selectItemAtIndex:selectedIndex]; | |
| 159 } | 167 } |
| 160 | 168 |
| 161 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { | 169 - (void)replaceItemAtIndex:(int)index withItem:(TabCollectionItem*)item { |
| 162 [self.items removeObjectAtIndex:index]; | 170 DCHECK_GE(index, 0); |
| 163 [self.items insertObject:item atIndex:index]; | 171 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); |
| 172 self.items[index] = item; | |
| 173 [self.tabs reloadItemsAtIndexPaths:@[ [self indexPathForIndex:index] ]]; | |
| 164 } | 174 } |
| 165 | 175 |
| 166 - (void)selectItemAtIndex:(int)index { | 176 - (void)selectItemAtIndex:(int)index { |
| 177 NSMutableArray* indexPaths = [[NSMutableArray alloc] init]; | |
| 178 if (self.selectedIndex >= 0 && | |
| 179 static_cast<NSUInteger>(self.selectedIndex) < self.items.count) { | |
| 180 [indexPaths addObject:[self indexPathForIndex:self.selectedIndex]]; | |
| 181 } | |
| 182 if (index >= 0 && self.selectedIndex != index) { | |
| 183 DCHECK_LT(static_cast<NSUInteger>(index), self.items.count); | |
| 184 [indexPaths addObject:[self indexPathForIndex:index]]; | |
| 185 } | |
| 167 self.selectedIndex = index; | 186 self.selectedIndex = index; |
| 168 [self.tabs selectItemAtIndexPath:[self indexPathForIndex:index] | 187 [self.tabs reloadItemsAtIndexPaths:indexPaths]; |
|
marq (ping after 24h)
2017/05/30 11:09:44
Should this use -selectItemsAtIndexPath:animated:s
edchin
2017/06/01 23:52:29
Yes, selection should be done on the collection vi
| |
| 169 animated:YES | |
| 170 scrollPosition:UITableViewScrollPositionNone]; | |
| 171 } | 188 } |
| 172 | 189 |
| 173 - (void)populateItems:(NSArray<TabCollectionItem*>*)items { | 190 - (void)populateItems:(NSArray<TabCollectionItem*>*)items |
| 191 selectedIndex:(int)selectedIndex { | |
| 174 self.items = [items mutableCopy]; | 192 self.items = [items mutableCopy]; |
| 193 self.selectedIndex = selectedIndex; | |
| 175 [self.tabs reloadData]; | 194 [self.tabs reloadData]; |
| 176 } | 195 } |
| 177 | 196 |
| 178 #pragma mark - Private | 197 #pragma mark - Private |
| 179 | 198 |
| 180 - (NSIndexPath*)indexPathForIndex:(int)index { | 199 - (NSIndexPath*)indexPathForIndex:(int)index { |
|
marq (ping after 24h)
2017/05/30 11:09:44
Does this helper method pull its weight?
edchin
2017/06/01 23:52:29
Removed.
| |
| 181 return [NSIndexPath indexPathForItem:index inSection:0]; | 200 return [NSIndexPath indexPathForItem:index inSection:0]; |
| 182 } | 201 } |
| 183 | 202 |
| 184 @end | 203 @end |
| OLD | NEW |