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( ...@@ -747,18 +747,8 @@ float ScrollableShelfView::CalculateScrollUpperBound(
if (layout_strategy_ == kNotShowArrowButtons) if (layout_strategy_ == kNotShowArrowButtons)
return 0.f; return 0.f;
// Calculate the length of the available space. return std::max(
int available_length = available_space_for_icons - 0, CalculateShelfIconsPreferredLength() - 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);
} }
float ScrollableShelfView::CalculateClampedScrollOffset( float ScrollableShelfView::CalculateClampedScrollOffset(
...@@ -805,11 +795,8 @@ void ScrollableShelfView::StartShelfScrollAnimation(float scroll_distance) { ...@@ -805,11 +795,8 @@ void ScrollableShelfView::StartShelfScrollAnimation(float scroll_distance) {
ScrollableShelfView::LayoutStrategy ScrollableShelfView::LayoutStrategy
ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis, ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis,
int space_for_icons, int available_length) const {
bool use_target_bounds) const { if (available_length >= CalculateShelfIconsPreferredLength()) {
if (CanFitAllAppsWithoutScrolling(
GetAvailableLocalBounds(use_target_bounds).size(),
CalculatePreferredSize())) {
return kNotShowArrowButtons; return kNotShowArrowButtons;
} }
...@@ -819,7 +806,7 @@ ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis, ...@@ -819,7 +806,7 @@ ScrollableShelfView::CalculateLayoutStrategy(float scroll_distance_on_main_axis,
} }
if (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 // If there is no invisible shelf button at the right side, hide the right
// button. // button.
return kShowLeftArrowButton; return kShowLeftArrowButton;
...@@ -1082,8 +1069,7 @@ void ScrollableShelfView::ScrollRectToVisible(const gfx::Rect& rect) { ...@@ -1082,8 +1069,7 @@ void ScrollableShelfView::ScrollRectToVisible(const gfx::Rect& rect) {
main_axis_offset_after_scroll = CalculateTargetOffsetAfterScroll( main_axis_offset_after_scroll = CalculateTargetOffsetAfterScroll(
main_axis_offset_after_scroll, page_scroll_distance); main_axis_offset_after_scroll, page_scroll_distance);
layout_strategy_after_scroll = CalculateLayoutStrategy( layout_strategy_after_scroll = CalculateLayoutStrategy(
main_axis_offset_after_scroll, GetSpaceForIcons(), main_axis_offset_after_scroll, GetSpaceForIcons());
/*use_target_bounds= =*/false);
visible_space_after_scroll = visible_space_after_scroll =
GetMirroredRect(CalculateVisibleSpace(layout_strategy_after_scroll)); GetMirroredRect(CalculateVisibleSpace(layout_strategy_after_scroll));
rect_after_scroll = rect_after_adjustment; rect_after_scroll = rect_after_adjustment;
...@@ -1792,8 +1778,7 @@ float ScrollableShelfView::CalculateTargetOffsetAfterScroll( ...@@ -1792,8 +1778,7 @@ float ScrollableShelfView::CalculateTargetOffsetAfterScroll(
target_offset = target_offset =
CalculateClampedScrollOffset(target_offset, GetSpaceForIcons()); CalculateClampedScrollOffset(target_offset, GetSpaceForIcons());
LayoutStrategy layout_strategy_after_scroll = LayoutStrategy layout_strategy_after_scroll =
CalculateLayoutStrategy(target_offset, GetSpaceForIcons(), CalculateLayoutStrategy(target_offset, GetSpaceForIcons());
/*use_target_bounds= =*/false);
target_offset = CalculateScrollDistanceAfterAdjustment( target_offset = CalculateScrollDistanceAfterAdjustment(
target_offset, layout_strategy_after_scroll); target_offset, layout_strategy_after_scroll);
...@@ -2297,8 +2282,7 @@ void ScrollableShelfView::UpdateScrollOffset(float target_offset) { ...@@ -2297,8 +2282,7 @@ void ScrollableShelfView::UpdateScrollOffset(float target_offset) {
// Calculating the layout strategy relies on |scroll_offset_|. // Calculating the layout strategy relies on |scroll_offset_|.
LayoutStrategy new_strategy = CalculateLayoutStrategy( LayoutStrategy new_strategy = CalculateLayoutStrategy(
CalculateMainAxisScrollDistance(), GetSpaceForIcons(), CalculateMainAxisScrollDistance(), GetSpaceForIcons());
/*use_target_bounds=*/false);
const bool strategy_needs_update = (layout_strategy_ != new_strategy); const bool strategy_needs_update = (layout_strategy_ != new_strategy);
if (strategy_needs_update) { if (strategy_needs_update) {
...@@ -2336,8 +2320,7 @@ int ScrollableShelfView::CalculateScrollOffsetForTargetAvailableSpace( ...@@ -2336,8 +2320,7 @@ int ScrollableShelfView::CalculateScrollOffsetForTargetAvailableSpace(
// Calculates the layout strategy based on the new scroll offset. // Calculates the layout strategy based on the new scroll offset.
LayoutStrategy new_strategy = LayoutStrategy new_strategy =
CalculateLayoutStrategy(target_scroll_offset, available_space_for_icons, CalculateLayoutStrategy(target_scroll_offset, available_space_for_icons);
/*use_target_bounds=*/true);
// Adjusts the scroll offset with the new strategy. // Adjusts the scroll offset with the new strategy.
target_scroll_offset += CalculateAdjustmentOffset( target_scroll_offset += CalculateAdjustmentOffset(
...@@ -2454,4 +2437,13 @@ void ScrollableShelfView::EnableLayerClipOnShelfContainerView(bool enable) { ...@@ -2454,4 +2437,13 @@ void ScrollableShelfView::EnableLayerClipOnShelfContainerView(bool enable) {
gfx::ToEnclosedRect(visible_space_in_shelf_container_coordinates)); 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 } // namespace ash
...@@ -198,13 +198,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView, ...@@ -198,13 +198,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView,
// Creates the animation for scrolling shelf by |scroll_distance|. // Creates the animation for scrolling shelf by |scroll_distance|.
void StartShelfScrollAnimation(float scroll_distance); void StartShelfScrollAnimation(float scroll_distance);
// Calculates the layout strategy based on the three factors: // Calculates the layout strategy based on:
// (1) scroll offset on the main axis; (2) available space for shelf icons; // (1) scroll offset on the main axis.
// (3) Bounds of the scrollable shelf view: actual view bounds or target // (2) length of the available space to accommodate shelf icons.
// bounds.
LayoutStrategy CalculateLayoutStrategy(float scroll_distance_on_main_axis, LayoutStrategy CalculateLayoutStrategy(float scroll_distance_on_main_axis,
int space_for_icons, int available_length) const;
bool use_target_bounds) const;
Shelf* GetShelf(); Shelf* GetShelf();
const Shelf* GetShelf() const; const Shelf* GetShelf() const;
...@@ -471,6 +469,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView, ...@@ -471,6 +469,11 @@ class ASH_EXPORT ScrollableShelfView : public views::AccessiblePaneView,
// Enable/disable the layer clip on |shelf_container_view_|. // Enable/disable the layer clip on |shelf_container_view_|.
void EnableLayerClipOnShelfContainerView(bool enable); 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; LayoutStrategy layout_strategy_ = kNotShowArrowButtons;
// Child views Owned by views hierarchy. // Child views Owned by views hierarchy.
......
...@@ -24,10 +24,8 @@ void ShelfContainerView::Initialize() { ...@@ -24,10 +24,8 @@ void ShelfContainerView::Initialize() {
} }
gfx::Size ShelfContainerView::CalculateIdealSize(int button_size) const { gfx::Size ShelfContainerView::CalculateIdealSize(int button_size) const {
const int button_strip_size = const int button_strip_size = ShelfView::GetSizeOfAppButtons(
ShelfView::GetSizeOfAppButtons(shelf_view_->last_visible_index() - shelf_view_->number_of_visible_apps(), button_size);
shelf_view_->first_visible_index() + 1,
button_size);
return shelf_view_->shelf()->IsHorizontalAlignment() return shelf_view_->shelf()->IsHorizontalAlignment()
? gfx::Size(button_strip_size, button_size) ? gfx::Size(button_strip_size, button_size)
: gfx::Size(button_size, button_strip_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