Commit 08bc1a1f authored by Nazerke's avatar Nazerke Committed by Commit Bot

[ios] Add layout to create TabStrip CollectionViewController.

This CL adds a collection view layout class for creating the tabstrip
viewcontroller.

Bug: 1128249,1130488.
Change-Id: Ie9faa75720fb0da8de3174887af945a4ee945bf4
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2421648Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Commit-Queue: Nazerke Kalidolda <nazerke@google.com>
Cr-Commit-Position: refs/heads/master@{#811942}
parent 06884c3c
......@@ -23,6 +23,8 @@ source_set("tab_strip_ui") {
sources = [
"tab_strip_view_controller.h",
"tab_strip_view_controller.mm",
"tab_strip_view_layout.h",
"tab_strip_view_layout.mm",
]
deps = [
"//ios/chrome/browser/browser_state",
......
......@@ -7,13 +7,20 @@
#import <UIKit/UIKit.h>
@protocol PopupMenuLongPressDelegate;
// ViewController for the TabStrip. This ViewController is contained by
// BrowserViewController. This TabStripViewController is responsible for
// responding to the different updates in the tabstrip view.
@interface TabStripViewController : UICollectionViewController
- (instancetype)init NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithNibName:(NSString*)nibNameOrNil
bundle:(NSBundle*)nibBundleOrNil NS_UNAVAILABLE;
- (instancetype)initWithCoder:(NSCoder*)aDecoder NS_UNAVAILABLE;
- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout*)layout
NS_UNAVAILABLE;
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_STRIP_TAB_STRIP_VIEW_CONTROLLER_H_
......@@ -4,10 +4,23 @@
#import "ios/chrome/browser/ui/tab_strip/tab_strip_view_controller.h"
#import "ios/chrome/browser/ui/tab_strip/tab_strip_view_layout.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
@implementation TabStripViewController
- (instancetype)init {
TabStripViewLayout* layout = [[TabStripViewLayout alloc] init];
self = [super initWithCollectionViewLayout:layout];
return self;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.translatesAutoresizingMaskIntoConstraints = NO;
}
@end
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef IOS_CHROME_BROWSER_UI_TAB_STRIP_TAB_STRIP_VIEW_LAYOUT_H_
#define IOS_CHROME_BROWSER_UI_TAB_STRIP_TAB_STRIP_VIEW_LAYOUT_H_
#import <UIKit/UIKit.h>
// Layout used for TabStripView. This layout makes sure the tabs collection
// is pinned to the top of view and the sizes of the resizable tabs are
// correct.
@interface TabStripViewLayout : UICollectionViewLayout
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_STRIP_TAB_STRIP_VIEW_LAYOUT_H_
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#import "ios/chrome/browser/ui/tab_strip/tab_strip_view_layout.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
namespace {
// Tab dimensions.
const CGFloat kTabOverlapStacked = 32.0;
const CGFloat kMinTabWidthStacked = 200.0;
} // namespace
@implementation TabStripViewLayout
- (CGSize)collectionViewContentSize {
UICollectionView* collection = self.collectionView;
NSInteger num = [collection numberOfItemsInSection:0];
CGFloat width = kMinTabWidthStacked * num;
width -= (num - 1) * kTabOverlapStacked;
width = MAX(width, collection.bounds.size.width);
return CGSizeMake(width, collection.bounds.size.height);
}
// Retrieves layout information for an item at the specified index path
// with a corresponding cell.
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:
(NSIndexPath*)indexPath {
UICollectionView* collection = self.collectionView;
CGRect bounds = collection.bounds;
// Calculating tab's length size depending on the number of tabs.
CGFloat x = indexPath.row * kMinTabWidthStacked;
if (indexPath.row > 0) {
x -= (kTabOverlapStacked * indexPath.row);
}
x = MAX(x, bounds.origin.x);
UICollectionViewLayoutAttributes* attr = [UICollectionViewLayoutAttributes
layoutAttributesForCellWithIndexPath:indexPath];
attr.frame =
CGRectMake(x, bounds.origin.y, kMinTabWidthStacked, bounds.size.height);
return attr;
}
- (NSArray<UICollectionViewLayoutAttributes*>*)
layoutAttributesForElementsInRect:(CGRect)rect {
NSInteger count = [self.collectionView numberOfItemsInSection:0];
NSMutableArray* result =
[NSMutableArray arrayWithCapacity:CGRectGetWidth(rect) / 190];
CGFloat x = CGRectGetMinX(rect);
NSInteger i = 0;
while (x < CGRectGetMaxX(rect) && i < count) {
// Modifies Layout attributes.
UICollectionViewLayoutAttributes* attr = [self
layoutAttributesForItemAtIndexPath:[NSIndexPath indexPathForRow:i
inSection:0]];
[result addObject:attr];
x = CGRectGetMaxX(attr.frame);
i++;
}
return result;
}
- (BOOL)shouldInvalidateLayoutForBoundsChange:(CGRect)newBounds {
return YES;
}
- (UICollectionViewLayoutAttributes*)
initialLayoutAttributesForAppearingItemAtIndexPath:
(NSIndexPath*)itemIndexPath {
UICollectionViewLayoutAttributes* attr =
[self layoutAttributesForItemAtIndexPath:itemIndexPath];
CGRect frame = attr.frame;
frame.origin.y = CGRectGetMaxY(frame);
attr.frame = frame;
return attr;
}
@end
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