Commit 1ab61f53 authored by Roberto Moura's avatar Roberto Moura Committed by Commit Bot

Add the HorizontalReorderingLayout and refactor GridReorderingLayout.

Add the HorizontalReorderingLayout, which is the layout of the thumb
strip when reordering cells while the DragAndDrop flag is disabled.

Add a set of helper functions to be called by both the
HorizontalReorderingLayout and the GridReorderingLayout.

Change-Id: Id0b475c7e9cb97c537ce3b30d3936d0c82d1f2cf
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2462125Reviewed-by: default avataredchin <edchin@chromium.org>
Commit-Queue: Roberto Moura <mouraroberto@google.com>
Auto-Submit: Roberto Moura <mouraroberto@google.com>
Cr-Commit-Position: refs/heads/master@{#816775}
parent 4d999a61
......@@ -32,6 +32,8 @@ source_set("grid_ui") {
"grid_view_controller.mm",
"horizontal_layout.h",
"horizontal_layout.mm",
"reordering_layout_util.h",
"reordering_layout_util.mm",
]
configs += [ "//build/config/compiler:enable_arc" ]
......
......@@ -5,6 +5,7 @@
#import "ios/chrome/browser/ui/tab_grid/grid/grid_layout.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_constants.h"
#import "ios/chrome/browser/ui/tab_grid/grid/reordering_layout_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -75,38 +76,23 @@
// default layout attributes.
- (NSArray<__kindof UICollectionViewLayoutAttributes*>*)
layoutAttributesForElementsInRect:(CGRect)rect {
NSArray* baseAttributes = [super layoutAttributesForElementsInRect:rect];
NSMutableArray<__kindof UICollectionViewLayoutAttributes*>* attributes =
[NSMutableArray array];
for (UICollectionViewLayoutAttributes* attribute in baseAttributes) {
UICollectionViewLayoutAttributes* newAttribute = [attribute copy];
newAttribute.alpha = kReorderingInactiveCellOpacity;
[attributes addObject:newAttribute];
}
return [attributes copy];
return CopyAttributesArrayAndSetInactiveOpacity(
[super layoutAttributesForElementsInRect:rect]);
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:
(NSIndexPath*)indexPath {
UICollectionViewLayoutAttributes* attributes =
[[super layoutAttributesForItemAtIndexPath:indexPath] copy];
attributes.alpha = kReorderingInactiveCellOpacity;
return attributes;
return CopyAttributesAndSetInactiveOpacity(
[super layoutAttributesForItemAtIndexPath:indexPath]);
}
- (UICollectionViewLayoutAttributes*)
layoutAttributesForInteractivelyMovingItemAtIndexPath:
(NSIndexPath*)indexPath
withTargetPosition:(CGPoint)position {
UICollectionViewLayoutAttributes* attributes = [[super
return CopyAttributesAndSetActiveProperties([super
layoutAttributesForInteractivelyMovingItemAtIndexPath:indexPath
withTargetPosition:position] copy];
// The moving item has regular opacity, but is scaled.
attributes.alpha = 1.0;
attributes.transform =
CGAffineTransformScale(attributes.transform, kReorderingActiveCellScale,
kReorderingActiveCellScale);
return attributes;
withTargetPosition:position]);
}
@end
......@@ -81,6 +81,8 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
@property(nonatomic, strong) HorizontalLayout* horizontalLayout;
// The layout used while the grid is being reordered.
@property(nonatomic, strong) UICollectionViewLayout* reorderingLayout;
// The layout used while the thumb strip is being reordered.
@property(nonatomic, strong) UICollectionViewLayout* horizontalReorderingLayout;
// YES if, when reordering is enabled, the order of the cells has changed.
@property(nonatomic, assign) BOOL hasChangedOrder;
#if defined(__IPHONE_13_4)
......@@ -150,6 +152,10 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
collectionView.dragInteractionEnabled = YES;
} else {
self.reorderingLayout = [[GridReorderingLayout alloc] init];
if (IsThumbStripEnabled()) {
self.horizontalReorderingLayout =
[[HorizontalReorderingLayout alloc] init];
}
self.itemReorderRecognizer = [[UILongPressGestureRecognizer alloc]
initWithTarget:self
action:@selector(handleItemReorderingWithGesture:)];
......@@ -858,8 +864,15 @@ NSIndexPath* CreateIndexPath(NSInteger index) {
self.itemReorderTouchPoint =
CGPointMake(location.x - cellCenter.x, location.y - cellCenter.y);
// Switch to the reordering layout.
[self.collectionView setCollectionViewLayout:self.reorderingLayout
animated:YES];
if (IsThumbStripEnabled() &&
self.defaultLayout == self.horizontalLayout) {
[self.collectionView
setCollectionViewLayout:self.horizontalReorderingLayout
animated:YES];
} else {
[self.collectionView setCollectionViewLayout:self.reorderingLayout
animated:YES];
}
// Immediately update the position of the moving item; otherwise, the
// collection view may relayout its subviews and briefly show the moving
// item at position (0,0).
......
......@@ -11,4 +11,9 @@
@interface HorizontalLayout : FlowLayout
@end
// A specialization of HorizontalLayout that shows the UI in its "reordering"
// state, with the moving cell enlarged and the non-moving cells transparent.
@interface HorizontalReorderingLayout : HorizontalLayout
@end
#endif // IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_HORIZONTAL_LAYOUT_H_
......@@ -5,6 +5,7 @@
#import "ios/chrome/browser/ui/tab_grid/grid/horizontal_layout.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_constants.h"
#import "ios/chrome/browser/ui/tab_grid/grid/reordering_layout_util.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
......@@ -35,3 +36,33 @@
}
@end
@implementation HorizontalReorderingLayout
#pragma mark - UICollectionViewLayout
// Both -layoutAttributesForElementsInRect: and
// -layoutAttributesForItemAtIndexPath: need to be overridden to change the
// default layout attributes.
- (NSArray<__kindof UICollectionViewLayoutAttributes*>*)
layoutAttributesForElementsInRect:(CGRect)rect {
return CopyAttributesArrayAndSetInactiveOpacity(
[super layoutAttributesForElementsInRect:rect]);
}
- (UICollectionViewLayoutAttributes*)layoutAttributesForItemAtIndexPath:
(NSIndexPath*)indexPath {
return CopyAttributesAndSetInactiveOpacity(
[super layoutAttributesForItemAtIndexPath:indexPath]);
}
- (UICollectionViewLayoutAttributes*)
layoutAttributesForInteractivelyMovingItemAtIndexPath:
(NSIndexPath*)indexPath
withTargetPosition:(CGPoint)position {
return CopyAttributesAndSetActiveProperties([super
layoutAttributesForInteractivelyMovingItemAtIndexPath:indexPath
withTargetPosition:position]);
}
@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_GRID_GRID_REORDERING_LAYOUT_UTIL_H_
#define IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_REORDERING_LAYOUT_UTIL_H_
#import <UIKit/UIKit.h>
// Returns an array whose elements are copies with added transparency of the
// elements of the |base_attributes_array| array.
NSArray<__kindof UICollectionViewLayoutAttributes*>*
CopyAttributesArrayAndSetInactiveOpacity(NSArray* base_attributes_array);
// Returns a copy of the |base_attributes| object with added transparency.
UICollectionViewLayoutAttributes* CopyAttributesAndSetInactiveOpacity(
UICollectionViewLayoutAttributes* base_attributes);
// Returns a copy of the |base_attributes| object with added opacity and
// scaling.
UICollectionViewLayoutAttributes* CopyAttributesAndSetActiveProperties(
UICollectionViewLayoutAttributes* base_attributes);
#endif // IOS_CHROME_BROWSER_UI_TAB_GRID_GRID_REORDERING_LAYOUT_UTIL_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_grid/grid/reordering_layout_util.h"
#import "ios/chrome/browser/ui/tab_grid/grid/grid_constants.h"
#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
NSArray<__kindof UICollectionViewLayoutAttributes*>*
CopyAttributesArrayAndSetInactiveOpacity(NSArray* base_attributes_array) {
NSMutableArray<__kindof UICollectionViewLayoutAttributes*>* attributes_array =
[NSMutableArray array];
for (UICollectionViewLayoutAttributes* attributes in base_attributes_array) {
UICollectionViewLayoutAttributes* new_attributes =
CopyAttributesAndSetInactiveOpacity(attributes);
[attributes_array addObject:new_attributes];
}
return [attributes_array copy];
}
UICollectionViewLayoutAttributes* CopyAttributesAndSetInactiveOpacity(
UICollectionViewLayoutAttributes* base_attributes) {
UICollectionViewLayoutAttributes* attributes = [base_attributes copy];
attributes.alpha = kReorderingInactiveCellOpacity;
return attributes;
}
UICollectionViewLayoutAttributes* CopyAttributesAndSetActiveProperties(
UICollectionViewLayoutAttributes* base_attributes) {
UICollectionViewLayoutAttributes* attributes = [base_attributes copy];
// The moving item has regular opacity, but is scaled.
attributes.alpha = 1.0;
attributes.transform =
CGAffineTransformScale(attributes.transform, kReorderingActiveCellScale,
kReorderingActiveCellScale);
return attributes;
}
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