Commit 50905e90 authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Update tab counts in tab grid page control.

This CL wires the tab grid page controls tab counts into the grid
view controllers. To do this, a public |itemCount| property is added to
the GridViewControllers, and the TabGridViewController updates the
counts in the page control whenever an event occurs that would change
the count (opening or closing a tab), and when the VC is initially set
up.

Bug: 804501, 804552
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I9162b45e04b102ce4c1b885356ebf3bf0873396b
Reviewed-on: https://chromium-review.googlesource.com/964143Reviewed-by: default avataredchin <edchin@chromium.org>
Commit-Queue: Mark Cogan <marq@chromium.org>
Cr-Commit-Position: refs/heads/master@{#543716}
parent af8eb3f1
......@@ -40,6 +40,8 @@
@property(nonatomic, strong) UIView* emptyStateView;
// Returns YES if the grid has no items.
@property(nonatomic, readonly, getter=isGridEmpty) BOOL gridEmpty;
// The number of items in the grid. Not all of the items may be visible.
@property(nonatomic, readonly) NSUInteger itemCount;
// The visual look of the grid.
@property(nonatomic, assign) GridTheme theme;
// Delegate is informed of user interactions in the grid UI.
......
......@@ -105,6 +105,10 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
return self.items.count == 0;
}
- (NSUInteger)itemCount {
return self.items.count;
}
- (BOOL)isSelectedCellVisible {
if (self.collectionView.indexPathsForSelectedItems.count == 0)
return NO;
......
......@@ -30,11 +30,11 @@
// Setting this property will *not* update the selected page.
@property(nonatomic, assign) CGFloat sliderPosition;
// Text displayed next to the incognito and inside the regular tabs icons. The
// available space for text is small -- no wider than two numerals. Text wider
// than this will be clipped.
@property(nonatomic, copy) NSString* incognitoText;
@property(nonatomic, copy) NSString* regularText;
// The numbers that the control should display in the appropriate sections.
// Numbers less than 1 are not displayed.
// Numbers greated than 99 are displayed as ':-)'.
@property(nonatomic, assign) NSUInteger incognitoTabCount;
@property(nonatomic, assign) NSUInteger regularTabCount;
// Create and return a new instance of this control. This is the preferred way
// to create instances of this class.
......
......@@ -99,6 +99,15 @@ const int kBackgroundColor = 0x5F6368;
CGPoint RectCenter(CGRect rect) {
return CGPointMake(CGRectGetMidX(rect), CGRectGetMidY(rect));
}
// Returns the string to use for a numeric item count.
NSString* StringForItemCount(long count) {
if (count == 0)
return @"";
if (count > 99)
return @":-)";
return [NSString stringWithFormat:@"%ld", count];
}
}
// View class used for the background of this control; it draws the grey
......@@ -138,8 +147,8 @@ CGPoint RectCenter(CGRect rect) {
// Public properties
@synthesize selectedPage = _selectedPage;
@synthesize sliderPosition = _sliderPosition;
@synthesize incognitoText = _incognitoText;
@synthesize regularText = _regularText;
@synthesize incognitoTabCount = _incognitoTabCount;
@synthesize regularTabCount = _regularTabCount;
// Private properties
@synthesize incognitoGuide = _incognitoGuide;
@synthesize regularGuide = _regularGuide;
......@@ -192,16 +201,18 @@ CGPoint RectCenter(CGRect rect) {
// the text in both labels (the regular and the "selected" versions that's
// visible when the slider is over a segment), and an ivar to store values that
// are set before the labels are created.
- (void)setIncognitoText:(NSString*)incognitoText {
- (void)setIncognitoTabCount:(NSUInteger)incognitoTabCount {
NSString* incognitoText = StringForItemCount(incognitoTabCount);
self.incognitoLabel.text = incognitoText;
self.incognitoSelectedLabel.text = incognitoText;
_incognitoText = [incognitoText copy];
_incognitoTabCount = incognitoTabCount;
}
- (void)setRegularText:(NSString*)regularText {
- (void)setRegularTabCount:(NSUInteger)regularTabCount {
NSString* regularText = StringForItemCount(regularTabCount);
self.regularLabel.text = regularText;
self.regularSelectedLabel.text = regularText;
_regularText = [regularText copy];
_regularTabCount = regularTabCount;
}
#pragma mark - Public methods
......@@ -374,8 +385,8 @@ CGPoint RectCenter(CGRect rect) {
// Update the label text, in case these properties have been set before the
// views were set up.
self.regularText = _regularText;
self.incognitoText = _incognitoText;
self.regularTabCount = _regularTabCount;
self.incognitoTabCount = _incognitoTabCount;
// Mark the control's layout as dirty so the the guides will be computed, then
// force a layout now so it won't be triggered later (perhaps during an
......
......@@ -427,6 +427,11 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
topToolbar.translatesAutoresizingMaskIntoConstraints = NO;
[self.view addSubview:topToolbar];
self.topToolbar = topToolbar;
// Configure and initialize the page control.
[self.topToolbar.pageControl addTarget:self
action:@selector(pageControlChanged:)
forControlEvents:UIControlEventValueChanged];
[self updatePageControlItemCounts];
NSArray* constraints = @[
[topToolbar.topAnchor constraintEqualToAnchor:self.view.topAnchor],
[topToolbar.leadingAnchor constraintEqualToAnchor:self.view.leadingAnchor],
......@@ -550,12 +555,6 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
[self.newTabButton addTarget:self
action:@selector(newTabButtonTapped:)
forControlEvents:UIControlEventTouchUpInside];
[self.topToolbar.pageControl addTarget:self
action:@selector(pageControlChanged:)
forControlEvents:UIControlEventValueChanged];
// TODO(crbug.com/804501): Use the actual live tab counts.
self.topToolbar.pageControl.regularText = @"5";
self.topToolbar.pageControl.incognitoText = @"0";
[self configureButtonsForCurrentPage];
}
......@@ -619,6 +618,13 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
}
}
- (void)updatePageControlItemCounts {
self.topToolbar.pageControl.incognitoTabCount =
self.incognitoTabsViewController.itemCount;
self.topToolbar.pageControl.regularTabCount =
self.regularTabsViewController.itemCount;
}
// Translates the toolbar views offscreen and then animates them back in using
// the transition coordinator. Transitions are preferred here since they don't
// interact with the layout system at all.
......@@ -677,6 +683,7 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
} else if (gridViewController == self.incognitoTabsViewController) {
[self.incognitoTabsDelegate closeItemAtIndex:index];
}
[self updatePageControlItemCounts];
}
- (void)lastItemWasClosedInGridViewController:
......@@ -687,6 +694,7 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
- (void)firstItemWasAddedInGridViewController:
(GridViewController*)gridViewController {
[self configureButtonsForCurrentPage];
[self updatePageControlItemCounts];
}
#pragma mark - Control actions
......@@ -723,6 +731,7 @@ typedef NS_ENUM(NSUInteger, TabGridConfiguration) {
// No-op. It is invalid to call insert new tab on remote tabs.
break;
}
[self updatePageControlItemCounts];
}
- (void)pageControlChanged:(id)sender {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment