Commit e407013f authored by mgiuca's avatar mgiuca Committed by Commit bot

Refactor app list event handling and prerendering.

ContentsView should not be responsible for apps-grid-specific logic.

To this end, moved scroll and gesture event handling from ContentsView
to AppsGridView, removing the special cases for NAMED_PAGE_APPS. Also
moved some of the prerender logic from ContentsView to AppsGridView.

BUG=377381

Review URL: https://codereview.chromium.org/497413003

Cr-Commit-Position: refs/heads/master@{#292591}
parent 885fee17
...@@ -92,6 +92,12 @@ const int kFolderItemReparentDelay = 50; ...@@ -92,6 +92,12 @@ const int kFolderItemReparentDelay = 50;
// UI. // UI.
const int kFolderDroppingCircleRadius = 15; const int kFolderDroppingCircleRadius = 15;
// Constants for dealing with scroll events.
const int kMinMouseWheelToSwitchPage = 20;
const int kMinScrollToSwitchPage = 20;
const int kMinHorizVelocityToSwitchPage = 800;
const double kFinishTransitionThreshold = 0.33;
// RowMoveAnimationDelegate is used when moving an item into a different row. // RowMoveAnimationDelegate is used when moving an item into a different row.
// Before running the animation, the item's layer is re-created and kept in // Before running the animation, the item's layer is re-created and kept in
...@@ -846,11 +852,12 @@ void AppsGridView::SetDragAndDropHostOfCurrentAppList( ...@@ -846,11 +852,12 @@ void AppsGridView::SetDragAndDropHostOfCurrentAppList(
drag_and_drop_host_ = drag_and_drop_host; drag_and_drop_host_ = drag_and_drop_host;
} }
void AppsGridView::Prerender(int page_index) { void AppsGridView::Prerender() {
Layout(); Layout();
int start = std::max(0, (page_index - kPrerenderPages) * tiles_per_page()); int selected_page = std::max(0, pagination_model_.selected_page());
int start = std::max(0, (selected_page - kPrerenderPages) * tiles_per_page());
int end = std::min(view_model_.view_size(), int end = std::min(view_model_.view_size(),
(page_index + 1 + kPrerenderPages) * tiles_per_page()); (selected_page + 1 + kPrerenderPages) * tiles_per_page());
for (int i = start; i < end; i++) { for (int i = start; i < end; i++) {
AppListItemView* v = static_cast<AppListItemView*>(view_model_.view_at(i)); AppListItemView* v = static_cast<AppListItemView*>(view_model_.view_at(i));
v->Prerender(); v->Prerender();
...@@ -953,6 +960,23 @@ bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) { ...@@ -953,6 +960,23 @@ bool AppsGridView::OnKeyReleased(const ui::KeyEvent& event) {
return handled; return handled;
} }
bool AppsGridView::OnMouseWheel(const ui::MouseWheelEvent& event) {
int offset;
if (abs(event.x_offset()) > abs(event.y_offset()))
offset = event.x_offset();
else
offset = event.y_offset();
if (abs(offset) > kMinMouseWheelToSwitchPage) {
if (!pagination_model_.has_transition()) {
pagination_model_.SelectPageRelative(offset > 0 ? -1 : 1, true);
}
return true;
}
return false;
}
void AppsGridView::ViewHierarchyChanged( void AppsGridView::ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) { const ViewHierarchyChangedDetails& details) {
if (!details.is_add && details.parent == this) { if (!details.is_add && details.parent == this) {
...@@ -971,6 +995,57 @@ void AppsGridView::ViewHierarchyChanged( ...@@ -971,6 +995,57 @@ void AppsGridView::ViewHierarchyChanged(
} }
} }
void AppsGridView::OnGestureEvent(ui::GestureEvent* event) {
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_BEGIN:
pagination_model_.StartScroll();
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_UPDATE:
// event->details.scroll_x() > 0 means moving contents to right. That is,
// transitioning to previous page.
pagination_model_.UpdateScroll(event->details().scroll_x() /
GetContentsBounds().width());
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_END:
pagination_model_.EndScroll(pagination_model_.transition().progress <
kFinishTransitionThreshold);
event->SetHandled();
return;
case ui::ET_SCROLL_FLING_START: {
pagination_model_.EndScroll(true);
if (fabs(event->details().velocity_x()) > kMinHorizVelocityToSwitchPage) {
pagination_model_.SelectPageRelative(
event->details().velocity_x() < 0 ? 1 : -1, true);
}
event->SetHandled();
return;
}
default:
break;
}
}
void AppsGridView::OnScrollEvent(ui::ScrollEvent* event) {
if (event->type() == ui::ET_SCROLL_FLING_CANCEL)
return;
float offset;
if (std::abs(event->x_offset()) > std::abs(event->y_offset()))
offset = event->x_offset();
else
offset = event->y_offset();
if (std::abs(offset) > kMinScrollToSwitchPage) {
if (!pagination_model_.has_transition()) {
pagination_model_.SelectPageRelative(offset > 0 ? -1 : 1, true);
}
event->SetHandled();
event->StopPropagation();
}
}
void AppsGridView::Update() { void AppsGridView::Update() {
DCHECK(!selected_view_ && !drag_view_); DCHECK(!selected_view_ && !drag_view_);
view_model_.Clear(); view_model_.Clear();
......
...@@ -117,8 +117,8 @@ class APP_LIST_EXPORT AppsGridView : public views::View, ...@@ -117,8 +117,8 @@ class APP_LIST_EXPORT AppsGridView : public views::View,
void SetDragAndDropHostOfCurrentAppList( void SetDragAndDropHostOfCurrentAppList(
ApplicationDragAndDropHost* drag_and_drop_host); ApplicationDragAndDropHost* drag_and_drop_host);
// Prerenders the icons on and around |page_index|. // Prerenders the icons on and around the currently selected page.
void Prerender(int page_index); void Prerender();
// Return true if the |bounds_animator_| is animating |view|. // Return true if the |bounds_animator_| is animating |view|.
bool IsAnimatingView(views::View* view); bool IsAnimatingView(views::View* view);
...@@ -134,6 +134,7 @@ class APP_LIST_EXPORT AppsGridView : public views::View, ...@@ -134,6 +134,7 @@ class APP_LIST_EXPORT AppsGridView : public views::View,
virtual void Layout() OVERRIDE; virtual void Layout() OVERRIDE;
virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE;
virtual bool OnMouseWheel(const ui::MouseWheelEvent& event) OVERRIDE;
virtual void ViewHierarchyChanged( virtual void ViewHierarchyChanged(
const ViewHierarchyChangedDetails& details) OVERRIDE; const ViewHierarchyChangedDetails& details) OVERRIDE;
virtual bool GetDropFormats( virtual bool GetDropFormats(
...@@ -142,6 +143,10 @@ class APP_LIST_EXPORT AppsGridView : public views::View, ...@@ -142,6 +143,10 @@ class APP_LIST_EXPORT AppsGridView : public views::View,
virtual bool CanDrop(const OSExchangeData& data) OVERRIDE; virtual bool CanDrop(const OSExchangeData& data) OVERRIDE;
virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE; virtual int OnDragUpdated(const ui::DropTargetEvent& event) OVERRIDE;
// Overridden from ui::EventHandler:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
// Stops the timer that triggers a page flip during a drag. // Stops the timer that triggers a page flip during a drag.
void StopPageFlipTimer(); void StopPageFlipTimer();
......
...@@ -25,16 +25,6 @@ ...@@ -25,16 +25,6 @@
namespace app_list { namespace app_list {
namespace {
const int kMinMouseWheelToSwitchPage = 20;
const int kMinScrollToSwitchPage = 20;
const int kMinHorizVelocityToSwitchPage = 800;
const double kFinishTransitionThreshold = 0.33;
} // namespace
ContentsView::ContentsView(AppListMainView* app_list_main_view) ContentsView::ContentsView(AppListMainView* app_list_main_view)
: search_results_view_(NULL), : search_results_view_(NULL),
start_page_view_(NULL), start_page_view_(NULL),
...@@ -236,9 +226,7 @@ void ContentsView::ShowFolderContent(AppListFolderItem* item) { ...@@ -236,9 +226,7 @@ void ContentsView::ShowFolderContent(AppListFolderItem* item) {
} }
void ContentsView::Prerender() { void ContentsView::Prerender() {
const int selected_page = apps_container_view_->apps_grid_view()->Prerender();
std::max(0, GetAppsPaginationModel()->selected_page());
apps_container_view_->apps_grid_view()->Prerender(selected_page);
} }
views::View* ContentsView::GetPageView(int index) { views::View* ContentsView::GetPageView(int index) {
...@@ -303,26 +291,6 @@ bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) { ...@@ -303,26 +291,6 @@ bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
return view_model_->view_at(GetActivePageIndex())->OnKeyPressed(event); return view_model_->view_at(GetActivePageIndex())->OnKeyPressed(event);
} }
bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) {
if (!IsNamedPageActive(NAMED_PAGE_APPS))
return false;
int offset;
if (abs(event.x_offset()) > abs(event.y_offset()))
offset = event.x_offset();
else
offset = event.y_offset();
if (abs(offset) > kMinMouseWheelToSwitchPage) {
if (!GetAppsPaginationModel()->has_transition()) {
GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
}
return true;
}
return false;
}
void ContentsView::TotalPagesChanged() { void ContentsView::TotalPagesChanged() {
} }
...@@ -336,61 +304,4 @@ void ContentsView::TransitionChanged() { ...@@ -336,61 +304,4 @@ void ContentsView::TransitionChanged() {
UpdatePageBounds(); UpdatePageBounds();
} }
void ContentsView::OnGestureEvent(ui::GestureEvent* event) {
if (!IsNamedPageActive(NAMED_PAGE_APPS))
return;
switch (event->type()) {
case ui::ET_GESTURE_SCROLL_BEGIN:
GetAppsPaginationModel()->StartScroll();
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_UPDATE:
// event->details.scroll_x() > 0 means moving contents to right. That is,
// transitioning to previous page.
GetAppsPaginationModel()->UpdateScroll(event->details().scroll_x() /
GetContentsBounds().width());
event->SetHandled();
return;
case ui::ET_GESTURE_SCROLL_END:
GetAppsPaginationModel()->EndScroll(
GetAppsPaginationModel()->transition().progress <
kFinishTransitionThreshold);
event->SetHandled();
return;
case ui::ET_SCROLL_FLING_START: {
GetAppsPaginationModel()->EndScroll(true);
if (fabs(event->details().velocity_x()) > kMinHorizVelocityToSwitchPage) {
GetAppsPaginationModel()->SelectPageRelative(
event->details().velocity_x() < 0 ? 1 : -1, true);
}
event->SetHandled();
return;
}
default:
break;
}
}
void ContentsView::OnScrollEvent(ui::ScrollEvent* event) {
if (!IsNamedPageActive(NAMED_PAGE_APPS) ||
event->type() == ui::ET_SCROLL_FLING_CANCEL) {
return;
}
float offset;
if (std::abs(event->x_offset()) > std::abs(event->y_offset()))
offset = event->x_offset();
else
offset = event->y_offset();
if (std::abs(offset) > kMinScrollToSwitchPage) {
if (!GetAppsPaginationModel()->has_transition()) {
GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
}
event->SetHandled();
event->StopPropagation();
}
}
} // namespace app_list } // namespace app_list
...@@ -103,7 +103,6 @@ class APP_LIST_EXPORT ContentsView : public views::View, ...@@ -103,7 +103,6 @@ class APP_LIST_EXPORT ContentsView : public views::View,
virtual gfx::Size GetPreferredSize() const OVERRIDE; virtual gfx::Size GetPreferredSize() const OVERRIDE;
virtual void Layout() OVERRIDE; virtual void Layout() OVERRIDE;
virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
virtual bool OnMouseWheel(const ui::MouseWheelEvent& event) OVERRIDE;
// Overridden from PaginationModelObserver: // Overridden from PaginationModelObserver:
virtual void TotalPagesChanged() OVERRIDE; virtual void TotalPagesChanged() OVERRIDE;
...@@ -143,10 +142,6 @@ class APP_LIST_EXPORT ContentsView : public views::View, ...@@ -143,10 +142,6 @@ class APP_LIST_EXPORT ContentsView : public views::View,
// launcher-page pagination. // launcher-page pagination.
PaginationModel* GetAppsPaginationModel(); PaginationModel* GetAppsPaginationModel();
// Overridden from ui::EventHandler:
virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
virtual void OnScrollEvent(ui::ScrollEvent* event) OVERRIDE;
// Special sub views of the ContentsView. All owned by the views hierarchy. // Special sub views of the ContentsView. All owned by the views hierarchy.
AppsContainerView* apps_container_view_; AppsContainerView* apps_container_view_;
SearchResultListView* search_results_view_; SearchResultListView* search_results_view_;
......
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