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/strings/string16.h" 13 #include "base/timer/timer.h" 14 #include "ui/app_list/app_list_export.h" 15 #include "ui/app_list/app_list_item_observer.h" 16 #include "ui/app_list/views/cached_label.h" 17 #include "ui/views/context_menu_controller.h" 18 #include "ui/views/controls/button/custom_button.h" 19 20 class SkBitmap; 21 22 namespace views { 23 class ImageView; 24 class Label; 25 class MenuRunner; 26 } 27 28 namespace app_list { 29 30 class AppListItem; 31 class AppsGridView; 32 class ProgressBarView; 33 34 class APP_LIST_EXPORT AppListItemView : public views::CustomButton, 35 public views::ContextMenuController, 36 public AppListItemObserver { 37 public: 38 // Internal class name. 39 static const char kViewClassName[]; 40 41 AppListItemView(AppsGridView* apps_grid_view, AppListItem* item); 42 virtual ~AppListItemView(); 43 44 // Set the icon of this image, adding a drop shadow if |has_shadow|. 45 void SetIcon(const gfx::ImageSkia& icon, bool has_shadow); 46 47 // Set the item name. 48 void SetItemName(const base::string16& display_name, 49 const base::string16& full_name); 50 void SetItemIsInstalling(bool is_installing); 51 void SetItemIsHighlighted(bool is_highlighted); 52 void SetItemPercentDownloaded(int percent_downloaded); 53 54 void Prerender(); 55 56 void CancelContextMenu(); 57 58 gfx::ImageSkia GetDragImage(); 59 void OnDragEnded(); 60 gfx::Point GetDragImageOffset(); 61 62 void SetAsAttemptedFolderTarget(bool is_target_folder); 63 64 AppListItem* item() const { return item_weak_; } 65 66 views::ImageView* icon() const { return icon_; } 67 68 const views::Label* title() const { return title_; } 69 70 // In a synchronous drag the item view isn't informed directly of the drag 71 // ending, so the runner of the drag should call this. 72 void OnSyncDragEnd(); 73 74 // Returns the icon bounds relative to AppListItemView. 75 const gfx::Rect& GetIconBounds() const; 76 77 // Sets UI state to dragging state. 78 void SetDragUIState(); 79 80 // Returns the icon bounds for the given |target_bounds| as 81 // the assuming bounds of this view. 82 gfx::Rect GetIconBoundsForTargetViewBounds(const gfx::Rect& target_bounds); 83 84 private: 85 enum UIState { 86 UI_STATE_NORMAL, // Normal UI (icon + label) 87 UI_STATE_DRAGGING, // Dragging UI (scaled icon only) 88 UI_STATE_DROPPING_IN_FOLDER, // Folder dropping preview UI 89 }; 90 91 // Get icon from |item_| and schedule background processing. 92 void UpdateIcon(); 93 94 // Update the tooltip text from |item_|. 95 void UpdateTooltip(); 96 97 void SetUIState(UIState state); 98 99 // Sets |touch_dragging_| flag and updates UI. 100 void SetTouchDragging(bool touch_dragging); 101 102 // Invoked when |mouse_drag_timer_| fires to show dragging UI. 103 void OnMouseDragTimer(); 104 105 // If the item is not in a folder, not highlighted, not being dragged, and not 106 // having something dropped onto it, enables subpixel AA for the title. 107 void SetTitleSubpixelAA(); 108 109 // views::View overrides: 110 virtual const char* GetClassName() const OVERRIDE; 111 virtual void Layout() OVERRIDE; 112 virtual void SchedulePaintInRect(const gfx::Rect& r) OVERRIDE; 113 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 114 115 // views::ContextMenuController overrides: 116 virtual void ShowContextMenuForView(views::View* source, 117 const gfx::Point& point, 118 ui::MenuSourceType source_type) OVERRIDE; 119 120 // views::CustomButton overrides: 121 virtual void StateChanged() OVERRIDE; 122 virtual bool ShouldEnterPushedState(const ui::Event& event) OVERRIDE; 123 124 // views::View overrides: 125 virtual bool OnKeyPressed(const ui::KeyEvent& event) OVERRIDE; 126 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 127 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 128 virtual void OnMouseCaptureLost() OVERRIDE; 129 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 130 131 // ui::EventHandler overrides: 132 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 133 134 // AppListItemObserver overrides: 135 virtual void ItemIconChanged() OVERRIDE; 136 virtual void ItemNameChanged() OVERRIDE; 137 virtual void ItemHighlightedChanged() OVERRIDE; 138 virtual void ItemIsInstallingChanged() OVERRIDE; 139 virtual void ItemPercentDownloadedChanged() OVERRIDE; 140 virtual void ItemBeingDestroyed() OVERRIDE; 141 142 const bool is_folder_; 143 const bool is_in_folder_; 144 145 AppListItem* item_weak_; // Owned by AppListModel. Can be NULL. 146 147 AppsGridView* apps_grid_view_; // Parent view, owns this. 148 views::ImageView* icon_; // Strongly typed child view. 149 CachedLabel* title_; // Strongly typed child view. 150 ProgressBarView* progress_bar_; // Strongly typed child view. 151 152 scoped_ptr<views::MenuRunner> context_menu_runner_; 153 154 UIState ui_state_; 155 156 // True if scroll gestures should contribute to dragging. 157 bool touch_dragging_; 158 159 bool is_installing_; 160 bool is_highlighted_; 161 162 // A timer to defer showing drag UI when mouse is pressed. 163 base::OneShotTimer<AppListItemView> mouse_drag_timer_; 164 165 DISALLOW_COPY_AND_ASSIGN(AppListItemView); 166 }; 167 168 } // namespace app_list 169 170 #endif // UI_APP_LIST_VIEWS_APP_LIST_ITEM_VIEW_H_ 171