Commit 9faf9381 authored by Andrew Xu's avatar Andrew Xu Committed by Commit Bot

Refactor the gesture scrolling handler in scrollable shelf

Refactor the handling functions for gesture scrolling in
ScrollableShelfView. This CL makes preparations for the incoming
CL which handles the gesture fling event.

Bug: 973481
Change-Id: Ia25daf84043e38c0c03f7dbf73b7beb204b5bcab
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1760545
Commit-Queue: Andrew Xu <andrewxu@chromium.org>
Reviewed-by: default avatarXiyuan Xia <xiyuan@chromium.org>
Cr-Commit-Position: refs/heads/master@{#688269}
parent 2b30b5a2
......@@ -111,11 +111,8 @@ int ScrollableShelfView::CalculateScrollUpperBound() const {
}
float ScrollableShelfView::CalculateClampedScrollOffset(float scroll) const {
const float old_scroll = GetShelf()->IsHorizontalAlignment()
? scroll_offset_.x()
: scroll_offset_.y();
const float scroll_upper_bound = CalculateScrollUpperBound();
scroll = std::min(scroll_upper_bound, std::max(0.f, old_scroll + scroll));
scroll = std::min(scroll_upper_bound, std::max(0.f, scroll));
return scroll;
}
......@@ -312,19 +309,7 @@ void ScrollableShelfView::ButtonPressed(views::Button* sender,
views::View* sender_view = sender;
DCHECK((sender_view == left_arrow_) || (sender_view == right_arrow_));
// Implement the arrow button handler in the same way with the gesture
// scrolling. The key is to calculate the suitable scroll distance.
int offset = space_for_icons_ - kArrowButtonGroupWidth -
ShelfConstants::button_size() - kAppIconEndPadding;
if (layout_strategy_ == kShowRightArrowButton)
offset -= (kArrowButtonGroupWidth - kAppIconEndPadding);
DCHECK_GT(offset, 0);
// If |forward| is true, scroll the scrollable shelf view rightward.
const bool forward = sender_view == right_arrow_;
if (!forward)
offset = -offset;
float offset = CalculatePageScrollingOffset(sender_view == right_arrow_);
if (GetShelf()->IsHorizontalAlignment())
ScrollByXOffset(offset, true);
else
......@@ -445,23 +430,49 @@ bool ScrollableShelfView::ProcessGestureEvent(const ui::GestureEvent& event) {
}
void ScrollableShelfView::ScrollByXOffset(float x_offset, bool animating) {
ScrollToXOffset(scroll_offset_.x() + x_offset, animating);
}
void ScrollableShelfView::ScrollByYOffset(float y_offset, bool animating) {
ScrollToYOffset(scroll_offset_.y() + y_offset, animating);
}
void ScrollableShelfView::ScrollToXOffset(float x_target_offset,
bool animating) {
x_target_offset = CalculateClampedScrollOffset(x_target_offset);
const float old_x = scroll_offset_.x();
const float x = CalculateClampedScrollOffset(x_offset);
scroll_offset_.set_x(x);
scroll_offset_.set_x(x_target_offset);
Layout();
const float diff = x - old_x;
const float diff = x_target_offset - old_x;
if (animating)
StartShelfScrollAnimation(diff);
}
void ScrollableShelfView::ScrollByYOffset(float y_offset, bool animating) {
void ScrollableShelfView::ScrollToYOffset(float y_target_offset,
bool animating) {
y_target_offset = CalculateClampedScrollOffset(y_target_offset);
const int old_y = scroll_offset_.y();
const int y = CalculateClampedScrollOffset(y_offset);
scroll_offset_.set_y(y);
scroll_offset_.set_y(y_target_offset);
Layout();
const float diff = y - old_y;
const float diff = y_target_offset - old_y;
if (animating)
StartShelfScrollAnimation(diff);
}
float ScrollableShelfView::CalculatePageScrollingOffset(bool forward) const {
// Implement the arrow button handler in the same way with the gesture
// scrolling. The key is to calculate the suitable scroll distance.
float offset = space_for_icons_ - kArrowButtonGroupWidth -
ShelfConstants::button_size() - kAppIconEndPadding;
if (layout_strategy_ == kShowRightArrowButton)
offset -= (kArrowButtonGroupWidth - kAppIconEndPadding);
DCHECK_GT(offset, 0);
if (!forward)
offset = -offset;
return offset;
}
} // namespace ash
......@@ -57,8 +57,7 @@ class ASH_EXPORT ScrollableShelfView : public views::View,
// Returns the maximum scroll distance.
int CalculateScrollUpperBound() const;
// Returns the clamped scroll offset after scrolling by the distance of
// |scroll|.
// Returns the clamped scroll offset.
float CalculateClampedScrollOffset(float scroll) const;
// Creates the animation for scrolling shelf by |scroll_distance|.
......@@ -103,12 +102,22 @@ class ASH_EXPORT ScrollableShelfView : public views::View,
// consumed.
bool ProcessGestureEvent(const ui::GestureEvent& event);
// Scrolls the view. |animating| indicates whether animation is needed for
// scrolling. |x_offset| or |y_offset| has to be float. Otherwise the slow
// gesture drag is neglected
// Scrolls the view by distance of |x_offset| or |y_offset|. |animating|
// indicates whether the animation displays. |x_offset| or |y_offset| has to
// be float. Otherwise the slow gesture drag is neglected.
void ScrollByXOffset(float x_offset, bool animating);
void ScrollByYOffset(float y_offset, bool animating);
// Scrolls the view to the target offset. After scrolling, |scroll_offset_| is
// |x_dst_offset| or |y_dst_offset|. |animating| indicates whether the
// animation shows.
void ScrollToXOffset(float x_target_offset, bool animating);
void ScrollToYOffset(float y_target_offset, bool animating);
// Calculates the distance of scrolling to show a new page of shelf icons.
// |forward| indicates whether the next page or previous page is shown.
float CalculatePageScrollingOffset(bool forward) const;
LayoutStrategy layout_strategy_ = kNotShowArrowButtons;
// Child views Owned by views hierarchy.
......
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