Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(77)

Unified Diff: ui/app_list/pagination_controller.cc

Issue 524503003: Refactor app list scrolling: introduce the PaginationController class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@ares-appgrid-vertical-scroll
Patch Set: Fix GN and Gyp. Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « ui/app_list/pagination_controller.h ('k') | ui/app_list/views/apps_grid_view.h » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: ui/app_list/pagination_controller.cc
diff --git a/ui/app_list/pagination_controller.cc b/ui/app_list/pagination_controller.cc
new file mode 100644
index 0000000000000000000000000000000000000000..d9aabc236aff0499d33ebf5ad1200cca6762ee60
--- /dev/null
+++ b/ui/app_list/pagination_controller.cc
@@ -0,0 +1,88 @@
+// Copyright 2014 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "ui/app_list/pagination_controller.h"
+
+#include "ui/app_list/pagination_model.h"
+#include "ui/events/event.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/geometry/vector2d.h"
+
+namespace app_list {
+
+namespace {
+
+// Constants for dealing with scroll events.
+const int kMinScrollToSwitchPage = 20;
+const int kMinHorizVelocityToSwitchPage = 800;
+
+const double kFinishTransitionThreshold = 0.33;
+
+} // namespace
+
+PaginationController::PaginationController(PaginationModel* model,
+ ScrollAxis scroll_axis)
+ : pagination_model_(model), scroll_axis_(scroll_axis) {
+}
+
+bool PaginationController::OnScroll(const gfx::Vector2d& offset) {
+ int offset_magnitude;
+ if (scroll_axis_ == SCROLL_AXIS_HORIZONTAL) {
+ // If the view scrolls horizontally, both horizontal and vertical scroll
+ // events are valid (since most mouse wheels only have vertical scrolling).
+ offset_magnitude =
+ abs(offset.x()) > abs(offset.y()) ? offset.x() : offset.y();
+ } else {
+ // If the view scrolls vertically, only vertical scroll events are valid.
+ offset_magnitude = offset.y();
+ }
+
+ if (abs(offset_magnitude) > kMinScrollToSwitchPage) {
+ if (!pagination_model_->has_transition()) {
+ pagination_model_->SelectPageRelative(offset_magnitude > 0 ? -1 : 1,
+ true);
+ }
+ return true;
+ }
+
+ return false;
+}
+
+bool PaginationController::OnGestureEvent(const ui::GestureEvent& event,
+ const gfx::Rect& bounds) {
+ const ui::GestureEventDetails& details = event.details();
+ switch (event.type()) {
+ case ui::ET_GESTURE_SCROLL_BEGIN:
+ pagination_model_->StartScroll();
+ return true;
+ case ui::ET_GESTURE_SCROLL_UPDATE: {
+ float scroll = scroll_axis_ == SCROLL_AXIS_HORIZONTAL
+ ? details.scroll_x()
+ : details.scroll_y();
+ int width = scroll_axis_ == SCROLL_AXIS_HORIZONTAL ? bounds.width()
+ : bounds.height();
+ // scroll > 0 means moving contents right or down. That is, transitioning
+ // to the previous page.
+ pagination_model_->UpdateScroll(scroll / width);
+ return true;
+ }
+ case ui::ET_GESTURE_SCROLL_END:
+ pagination_model_->EndScroll(pagination_model_->transition().progress <
+ kFinishTransitionThreshold);
+ return true;
+ case ui::ET_SCROLL_FLING_START: {
+ float velocity = scroll_axis_ == SCROLL_AXIS_HORIZONTAL
+ ? details.velocity_x()
+ : details.velocity_y();
+ pagination_model_->EndScroll(true);
+ if (fabs(velocity) > kMinHorizVelocityToSwitchPage)
+ pagination_model_->SelectPageRelative(velocity < 0 ? 1 : -1, true);
+ return true;
+ }
+ default:
+ return false;
+ }
+}
+
+} // namespace app_list
« no previous file with comments | « ui/app_list/pagination_controller.h ('k') | ui/app_list/views/apps_grid_view.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698