app_list: Use scaled icon for mouse drag.
[chromium-blink-merge.git] / ui / app_list / apps_grid_view.h
blob15907ac25d82683da9e402e300284abd0e3602a9
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_APPS_GRID_VIEW_H_
6 #define UI_APP_LIST_APPS_GRID_VIEW_H_
8 #include <vector>
10 #include "base/basictypes.h"
11 #include "base/compiler_specific.h"
12 #include "base/timer.h"
13 #include "ui/app_list/app_list_export.h"
14 #include "ui/app_list/app_list_model.h"
15 #include "ui/app_list/pagination_model_observer.h"
16 #include "ui/base/models/list_model_observer.h"
17 #include "ui/views/animation/bounds_animator.h"
18 #include "ui/views/controls/button/button.h"
19 #include "ui/views/view.h"
20 #include "ui/views/view_model.h"
22 namespace views {
23 class ButtonListener;
26 namespace app_list {
28 namespace test {
29 class AppsGridViewTestApi;
32 class AppListItemView;
33 class AppsGridViewDelegate;
34 class PageSwitcher;
35 class PaginationModel;
37 // AppsGridView displays a grid for AppListModel::Apps sub model.
38 class APP_LIST_EXPORT AppsGridView : public views::View,
39 public views::ButtonListener,
40 public ui::ListModelObserver,
41 public PaginationModelObserver {
42 public:
43 enum Pointer {
44 NONE,
45 MOUSE,
46 TOUCH,
49 AppsGridView(AppsGridViewDelegate* delegate,
50 PaginationModel* pagination_model);
51 virtual ~AppsGridView();
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);
57 // Sets |model| to use. Note this does not take ownership of |model|.
58 void SetModel(AppListModel::Apps* model);
60 void SetSelectedView(views::View* view);
61 void ClearSelectedView(views::View* view);
62 bool IsSelectedView(const views::View* view) const;
64 // Ensures the view is visible. Note that if there is a running page
65 // transition, this does not thing.
66 void EnsureViewVisible(const views::View* view);
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;
77 bool has_dragged_view() const { return drag_view_ != NULL; }
78 bool dragging() const { return drag_pointer_ != NONE; }
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;
89 private:
90 friend class app_list::test::AppsGridViewTestApi;
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) {}
97 bool operator==(const Index& other) const {
98 return page == other.page && slot == other.slot;
101 int page; // Which page an item view is on.
102 int slot; // Which slot in the page an item view is in.
105 int tiles_per_page() const { return cols_ * rows_per_page_; }
107 // Updates from model.
108 void Update();
110 // Updates page splits for item views.
111 void UpdatePaging();
113 views::View* CreateViewForItemAtIndex(size_t index);
115 void SetSelectedItemByIndex(const Index& index);
116 bool IsValidIndex(const Index& index) const;
118 Index GetIndexOfView(const views::View* view) const;
119 views::View* GetViewAtIndex(const Index& index) const;
121 void MoveSelected(int page_delta, int slot_delta);
123 void CalculateIdealBounds();
124 void AnimateToIdealBounds();
126 // Calculates |drop_target_| based on |drag_point|. |drag_point| is in the
127 // grid view's coordinates.
128 void CalculateDropTarget(const gfx::Point& drag_point);
130 // Starts the page flip timer if |drag_point| is in left/right side page flip
131 // zone or is over page switcher.
132 void MaybeStartPageFlipTimer(const gfx::Point& drag_point);
134 // Invoked when |page_flip_timer_| fires.
135 void OnPageFlipTimer();
137 // Updates |model_| to move item represented by |item_view| to |target| slot.
138 void MoveItemInModel(views::View* item_view, const Index& target);
140 // Overridden from views::ButtonListener:
141 virtual void ButtonPressed(views::Button* sender,
142 const ui::Event& event) OVERRIDE;
144 // Overridden from ListModelObserver:
145 virtual void ListItemsAdded(size_t start, size_t count) OVERRIDE;
146 virtual void ListItemsRemoved(size_t start, size_t count) OVERRIDE;
147 virtual void ListItemMoved(size_t index, size_t target_index) OVERRIDE;
148 virtual void ListItemsChanged(size_t start, size_t count) OVERRIDE;
150 // Overridden from PaginationModelObserver:
151 virtual void TotalPagesChanged() OVERRIDE;
152 virtual void SelectedPageChanged(int old_selected, int new_selected) OVERRIDE;
153 virtual void TransitionChanged() OVERRIDE;
155 AppListModel::Apps* model_; // Owned by AppListModel.
156 AppsGridViewDelegate* delegate_;
157 PaginationModel* pagination_model_; // Owned by AppListController.
158 PageSwitcher* page_switcher_view_; // Owned by views hierarchy.
160 gfx::Size icon_size_;
161 int cols_;
162 int rows_per_page_;
164 // Tracks app item views. There is a view per item in |model_|.
165 views::ViewModel view_model_;
167 views::View* selected_view_;
169 views::View* drag_view_;
170 gfx::Point drag_offset_;
171 Pointer drag_pointer_;
172 Index drop_target_;
174 gfx::Point last_drag_point_;
176 // Timer to auto flip page when dragging an item near the left/right edges.
177 base::OneShotTimer<AppsGridView> page_flip_timer_;
179 // Target page to switch to when |page_flip_timer_| fires.
180 int page_flip_target_;
182 // Delay in milliseconds of when |page_flip_timer_| should fire after user
183 // drags an item near the edges.
184 int page_flip_delay_in_ms_;
186 views::BoundsAnimator bounds_animator_;
188 DISALLOW_COPY_AND_ASSIGN(AppsGridView);
191 } // namespace app_list
193 #endif // UI_APP_LIST_APPS_GRID_VIEW_H_