OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ | 5 #ifndef CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ | 6 #define CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 | 9 |
10 #include "chrome/browser/extensions/image_loading_tracker.h" | 10 #include "chrome/browser/extensions/image_loading_tracker.h" |
11 #include "content/public/browser/notification_observer.h" | 11 #include "content/public/browser/notification_observer.h" |
12 #include "ui/views/context_menu_controller.h" | 12 #include "ui/views/context_menu_controller.h" |
13 #include "ui/views/controls/button/menu_button.h" | 13 #include "ui/views/controls/button/menu_button.h" |
14 #include "ui/views/controls/button/menu_button_listener.h" | 14 #include "ui/views/controls/button/menu_button_listener.h" |
15 #include "ui/views/drag_controller.h" | |
15 #include "ui/views/view.h" | 16 #include "ui/views/view.h" |
16 | 17 |
17 class BrowserActionsContainer; | 18 class Browser; |
19 class BrowserActionButton; | |
18 class ExtensionAction; | 20 class ExtensionAction; |
19 | 21 |
22 namespace extensions { | |
23 class Extension; | |
24 } | |
25 | |
20 namespace views { | 26 namespace views { |
21 class MenuItemView; | 27 class MenuItemView; |
28 class MenuRunner; | |
22 } | 29 } |
23 | 30 |
24 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
32 // BrowserActionView | |
33 // A single entry in the browser action container. This contains the actual | |
34 // BrowserActionButton, as well as the logic to paint the badge. | |
35 class BrowserActionView : public views::View { | |
36 public: | |
37 // Need DragControler here because BrowserActionView could be dragged/dropped. | |
38 class Delegate : public views::DragController { | |
39 public: | |
40 // Returns the current tab's ID, or -1 if there is no current tab. | |
41 virtual int GetCurrentTabId() const = 0; | |
42 | |
43 // Called when the user clicks on the browser action icon. | |
44 virtual void OnBrowserActionExecuted(BrowserActionButton* button) = 0; | |
45 | |
46 // Called when a browser action becomes visible/hidden. | |
47 virtual void OnBrowserActionVisibilityChanged() = 0; | |
48 | |
49 // Returns relative position of a button inside BrowserActionView. | |
50 virtual gfx::Size GetViewContentOffset() const = 0; | |
Matt Perry
2012/07/31 15:59:14
This should return a gfx::Point, right? It's an of
yefimt
2012/07/31 17:19:35
Done.
| |
51 | |
52 virtual bool NeedToShowMultipleIconStates() const { return true; } | |
53 virtual bool NeedToShowTooltip() const { return true; } | |
54 | |
55 protected: | |
56 Delegate() {} | |
57 virtual ~Delegate() {} | |
58 }; | |
59 | |
60 BrowserActionView(const extensions::Extension* extension, | |
61 Browser* browser, | |
62 Delegate* delegate); | |
63 virtual ~BrowserActionView(); | |
64 | |
65 BrowserActionButton* button() { return button_; } | |
66 | |
67 // Allocates a canvas object on the heap and draws into it the icon for the | |
68 // view as well as the badge (if any). Caller is responsible for deleting the | |
69 // returned object. | |
70 gfx::Canvas* GetIconWithBadge(); | |
71 | |
72 // Overridden from views::View: | |
73 virtual void Layout() OVERRIDE; | |
74 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | |
75 virtual gfx::Size GetPreferredSize() OVERRIDE; | |
76 | |
77 protected: | |
78 // Overridden from views::View to paint the badge on top of children. | |
79 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE; | |
80 virtual void ViewHierarchyChanged(bool is_add, | |
81 View* parent, | |
82 View* child) OVERRIDE; | |
83 | |
84 private: | |
85 // The Browser object this view is associated with. | |
86 Browser* browser_; | |
87 | |
88 // Usually a container for this view. | |
89 Delegate* delegate_; | |
90 | |
91 // The button this view contains. | |
92 BrowserActionButton* button_; | |
93 | |
94 // Extension this view associated with. | |
95 const extensions::Extension* extension_; | |
96 | |
97 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); | |
98 }; | |
99 | |
100 //////////////////////////////////////////////////////////////////////////////// | |
25 // BrowserActionButton | 101 // BrowserActionButton |
26 | 102 |
27 // The BrowserActionButton is a specialization of the MenuButton class. | 103 // The BrowserActionButton is a specialization of the MenuButton class. |
28 // It acts on a ExtensionAction, in this case a BrowserAction and handles | 104 // It acts on a ExtensionAction, in this case a BrowserAction and handles |
29 // loading the image for the button asynchronously on the file thread. | 105 // loading the image for the button asynchronously on the file thread. |
30 class BrowserActionButton : public views::MenuButton, | 106 class BrowserActionButton : public views::MenuButton, |
31 public views::ButtonListener, | 107 public views::ButtonListener, |
32 public views::ContextMenuController, | 108 public views::ContextMenuController, |
33 public ImageLoadingTracker::Observer, | 109 public ImageLoadingTracker::Observer, |
34 public content::NotificationObserver { | 110 public content::NotificationObserver { |
35 public: | 111 public: |
36 BrowserActionButton(const extensions::Extension* extension, | 112 BrowserActionButton(const extensions::Extension* extension, |
37 BrowserActionsContainer* panel); | 113 Browser* browser_, |
114 BrowserActionView::Delegate* delegate); | |
38 | 115 |
39 // Call this instead of delete. | 116 // Call this instead of delete. |
40 void Destroy(); | 117 void Destroy(); |
41 | 118 |
42 ExtensionAction* browser_action() const { return browser_action_; } | 119 ExtensionAction* browser_action() const { return browser_action_; } |
43 const extensions::Extension* extension() { return extension_; } | 120 const extensions::Extension* extension() { return extension_; } |
44 | 121 |
45 // Called to update the display to match the browser action's state. | 122 // Called to update the display to match the browser action's state. |
46 void UpdateState(); | 123 void UpdateState(); |
47 | 124 |
(...skipping 28 matching lines...) Expand all Loading... | |
76 // MenuButton behavior overrides. These methods all default to TextButton | 153 // MenuButton behavior overrides. These methods all default to TextButton |
77 // behavior unless this button is a popup. In that case, it uses MenuButton | 154 // behavior unless this button is a popup. In that case, it uses MenuButton |
78 // behavior. MenuButton has the notion of a child popup being shown where the | 155 // behavior. MenuButton has the notion of a child popup being shown where the |
79 // button will stay in the pushed state until the "menu" (a popup in this | 156 // button will stay in the pushed state until the "menu" (a popup in this |
80 // case) is dismissed. | 157 // case) is dismissed. |
81 virtual bool Activate() OVERRIDE; | 158 virtual bool Activate() OVERRIDE; |
82 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; | 159 virtual bool OnMousePressed(const views::MouseEvent& event) OVERRIDE; |
83 virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE; | 160 virtual void OnMouseReleased(const views::MouseEvent& event) OVERRIDE; |
84 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; | 161 virtual void OnMouseExited(const views::MouseEvent& event) OVERRIDE; |
85 virtual bool OnKeyReleased(const views::KeyEvent& event) OVERRIDE; | 162 virtual bool OnKeyReleased(const views::KeyEvent& event) OVERRIDE; |
163 virtual void ShowContextMenu(const gfx::Point& p, | |
164 bool is_mouse_gesture) OVERRIDE; | |
86 | 165 |
87 // Overridden from ui::AcceleratorTarget. | 166 // Overridden from ui::AcceleratorTarget. |
88 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; | 167 virtual bool AcceleratorPressed(const ui::Accelerator& accelerator) OVERRIDE; |
89 | 168 |
90 // Notifications when to set button state to pushed/not pushed (for when the | 169 // Notifications when to set button state to pushed/not pushed (for when the |
91 // popup/context menu is hidden or shown by the container). | 170 // popup/context menu is hidden or shown by the container). |
92 void SetButtonPushed(); | 171 void SetButtonPushed(); |
93 void SetButtonNotPushed(); | 172 void SetButtonNotPushed(); |
94 | 173 |
95 // Whether the browser action is enabled on this tab. Note that we cannot use | 174 // Whether the browser action is enabled on this tab. Note that we cannot use |
(...skipping 10 matching lines...) Expand all Loading... | |
106 private: | 185 private: |
107 virtual ~BrowserActionButton(); | 186 virtual ~BrowserActionButton(); |
108 | 187 |
109 // Register an extension command if the extension has an active one. | 188 // Register an extension command if the extension has an active one. |
110 void MaybeRegisterExtensionCommand(); | 189 void MaybeRegisterExtensionCommand(); |
111 | 190 |
112 // Unregisters an extension command, if the extension has registered one and | 191 // Unregisters an extension command, if the extension has registered one and |
113 // it is active. | 192 // it is active. |
114 void MaybeUnregisterExtensionCommand(bool only_if_active); | 193 void MaybeUnregisterExtensionCommand(bool only_if_active); |
115 | 194 |
195 // Returns tooltip for the button. | |
196 string16 GetName(); | |
197 | |
198 void ShowContextMenuImpl(); | |
199 | |
200 // The Browser object this button is associated with. | |
201 Browser* browser_; | |
202 | |
116 // The browser action this view represents. The ExtensionAction is not owned | 203 // The browser action this view represents. The ExtensionAction is not owned |
117 // by this class. | 204 // by this class. |
118 ExtensionAction* browser_action_; | 205 ExtensionAction* browser_action_; |
119 | 206 |
120 // The extension associated with the browser action we're displaying. | 207 // The extension associated with the browser action we're displaying. |
121 const extensions::Extension* extension_; | 208 const extensions::Extension* extension_; |
122 | 209 |
123 // The object that is waiting for the image loading to complete | 210 // The object that is waiting for the image loading to complete |
124 // asynchronously. | 211 // asynchronously. |
125 ImageLoadingTracker tracker_; | 212 ImageLoadingTracker tracker_; |
126 | 213 |
127 // The default icon for our browser action. This might be non-empty if the | 214 // The default icon for our browser action. This might be non-empty if the |
128 // browser action had a value for default_icon in the manifest. | 215 // browser action had a value for default_icon in the manifest. |
129 SkBitmap default_icon_; | 216 SkBitmap default_icon_; |
130 | 217 |
131 // The browser action shelf. | 218 // Delegate that usually represents a container for BrowserActionView. |
132 BrowserActionsContainer* panel_; | 219 BrowserActionView::Delegate* delegate_; |
133 | 220 |
134 // The context menu. This member is non-NULL only when the menu is shown. | 221 // The context menu. This member is non-NULL only when the menu is shown. |
135 views::MenuItemView* context_menu_; | 222 views::MenuItemView* context_menu_; |
136 | 223 |
137 content::NotificationRegistrar registrar_; | 224 content::NotificationRegistrar registrar_; |
138 | 225 |
139 // The extension keybinding accelerator this browser action is listening for | 226 // The extension key binding accelerator this browser action is listening for |
140 // (to show the popup). | 227 // (to show the popup). |
141 scoped_ptr<ui::Accelerator> keybinding_; | 228 scoped_ptr<ui::Accelerator> keybinding_; |
142 | 229 |
230 // Responsible for running the menu. | |
231 scoped_ptr<views::MenuRunner> menu_runner_; | |
232 | |
143 friend class base::DeleteHelper<BrowserActionButton>; | 233 friend class base::DeleteHelper<BrowserActionButton>; |
144 | 234 |
145 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); | 235 DISALLOW_COPY_AND_ASSIGN(BrowserActionButton); |
146 }; | 236 }; |
147 | 237 |
148 | |
149 //////////////////////////////////////////////////////////////////////////////// | |
150 // BrowserActionView | |
151 // A single section in the browser action container. This contains the actual | |
152 // BrowserActionButton, as well as the logic to paint the badge. | |
153 | |
154 class BrowserActionView : public views::View { | |
155 public: | |
156 BrowserActionView(const extensions::Extension* extension, | |
157 BrowserActionsContainer* panel); | |
158 virtual ~BrowserActionView(); | |
159 | |
160 BrowserActionButton* button() { return button_; } | |
161 | |
162 // Allocates a canvas object on the heap and draws into it the icon for the | |
163 // view as well as the badge (if any). Caller is responsible for deleting the | |
164 // returned object. | |
165 gfx::Canvas* GetIconWithBadge(); | |
166 | |
167 // Overridden from views::View: | |
168 virtual void Layout() OVERRIDE; | |
169 virtual void GetAccessibleState(ui::AccessibleViewState* state) OVERRIDE; | |
170 | |
171 protected: | |
172 // Overridden from views::View to paint the badge on top of children. | |
173 virtual void PaintChildren(gfx::Canvas* canvas) OVERRIDE; | |
174 | |
175 private: | |
176 // The container for this view. | |
177 BrowserActionsContainer* panel_; | |
178 | |
179 // The button this view contains. | |
180 BrowserActionButton* button_; | |
181 | |
182 DISALLOW_COPY_AND_ASSIGN(BrowserActionView); | |
183 }; | |
184 | |
185 #endif // CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ | 238 #endif // CHROME_BROWSER_UI_VIEWS_BROWSER_ACTION_VIEW_H_ |
OLD | NEW |