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

Side by Side Diff: ui/app_list/views/contents_view.cc

Issue 316393002: App list uses PaginationModel to transition between pages. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Rebase. Created 6 years, 6 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « ui/app_list/views/contents_view.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "ui/app_list/views/contents_view.h" 5 #include "ui/app_list/views/contents_view.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 8
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "ui/app_list/app_list_constants.h" 10 #include "ui/app_list/app_list_constants.h"
11 #include "ui/app_list/app_list_switches.h" 11 #include "ui/app_list/app_list_switches.h"
12 #include "ui/app_list/app_list_view_delegate.h" 12 #include "ui/app_list/app_list_view_delegate.h"
13 #include "ui/app_list/pagination_model.h"
14 #include "ui/app_list/views/app_list_folder_view.h" 13 #include "ui/app_list/views/app_list_folder_view.h"
15 #include "ui/app_list/views/app_list_main_view.h" 14 #include "ui/app_list/views/app_list_main_view.h"
16 #include "ui/app_list/views/apps_container_view.h" 15 #include "ui/app_list/views/apps_container_view.h"
17 #include "ui/app_list/views/apps_grid_view.h" 16 #include "ui/app_list/views/apps_grid_view.h"
18 #include "ui/app_list/views/search_result_list_view.h" 17 #include "ui/app_list/views/search_result_list_view.h"
19 #include "ui/app_list/views/start_page_view.h" 18 #include "ui/app_list/views/start_page_view.h"
20 #include "ui/events/event.h" 19 #include "ui/events/event.h"
21 #include "ui/views/animation/bounds_animator.h"
22 #include "ui/views/view_model.h" 20 #include "ui/views/view_model.h"
23 #include "ui/views/view_model_utils.h" 21 #include "ui/views/view_model_utils.h"
24 22
25 namespace app_list { 23 namespace app_list {
26 24
27 namespace { 25 namespace {
28 26
29 const int kMinMouseWheelToSwitchPage = 20; 27 const int kMinMouseWheelToSwitchPage = 20;
30 const int kMinScrollToSwitchPage = 20; 28 const int kMinScrollToSwitchPage = 20;
31 const int kMinHorizVelocityToSwitchPage = 800; 29 const int kMinHorizVelocityToSwitchPage = 800;
32 30
33 const double kFinishTransitionThreshold = 0.33; 31 const double kFinishTransitionThreshold = 0.33;
34 32
35 } // namespace 33 } // namespace
36 34
37 ContentsView::ContentsView(AppListMainView* app_list_main_view, 35 ContentsView::ContentsView(AppListMainView* app_list_main_view,
38 AppListModel* model, 36 AppListModel* model,
39 AppListViewDelegate* view_delegate) 37 AppListViewDelegate* view_delegate)
40 : search_results_view_(NULL), 38 : search_results_view_(NULL),
41 start_page_view_(NULL), 39 start_page_view_(NULL),
42 app_list_main_view_(app_list_main_view), 40 app_list_main_view_(app_list_main_view),
43 view_model_(new views::ViewModel), 41 view_model_(new views::ViewModel) {
44 bounds_animator_(new views::BoundsAnimator(this)) {
45 DCHECK(model); 42 DCHECK(model);
46 43
47 if (app_list::switches::IsExperimentalAppListEnabled()) { 44 if (app_list::switches::IsExperimentalAppListEnabled()) {
48 start_page_view_ = new StartPageView(app_list_main_view, view_delegate); 45 start_page_view_ = new StartPageView(app_list_main_view, view_delegate);
49 AddLauncherPage(start_page_view_, NAMED_PAGE_START); 46 AddLauncherPage(start_page_view_, NAMED_PAGE_START);
50 } else { 47 } else {
51 search_results_view_ = 48 search_results_view_ =
52 new SearchResultListView(app_list_main_view, view_delegate); 49 new SearchResultListView(app_list_main_view, view_delegate);
53 AddLauncherPage(search_results_view_, NAMED_PAGE_SEARCH_RESULTS); 50 AddLauncherPage(search_results_view_, NAMED_PAGE_SEARCH_RESULTS);
54 search_results_view_->SetResults(model->results()); 51 search_results_view_->SetResults(model->results());
55 } 52 }
56 53
57 apps_container_view_ = new AppsContainerView(app_list_main_view, model); 54 apps_container_view_ = new AppsContainerView(app_list_main_view, model);
58 active_page_ = AddLauncherPage(apps_container_view_, NAMED_PAGE_APPS); 55 int apps_page_index = AddLauncherPage(apps_container_view_, NAMED_PAGE_APPS);
59 56
57 pagination_model_.SelectPage(apps_page_index, false);
58 pagination_model_.AddObserver(this);
60 } 59 }
61 60
62 ContentsView::~ContentsView() { 61 ContentsView::~ContentsView() {
62 pagination_model_.RemoveObserver(this);
63 } 63 }
64 64
65 void ContentsView::CancelDrag() { 65 void ContentsView::CancelDrag() {
66 if (apps_container_view_->apps_grid_view()->has_dragged_view()) 66 if (apps_container_view_->apps_grid_view()->has_dragged_view())
67 apps_container_view_->apps_grid_view()->EndDrag(true); 67 apps_container_view_->apps_grid_view()->EndDrag(true);
68 if (apps_container_view_->app_list_folder_view() 68 if (apps_container_view_->app_list_folder_view()
69 ->items_grid_view() 69 ->items_grid_view()
70 ->has_dragged_view()) { 70 ->has_dragged_view()) {
71 apps_container_view_->app_list_folder_view()->items_grid_view()->EndDrag( 71 apps_container_view_->app_list_folder_view()->items_grid_view()->EndDrag(
72 true); 72 true);
73 } 73 }
74 } 74 }
75 75
76 void ContentsView::SetDragAndDropHostOfCurrentAppList( 76 void ContentsView::SetDragAndDropHostOfCurrentAppList(
77 ApplicationDragAndDropHost* drag_and_drop_host) { 77 ApplicationDragAndDropHost* drag_and_drop_host) {
78 apps_container_view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host); 78 apps_container_view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
79 } 79 }
80 80
81 void ContentsView::SetActivePage(int page_index) { 81 void ContentsView::SetActivePage(int page_index) {
82 if (active_page_ == page_index) 82 if (GetActivePageIndex() == page_index)
83 return; 83 return;
84 84
85 SetActivePageInternal(page_index, false); 85 SetActivePageInternal(page_index, false);
86 } 86 }
87 87
88 int ContentsView::GetActivePageIndex() const {
89 // The active page is changed at the beginning of an animation, not the end.
90 return pagination_model_.SelectedTargetPage();
91 }
92
88 bool ContentsView::IsNamedPageActive(NamedPage named_page) const { 93 bool ContentsView::IsNamedPageActive(NamedPage named_page) const {
89 std::map<NamedPage, int>::const_iterator it = 94 std::map<NamedPage, int>::const_iterator it =
90 named_page_to_view_.find(named_page); 95 named_page_to_view_.find(named_page);
91 if (it == named_page_to_view_.end()) 96 if (it == named_page_to_view_.end())
92 return false; 97 return false;
93 return it->second == active_page_; 98 return it->second == GetActivePageIndex();
94 } 99 }
95 100
96 int ContentsView::GetPageIndexForNamedPage(NamedPage named_page) const { 101 int ContentsView::GetPageIndexForNamedPage(NamedPage named_page) const {
97 // Find the index of the view corresponding to the given named_page. 102 // Find the index of the view corresponding to the given named_page.
98 std::map<NamedPage, int>::const_iterator it = 103 std::map<NamedPage, int>::const_iterator it =
99 named_page_to_view_.find(named_page); 104 named_page_to_view_.find(named_page);
100 // GetPageIndexForNamedPage should never be called on a named_page that does 105 // GetPageIndexForNamedPage should never be called on a named_page that does
101 // not have a corresponding view. 106 // not have a corresponding view.
102 DCHECK(it != named_page_to_view_.end()); 107 DCHECK(it != named_page_to_view_.end());
103 return it->second; 108 return it->second;
104 } 109 }
105 110
111 int ContentsView::NumLauncherPages() const {
112 return pagination_model_.total_pages();
113 }
114
106 void ContentsView::SetActivePageInternal(int page_index, 115 void ContentsView::SetActivePageInternal(int page_index,
107 bool show_search_results) { 116 bool show_search_results) {
108 active_page_ = page_index; 117 // Start animating to the new page.
118 pagination_model_.SelectPage(page_index, true);
109 ActivePageChanged(show_search_results); 119 ActivePageChanged(show_search_results);
110 } 120 }
111 121
112 void ContentsView::ActivePageChanged(bool show_search_results) { 122 void ContentsView::ActivePageChanged(bool show_search_results) {
113 // TODO(xiyuan): Highlight default match instead of the first. 123 // TODO(xiyuan): Highlight default match instead of the first.
114 if (IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS) && 124 if (IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS) &&
115 search_results_view_->visible()) { 125 search_results_view_->visible()) {
116 search_results_view_->SetSelectedIndex(0); 126 search_results_view_->SetSelectedIndex(0);
117 } 127 }
118 if (search_results_view_) 128 if (search_results_view_)
119 search_results_view_->UpdateAutoLaunchState(); 129 search_results_view_->UpdateAutoLaunchState();
120 130
121 if (IsNamedPageActive(NAMED_PAGE_START)) { 131 if (IsNamedPageActive(NAMED_PAGE_START)) {
122 if (show_search_results) 132 if (show_search_results)
123 start_page_view_->ShowSearchResults(); 133 start_page_view_->ShowSearchResults();
124 else 134 else
125 start_page_view_->Reset(); 135 start_page_view_->Reset();
126 } 136 }
127 137
128 // Notify parent AppListMainView of the page change. 138 // Notify parent AppListMainView of the page change.
129 app_list_main_view_->UpdateSearchBoxVisibility(); 139 app_list_main_view_->UpdateSearchBoxVisibility();
130
131 AnimateToIdealBounds();
132 } 140 }
133 141
134 void ContentsView::ShowSearchResults(bool show) { 142 void ContentsView::ShowSearchResults(bool show) {
135 NamedPage new_named_page = show ? NAMED_PAGE_SEARCH_RESULTS : NAMED_PAGE_APPS; 143 NamedPage new_named_page = show ? NAMED_PAGE_SEARCH_RESULTS : NAMED_PAGE_APPS;
136 if (app_list::switches::IsExperimentalAppListEnabled()) 144 if (app_list::switches::IsExperimentalAppListEnabled())
137 new_named_page = NAMED_PAGE_START; 145 new_named_page = NAMED_PAGE_START;
138 146
139 SetActivePageInternal(GetPageIndexForNamedPage(new_named_page), show); 147 SetActivePageInternal(GetPageIndexForNamedPage(new_named_page), show);
140 } 148 }
141 149
142 bool ContentsView::IsShowingSearchResults() const { 150 bool ContentsView::IsShowingSearchResults() const {
143 return app_list::switches::IsExperimentalAppListEnabled() 151 return app_list::switches::IsExperimentalAppListEnabled()
144 ? IsNamedPageActive(NAMED_PAGE_START) && 152 ? IsNamedPageActive(NAMED_PAGE_START) &&
145 start_page_view_->IsShowingSearchResults() 153 start_page_view_->IsShowingSearchResults()
146 : IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS); 154 : IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS);
147 } 155 }
148 156
149 void ContentsView::CalculateIdealBounds() { 157 void ContentsView::UpdatePageBounds() {
150 gfx::Rect rect(GetContentsBounds()); 158 gfx::Rect rect(GetContentsBounds());
151 if (rect.IsEmpty()) 159 if (rect.IsEmpty())
152 return; 160 return;
153 161
154 if (app_list::switches::IsExperimentalAppListEnabled()) { 162 // The bounds calculations will potentially be mid-transition (depending on
155 gfx::Rect incoming_target(rect); 163 // the state of the PaginationModel).
156 gfx::Rect outgoing_target(rect); 164 int current_page = std::max(0, pagination_model_.selected_page());
157 outgoing_target.set_x(-outgoing_target.width()); 165 int target_page = current_page;
158 166 double progress = 1;
159 for (int i = 0; i < view_model_->view_size(); ++i) { 167 if (pagination_model_.has_transition()) {
160 view_model_->set_ideal_bounds( 168 const PaginationModel::Transition& transition =
161 i, i == active_page_ ? incoming_target : outgoing_target); 169 pagination_model_.transition();
170 if (pagination_model_.is_valid_page(transition.target_page)) {
171 target_page = transition.target_page;
172 progress = transition.progress;
162 } 173 }
163 return;
164 } 174 }
165 175
166 gfx::Rect container_frame(rect); 176 gfx::Rect incoming_target(rect);
167 gfx::Rect results_frame(rect); 177 gfx::Rect outgoing_target(rect);
178 int dir = target_page > current_page ? -1 : 1;
168 179
169 // Offsets apps grid and result list based on |active_page_|. 180 if (app_list::switches::IsExperimentalAppListEnabled()) {
170 // SearchResultListView is on top of apps grid. Visible view is left in 181 // The experimental app list transitions horizontally.
171 // visible area and invisible ones is put out of the visible area. 182 int page_width = rect.width();
172 int contents_area_height = rect.height(); 183 int transition_offset = progress * page_width * dir;
173 if (IsNamedPageActive(NAMED_PAGE_APPS))
174 results_frame.Offset(0, -contents_area_height);
175 else if (IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS))
176 container_frame.Offset(0, contents_area_height);
177 else
178 NOTREACHED() << "Page " << active_page_ << " invalid in current app list.";
179 184
180 view_model_->set_ideal_bounds(GetPageIndexForNamedPage(NAMED_PAGE_APPS), 185 outgoing_target.set_x(transition_offset);
181 container_frame); 186 incoming_target.set_x(dir < 0 ? transition_offset + page_width
182 view_model_->set_ideal_bounds( 187 : transition_offset - page_width);
183 GetPageIndexForNamedPage(NAMED_PAGE_SEARCH_RESULTS), results_frame); 188 } else {
184 } 189 // The normal app list transitions vertically.
190 int page_height = rect.height();
191 int transition_offset = progress * page_height * dir;
185 192
186 void ContentsView::AnimateToIdealBounds() { 193 outgoing_target.set_y(transition_offset);
187 CalculateIdealBounds(); 194 incoming_target.set_y(dir < 0 ? transition_offset + page_height
188 for (int i = 0; i < view_model_->view_size(); ++i) { 195 : transition_offset - page_height);
189 bounds_animator_->AnimateViewTo(view_model_->view_at(i),
190 view_model_->ideal_bounds(i));
191 } 196 }
197
198 view_model_->view_at(current_page)->SetBoundsRect(outgoing_target);
199 view_model_->view_at(target_page)->SetBoundsRect(incoming_target);
192 } 200 }
193 201
194 PaginationModel* ContentsView::GetAppsPaginationModel() { 202 PaginationModel* ContentsView::GetAppsPaginationModel() {
195 return apps_container_view_->apps_grid_view()->pagination_model(); 203 return apps_container_view_->apps_grid_view()->pagination_model();
196 } 204 }
197 205
198 void ContentsView::ShowFolderContent(AppListFolderItem* item) { 206 void ContentsView::ShowFolderContent(AppListFolderItem* item) {
199 apps_container_view_->ShowActiveFolder(item); 207 apps_container_view_->ShowActiveFolder(item);
200 } 208 }
201 209
202 void ContentsView::Prerender() { 210 void ContentsView::Prerender() {
203 const int selected_page = 211 const int selected_page =
204 std::max(0, GetAppsPaginationModel()->selected_page()); 212 std::max(0, GetAppsPaginationModel()->selected_page());
205 apps_container_view_->apps_grid_view()->Prerender(selected_page); 213 apps_container_view_->apps_grid_view()->Prerender(selected_page);
206 } 214 }
207 215
216 views::View* ContentsView::GetPageView(int index) {
217 return view_model_->view_at(index);
218 }
219
220 void ContentsView::AddBlankPageForTesting() {
221 AddLauncherPage(new views::View);
222 }
223
208 int ContentsView::AddLauncherPage(views::View* view) { 224 int ContentsView::AddLauncherPage(views::View* view) {
209 int page_index = view_model_->view_size(); 225 int page_index = view_model_->view_size();
210 AddChildView(view); 226 AddChildView(view);
211 view_model_->Add(view, page_index); 227 view_model_->Add(view, page_index);
228 pagination_model_.SetTotalPages(view_model_->view_size());
212 return page_index; 229 return page_index;
213 } 230 }
214 231
215 int ContentsView::AddLauncherPage(views::View* view, NamedPage named_page) { 232 int ContentsView::AddLauncherPage(views::View* view, NamedPage named_page) {
216 int page_index = AddLauncherPage(view); 233 int page_index = AddLauncherPage(view);
217 named_page_to_view_.insert(std::pair<NamedPage, int>(named_page, page_index)); 234 named_page_to_view_.insert(std::pair<NamedPage, int>(named_page, page_index));
218 return page_index; 235 return page_index;
219 } 236 }
220 237
221 gfx::Size ContentsView::GetPreferredSize() const { 238 gfx::Size ContentsView::GetPreferredSize() const {
222 const gfx::Size container_size = 239 const gfx::Size container_size =
223 apps_container_view_->apps_grid_view()->GetPreferredSize(); 240 apps_container_view_->apps_grid_view()->GetPreferredSize();
224 const gfx::Size results_size = search_results_view_ 241 const gfx::Size results_size = search_results_view_
225 ? search_results_view_->GetPreferredSize() 242 ? search_results_view_->GetPreferredSize()
226 : gfx::Size(); 243 : gfx::Size();
227 244
228 int width = std::max(container_size.width(), results_size.width()); 245 int width = std::max(container_size.width(), results_size.width());
229 int height = std::max(container_size.height(), results_size.height()); 246 int height = std::max(container_size.height(), results_size.height());
230 return gfx::Size(width, height); 247 return gfx::Size(width, height);
231 } 248 }
232 249
233 void ContentsView::Layout() { 250 void ContentsView::Layout() {
234 CalculateIdealBounds(); 251 // Immediately finish all current animations.
235 views::ViewModelUtils::SetViewBoundsToIdealBounds(*view_model_); 252 pagination_model_.FinishAnimation();
253
254 // Move the current view onto the screen, and all other views off screen to
255 // the left. (Since we are not animating, we don't need to be careful about
256 // which side we place the off-screen views onto.)
257 gfx::Rect rect(GetContentsBounds());
258 if (rect.IsEmpty())
259 return;
260
261 gfx::Rect offscreen_target(rect);
262 offscreen_target.set_x(-rect.width());
263
264 for (int i = 0; i < view_model_->view_size(); ++i) {
265 view_model_->view_at(i)->SetBoundsRect(
266 i == pagination_model_.SelectedTargetPage() ? rect : offscreen_target);
267 }
236 } 268 }
237 269
238 bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) { 270 bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
239 return view_model_->view_at(active_page_)->OnKeyPressed(event); 271 return view_model_->view_at(GetActivePageIndex())->OnKeyPressed(event);
240 } 272 }
241 273
242 bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) { 274 bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) {
243 if (!IsNamedPageActive(NAMED_PAGE_APPS)) 275 if (!IsNamedPageActive(NAMED_PAGE_APPS))
244 return false; 276 return false;
245 277
246 int offset; 278 int offset;
247 if (abs(event.x_offset()) > abs(event.y_offset())) 279 if (abs(event.x_offset()) > abs(event.y_offset()))
248 offset = event.x_offset(); 280 offset = event.x_offset();
249 else 281 else
250 offset = event.y_offset(); 282 offset = event.y_offset();
251 283
252 if (abs(offset) > kMinMouseWheelToSwitchPage) { 284 if (abs(offset) > kMinMouseWheelToSwitchPage) {
253 if (!GetAppsPaginationModel()->has_transition()) { 285 if (!GetAppsPaginationModel()->has_transition()) {
254 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true); 286 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
255 } 287 }
256 return true; 288 return true;
257 } 289 }
258 290
259 return false; 291 return false;
260 } 292 }
261 293
294 void ContentsView::TotalPagesChanged() {
295 }
296
297 void ContentsView::SelectedPageChanged(int old_selected, int new_selected) {
298 }
299
300 void ContentsView::TransitionStarted() {
301 }
302
303 void ContentsView::TransitionChanged() {
304 UpdatePageBounds();
305 }
306
262 void ContentsView::OnGestureEvent(ui::GestureEvent* event) { 307 void ContentsView::OnGestureEvent(ui::GestureEvent* event) {
263 if (!IsNamedPageActive(NAMED_PAGE_APPS)) 308 if (!IsNamedPageActive(NAMED_PAGE_APPS))
264 return; 309 return;
265 310
266 switch (event->type()) { 311 switch (event->type()) {
267 case ui::ET_GESTURE_SCROLL_BEGIN: 312 case ui::ET_GESTURE_SCROLL_BEGIN:
268 GetAppsPaginationModel()->StartScroll(); 313 GetAppsPaginationModel()->StartScroll();
269 event->SetHandled(); 314 event->SetHandled();
270 return; 315 return;
271 case ui::ET_GESTURE_SCROLL_UPDATE: 316 case ui::ET_GESTURE_SCROLL_UPDATE:
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 if (std::abs(offset) > kMinScrollToSwitchPage) { 355 if (std::abs(offset) > kMinScrollToSwitchPage) {
311 if (!GetAppsPaginationModel()->has_transition()) { 356 if (!GetAppsPaginationModel()->has_transition()) {
312 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true); 357 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
313 } 358 }
314 event->SetHandled(); 359 event->SetHandled();
315 event->StopPropagation(); 360 event->StopPropagation();
316 } 361 }
317 } 362 }
318 363
319 } // namespace app_list 364 } // namespace app_list
OLDNEW
« no previous file with comments | « ui/app_list/views/contents_view.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698