1 // Copyright 2013 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 ASH_SHELF_SHELF_BUTTON_H_ 6 #define ASH_SHELF_SHELF_BUTTON_H_ 7 8 #include "ash/ash_export.h" 9 #include "ui/gfx/shadow_value.h" 10 #include "ui/views/controls/button/custom_button.h" 11 #include "ui/views/controls/image_view.h" 12 13 namespace ash { 14 namespace internal { 15 16 class ShelfButtonHost; 17 class ShelfLayoutManager; 18 19 // Button used for items on the launcher, except for the AppList. 20 class ASH_EXPORT ShelfButton : public views::CustomButton { 21 public: 22 // Used to indicate the current state of the button. 23 enum State { 24 // Nothing special. Usually represents an app shortcut item with no running 25 // instance. 26 STATE_NORMAL = 0, 27 // Button has mouse hovering on it. 28 STATE_HOVERED = 1 << 0, 29 // Underlying LauncherItem has a running instance. 30 STATE_RUNNING = 1 << 1, 31 // Underlying LauncherItem is active (i.e. has focus). 32 STATE_ACTIVE = 1 << 2, 33 // Underlying LauncherItem needs user's attention. 34 STATE_ATTENTION = 1 << 3, 35 STATE_FOCUSED = 1 << 4, 36 // Hide the status (temporarily for some animations). 37 STATE_HIDDEN = 1 << 5, 38 }; 39 40 virtual ~ShelfButton(); 41 42 // Called to create an instance of a ShelfButton. 43 static ShelfButton* Create(views::ButtonListener* listener, 44 ShelfButtonHost* host, 45 ShelfLayoutManager* shelf_layout_manager); 46 47 // Sets the image to display for this entry. 48 void SetImage(const gfx::ImageSkia& image); 49 50 // Retrieve the image to show proxy operations. 51 const gfx::ImageSkia& GetImage() const; 52 53 // |state| is or'd into the current state. 54 void AddState(State state); 55 void ClearState(State state); 56 int state() const { return state_; } 57 const ShelfLayoutManager* shelf_layout_manager() const { 58 return shelf_layout_manager_; 59 } 60 61 // Returns the bounds of the icon. 62 gfx::Rect GetIconBounds() const; 63 64 // Overrides to views::CustomButton: 65 virtual void ShowContextMenu(const gfx::Point& p, 66 ui::MenuSourceType source_type) OVERRIDE; 67 68 // View override - needed by unit test. 69 virtual void OnMouseCaptureLost() OVERRIDE; 70 71 protected: 72 ShelfButton(views::ButtonListener* listener, 73 ShelfButtonHost* host, 74 ShelfLayoutManager* shelf_layout_manager); 75 76 // Class that draws the icon part of a button, so it can be animated 77 // independently of the rest. This can be subclassed to provide a custom 78 // implementation, by overriding CreateIconView(). 79 class IconView : public views::ImageView { 80 public: 81 IconView(); 82 virtual ~IconView(); 83 84 void set_icon_size(int icon_size) { icon_size_ = icon_size; } 85 int icon_size() const { return icon_size_; } 86 87 // views::View overrides. 88 virtual bool HitTestRect(const gfx::Rect& rect) const OVERRIDE; 89 90 private: 91 // Set to non-zero to force icons to be resized to fit within a square, 92 // while maintaining original proportions. 93 int icon_size_; 94 95 DISALLOW_COPY_AND_ASSIGN(IconView); 96 }; 97 98 // View overrides: 99 virtual bool OnMousePressed(const ui::MouseEvent& event) OVERRIDE; 100 virtual void OnMouseReleased(const ui::MouseEvent& event) OVERRIDE; 101 virtual bool OnMouseDragged(const ui::MouseEvent& event) OVERRIDE; 102 virtual void OnMouseMoved(const ui::MouseEvent& event) OVERRIDE; 103 virtual void OnMouseEntered(const ui::MouseEvent& event) OVERRIDE; 104 virtual void OnMouseExited(const ui::MouseEvent& event) OVERRIDE; 105 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; 106 virtual void Layout() OVERRIDE; 107 virtual void ChildPreferredSizeChanged(views::View* child) OVERRIDE; 108 virtual void OnFocus() OVERRIDE; 109 virtual void OnBlur() OVERRIDE; 110 virtual void OnPaint(gfx::Canvas* canvas) OVERRIDE; 111 112 // ui::EventHandler overrides: 113 virtual void OnGestureEvent(ui::GestureEvent* event) OVERRIDE; 114 115 // Sets the icon image with a shadow. 116 void SetShadowedImage(const gfx::ImageSkia& bitmap); 117 // Override for custom initialization. 118 virtual void Init(); 119 // Override to subclass IconView. 120 virtual IconView* CreateIconView(); 121 IconView* icon_view() const { return icon_view_; } 122 ShelfButtonHost* host() const { return host_; } 123 124 private: 125 class BarView; 126 127 // Returns true if the shelf is horizontal. If this returns false the shelf is 128 // vertical. 129 bool IsShelfHorizontal() const; 130 131 // Updates the parts of the button to reflect the current |state_| and 132 // alignment. This may add or remove views, layout and paint. 133 void UpdateState(); 134 135 // Updates the status bar (bitmap, orientation, visibility). 136 void UpdateBar(); 137 138 ShelfButtonHost* host_; 139 IconView* icon_view_; 140 // Draws a bar underneath the image to represent the state of the application. 141 BarView* bar_; 142 // The current state of the application, multiple values of AppState are or'd 143 // together. 144 int state_; 145 146 ShelfLayoutManager* shelf_layout_manager_; 147 148 gfx::ShadowValues icon_shadows_; 149 150 // If non-null the destuctor sets this to true. This is set while the menu is 151 // showing and used to detect if the menu was deleted while running. 152 bool* destroyed_flag_; 153 154 DISALLOW_COPY_AND_ASSIGN(ShelfButton); 155 }; 156 157 } // namespace internal 158 } // namespace ash 159 160 #endif // ASH_SHELF_SHELF_BUTTON_H_ 161