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_VIEWS_APP_LIST_ITEM_VIEW_H_
6#define UI_APP_LIST_VIEWS_APP_LIST_ITEM_VIEW_H_
7
8#include <string>
9
10#include "base/memory/ref_counted.h"
11#include "base/memory/scoped_ptr.h"
12#include "base/timer/timer.h"
13#include "ui/app_list/app_list_export.h"
14#include "ui/app_list/app_list_item_model_observer.h"
15#include "ui/app_list/views/cached_label.h"
16#include "ui/gfx/shadow_value.h"
17#include "ui/views/context_menu_controller.h"
18#include "ui/views/controls/button/custom_button.h"
19
20class SkBitmap;
21
22namespace views {
23class ImageView;
24class Label;
25class MenuRunner;
26}
27
28namespace app_list {
29
30class AppListItemModel;
31class AppsGridView;
32class ProgressBarView;
33
34class APP_LIST_EXPORT AppListItemView : public views::CustomButton,
35                                        public views::ContextMenuController,
36                                        public AppListItemModelObserver {
37 public:
38  // Internal class name.
39  static const char kViewClassName[];
40
41  AppListItemView(AppsGridView* apps_grid_view, AppListItemModel* model);
42  virtual ~AppListItemView();
43
44  void SetIconSize(const gfx::Size& size);
45
46  void Prerender();
47
48  void CancelContextMenu();
49
50  gfx::ImageSkia GetDragImage();
51  void OnDragEnded();
52  gfx::Point GetDragImageOffset();
53
54  void SetAsAttemptedFolderTarget(bool is_target_folder);
55
56  AppListItemModel* model() const { return model_; }
57
58  const views::Label* title() const { return title_; }
59
60  // In a synchronous drag the item view isn't informed directly of the drag
61  // ending, so the runner of the drag should call this.
62  void OnSyncDragEnd();
63
64 private:
65  enum UIState {
66    UI_STATE_NORMAL,    // Normal UI (icon + label)
67    UI_STATE_DRAGGING,  // Dragging UI (scaled icon only)
68    UI_STATE_DROPPING_IN_FOLDER,  // Folder dropping preview UI
69  };
70
71  // Get icon from model and schedule background processing.
72  void UpdateIcon();
73
74  // Update the tooltip text from the model.
75  void UpdateTooltip();
76
77  void SetUIState(UIState state);
78
79  // Sets |touch_dragging_| flag and updates UI.
80  void SetTouchDragging(bool touch_dragging);
81
82  // Invoked when |mouse_drag_timer_| fires to show dragging UI.
83  void OnMouseDragTimer();
84
85  // AppListItemModelObserver overrides:
86  virtual void ItemIconChanged() OVERRIDE;
87  virtual void ItemTitleChanged() OVERRIDE;
88  virtual void ItemHighlightedChanged() OVERRIDE;
89  virtual void ItemIsInstallingChanged() OVERRIDE;
90  virtual void ItemPercentDownloadedChanged() OVERRIDE;
91
92  // views::View overrides:
93  virtual const char* GetClassName() const OVERRIDE;
94  virtual void Layout() OVERRIDE;
95  virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE;
96  virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE;
97
98  // views::ContextMenuController overrides:
99  virtual void ShowContextMenuForView(views::View* source,
100                                      const gfx::Point& point,
101                                      ui::MenuSourceType source_type) OVERRIDE;
102
103  // views::CustomButton overrides:
104  virtual void StateChanged() OVERRIDE;
105  virtual bool ShouldEnterPushedState(const ui::Event& event) OVERRIDE;
106
107  // views::View overrides:
108  virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE;
109  virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE;
110  virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE;
111  virtual void OnMouseCaptureLost() OVERRIDE;
112  virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE;
113
114  // ui::EventHandler overrides:
115  virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE;
116
117  AppListItemModel* model_;  // Owned by AppListModel.
118
119  AppsGridView* apps_grid_view_;  // Owned by views hierarchy.
120  views::ImageView* icon_;  // Owned by views hierarchy.
121  CachedLabel* title_;  // Owned by views hierarchy.
122  ProgressBarView* progress_bar_;  // Owned by views hierarchy.
123
124  scoped_ptr<views::MenuRunner> context_menu_runner_;
125
126  gfx::Size icon_size_;
127  gfx::ShadowValues icon_shadows_;
128
129  UIState ui_state_;
130
131  // True if scroll gestures should contribute to dragging.
132  bool touch_dragging_;
133
134  // A timer to defer showing drag UI when mouse is pressed.
135  base::OneShotTimer<AppListItemView> mouse_drag_timer_;
136
137  DISALLOW_COPY_AND_ASSIGN(AppListItemView);
138};
139
140}  // namespace app_list
141
142#endif  // UI_APP_LIST_VIEWS_APP_LIST_ITEM_VIEW_H_
143