Commit a9865d7b authored by Andrew Xu's avatar Andrew Xu Committed by Commit Bot

[Shelf] Refactor on layout strategy calculation

When calculating the target layout strategy, only two parameters are
required: the scroll distance on the main axis and the length of
available space to layout shelf icons. In other words, we don't need
the scrollable shelf's view bounds which is the third parameter of
the current function.

This CL should not introduce any visible difference.

Bug: 1042911
Change-Id: I57431969cb0bdaee71e5ba2b82faa4fdcc02428a
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2231090Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Commit-Queue: Andrew Xu <andrewxu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#775253}
parent 32bf6be7
......@@ -747,18 +747,8 @@ float ScrollableShelfView::CalculateScrollUpperBound(
if (layout_strategy_ == kNotShowArrowButtons)
return 0.f;
// Calculate the length of the available space.
int available_length = available_space_for_icons -
2 * ShelfConfig::Get()->GetAppIconEndPadding();
// Calculate the length of the preferred size.
const gfx::Size shelf_preferred_size(
shelf_container_view_->GetPreferredSize());
const int preferred_length =
(GetShelf()->IsHorizontalAlignment() ? shelf_preferred_size.width()
: shelf_preferred_size.height());
return std::max(0, preferred_length - available_length);
return std::max(
0, CalculateShelfIconsPreferredLength() - available_space_for_icons);
}
float ScrollableShelfView::CalculateClampedScrollOffset(
......@@ -805,11 +795,8 @@ void ScrollableShelfView::StartShelfScrollAnimation(float scroll_distance) {
ScrollableShelfView::LayoutStrategy
ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis,
int space_for_icons,
bool use_target_bounds) const {
if (CanFitAllAppsWithoutScrolling(
GetAvailableLocalBounds(use_target_bounds).size(),
CalculatePreferredSize())) {
int available_length) const {
if (available_length >= CalculateShelfIconsPreferredLength()) {
return kNotShowArrowButtons;
}
......@@ -819,7 +806,7 @@ ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis,
}
if (scroll_distance_on_main_axis ==
CalculateScrollUpperBound(space_for_icons)) {
CalculateScrollUpperBound(available_length)) {
// If there is no invisible shelf button at the right side, hide the right
// button.
return kShowLeftArrowButton;
......@@ -1082,8 +1069,7 @@ void ScrollableShelfView::ScrollRectToVisible(const gfx::Rect& rect) {
main_axis_offset_after_scroll = CalculateTargetOffsetAfterScroll(
main_axis_offset_after_scroll, page_scroll_distance);
layout_strategy_after_scroll = CalculateLayoutStrategy(
main_axis_offset_after_scroll, GetSpaceForIcons(),
/*use_target_bounds= =*/false);
main_axis_offset_after_scroll, GetSpaceForIcons());
visible_space_after_scroll =
GetMirroredRect(CalculateVisibleSpace(layout_strategy_after_scroll));
rect_after_scroll = rect_after_adjustment;
......@@ -1792,8 +1778,7 @@ float ScrollableShelfView::CalculateTargetOffsetAfterScroll(
target_offset =
CalculateClampedScrollOffset(target_offset, GetSpaceForIcons());
LayoutStrategy layout_strategy_after_scroll =
CalculateLayoutStrategy(target_offset, GetSpaceForIcons(),
/*use_target_bounds= =*/false);
CalculateLayoutStrategy(target_offset, GetSpaceForIcons());
target_offset = CalculateScrollDistanceAfterAdjustment(
target_offset, layout_strategy_after_scroll);
......@@ -2297,8 +2282,7 @@ void ScrollableShelfView::UpdateScrollOffset(float target_offset) {
// Calculating the layout strategy relies on |scroll_offset_|.
LayoutStrategy new_strategy = CalculateLayoutStrategy(
CalculateMainAxisScrollDistance(), GetSpaceForIcons(),
/*use_target_bounds=*/false);
CalculateMainAxisScrollDistance(), GetSpaceForIcons());
const bool strategy_needs_update = (layout_strategy_ != new_strategy);
if (strategy_needs_update) {
......@@ -2336,8 +2320,7 @@ int ScrollableShelfView::CalculateScrollOffsetForTargetAvailableSpace(
// Calculates the layout strategy based on the new scroll offset.
LayoutStrategy new_strategy =
CalculateLayoutStrategy(target_scroll_offset, available_space_for_icons,
/*use_target_bounds=*/true);
CalculateLayoutStrategy(target_scroll_offset, available_space_for_icons);
// Adjusts the scroll offset with the new strategy.
target_scroll_offset += CalculateAdjustmentOffset(
......@@ -2454,4 +2437,13 @@ void ScrollableShelfView::EnableLayerClipOnShelfContainerView(bool enable) {
gfx::ToEnclosedRect(visible_space_in_shelf_container_coordinates));
}
int ScrollableShelfView::CalculateShelfIconsPreferredLength() const {
const gfx::Size shelf_preferred_size(
shelf_container_view_->GetPreferredSize());
const int preferred_length =
(GetShelf()->IsHorizontalAlignment() ? shelf_preferred_size.width()
: shelf_preferred_size.height());
return preferred_length + 2 * ShelfConfig::Get()->GetAppIconEndPadding();
}
} // namespace ash
......@@ -198,13 +198,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView,
// Creates the animation for scrolling shelf by |scroll_distance|.
void StartShelfScrollAnimation(float scroll_distance);
// Calculates the layout strategy based on the three factors:
// (1) scroll offset on the main axis; (2) available space for shelf icons;
// (3) Bounds of the scrollable shelf view: actual view bounds or target
// bounds.
// Calculates the layout strategy based on:
// (1) scroll offset on the main axis.
// (2) length of the available space to accommodate shelf icons.
LayoutStrategy CalculateLayoutStrategy(float scroll_distance_on_main_axis,
int space_for_icons,
bool use_target_bounds) const;
int available_length) const;
Shelf* GetShelf();
const Shelf* GetShelf() const;
......@@ -471,6 +469,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView,
// Enable/disable the layer clip on |shelf_container_view_|.
void EnableLayerClipOnShelfContainerView(bool enable);
// Calculates the length of space required by shelf icons to show without
// scroll. Note that the return value includes the padding space between the
// app icon and the end of scrollable shelf.
int CalculateShelfIconsPreferredLength() const;
LayoutStrategy layout_strategy_ = kNotShowArrowButtons;
// Child views Owned by views hierarchy.
......
......@@ -24,10 +24,8 @@ void ShelfContainerView::Initialize() {
}
gfx::Size ShelfContainerView::CalculateIdealSize(int button_size) const {
const int button_strip_size =
ShelfView::GetSizeOfAppButtons(shelf_view_->last_visible_index() -
shelf_view_->first_visible_index() + 1,
button_size);
const int button_strip_size = ShelfView::GetSizeOfAppButtons(
shelf_view_->number_of_visible_apps(), button_size);
return shelf_view_->shelf()->IsHorizontalAlignment()
? gfx::Size(button_strip_size, button_size)
: gfx::Size(button_size, button_strip_size);
......
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