Commit 01fca2bb authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

NTP focus animation tweaks

Tweaks the animation when focusing the fakebox in NTP.

Bug: 867455
Cq-Include-Trybots: luci.chromium.try:ios-simulator-full-configs;master.tryserver.chromium.mac:ios-simulator-cronet
Change-Id: I44a7c799e6269a1fdf6806b2cd54cdcdfc46336f
Reviewed-on: https://chromium-review.googlesource.com/1156697
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Reviewed-by: default avatarStepan Khapugin <stkhapugin@chromium.org>
Cr-Commit-Position: refs/heads/master@{#579730}
parent ca70b72c
......@@ -50,7 +50,6 @@ CGFloat heightForLogoHeader(BOOL logoIsShowing,
// added to |hintLabelContainer| with autoresizing. This is done due to the
// way searchHintLabel is later tranformed.
void configureSearchHintLabel(UILabel* searchHintLabel,
UIView* hintLabelContainer,
UIButton* searchTapTarget);
// Configure the |voiceSearchButton|, adding it to the |searchTapTarget| and
// constraining it.
......
......@@ -204,28 +204,26 @@ CGFloat heightForLogoHeader(BOOL logoIsShowing,
}
void configureSearchHintLabel(UILabel* searchHintLabel,
UIView* hintLabelContainer,
UIButton* searchTapTarget) {
// searchHintLabel is intentionally not using autolayout because it will need
// to use a CGAffineScale transform that will not work correctly with
// autolayout. Instead, |hintLabelContainer| will use autolayout and will
// contain |searchHintLabel|.
searchHintLabel.autoresizingMask =
UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
[hintLabelContainer setTranslatesAutoresizingMaskIntoConstraints:NO];
[searchTapTarget addSubview:hintLabelContainer];
[hintLabelContainer addSubview:searchHintLabel];
[searchHintLabel setTranslatesAutoresizingMaskIntoConstraints:NO];
[searchTapTarget addSubview:searchHintLabel];
CGFloat centerYOffsetConstant =
IsUIRefreshPhase1Enabled() ? 0 : kSearchHintVerticalOffset;
[NSLayoutConstraint activateConstraints:@[
[hintLabelContainer.centerYAnchor
[searchHintLabel.centerYAnchor
constraintEqualToAnchor:searchTapTarget.centerYAnchor
constant:centerYOffsetConstant],
[hintLabelContainer.heightAnchor
[searchHintLabel.heightAnchor
constraintEqualToConstant:kSearchFieldHeight - 2 * kSearchHintMargin],
]];
if (IsUIRefreshPhase1Enabled()) {
[searchHintLabel.centerXAnchor
constraintEqualToAnchor:searchTapTarget.centerXAnchor]
.active = YES;
}
[searchHintLabel setText:l10n_util::GetNSString(IDS_OMNIBOX_EMPTY_HINT)];
if (base::i18n::IsRTL()) {
[searchHintLabel setTextAlignment:NSTextAlignmentRight];
......
......@@ -49,7 +49,6 @@ UIEdgeInsets SafeAreaInsetsForViewWithinNTP(UIView* view) {
@property(nonatomic, weak) id<ContentSuggestionsHeaderControlling>
headerController;
@property(nonatomic, assign) CFTimeInterval shiftTileStartTime;
@property(nonatomic, strong) ProceduralBlock shiftUpCompletionBlock;
// Tap gesture recognizer when the omnibox is focused.
@property(nonatomic, strong) UITapGestureRecognizer* tapGestureRecognizer;
......@@ -63,7 +62,6 @@ UIEdgeInsets SafeAreaInsetsForViewWithinNTP(UIView* view) {
@synthesize shiftTileStartTime = _shiftTileStartTime;
@synthesize tapGestureRecognizer = _tapGestureRecognizer;
@synthesize collectionShiftingOffset = _collectionShiftingOffset;
@synthesize shiftUpCompletionBlock = _shiftUpCompletionBlock;
- (instancetype)
initWithCollectionController:
......@@ -143,19 +141,28 @@ initWithCollectionController:
self.collectionController.scrolledToTop = YES;
self.shouldAnimateHeader = YES;
self.shiftUpCompletionBlock = completion;
// Layout the header for the constraints to be animated.
[self.headerController layoutHeader];
[self.collectionView.collectionViewLayout invalidateLayout];
// Similar to -shiftTilesDown, also use a CADisplayLink so each contentOffset
// tick forces an update for the fake omnibox. Otherwise the fakebox and its
// label will be sized incorrectly when tapped.
CADisplayLink* link = [CADisplayLink
displayLinkWithTarget:self
selector:@selector(shiftTilesUpAnimationDidFire:)];
[link addToRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
[UIView animateWithDuration:kShiftTilesUpAnimationDuration
animations:^{
if (self.collectionView.contentOffset.y < pinnedOffsetY) {
// Changing the contentOffset of the collection results in a scroll
// and a change in the constraints of the header.
self.collectionView.contentOffset = CGPointMake(0, pinnedOffsetY);
// Layout the header for the constraints to be animated.
[self.headerController layoutHeader];
[self.collectionView.collectionViewLayout invalidateLayout];
}
}
completion:^(BOOL finished) {
// Check to see if the collection are still scrolled to the top -- it's
// possible (and difficult) to unfocus the omnibox and initiate a
// -shiftTilesDown before the animation here completes.
if (self.collectionController.scrolledToTop) {
self.shouldAnimateHeader = NO;
if (completion)
completion();
}
}];
}
- (void)invalidateLayout {
......@@ -229,47 +236,6 @@ initWithCollectionController:
return [self.collectionController collectionView];
}
// Updates the collection view's scroll view offset for the next frame of the
// -shiftTilesUpWithCompletionBlock animation.
- (void)shiftTilesUpAnimationDidFire:(CADisplayLink*)link {
// If this is the first frame of the animation, store the starting timestamp
// and do nothing.
if (self.shiftTileStartTime == -1) {
self.shiftTileStartTime = link.timestamp;
return;
}
CFTimeInterval timeElapsed = link.timestamp - self.shiftTileStartTime;
double percentComplete = timeElapsed / kShiftTilesUpAnimationDuration;
// Ensure that the percentage cannot be above 1.0.
if (percentComplete > 1.0)
percentComplete = 1.0;
// Find how much the collection view should be scrolled up in the next frame.
CGFloat pinnedOffsetY = [self.headerController pinnedOffsetY];
CGFloat startingOffset = pinnedOffsetY - self.collectionShiftingOffset;
CGFloat yOffset =
startingOffset + percentComplete * (pinnedOffsetY - startingOffset);
self.collectionView.contentOffset = CGPointMake(0, yOffset);
if (percentComplete == 1.0) {
[link invalidate];
// Reset |shiftTileStartTime| to its sentinel value.
self.shiftTileStartTime = -1;
// Check to see if the collection are still scrolled to the top -- it's
// possible (and difficult) to initiate a -shiftTilesDown before the
// animation here completes.
if (self.collectionController.scrolledToTop) {
self.shouldAnimateHeader = NO;
if (self.shiftUpCompletionBlock) {
self.shiftUpCompletionBlock();
self.shiftUpCompletionBlock = nil;
}
}
}
}
// Updates the collection view's scroll view offset for the next frame of the
// shiftTilesDown animation.
- (void)shiftTilesDownAnimationDidFire:(CADisplayLink*)link {
......
......@@ -235,8 +235,10 @@ const CGFloat kBackgroundLandscapeInset = 169;
// Adjust the position of the search field's subviews by adjusting their
// constraint constant value.
CGFloat constantDiff = percent * (ntp_header::kMaxHorizontalMarginDiff +
inset + safeAreaInsets.left);
CGFloat constantDiff = IsUIRefreshPhase1Enabled()
? -maxXInset * percent
: percent * (ntp_header::kMaxHorizontalMarginDiff +
inset + safeAreaInsets.left);
for (NSLayoutConstraint* constraint in constraints) {
if (constraint.constant > 0)
constraint.constant = constantDiff + ntp_header::kHintLabelSidePadding;
......
......@@ -303,24 +303,26 @@ const UIEdgeInsets kSearchBoxStretchInsets = {3, 3, 3, 3};
ntp_home::FakeOmniboxAccessibilityID();
// Set up fakebox hint label.
_searchHintLabel = [[UILabel alloc] init];
UIView* hintLabelContainer = [[UIView alloc] init];
content_suggestions::configureSearchHintLabel(
_searchHintLabel, hintLabelContainer, self.fakeOmnibox);
self.hintLabelLeadingConstraint = [hintLabelContainer.leadingAnchor
constraintEqualToAnchor:[self.fakeOmnibox leadingAnchor]
constant:ntp_header::kHintLabelSidePadding];
if (!IsUIRefreshPhase1Enabled())
self.hintLabelLeadingConstraint.constant =
ntp_header::kHintLabelSidePaddingLegacy;
[self.hintLabelLeadingConstraint setActive:YES];
self.searchHintLabel = [[UILabel alloc] init];
content_suggestions::configureSearchHintLabel(self.searchHintLabel,
self.fakeOmnibox);
if (IsUIRefreshPhase1Enabled()) {
self.hintLabelLeadingConstraint = [self.searchHintLabel.leadingAnchor
constraintGreaterThanOrEqualToAnchor:[self.fakeOmnibox leadingAnchor]
constant:ntp_header::kHintLabelSidePadding];
} else {
self.hintLabelLeadingConstraint = [self.searchHintLabel.leadingAnchor
constraintEqualToAnchor:[self.fakeOmnibox leadingAnchor]
constant:ntp_header::kHintLabelSidePaddingLegacy];
}
self.hintLabelLeadingConstraint.active = YES;
// Set a button the same size as the fake omnibox as the accessibility
// element. If the hint is the only accessible element, when the fake omnibox
// is taking the full width, there are few points that are not accessible and
// allow to select the content below it.
_searchHintLabel.isAccessibilityElement = NO;
self.searchHintLabel.isAccessibilityElement = NO;
UIButton* accessibilityButton = [[UIButton alloc] init];
[accessibilityButton addTarget:self
action:@selector(fakeOmniboxTapped:)
......@@ -340,8 +342,8 @@ const UIEdgeInsets kSearchBoxStretchInsets = {3, 3, 3, 3};
self.voiceTapTrailingConstraint = [voiceTapTarget.trailingAnchor
constraintEqualToAnchor:[self.fakeOmnibox trailingAnchor]];
[NSLayoutConstraint activateConstraints:@[
[hintLabelContainer.trailingAnchor
constraintEqualToAnchor:voiceTapTarget.leadingAnchor],
[self.searchHintLabel.trailingAnchor
constraintLessThanOrEqualToAnchor:voiceTapTarget.leadingAnchor],
_voiceTapTrailingConstraint
]];
......
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