Refactor app list event handling and prerendering.
[chromium-blink-merge.git] / ui / app_list / views / contents_view.h
blob17ecf9366e29ae6988ce2bacfb969a3303db6ba1
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 #ifndef UI_APP_LIST_VIEWS_CONTENTS_VIEW_H_
6 #define UI_APP_LIST_VIEWS_CONTENTS_VIEW_H_
8 #include <map>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "ui/app_list/app_list_export.h"
14 #include "ui/app_list/pagination_model.h"
15 #include "ui/app_list/pagination_model_observer.h"
16 #include "ui/views/view.h"
18 namespace views {
19 class ViewModel;
22 namespace app_list {
24 class AppsGridView;
25 class ApplicationDragAndDropHost;
26 class AppListFolderItem;
27 class AppListMainView;
28 class AppListModel;
29 class AppListViewDelegate;
30 class AppsContainerView;
31 class ContentsSwitcherView;
32 class PaginationModel;
33 class SearchResultListView;
34 class StartPageView;
36 // A view to manage launcher pages within the Launcher (eg. start page, apps
37 // grid view, search results). There can be any number of launcher pages, only
38 // one of which can be active at a given time. ContentsView provides the user
39 // interface for switching between launcher pages, and animates the transition
40 // between them.
41 class APP_LIST_EXPORT ContentsView : public views::View,
42 public PaginationModelObserver {
43 public:
44 // Values of this enum denote special launcher pages that require hard-coding.
45 // Launcher pages are not required to have a NamedPage enum value.
46 enum NamedPage {
47 NAMED_PAGE_APPS,
48 NAMED_PAGE_SEARCH_RESULTS,
49 NAMED_PAGE_START,
52 ContentsView(AppListMainView* app_list_main_view);
53 virtual ~ContentsView();
55 // Initialize the named (special) pages of the launcher. In the experimental
56 // launcher, should be called after set_contents_switcher_view(), or switcher
57 // buttons will not be created.
58 void InitNamedPages(AppListModel* model, AppListViewDelegate* view_delegate);
60 // The app list gets closed and drag and drop operations need to be cancelled.
61 void CancelDrag();
63 // If |drag_and_drop| is not NULL it will be called upon drag and drop
64 // operations outside the application list.
65 void SetDragAndDropHostOfCurrentAppList(
66 ApplicationDragAndDropHost* drag_and_drop_host);
68 void SetContentsSwitcherView(ContentsSwitcherView* contents_switcher_view);
70 // Shows/hides the search results. Hiding the search results will cause the
71 // app list to return to the page that was displayed before
72 // ShowSearchResults(true) was invoked.
73 void ShowSearchResults(bool show);
74 bool IsShowingSearchResults() const;
76 void ShowFolderContent(AppListFolderItem* folder);
78 // Sets the active launcher page and animates the pages into place.
79 void SetActivePage(int page_index);
81 // The index of the currently active launcher page.
82 int GetActivePageIndex() const;
84 // True if |named_page| is the current active laucher page.
85 bool IsNamedPageActive(NamedPage named_page) const;
87 // Gets the index of a launcher page in |view_model_|, by NamedPage.
88 int GetPageIndexForNamedPage(NamedPage named_page) const;
90 int NumLauncherPages() const;
92 void Prerender();
94 AppsContainerView* apps_container_view() { return apps_container_view_; }
95 StartPageView* start_page_view() { return start_page_view_; }
96 SearchResultListView* search_results_view() { return search_results_view_; }
97 views::View* GetPageView(int index);
99 // Adds a blank launcher page. For use in tests only.
100 void AddBlankPageForTesting();
102 // Overridden from views::View:
103 virtual gfx::Size GetPreferredSize() const OVERRIDE;
104 virtual void Layout() OVERRIDE;
105 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
107 // Overridden from PaginationModelObserver:
108 virtual void TotalPagesChanged() OVERRIDE;
109 virtual void SelectedPageChanged(int old_selected, int new_selected) OVERRIDE;
110 virtual void TransitionStarted() OVERRIDE;
111 virtual void TransitionChanged() OVERRIDE;
113 // Returns the pagination model for the ContentsView.
114 const PaginationModel& pagination_model() { return pagination_model_; }
116 private:
117 // Sets the active launcher page, accounting for whether the change is for
118 // search results.
119 void SetActivePageInternal(int page_index, bool show_search_results);
121 // Invoked when active view is changed.
122 void ActivePageChanged(bool show_search_results);
124 // Calculates and sets the bounds for the subviews. If there is currently an
125 // animation, this positions the views as appropriate for the current frame.
126 void UpdatePageBounds();
128 // Adds |view| as a new page to the end of the list of launcher pages. The
129 // view is inserted as a child of the ContentsView, and a button with
130 // |resource_id| is added to the ContentsSwitcherView. There is no name
131 // associated with the page. Returns the index of the new page.
132 int AddLauncherPage(views::View* view, int resource_id);
134 // Adds |view| as a new page to the end of the list of launcher pages. The
135 // view is inserted as a child of the ContentsView, and a button with
136 // |resource_id| is added to the ContentsSwitcherView. The page is associated
137 // with the name |named_page|. Returns the index of the new page.
138 int AddLauncherPage(views::View* view, int resource_id, NamedPage named_page);
140 // Gets the PaginationModel owned by the AppsGridView.
141 // Note: This is different to |pagination_model_|, which manages top-level
142 // launcher-page pagination.
143 PaginationModel* GetAppsPaginationModel();
145 // Special sub views of the ContentsView. All owned by the views hierarchy.
146 AppsContainerView* apps_container_view_;
147 SearchResultListView* search_results_view_;
148 StartPageView* start_page_view_;
150 AppListMainView* app_list_main_view_; // Parent view, owns this.
151 // Sibling view, owned by |app_list_main_view_|.
152 ContentsSwitcherView* contents_switcher_view_;
154 scoped_ptr<views::ViewModel> view_model_;
155 // Maps NamedPage onto |view_model_| indices.
156 std::map<NamedPage, int> named_page_to_view_;
158 // The page that was showing before ShowSearchResults(true) was invoked.
159 int page_before_search_;
161 // Manages the pagination for the launcher pages.
162 PaginationModel pagination_model_;
164 DISALLOW_COPY_AND_ASSIGN(ContentsView);
167 } // namespace app_list
169 #endif // UI_APP_LIST_VIEWS_CONTENTS_VIEW_H_