OLD | NEW |
| (Empty) |
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. | |
4 | |
5 #ifndef UI_APP_LIST_APPS_GRID_VIEW_H_ | |
6 #define UI_APP_LIST_APPS_GRID_VIEW_H_ | |
7 | |
8 #include "base/basictypes.h" | |
9 #include "base/compiler_specific.h" | |
10 #include "base/timer.h" | |
11 #include "ui/app_list/app_list_export.h" | |
12 #include "ui/app_list/app_list_model.h" | |
13 #include "ui/app_list/app_list_model_observer.h" | |
14 #include "ui/app_list/pagination_model_observer.h" | |
15 #include "ui/base/models/list_model_observer.h" | |
16 #include "ui/views/animation/bounds_animator.h" | |
17 #include "ui/views/controls/button/button.h" | |
18 #include "ui/views/view.h" | |
19 #include "ui/views/view_model.h" | |
20 | |
21 namespace views { | |
22 class ButtonListener; | |
23 } | |
24 | |
25 namespace app_list { | |
26 | |
27 namespace test { | |
28 class AppsGridViewTestApi; | |
29 } | |
30 | |
31 class AppListItemView; | |
32 class AppsGridViewDelegate; | |
33 class PageSwitcher; | |
34 class PaginationModel; | |
35 | |
36 // AppsGridView displays a grid for AppListModel::Apps sub model. | |
37 class APP_LIST_EXPORT AppsGridView : public views::View, | |
38 public views::ButtonListener, | |
39 public ui::ListModelObserver, | |
40 public PaginationModelObserver, | |
41 public AppListModelObserver { | |
42 public: | |
43 enum Pointer { | |
44 NONE, | |
45 MOUSE, | |
46 TOUCH, | |
47 }; | |
48 | |
49 AppsGridView(AppsGridViewDelegate* delegate, | |
50 PaginationModel* pagination_model); | |
51 virtual ~AppsGridView(); | |
52 | |
53 // Sets fixed layout parameters. After setting this, CalculateLayout below | |
54 // is no longer called to dynamically choosing those layout params. | |
55 void SetLayout(int icon_size, int cols, int rows_per_page); | |
56 | |
57 // Sets |model| to use. Note this does not take ownership of |model|. | |
58 void SetModel(AppListModel* model); | |
59 | |
60 void SetSelectedView(views::View* view); | |
61 void ClearSelectedView(views::View* view); | |
62 bool IsSelectedView(const views::View* view) const; | |
63 | |
64 // Ensures the view is visible. Note that if there is a running page | |
65 // transition, this does nothing. | |
66 void EnsureViewVisible(const views::View* view); | |
67 | |
68 void InitiateDrag(views::View* view, | |
69 Pointer pointer, | |
70 const ui::LocatedEvent& event); | |
71 void UpdateDrag(views::View* view, | |
72 Pointer pointer, | |
73 const ui::LocatedEvent& event); | |
74 void EndDrag(bool cancel); | |
75 bool IsDraggedView(const views::View* view) const; | |
76 | |
77 bool has_dragged_view() const { return drag_view_ != NULL; } | |
78 bool dragging() const { return drag_pointer_ != NONE; } | |
79 | |
80 // Overridden from views::View: | |
81 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
82 virtual void Layout() OVERRIDE; | |
83 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; | |
84 virtual bool OnKeyReleased(const ui::KeyEvent& event) OVERRIDE; | |
85 virtual void ViewHierarchyChanged(bool is_add, | |
86 views::View* parent, | |
87 views::View* child) OVERRIDE; | |
88 | |
89 private: | |
90 friend class app_list::test::AppsGridViewTestApi; | |
91 | |
92 // Represents the index to an item view in the grid. | |
93 struct Index { | |
94 Index() : page(-1), slot(-1) {} | |
95 Index(int page, int slot) : page(page), slot(slot) {} | |
96 | |
97 bool operator==(const Index& other) const { | |
98 return page == other.page && slot == other.slot; | |
99 } | |
100 bool operator!=(const Index& other) const { | |
101 return page != other.page || slot != other.slot; | |
102 } | |
103 | |
104 int page; // Which page an item view is on. | |
105 int slot; // Which slot in the page an item view is in. | |
106 }; | |
107 | |
108 int tiles_per_page() const { return cols_ * rows_per_page_; } | |
109 | |
110 // Updates from model. | |
111 void Update(); | |
112 | |
113 // Updates page splits for item views. | |
114 void UpdatePaging(); | |
115 | |
116 // Updates the number of pulsing block views based on AppListModel status and | |
117 // number of apps. | |
118 void UpdatePulsingBlockViews(); | |
119 | |
120 views::View* CreateViewForItemAtIndex(size_t index); | |
121 | |
122 void SetSelectedItemByIndex(const Index& index); | |
123 bool IsValidIndex(const Index& index) const; | |
124 | |
125 Index GetIndexOfView(const views::View* view) const; | |
126 views::View* GetViewAtIndex(const Index& index) const; | |
127 | |
128 void MoveSelected(int page_delta, int slot_delta); | |
129 | |
130 void CalculateIdealBounds(); | |
131 void AnimateToIdealBounds(); | |
132 | |
133 // Invoked when the given |view|'s current bounds and target bounds are on | |
134 // different rows. To avoid moving diagonally, |view| would be put into a | |
135 // slot prior |target| and fade in while moving to |target|. In the meanwhile, | |
136 // a layer copy of |view| would start at |current| and fade out while moving | |
137 // to succeeding slot of |current|. |animate_current| controls whether to run | |
138 // fading out animation from |current|. |animate_target| controls whether to | |
139 // run fading in animation to |target|. | |
140 void AnimationBetweenRows(views::View* view, | |
141 bool animate_current, | |
142 const gfx::Rect& current, | |
143 bool animate_target, | |
144 const gfx::Rect& target); | |
145 | |
146 // Extracts drag location info from |event| into |drag_point|. | |
147 void ExtractDragLocation(const ui::LocatedEvent& event, | |
148 gfx::Point* drag_point); | |
149 | |
150 // Calculates |drop_target_| based on |drag_point|. |drag_point| is in the | |
151 // grid view's coordinates. When |use_page_button_hovering| is true and | |
152 // |drag_point| is hovering on a page button, use the last slot on that page | |
153 // as drop target. | |
154 void CalculateDropTarget(const gfx::Point& drag_point, | |
155 bool use_page_button_hovering); | |
156 | |
157 // Starts the page flip timer if |drag_point| is in left/right side page flip | |
158 // zone or is over page switcher. | |
159 void MaybeStartPageFlipTimer(const gfx::Point& drag_point); | |
160 | |
161 // Invoked when |page_flip_timer_| fires. | |
162 void OnPageFlipTimer(); | |
163 | |
164 // Updates |model_| to move item represented by |item_view| to |target| slot. | |
165 void MoveItemInModel(views::View* item_view, const Index& target); | |
166 | |
167 // Overridden from views::ButtonListener: | |
168 virtual void ButtonPressed(views::Button* sender, | |
169 const ui::Event& event) OVERRIDE; | |
170 | |
171 // Overridden from ListModelObserver: | |
172 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE; | |
173 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE; | |
174 virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE; | |
175 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE; | |
176 | |
177 // Overridden from PaginationModelObserver: | |
178 virtual void TotalPagesChanged() OVERRIDE; | |
179 virtual void SelectedPageChanged(int old_selected, int new_selected) OVERRIDE; | |
180 virtual void TransitionChanged() OVERRIDE; | |
181 | |
182 // Overridden from AppListModelObserver: | |
183 virtual void OnAppListModelStatusChanged() OVERRIDE; | |
184 | |
185 AppListModel* model_; // Owned by AppListView. | |
186 AppsGridViewDelegate* delegate_; | |
187 PaginationModel* pagination_model_; // Owned by AppListController. | |
188 PageSwitcher* page_switcher_view_; // Owned by views hierarchy. | |
189 | |
190 gfx::Size icon_size_; | |
191 int cols_; | |
192 int rows_per_page_; | |
193 | |
194 // Tracks app item views. There is a view per item in |model_|. | |
195 views::ViewModel view_model_; | |
196 | |
197 // Tracks pulsing block views. | |
198 views::ViewModel pulsing_blocks_model_; | |
199 | |
200 views::View* selected_view_; | |
201 | |
202 views::View* drag_view_; | |
203 gfx::Point drag_start_; | |
204 Pointer drag_pointer_; | |
205 Index drop_target_; | |
206 | |
207 // Last mouse drag location in this view's coordinates. | |
208 gfx::Point last_drag_point_; | |
209 | |
210 // Timer to auto flip page when dragging an item near the left/right edges. | |
211 base::OneShotTimer<AppsGridView> page_flip_timer_; | |
212 | |
213 // Target page to switch to when |page_flip_timer_| fires. | |
214 int page_flip_target_; | |
215 | |
216 // Delay in milliseconds of when |page_flip_timer_| should fire after user | |
217 // drags an item near the edges. | |
218 int page_flip_delay_in_ms_; | |
219 | |
220 views::BoundsAnimator bounds_animator_; | |
221 | |
222 DISALLOW_COPY_AND_ASSIGN(AppsGridView); | |
223 }; | |
224 | |
225 } // namespace app_list | |
226 | |
227 #endif // UI_APP_LIST_APPS_GRID_VIEW_H_ | |
OLD | NEW |