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