Make app list return to previous page after search.
[chromium-blink-merge.git] / ui / app_list / views / contents_view.cc
blob9057abc8524f7ac4f61f83e91a398fd17a2fc8d2
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
3 // found in the LICENSE file.
5 #include "ui/app_list/views/contents_view.h"
7 #include <algorithm>
9 #include "base/logging.h"
10 #include "grit/ui_resources.h"
11 #include "ui/app_list/app_list_constants.h"
12 #include "ui/app_list/app_list_switches.h"
13 #include "ui/app_list/app_list_view_delegate.h"
14 #include "ui/app_list/views/app_list_folder_view.h"
15 #include "ui/app_list/views/app_list_main_view.h"
16 #include "ui/app_list/views/apps_container_view.h"
17 #include "ui/app_list/views/apps_grid_view.h"
18 #include "ui/app_list/views/contents_switcher_view.h"
19 #include "ui/app_list/views/search_result_list_view.h"
20 #include "ui/app_list/views/start_page_view.h"
21 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/events/event.h"
23 #include "ui/views/view_model.h"
24 #include "ui/views/view_model_utils.h"
26 namespace app_list {
28 namespace {
30 const int kMinMouseWheelToSwitchPage = 20;
31 const int kMinScrollToSwitchPage = 20;
32 const int kMinHorizVelocityToSwitchPage = 800;
34 const double kFinishTransitionThreshold = 0.33;
36 } // namespace
38 ContentsView::ContentsView(AppListMainView* app_list_main_view)
39 : search_results_view_(NULL),
40 start_page_view_(NULL),
41 app_list_main_view_(app_list_main_view),
42 contents_switcher_view_(NULL),
43 view_model_(new views::ViewModel),
44 page_before_search_(0) {
45 pagination_model_.AddObserver(this);
48 ContentsView::~ContentsView() {
49 pagination_model_.RemoveObserver(this);
50 if (contents_switcher_view_)
51 pagination_model_.RemoveObserver(contents_switcher_view_);
54 void ContentsView::InitNamedPages(AppListModel* model,
55 AppListViewDelegate* view_delegate) {
56 DCHECK(model);
58 if (app_list::switches::IsExperimentalAppListEnabled()) {
59 views::View* custom_page_view =
60 view_delegate->CreateCustomPageWebView(GetLocalBounds().size());
61 if (custom_page_view)
62 AddLauncherPage(custom_page_view, IDR_APP_LIST_NOTIFICATIONS_ICON);
64 start_page_view_ = new StartPageView(app_list_main_view_, view_delegate);
65 AddLauncherPage(
66 start_page_view_, IDR_APP_LIST_SEARCH_ICON, NAMED_PAGE_START);
67 } else {
68 search_results_view_ =
69 new SearchResultListView(app_list_main_view_, view_delegate);
70 AddLauncherPage(search_results_view_, 0, NAMED_PAGE_SEARCH_RESULTS);
71 search_results_view_->SetResults(model->results());
74 apps_container_view_ = new AppsContainerView(app_list_main_view_, model);
76 int initial_page_index = AddLauncherPage(
77 apps_container_view_, IDR_APP_LIST_APPS_ICON, NAMED_PAGE_APPS);
78 if (app_list::switches::IsExperimentalAppListEnabled())
79 initial_page_index = GetPageIndexForNamedPage(NAMED_PAGE_START);
81 page_before_search_ = initial_page_index;
82 pagination_model_.SelectPage(initial_page_index, false);
84 // Needed to update the main search box visibility.
85 ActivePageChanged(false);
88 void ContentsView::CancelDrag() {
89 if (apps_container_view_->apps_grid_view()->has_dragged_view())
90 apps_container_view_->apps_grid_view()->EndDrag(true);
91 if (apps_container_view_->app_list_folder_view()
92 ->items_grid_view()
93 ->has_dragged_view()) {
94 apps_container_view_->app_list_folder_view()->items_grid_view()->EndDrag(
95 true);
99 void ContentsView::SetDragAndDropHostOfCurrentAppList(
100 ApplicationDragAndDropHost* drag_and_drop_host) {
101 apps_container_view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
104 void ContentsView::SetContentsSwitcherView(
105 ContentsSwitcherView* contents_switcher_view) {
106 DCHECK(!contents_switcher_view_);
107 contents_switcher_view_ = contents_switcher_view;
108 if (contents_switcher_view_)
109 pagination_model_.AddObserver(contents_switcher_view_);
112 void ContentsView::SetActivePage(int page_index) {
113 if (GetActivePageIndex() == page_index)
114 return;
116 SetActivePageInternal(page_index, false);
119 int ContentsView::GetActivePageIndex() const {
120 // The active page is changed at the beginning of an animation, not the end.
121 return pagination_model_.SelectedTargetPage();
124 bool ContentsView::IsNamedPageActive(NamedPage named_page) const {
125 std::map<NamedPage, int>::const_iterator it =
126 named_page_to_view_.find(named_page);
127 if (it == named_page_to_view_.end())
128 return false;
129 return it->second == GetActivePageIndex();
132 int ContentsView::GetPageIndexForNamedPage(NamedPage named_page) const {
133 // Find the index of the view corresponding to the given named_page.
134 std::map<NamedPage, int>::const_iterator it =
135 named_page_to_view_.find(named_page);
136 // GetPageIndexForNamedPage should never be called on a named_page that does
137 // not have a corresponding view.
138 DCHECK(it != named_page_to_view_.end());
139 return it->second;
142 int ContentsView::NumLauncherPages() const {
143 return pagination_model_.total_pages();
146 void ContentsView::SetActivePageInternal(int page_index,
147 bool show_search_results) {
148 if (!show_search_results)
149 page_before_search_ = page_index;
150 // Start animating to the new page.
151 pagination_model_.SelectPage(page_index, true);
152 ActivePageChanged(show_search_results);
155 void ContentsView::ActivePageChanged(bool show_search_results) {
156 // TODO(xiyuan): Highlight default match instead of the first.
157 if (IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS) &&
158 search_results_view_->visible()) {
159 search_results_view_->SetSelectedIndex(0);
161 if (search_results_view_)
162 search_results_view_->UpdateAutoLaunchState();
164 if (IsNamedPageActive(NAMED_PAGE_START)) {
165 if (show_search_results)
166 start_page_view_->ShowSearchResults();
167 else
168 start_page_view_->Reset();
171 // Notify parent AppListMainView of the page change.
172 app_list_main_view_->UpdateSearchBoxVisibility();
175 void ContentsView::ShowSearchResults(bool show) {
176 int search_page = GetPageIndexForNamedPage(
177 app_list::switches::IsExperimentalAppListEnabled()
178 ? NAMED_PAGE_START
179 : NAMED_PAGE_SEARCH_RESULTS);
181 SetActivePageInternal(show ? search_page : page_before_search_, show);
184 bool ContentsView::IsShowingSearchResults() const {
185 return app_list::switches::IsExperimentalAppListEnabled()
186 ? IsNamedPageActive(NAMED_PAGE_START) &&
187 start_page_view_->IsShowingSearchResults()
188 : IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS);
191 void ContentsView::UpdatePageBounds() {
192 gfx::Rect rect(GetContentsBounds());
193 if (rect.IsEmpty())
194 return;
196 // The bounds calculations will potentially be mid-transition (depending on
197 // the state of the PaginationModel).
198 int current_page = std::max(0, pagination_model_.selected_page());
199 int target_page = current_page;
200 double progress = 1;
201 if (pagination_model_.has_transition()) {
202 const PaginationModel::Transition& transition =
203 pagination_model_.transition();
204 if (pagination_model_.is_valid_page(transition.target_page)) {
205 target_page = transition.target_page;
206 progress = transition.progress;
210 gfx::Rect incoming_target(rect);
211 gfx::Rect outgoing_target(rect);
212 int dir = target_page > current_page ? -1 : 1;
214 if (app_list::switches::IsExperimentalAppListEnabled()) {
215 // The experimental app list transitions horizontally.
216 int page_width = rect.width();
217 int transition_offset = progress * page_width * dir;
219 outgoing_target.set_x(transition_offset);
220 incoming_target.set_x(dir < 0 ? transition_offset + page_width
221 : transition_offset - page_width);
222 } else {
223 // The normal app list transitions vertically.
224 int page_height = rect.height();
225 int transition_offset = progress * page_height * dir;
227 outgoing_target.set_y(transition_offset);
228 incoming_target.set_y(dir < 0 ? transition_offset + page_height
229 : transition_offset - page_height);
232 view_model_->view_at(current_page)->SetBoundsRect(outgoing_target);
233 view_model_->view_at(target_page)->SetBoundsRect(incoming_target);
236 PaginationModel* ContentsView::GetAppsPaginationModel() {
237 return apps_container_view_->apps_grid_view()->pagination_model();
240 void ContentsView::ShowFolderContent(AppListFolderItem* item) {
241 apps_container_view_->ShowActiveFolder(item);
244 void ContentsView::Prerender() {
245 const int selected_page =
246 std::max(0, GetAppsPaginationModel()->selected_page());
247 apps_container_view_->apps_grid_view()->Prerender(selected_page);
250 views::View* ContentsView::GetPageView(int index) {
251 return view_model_->view_at(index);
254 void ContentsView::AddBlankPageForTesting() {
255 AddLauncherPage(new views::View, 0);
258 int ContentsView::AddLauncherPage(views::View* view, int resource_id) {
259 int page_index = view_model_->view_size();
260 AddChildView(view);
261 view_model_->Add(view, page_index);
262 if (contents_switcher_view_)
263 contents_switcher_view_->AddSwitcherButton(resource_id, page_index);
264 pagination_model_.SetTotalPages(view_model_->view_size());
265 return page_index;
268 int ContentsView::AddLauncherPage(views::View* view,
269 int resource_id,
270 NamedPage named_page) {
271 int page_index = AddLauncherPage(view, resource_id);
272 named_page_to_view_.insert(std::pair<NamedPage, int>(named_page, page_index));
273 return page_index;
276 gfx::Size ContentsView::GetPreferredSize() const {
277 const gfx::Size container_size =
278 apps_container_view_->apps_grid_view()->GetPreferredSize();
279 const gfx::Size results_size = search_results_view_
280 ? search_results_view_->GetPreferredSize()
281 : gfx::Size();
283 int width = std::max(container_size.width(), results_size.width());
284 int height = std::max(container_size.height(), results_size.height());
285 return gfx::Size(width, height);
288 void ContentsView::Layout() {
289 // Immediately finish all current animations.
290 pagination_model_.FinishAnimation();
292 // Move the current view onto the screen, and all other views off screen to
293 // the left. (Since we are not animating, we don't need to be careful about
294 // which side we place the off-screen views onto.)
295 gfx::Rect rect(GetContentsBounds());
296 if (rect.IsEmpty())
297 return;
299 gfx::Rect offscreen_target(rect);
300 offscreen_target.set_x(-rect.width());
302 for (int i = 0; i < view_model_->view_size(); ++i) {
303 view_model_->view_at(i)->SetBoundsRect(
304 i == pagination_model_.SelectedTargetPage() ? rect : offscreen_target);
308 bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
309 return view_model_->view_at(GetActivePageIndex())->OnKeyPressed(event);
312 bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) {
313 if (!IsNamedPageActive(NAMED_PAGE_APPS))
314 return false;
316 int offset;
317 if (abs(event.x_offset()) > abs(event.y_offset()))
318 offset = event.x_offset();
319 else
320 offset = event.y_offset();
322 if (abs(offset) > kMinMouseWheelToSwitchPage) {
323 if (!GetAppsPaginationModel()->has_transition()) {
324 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
326 return true;
329 return false;
332 void ContentsView::TotalPagesChanged() {
335 void ContentsView::SelectedPageChanged(int old_selected, int new_selected) {
338 void ContentsView::TransitionStarted() {
341 void ContentsView::TransitionChanged() {
342 UpdatePageBounds();
345 void ContentsView::OnGestureEvent(ui::GestureEvent* event) {
346 if (!IsNamedPageActive(NAMED_PAGE_APPS))
347 return;
349 switch (event->type()) {
350 case ui::ET_GESTURE_SCROLL_BEGIN:
351 GetAppsPaginationModel()->StartScroll();
352 event->SetHandled();
353 return;
354 case ui::ET_GESTURE_SCROLL_UPDATE:
355 // event->details.scroll_x() > 0 means moving contents to right. That is,
356 // transitioning to previous page.
357 GetAppsPaginationModel()->UpdateScroll(event->details().scroll_x() /
358 GetContentsBounds().width());
359 event->SetHandled();
360 return;
361 case ui::ET_GESTURE_SCROLL_END:
362 GetAppsPaginationModel()->EndScroll(
363 GetAppsPaginationModel()->transition().progress <
364 kFinishTransitionThreshold);
365 event->SetHandled();
366 return;
367 case ui::ET_SCROLL_FLING_START: {
368 GetAppsPaginationModel()->EndScroll(true);
369 if (fabs(event->details().velocity_x()) > kMinHorizVelocityToSwitchPage) {
370 GetAppsPaginationModel()->SelectPageRelative(
371 event->details().velocity_x() < 0 ? 1 : -1, true);
373 event->SetHandled();
374 return;
376 default:
377 break;
381 void ContentsView::OnScrollEvent(ui::ScrollEvent* event) {
382 if (!IsNamedPageActive(NAMED_PAGE_APPS) ||
383 event->type() == ui::ET_SCROLL_FLING_CANCEL) {
384 return;
387 float offset;
388 if (std::abs(event->x_offset()) > std::abs(event->y_offset()))
389 offset = event->x_offset();
390 else
391 offset = event->y_offset();
393 if (std::abs(offset) > kMinScrollToSwitchPage) {
394 if (!GetAppsPaginationModel()->has_transition()) {
395 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
397 event->SetHandled();
398 event->StopPropagation();
402 } // namespace app_list