Chromium Code Reviews| Index: ios/chrome/browser/ui/tabs/tab_strip_controller.mm |
| diff --git a/ios/chrome/browser/ui/tabs/tab_strip_controller.mm b/ios/chrome/browser/ui/tabs/tab_strip_controller.mm |
| index 715b40e8287fe44be710e795880c888358ca1083..92a36c007e6985cd9ef2261b10b6a007b8987099 100644 |
| --- a/ios/chrome/browser/ui/tabs/tab_strip_controller.mm |
| +++ b/ios/chrome/browser/ui/tabs/tab_strip_controller.mm |
| @@ -286,6 +286,11 @@ - (NSUInteger)modelIndexForDraggedTab; |
| // Takes into account whether or not the mode toggle button is showing. |
| - (CGFloat)tabStripVisibleSpace; |
| +// shift all of the tab strip subviews by an amount equal to the content offset |
|
rohitrao (ping after 24h)
2017/01/24 14:19:23
Capitalize "Shift".
liaoyuke
2017/01/25 16:49:37
Done.
|
| +// change, which effectively places the subviews back where they were before the |
| +// change, in terms of screen coordinates. |
| +- (void)shiftTabStripSubviews:(CGPoint)oldContentOffset; |
| + |
| // Updates the scroll view's content size based on the current set of tabs and |
| // closing tabs. After updating the content size, repositions views so they |
| // they will appear stationary on screen. |
| @@ -313,6 +318,11 @@ - (CGFloat)tabOverlap; |
| // Returns the minimum tab view width depending on the current layout mode. |
| - (CGFloat)minTabWidth; |
| +// Automatically scroll the tab strip view to keep the newly inserted tab view |
| +// visible. |
| +// This method must be called with a valid |tabIndex|. |
| +- (void)AutoScrollForNewTab:(NSUInteger)tabIndex; |
|
rohitrao (ping after 24h)
2017/01/24 14:19:23
Lowercase "a" to start the method name.
liaoyuke
2017/01/25 16:49:37
Done.
|
| + |
| // Updates the content offset of the tab strip view in order to keep the |
| // selected tab view visible. |
| // Content offset adjustement is only needed/performed in compact mode or |
| @@ -1169,6 +1179,16 @@ - (void)removeTabSwitcherToggleButton { |
| [self updateScrollViewFrameForToggleButton]; |
| } |
| +- (void)shiftTabStripSubviews:(CGPoint)oldContentOffset { |
| + CGFloat dx = [_tabStripView contentOffset].x - oldContentOffset.x; |
| + for (UIView* view in [_tabStripView subviews]) { |
| + CGRect frame = [view frame]; |
| + frame.origin.x += dx; |
| + [view setFrame:frame]; |
| + _targetFrames.AddFrame(view, frame); |
| + } |
| +} |
| + |
| - (void)updateContentSizeAndRepositionViews { |
| // TODO(rohitrao): The following lines are duplicated in |
| // layoutTabStripSubviews. Find a way to consolidate this logic. |
| @@ -1197,19 +1217,10 @@ - (void)updateContentSizeAndRepositionViews { |
| // will never change if the content size is growing.) |
| // |
| // To handle this without making views appear to jump, shift all of the |
| - // subviews by an amount equal to the size change. This effectively places |
| - // the subviews back where they were before the change, in terms of screen |
| - // coordinates. |
| + // subviews by an amount equal to the size change. |
| CGPoint oldOffset = [_tabStripView contentOffset]; |
| [_tabStripView setContentSize:contentSize]; |
| - |
| - CGFloat dx = [_tabStripView contentOffset].x - oldOffset.x; |
| - for (UIView* view in [_tabStripView subviews]) { |
| - CGRect frame = [view frame]; |
| - frame.origin.x += dx; |
| - [view setFrame:frame]; |
| - _targetFrames.AddFrame(view, frame); |
| - } |
| + [self shiftTabStripSubviews:oldOffset]; |
| } |
| - (CGRect)scrollViewFrameForTab:(TabView*)view { |
| @@ -1252,36 +1263,61 @@ - (CGFloat)minTabWidth { |
| return IsCompactTablet() ? kMinTabWidthForCompactLayout : kMinTabWidth; |
| } |
| -- (void)updateContentOffsetForTabIndex:(NSUInteger)tabIndex |
| - isNewTab:(BOOL)isNewTab { |
| +- (void)updateContentOffsetForLastTab { |
|
rohitrao (ping after 24h)
2017/01/24 14:19:23
Is this needed?
liaoyuke
2017/01/25 16:49:37
Done.
|
| +} |
| + |
| +- (void)AutoScrollForNewTab:(NSUInteger)tabIndex { |
| DCHECK_NE(NSNotFound, static_cast<NSInteger>(tabIndex)); |
| - if (experimental_flags::IsTabStripAutoScrollNewTabsEnabled() && isNewTab) { |
| - // The following code calculates the amount of scroll needed to make |
| - // |tabIndex| visible in the "virtual" coordinate system, where root is x=0 |
| - // and it contains all the tabs laid out as if the tabstrip was infinitely |
| - // long. The amount of scroll is calculated as a desired length that it is |
| - // just large enough to contain all the tabs to the left of |tabIndex|, with |
| - // the standard overlap. |
| - NSUInteger numNonClosingTabsToLeft = 0; |
| - NSUInteger i = 0; |
| - for (TabView* tab in _tabArray.get()) { |
| - if ([_closingTabs containsObject:tab]) |
| - ++i; |
| + // The following code calculates the amount of scroll needed to make |
| + // |tabIndex| visible in the "virtual" coordinate system, where root is x=0 |
| + // and it contains all the tabs laid out as if the tabstrip was infinitely |
| + // long. The amount of scroll is calculated as a desired length that it is |
| + // just large enough to contain all the tabs to the left of |tabIndex|, with |
| + // the standard overlap. |
| + if (tabIndex == [_tabArray count] - 1) { |
| + const CGFloat tabStripAvailableSpace = |
| + _tabStripView.frame.size.width - _tabStripView.contentInset.right; |
| + CGPoint oldOffset = [_tabStripView contentOffset]; |
| + if (_tabStripView.contentSize.width > tabStripAvailableSpace) { |
| + CGFloat scrollToPoint = |
| + _tabStripView.contentSize.width - tabStripAvailableSpace; |
| + [_tabStripView setContentOffset:CGPointMake(scrollToPoint, 0)]; |
| + } |
| - if (i == tabIndex) |
| - break; |
| + // To handle content offset change without making views appear to jump, |
| + // shift all of the subviews by an amount equal to the size change. |
| + [self shiftTabStripSubviews:oldOffset]; |
| + return; |
| + } |
| - ++numNonClosingTabsToLeft; |
| + NSUInteger numNonClosingTabsToLeft = 0; |
| + NSUInteger i = 0; |
| + for (TabView* tab in _tabArray.get()) { |
| + if ([_closingTabs containsObject:tab]) |
| ++i; |
| - } |
| - const CGFloat tabHeight = CGRectGetHeight([_tabStripView bounds]); |
| - CGRect scrollRect = |
| - CGRectMake(_currentTabWidth * numNonClosingTabsToLeft - |
| - ([self tabOverlap] * (numNonClosingTabsToLeft - 1)), |
| - 0, _currentTabWidth, tabHeight); |
| - [_tabStripView scrollRectToVisible:scrollRect animated:YES]; |
| + if (i == tabIndex) |
| + break; |
| + |
| + ++numNonClosingTabsToLeft; |
| + ++i; |
| + } |
| + |
| + const CGFloat tabHeight = CGRectGetHeight([_tabStripView bounds]); |
| + CGRect scrollRect = |
| + CGRectMake(_currentTabWidth * numNonClosingTabsToLeft - |
| + ([self tabOverlap] * (numNonClosingTabsToLeft - 1)), |
| + 0, _currentTabWidth, tabHeight); |
| + [_tabStripView scrollRectToVisible:scrollRect animated:YES]; |
| +} |
| + |
| +- (void)updateContentOffsetForTabIndex:(NSUInteger)tabIndex |
| + isNewTab:(BOOL)isNewTab { |
| + DCHECK_NE(NSNotFound, static_cast<NSInteger>(tabIndex)); |
| + |
| + if (experimental_flags::IsTabStripAutoScrollNewTabsEnabled() && isNewTab) { |
| + [self AutoScrollForNewTab:tabIndex]; |
| return; |
| } |