Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / ui / app_list / views / contents_view.cc
blobe6ae0a847c6114b7700cba88486a2344bf539f39
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>
8 #include <vector>
10 #include "base/logging.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/events/event.h"
22 #include "ui/resources/grit/ui_resources.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 std::vector<views::View*> custom_page_views =
60 view_delegate->CreateCustomPageWebViews(GetLocalBounds().size());
61 for (std::vector<views::View*>::const_iterator it =
62 custom_page_views.begin();
63 it != custom_page_views.end();
64 ++it) {
65 AddLauncherPage(*it, IDR_APP_LIST_NOTIFICATIONS_ICON);
68 start_page_view_ = new StartPageView(app_list_main_view_, view_delegate);
69 AddLauncherPage(
70 start_page_view_, IDR_APP_LIST_SEARCH_ICON, NAMED_PAGE_START);
71 } else {
72 search_results_view_ =
73 new SearchResultListView(app_list_main_view_, view_delegate);
74 AddLauncherPage(search_results_view_, 0, NAMED_PAGE_SEARCH_RESULTS);
75 search_results_view_->SetResults(model->results());
78 apps_container_view_ = new AppsContainerView(app_list_main_view_, model);
80 int initial_page_index = AddLauncherPage(
81 apps_container_view_, IDR_APP_LIST_APPS_ICON, NAMED_PAGE_APPS);
82 if (app_list::switches::IsExperimentalAppListEnabled())
83 initial_page_index = GetPageIndexForNamedPage(NAMED_PAGE_START);
85 page_before_search_ = initial_page_index;
86 pagination_model_.SelectPage(initial_page_index, false);
88 // Needed to update the main search box visibility.
89 ActivePageChanged(false);
92 void ContentsView::CancelDrag() {
93 if (apps_container_view_->apps_grid_view()->has_dragged_view())
94 apps_container_view_->apps_grid_view()->EndDrag(true);
95 if (apps_container_view_->app_list_folder_view()
96 ->items_grid_view()
97 ->has_dragged_view()) {
98 apps_container_view_->app_list_folder_view()->items_grid_view()->EndDrag(
99 true);
103 void ContentsView::SetDragAndDropHostOfCurrentAppList(
104 ApplicationDragAndDropHost* drag_and_drop_host) {
105 apps_container_view_->SetDragAndDropHostOfCurrentAppList(drag_and_drop_host);
108 void ContentsView::SetContentsSwitcherView(
109 ContentsSwitcherView* contents_switcher_view) {
110 DCHECK(!contents_switcher_view_);
111 contents_switcher_view_ = contents_switcher_view;
112 if (contents_switcher_view_)
113 pagination_model_.AddObserver(contents_switcher_view_);
116 void ContentsView::SetActivePage(int page_index) {
117 if (GetActivePageIndex() == page_index)
118 return;
120 SetActivePageInternal(page_index, false);
123 int ContentsView::GetActivePageIndex() const {
124 // The active page is changed at the beginning of an animation, not the end.
125 return pagination_model_.SelectedTargetPage();
128 bool ContentsView::IsNamedPageActive(NamedPage named_page) const {
129 std::map<NamedPage, int>::const_iterator it =
130 named_page_to_view_.find(named_page);
131 if (it == named_page_to_view_.end())
132 return false;
133 return it->second == GetActivePageIndex();
136 int ContentsView::GetPageIndexForNamedPage(NamedPage named_page) const {
137 // Find the index of the view corresponding to the given named_page.
138 std::map<NamedPage, int>::const_iterator it =
139 named_page_to_view_.find(named_page);
140 // GetPageIndexForNamedPage should never be called on a named_page that does
141 // not have a corresponding view.
142 DCHECK(it != named_page_to_view_.end());
143 return it->second;
146 int ContentsView::NumLauncherPages() const {
147 return pagination_model_.total_pages();
150 void ContentsView::SetActivePageInternal(int page_index,
151 bool show_search_results) {
152 if (!show_search_results)
153 page_before_search_ = page_index;
154 // Start animating to the new page.
155 pagination_model_.SelectPage(page_index, true);
156 ActivePageChanged(show_search_results);
159 void ContentsView::ActivePageChanged(bool show_search_results) {
160 // TODO(xiyuan): Highlight default match instead of the first.
161 if (IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS) &&
162 search_results_view_->visible()) {
163 search_results_view_->SetSelectedIndex(0);
165 if (search_results_view_)
166 search_results_view_->UpdateAutoLaunchState();
168 if (IsNamedPageActive(NAMED_PAGE_START)) {
169 if (show_search_results)
170 start_page_view_->ShowSearchResults();
171 else
172 start_page_view_->Reset();
175 // Notify parent AppListMainView of the page change.
176 app_list_main_view_->UpdateSearchBoxVisibility();
179 void ContentsView::ShowSearchResults(bool show) {
180 int search_page = GetPageIndexForNamedPage(
181 app_list::switches::IsExperimentalAppListEnabled()
182 ? NAMED_PAGE_START
183 : NAMED_PAGE_SEARCH_RESULTS);
185 SetActivePageInternal(show ? search_page : page_before_search_, show);
188 bool ContentsView::IsShowingSearchResults() const {
189 return app_list::switches::IsExperimentalAppListEnabled()
190 ? IsNamedPageActive(NAMED_PAGE_START) &&
191 start_page_view_->IsShowingSearchResults()
192 : IsNamedPageActive(NAMED_PAGE_SEARCH_RESULTS);
195 void ContentsView::UpdatePageBounds() {
196 gfx::Rect rect(GetContentsBounds());
197 if (rect.IsEmpty())
198 return;
200 // The bounds calculations will potentially be mid-transition (depending on
201 // the state of the PaginationModel).
202 int current_page = std::max(0, pagination_model_.selected_page());
203 int target_page = current_page;
204 double progress = 1;
205 if (pagination_model_.has_transition()) {
206 const PaginationModel::Transition& transition =
207 pagination_model_.transition();
208 if (pagination_model_.is_valid_page(transition.target_page)) {
209 target_page = transition.target_page;
210 progress = transition.progress;
214 gfx::Rect incoming_target(rect);
215 gfx::Rect outgoing_target(rect);
216 int dir = target_page > current_page ? -1 : 1;
218 // Pages transition vertically.
219 int page_height = rect.height();
220 int transition_offset = progress * page_height * dir;
222 outgoing_target.set_y(transition_offset);
223 incoming_target.set_y(dir < 0 ? transition_offset + page_height
224 : transition_offset - page_height);
226 view_model_->view_at(current_page)->SetBoundsRect(outgoing_target);
227 view_model_->view_at(target_page)->SetBoundsRect(incoming_target);
230 PaginationModel* ContentsView::GetAppsPaginationModel() {
231 return apps_container_view_->apps_grid_view()->pagination_model();
234 void ContentsView::ShowFolderContent(AppListFolderItem* item) {
235 apps_container_view_->ShowActiveFolder(item);
238 void ContentsView::Prerender() {
239 const int selected_page =
240 std::max(0, GetAppsPaginationModel()->selected_page());
241 apps_container_view_->apps_grid_view()->Prerender(selected_page);
244 views::View* ContentsView::GetPageView(int index) {
245 return view_model_->view_at(index);
248 void ContentsView::AddBlankPageForTesting() {
249 AddLauncherPage(new views::View, 0);
252 int ContentsView::AddLauncherPage(views::View* view, int resource_id) {
253 int page_index = view_model_->view_size();
254 AddChildView(view);
255 view_model_->Add(view, page_index);
256 if (contents_switcher_view_)
257 contents_switcher_view_->AddSwitcherButton(resource_id, page_index);
258 pagination_model_.SetTotalPages(view_model_->view_size());
259 return page_index;
262 int ContentsView::AddLauncherPage(views::View* view,
263 int resource_id,
264 NamedPage named_page) {
265 int page_index = AddLauncherPage(view, resource_id);
266 named_page_to_view_.insert(std::pair<NamedPage, int>(named_page, page_index));
267 return page_index;
270 gfx::Size ContentsView::GetPreferredSize() const {
271 const gfx::Size container_size =
272 apps_container_view_->apps_grid_view()->GetPreferredSize();
273 const gfx::Size results_size = search_results_view_
274 ? search_results_view_->GetPreferredSize()
275 : gfx::Size();
277 int width = std::max(container_size.width(), results_size.width());
278 int height = std::max(container_size.height(), results_size.height());
279 return gfx::Size(width, height);
282 void ContentsView::Layout() {
283 // Immediately finish all current animations.
284 pagination_model_.FinishAnimation();
286 // Move the current view onto the screen, and all other views off screen to
287 // the left. (Since we are not animating, we don't need to be careful about
288 // which side we place the off-screen views onto.)
289 gfx::Rect rect(GetContentsBounds());
290 if (rect.IsEmpty())
291 return;
293 gfx::Rect offscreen_target(rect);
294 offscreen_target.set_x(-rect.width());
296 for (int i = 0; i < view_model_->view_size(); ++i) {
297 view_model_->view_at(i)->SetBoundsRect(
298 i == pagination_model_.SelectedTargetPage() ? rect : offscreen_target);
302 bool ContentsView::OnKeyPressed(const ui::KeyEvent& event) {
303 return view_model_->view_at(GetActivePageIndex())->OnKeyPressed(event);
306 bool ContentsView::OnMouseWheel(const ui::MouseWheelEvent& event) {
307 if (!IsNamedPageActive(NAMED_PAGE_APPS))
308 return false;
310 int offset;
311 if (abs(event.x_offset()) > abs(event.y_offset()))
312 offset = event.x_offset();
313 else
314 offset = event.y_offset();
316 if (abs(offset) > kMinMouseWheelToSwitchPage) {
317 if (!GetAppsPaginationModel()->has_transition()) {
318 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
320 return true;
323 return false;
326 void ContentsView::TotalPagesChanged() {
329 void ContentsView::SelectedPageChanged(int old_selected, int new_selected) {
332 void ContentsView::TransitionStarted() {
335 void ContentsView::TransitionChanged() {
336 UpdatePageBounds();
339 void ContentsView::OnGestureEvent(ui::GestureEvent* event) {
340 if (!IsNamedPageActive(NAMED_PAGE_APPS))
341 return;
343 switch (event->type()) {
344 case ui::ET_GESTURE_SCROLL_BEGIN:
345 GetAppsPaginationModel()->StartScroll();
346 event->SetHandled();
347 return;
348 case ui::ET_GESTURE_SCROLL_UPDATE:
349 // event->details.scroll_x() > 0 means moving contents to right. That is,
350 // transitioning to previous page.
351 GetAppsPaginationModel()->UpdateScroll(event->details().scroll_x() /
352 GetContentsBounds().width());
353 event->SetHandled();
354 return;
355 case ui::ET_GESTURE_SCROLL_END:
356 GetAppsPaginationModel()->EndScroll(
357 GetAppsPaginationModel()->transition().progress <
358 kFinishTransitionThreshold);
359 event->SetHandled();
360 return;
361 case ui::ET_SCROLL_FLING_START: {
362 GetAppsPaginationModel()->EndScroll(true);
363 if (fabs(event->details().velocity_x()) > kMinHorizVelocityToSwitchPage) {
364 GetAppsPaginationModel()->SelectPageRelative(
365 event->details().velocity_x() < 0 ? 1 : -1, true);
367 event->SetHandled();
368 return;
370 default:
371 break;
375 void ContentsView::OnScrollEvent(ui::ScrollEvent* event) {
376 if (!IsNamedPageActive(NAMED_PAGE_APPS) ||
377 event->type() == ui::ET_SCROLL_FLING_CANCEL) {
378 return;
381 float offset;
382 if (std::abs(event->x_offset()) > std::abs(event->y_offset()))
383 offset = event->x_offset();
384 else
385 offset = event->y_offset();
387 if (std::abs(offset) > kMinScrollToSwitchPage) {
388 if (!GetAppsPaginationModel()->has_transition()) {
389 GetAppsPaginationModel()->SelectPageRelative(offset > 0 ? -1 : 1, true);
391 event->SetHandled();
392 event->StopPropagation();
396 } // namespace app_list