Commit b60f95cc authored by adamta's avatar adamta Committed by Chromium LUCI CQ

[iOS] Default scroll position for refactored NTP

Ensures correct scroll position on refactored NTP when opening new tabs
and when navigating back from a webpage to the NTP.

Bug: 1114792
Change-Id: I365034f218445e6ff8e1483dabb18202f7af976c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2633581
Commit-Queue: Adam Trudeau-Arcaro <adamta@google.com>
Reviewed-by: default avatarSergio Collazos <sczs@chromium.org>
Cr-Commit-Position: refs/heads/master@{#844311}
parent f352a750
...@@ -50,8 +50,8 @@ ...@@ -50,8 +50,8 @@
// Called when a snapshot of the content will be taken. // Called when a snapshot of the content will be taken.
- (void)willUpdateSnapshot; - (void)willUpdateSnapshot;
// Sets the collection contentOffset to |offset|, or caches the value and // Sets the feed collection contentOffset to |offset| to set the initial scroll
// applies it after the first layout. // position.
- (void)setContentOffset:(CGFloat)offset; - (void)setContentOffset:(CGFloat)offset;
@end @end
......
...@@ -51,8 +51,10 @@ const CGFloat kOffsetToPinOmnibox = 100; ...@@ -51,8 +51,10 @@ const CGFloat kOffsetToPinOmnibox = 100;
// view. // view.
@property(nonatomic, weak) ContentSuggestionsLayout* contentSuggestionsLayout; @property(nonatomic, weak) ContentSuggestionsLayout* contentSuggestionsLayout;
// Initial scrolling offset, used to restore the previous scrolling position. // Content suggestions collection view height for setting the initial NTP offset
@property(nonatomic, assign) CGFloat initialContentOffset; // to be the top of the page. If value is |NAN|, then the offset was calculated
// from the saved web state instead.
@property(nonatomic, assign) CGFloat initialContentOffsetFromContentSuggestions;
@end @end
...@@ -69,8 +71,10 @@ const CGFloat kOffsetToPinOmnibox = 100; ...@@ -69,8 +71,10 @@ const CGFloat kOffsetToPinOmnibox = 100;
_contentSuggestionsViewController = contentSuggestionsViewController; _contentSuggestionsViewController = contentSuggestionsViewController;
// TODO(crbug.com/1114792): Instantiate this depending on the initial scroll // TODO(crbug.com/1114792): Instantiate this depending on the initial scroll
// position. // position.
// TODO(crbug.com/1114792): Stick the fake omnibox based on default scroll
// position.
_scrolledIntoFeed = NO; _scrolledIntoFeed = NO;
_initialContentOffset = NAN; _initialContentOffsetFromContentSuggestions = 0;
} }
return self; return self;
...@@ -148,7 +152,15 @@ const CGFloat kOffsetToPinOmnibox = 100; ...@@ -148,7 +152,15 @@ const CGFloat kOffsetToPinOmnibox = 100;
UIEdgeInsetsMake(collectionView.contentSize.height, 0, 0, 0); UIEdgeInsetsMake(collectionView.contentSize.height, 0, 0, 0);
self.headerSynchronizer.additionalOffset = collectionView.contentSize.height; self.headerSynchronizer.additionalOffset = collectionView.contentSize.height;
[self applyContentOffset]; // If scroll position was not set from the saved scroll position, and content
// suggestions collection height has increased, then set the content offset to
// reach the top of the page.
if (!isnan(self.initialContentOffsetFromContentSuggestions) &&
-collectionView.contentSize.height <
self.initialContentOffsetFromContentSuggestions) {
[self setContentOffset:-collectionView.contentSize.height
fromSavedState:NO];
}
} }
- (void)viewWillAppear:(BOOL)animated { - (void)viewWillAppear:(BOOL)animated {
...@@ -216,11 +228,7 @@ const CGFloat kOffsetToPinOmnibox = 100; ...@@ -216,11 +228,7 @@ const CGFloat kOffsetToPinOmnibox = 100;
} }
- (void)setContentOffset:(CGFloat)offset { - (void)setContentOffset:(CGFloat)offset {
self.initialContentOffset = offset; [self setContentOffset:offset fromSavedState:YES];
if (self.isViewLoaded && self.collectionView.window &&
self.collectionView.contentSize.height != 0) {
[self applyContentOffset];
}
} }
#pragma mark - UIScrollViewDelegate #pragma mark - UIScrollViewDelegate
...@@ -343,26 +351,16 @@ const CGFloat kOffsetToPinOmnibox = 100; ...@@ -343,26 +351,16 @@ const CGFloat kOffsetToPinOmnibox = 100;
[self.ntpContentDelegate reloadContentSuggestions]; [self.ntpContentDelegate reloadContentSuggestions];
} }
// Sets the collectionView's contentOffset if |_initialContentOffset| is set. // Sets the feed collection contentOffset to |offset| to set the initial scroll
- (void)applyContentOffset { // position. If |fromSavedState| is NO, then the offset is set from the content
if (!isnan(self.initialContentOffset)) { // suggestions collection height. If |fromSavedState| is YES, then the offset is
UICollectionView* collection = // forcefully set from a different source (like the cached navigation scroll
self.discoverFeedWrapperViewController.feedCollectionView; // position).
// Don't set the offset such as the content of the collection is smaller - (void)setContentOffset:(CGFloat)offset fromSavedState:(BOOL)isFromSavedState {
// than the part of the collection which should be displayed with that self.discoverFeedWrapperViewController.feedCollectionView.contentOffset =
// offset, taking into account the size of the toolbar. CGPointMake(0, offset);
CGFloat offset = MAX( self.initialContentOffsetFromContentSuggestions =
0, MIN(self.initialContentOffset, isFromSavedState ? NAN : offset;
collection.contentSize.height - collection.bounds.size.height -
ToolbarExpandedHeight(
self.traitCollection.preferredContentSizeCategory) +
collection.contentInset.bottom));
if (collection.contentOffset.y != offset) {
collection.contentOffset = CGPointMake(0, offset);
// TODO(crbug.com/1114792): Add the FakeOmnibox if necessary.
}
}
self.initialContentOffset = NAN;
} }
#pragma mark - Setters #pragma mark - Setters
......
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