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 #include "chrome/browser/ui/views/browser_action_view.h" | 5 #include "chrome/browser/ui/views/browser_action_view.h" |
6 | 6 |
7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
8 #include "chrome/browser/extensions/api/commands/command_service.h" | 8 #include "chrome/browser/extensions/api/commands/command_service.h" |
9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" | 9 #include "chrome/browser/extensions/api/commands/command_service_factory.h" |
10 #include "chrome/browser/extensions/extension_context_menu_model.h" | 10 #include "chrome/browser/extensions/extension_context_menu_model.h" |
11 #include "chrome/browser/ui/browser.h" | 11 #include "chrome/browser/ui/browser.h" |
12 #include "chrome/browser/ui/views/browser_actions_container.h" | 12 #include "chrome/browser/ui/views/browser_actions_container.h" |
13 #include "chrome/browser/ui/views/browser_action_view.h" | |
13 #include "chrome/browser/ui/views/toolbar_view.h" | 14 #include "chrome/browser/ui/views/toolbar_view.h" |
14 #include "chrome/common/chrome_notification_types.h" | 15 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/extensions/extension.h" | 16 #include "chrome/common/extensions/extension.h" |
16 #include "chrome/common/extensions/extension_manifest_constants.h" | 17 #include "chrome/common/extensions/extension_manifest_constants.h" |
17 #include "grit/generated_resources.h" | 18 #include "grit/generated_resources.h" |
18 #include "grit/theme_resources.h" | 19 #include "grit/theme_resources.h" |
19 #include "ui/base/accessibility/accessible_view_state.h" | 20 #include "ui/base/accessibility/accessible_view_state.h" |
20 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
(...skipping 11 matching lines...) Expand all Loading... | |
34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | 35 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); |
35 alpha.allocPixels(); | 36 alpha.allocPixels(); |
36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | 37 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); |
37 | 38 |
38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | 39 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); |
39 } | 40 } |
40 | 41 |
41 } // namespace | 42 } // namespace |
42 | 43 |
43 //////////////////////////////////////////////////////////////////////////////// | 44 //////////////////////////////////////////////////////////////////////////////// |
45 // BrowserActionView | |
46 | |
47 BrowserActionView::BrowserActionView(const Extension* extension, | |
48 Browser* browser, | |
49 BrowserActionView::Delegate* delegate) | |
50 : browser_(browser), | |
51 delegate_(delegate), | |
52 button_(NULL) { | |
53 button_ = new BrowserActionButton(extension, browser_, delegate_); | |
Peter Kasting
2012/07/26 20:37:17
Can move this into ViewHierarchyChanged() and remo
yefimt
2012/07/31 00:10:11
Done.
| |
54 button_->set_owned_by_client(); | |
55 button_->set_drag_controller(delegate_); | |
56 } | |
57 | |
58 BrowserActionView::~BrowserActionView() { | |
59 RemoveChildView(button_); | |
60 button_->Destroy(); | |
61 } | |
62 | |
63 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | |
64 int tab_id = delegate_->GetCurrentTabId(); | |
65 | |
66 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); | |
67 if (icon.isNull()) | |
68 icon = button_->default_icon(); | |
69 | |
70 gfx::Canvas* canvas = | |
71 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | |
72 | |
73 if (tab_id >= 0) { | |
74 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | |
75 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | |
76 } | |
77 | |
78 return canvas; | |
79 } | |
80 | |
81 void BrowserActionView::Layout() { | |
82 // We can't rely on button_->GetPreferredSize() here because that's not set | |
83 // correctly until the first call to | |
84 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | |
85 // called before that when the initial bounds are set (and then not after, | |
86 // since the bounds don't change). So instead of setting the height from the | |
87 // button's preferred size, we use IconHeight(), since that's how big the | |
88 // button should be regardless of what it's displaying. | |
89 gfx::Size size = delegate_->GetViewContentOffset(); | |
90 button_->SetBounds(size.width(), size.height(), width(), | |
Peter Kasting
2012/07/26 20:37:17
Calling width() here isn't right, Layout() is resp
yefimt
2012/07/31 00:10:11
I think you need to ask someone who wrote this cod
Peter Kasting
2012/07/31 02:30:42
You at least need to use width() - size.width() fo
yefimt
2012/07/31 17:19:35
It has nothing to do with architecture. It was an
| |
91 BrowserActionsContainer::IconHeight()); | |
92 } | |
93 | |
94 void BrowserActionView::ViewHierarchyChanged(bool is_add, | |
95 View* parent, | |
96 View* child) { | |
97 if (is_add && (child == this)) { | |
98 AddChildView(button_); | |
99 button_->UpdateState(); | |
100 } | |
101 } | |
102 | |
103 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | |
104 state->name = l10n_util::GetStringUTF16( | |
105 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | |
106 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | |
107 } | |
108 | |
109 gfx::Size BrowserActionView::GetPreferredSize() { | |
110 return gfx::Size(BrowserActionsContainer::IconWidth(false), | |
111 BrowserActionsContainer::IconHeight()); | |
112 } | |
113 | |
114 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | |
115 View::PaintChildren(canvas); | |
116 ExtensionAction* action = button()->browser_action(); | |
117 int tab_id = delegate_->GetCurrentTabId(); | |
118 if (tab_id >= 0) | |
119 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | |
120 } | |
121 | |
122 //////////////////////////////////////////////////////////////////////////////// | |
44 // BrowserActionButton | 123 // BrowserActionButton |
45 | 124 |
46 BrowserActionButton::BrowserActionButton(const Extension* extension, | 125 BrowserActionButton::BrowserActionButton(const Extension* extension, |
47 BrowserActionsContainer* panel) | 126 Browser* browser, |
127 BrowserActionView::Delegate* delegate) | |
48 : ALLOW_THIS_IN_INITIALIZER_LIST( | 128 : ALLOW_THIS_IN_INITIALIZER_LIST( |
49 MenuButton(this, string16(), NULL, false)), | 129 MenuButton(this, string16(), NULL, false)), |
130 browser_(browser), | |
50 browser_action_(extension->browser_action()), | 131 browser_action_(extension->browser_action()), |
51 extension_(extension), | 132 extension_(extension), |
52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 133 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
53 panel_(panel), | 134 delegate_(delegate), |
54 context_menu_(NULL) { | 135 context_menu_(NULL), |
136 disable_tooltip_(false) { | |
55 set_border(NULL); | 137 set_border(NULL); |
56 set_alignment(TextButton::ALIGN_CENTER); | 138 set_alignment(TextButton::ALIGN_CENTER); |
57 set_context_menu_controller(this); | 139 set_context_menu_controller(this); |
58 | 140 |
59 // No UpdateState() here because View hierarchy not setup yet. Our parent | 141 // No UpdateState() here because View hierarchy not setup yet. Our parent |
60 // should call UpdateState() after creation. | 142 // should call UpdateState() after creation. |
61 | 143 |
144 content::NotificationSource notification_source = | |
145 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); | |
62 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 146 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
63 content::Source<ExtensionAction>(browser_action_)); | 147 content::Source<ExtensionAction>(browser_action_)); |
64 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 148 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
65 content::Source<Profile>( | 149 notification_source); |
66 panel_->profile()->GetOriginalProfile())); | |
67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, | 150 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, |
68 content::Source<Profile>( | 151 notification_source); |
69 panel_->profile()->GetOriginalProfile())); | |
70 } | 152 } |
71 | 153 |
72 void BrowserActionButton::Destroy() { | 154 void BrowserActionButton::Destroy() { |
73 MaybeUnregisterExtensionCommand(false); | 155 MaybeUnregisterExtensionCommand(false); |
74 | 156 |
75 if (context_menu_) { | 157 if (context_menu_) { |
76 context_menu_->Cancel(); | 158 context_menu_->Cancel(); |
77 MessageLoop::current()->DeleteSoon(FROM_HERE, this); | 159 MessageLoop::current()->DeleteSoon(FROM_HERE, this); |
78 } else { | 160 } else { |
79 delete this; | 161 delete this; |
(...skipping 30 matching lines...) Expand all Loading... | |
110 | 192 |
111 bool BrowserActionButton::CanHandleAccelerators() const { | 193 bool BrowserActionButton::CanHandleAccelerators() const { |
112 // View::CanHandleAccelerators() checks to see if the view is visible before | 194 // View::CanHandleAccelerators() checks to see if the view is visible before |
113 // allowing it to process accelerators. This is not appropriate for browser | 195 // allowing it to process accelerators. This is not appropriate for browser |
114 // actions buttons, which can be hidden inside the overflow area. | 196 // actions buttons, which can be hidden inside the overflow area. |
115 return true; | 197 return true; |
116 } | 198 } |
117 | 199 |
118 void BrowserActionButton::ButtonPressed(views::Button* sender, | 200 void BrowserActionButton::ButtonPressed(views::Button* sender, |
119 const views::Event& event) { | 201 const views::Event& event) { |
120 panel_->OnBrowserActionExecuted(this); | 202 delegate_->OnBrowserActionExecuted(this); |
121 } | 203 } |
122 | 204 |
123 void BrowserActionButton::ShowContextMenuForView(View* source, | 205 void BrowserActionButton::ShowContextMenuForView(View* source, |
124 const gfx::Point& point) { | 206 const gfx::Point& point) { |
125 if (!extension()->ShowConfigureContextMenus()) | 207 ShowContextMenuImpl(); |
Peter Kasting
2012/07/26 20:37:17
Nit: Should you have a TODO about handling |source
yefimt
2012/07/31 00:10:11
I dont know, it is an existing implementation and
Peter Kasting
2012/07/31 02:30:42
But you're adding one of the calls to this functio
yefimt
2012/07/31 17:19:35
:)
As you noted earlier it were two functions with
Peter Kasting
2012/07/31 18:29:30
I'm talking about BrowserActionButton::ShowContext
yefimt
2012/08/03 17:29:21
It is so strange. Somehow codereview is confused.
| |
126 return; | |
127 | |
128 SetButtonPushed(); | |
129 | |
130 // Reconstructs the menu every time because the menu's contents are dynamic. | |
131 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( | |
132 new ExtensionContextMenuModel(extension(), panel_->browser())); | |
133 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); | |
134 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); | |
135 | |
136 context_menu_ = menu_runner.GetMenu(); | |
137 gfx::Point screen_loc; | |
138 views::View::ConvertPointToScreen(this, &screen_loc); | |
139 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), | |
140 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == | |
141 views::MenuRunner::MENU_DELETED) | |
142 return; | |
143 | |
144 SetButtonNotPushed(); | |
145 context_menu_ = NULL; | |
146 } | 208 } |
147 | 209 |
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, | 210 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, |
149 const std::string& extension_id, | 211 const std::string& extension_id, |
150 int index) { | 212 int index) { |
151 if (!image.IsEmpty()) | 213 if (!image.IsEmpty()) |
152 default_icon_ = *image.ToSkBitmap(); | 214 default_icon_ = *image.ToSkBitmap(); |
153 | 215 |
154 // Call back to UpdateState() because a more specific icon might have been set | 216 // Call back to UpdateState() because a more specific icon might have been set |
155 // while the load was outstanding. | 217 // while the load was outstanding. |
156 UpdateState(); | 218 UpdateState(); |
157 } | 219 } |
158 | 220 |
159 void BrowserActionButton::UpdateState() { | 221 void BrowserActionButton::UpdateState() { |
160 int tab_id = panel_->GetCurrentTabId(); | 222 int tab_id = delegate_->GetCurrentTabId(); |
161 if (tab_id < 0) | 223 if (tab_id < 0) |
162 return; | 224 return; |
163 | 225 |
164 if (!IsEnabled(tab_id)) { | 226 if (!IsEnabled(tab_id)) { |
165 SetState(views::CustomButton::BS_DISABLED); | 227 SetState(views::CustomButton::BS_DISABLED); |
166 } else { | 228 } else { |
167 SetState(menu_visible_ ? | 229 SetState(menu_visible_ ? |
168 views::CustomButton::BS_NORMAL : | 230 views::CustomButton::BS_NORMAL : |
169 views::CustomButton::BS_PUSHED); | 231 views::CustomButton::BS_PUSHED); |
170 } | 232 } |
(...skipping 28 matching lines...) Expand all Loading... | |
199 SkBitmap bg_p; | 261 SkBitmap bg_p; |
200 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, | 262 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, |
201 SkBitmap::kARGB_8888_Config); | 263 SkBitmap::kARGB_8888_Config); |
202 SkCanvas bg_p_canvas(bg_p); | 264 SkCanvas bg_p_canvas(bg_p); |
203 bg_p_canvas.drawBitmap(icon, | 265 bg_p_canvas.drawBitmap(icon, |
204 SkIntToScalar((bg_p.width() - icon.width()) / 2), | 266 SkIntToScalar((bg_p.width() - icon.width()) / 2), |
205 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | 267 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); |
206 SetPushedIcon(bg_p); | 268 SetPushedIcon(bg_p); |
207 } | 269 } |
208 | 270 |
209 // If the browser action name is empty, show the extension name instead. | 271 string16 name = GetTextForTooltip(); |
210 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 272 SetTooltipText(disable_tooltip_ ? string16() : name); |
211 if (name.empty()) | |
212 name = UTF8ToUTF16(extension()->name()); | |
213 SetTooltipText(name); | |
214 SetAccessibleName(name); | 273 SetAccessibleName(name); |
215 | 274 |
216 parent()->SchedulePaint(); | 275 parent()->SchedulePaint(); |
217 } | 276 } |
218 | 277 |
219 bool BrowserActionButton::IsPopup() { | 278 bool BrowserActionButton::IsPopup() { |
220 int tab_id = panel_->GetCurrentTabId(); | 279 int tab_id = delegate_->GetCurrentTabId(); |
221 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); | 280 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); |
222 } | 281 } |
223 | 282 |
224 GURL BrowserActionButton::GetPopupUrl() { | 283 GURL BrowserActionButton::GetPopupUrl() { |
225 int tab_id = panel_->GetCurrentTabId(); | 284 int tab_id = delegate_->GetCurrentTabId(); |
226 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); | 285 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); |
227 } | 286 } |
228 | 287 |
229 void BrowserActionButton::Observe(int type, | 288 void BrowserActionButton::Observe(int type, |
230 const content::NotificationSource& source, | 289 const content::NotificationSource& source, |
231 const content::NotificationDetails& details) { | 290 const content::NotificationDetails& details) { |
232 switch (type) { | 291 switch (type) { |
233 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: | 292 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: |
234 UpdateState(); | 293 UpdateState(); |
235 // The browser action may have become visible/hidden so we need to make | 294 // The browser action may have become visible/hidden so we need to make |
236 // sure the state gets updated. | 295 // sure the state gets updated. |
237 panel_->OnBrowserActionVisibilityChanged(); | 296 delegate_->OnBrowserActionVisibilityChanged(); |
238 break; | 297 break; |
239 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: | 298 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: |
240 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { | 299 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { |
241 std::pair<const std::string, const std::string>* payload = | 300 std::pair<const std::string, const std::string>* payload = |
242 content::Details<std::pair<const std::string, const std::string> >( | 301 content::Details<std::pair<const std::string, const std::string> >( |
243 details).ptr(); | 302 details).ptr(); |
244 if (extension_->id() == payload->first && | 303 if (extension_->id() == payload->first && |
245 payload->second == | 304 payload->second == |
246 extension_manifest_values::kBrowserActionKeybindingEvent) { | 305 extension_manifest_values::kBrowserActionKeybindingEvent) { |
247 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) | 306 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) |
248 MaybeRegisterExtensionCommand(); | 307 MaybeRegisterExtensionCommand(); |
249 else | 308 else |
250 MaybeUnregisterExtensionCommand(true); | 309 MaybeUnregisterExtensionCommand(true); |
251 } | 310 } |
252 break; | 311 break; |
253 } | 312 } |
254 default: | 313 default: |
255 NOTREACHED(); | 314 NOTREACHED(); |
256 break; | 315 break; |
257 } | 316 } |
258 } | 317 } |
259 | 318 |
260 bool BrowserActionButton::Activate() { | 319 bool BrowserActionButton::Activate() { |
261 if (!IsPopup()) | 320 if (!IsPopup()) |
262 return true; | 321 return true; |
263 | 322 |
264 panel_->OnBrowserActionExecuted(this); | 323 delegate_->OnBrowserActionExecuted(this); |
265 | 324 |
266 // TODO(erikkay): Run a nested modal loop while the mouse is down to | 325 // TODO(erikkay): Run a nested modal loop while the mouse is down to |
267 // enable menu-like drag-select behavior. | 326 // enable menu-like drag-select behavior. |
268 | 327 |
269 // The return value of this method is returned via OnMousePressed. | 328 // The return value of this method is returned via OnMousePressed. |
270 // We need to return false here since we're handing off focus to another | 329 // We need to return false here since we're handing off focus to another |
271 // widget/view, and true will grab it right back and try to send events | 330 // widget/view, and true will grab it right back and try to send events |
272 // to us. | 331 // to us. |
273 return false; | 332 return false; |
274 } | 333 } |
(...skipping 26 matching lines...) Expand all Loading... | |
301 MenuButton::OnMouseExited(event); | 360 MenuButton::OnMouseExited(event); |
302 else | 361 else |
303 TextButton::OnMouseExited(event); | 362 TextButton::OnMouseExited(event); |
304 } | 363 } |
305 | 364 |
306 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { | 365 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { |
307 return IsPopup() ? MenuButton::OnKeyReleased(event) | 366 return IsPopup() ? MenuButton::OnKeyReleased(event) |
308 : TextButton::OnKeyReleased(event); | 367 : TextButton::OnKeyReleased(event); |
309 } | 368 } |
310 | 369 |
370 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, | |
371 bool is_mouse_gesture) { | |
372 ShowContextMenuImpl(); | |
373 } | |
374 | |
311 bool BrowserActionButton::AcceleratorPressed( | 375 bool BrowserActionButton::AcceleratorPressed( |
312 const ui::Accelerator& accelerator) { | 376 const ui::Accelerator& accelerator) { |
313 panel_->OnBrowserActionExecuted(this); | 377 delegate_->OnBrowserActionExecuted(this); |
314 return true; | 378 return true; |
315 } | 379 } |
316 | 380 |
317 void BrowserActionButton::SetButtonPushed() { | 381 void BrowserActionButton::SetButtonPushed() { |
318 SetState(views::CustomButton::BS_PUSHED); | 382 SetState(views::CustomButton::BS_PUSHED); |
319 menu_visible_ = true; | 383 menu_visible_ = true; |
320 } | 384 } |
321 | 385 |
322 void BrowserActionButton::SetButtonNotPushed() { | 386 void BrowserActionButton::SetButtonNotPushed() { |
323 SetState(views::CustomButton::BS_NORMAL); | 387 SetState(views::CustomButton::BS_NORMAL); |
324 menu_visible_ = false; | 388 menu_visible_ = false; |
325 } | 389 } |
326 | 390 |
391 void BrowserActionButton::SetTooltipDisabled(bool disable_tooltip) { | |
392 disable_tooltip_ = disable_tooltip; | |
393 SetTooltipText(disable_tooltip_ ? string16() : GetTextForTooltip()); | |
394 } | |
395 | |
327 bool BrowserActionButton::IsEnabled(int tab_id) const { | 396 bool BrowserActionButton::IsEnabled(int tab_id) const { |
328 return browser_action_->GetIsVisible(tab_id); | 397 return browser_action_->GetIsVisible(tab_id); |
329 } | 398 } |
330 | 399 |
331 BrowserActionButton::~BrowserActionButton() { | 400 BrowserActionButton::~BrowserActionButton() { |
332 } | 401 } |
333 | 402 |
334 void BrowserActionButton::MaybeRegisterExtensionCommand() { | 403 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
335 extensions::CommandService* command_service = | 404 extensions::CommandService* command_service = |
336 extensions::CommandServiceFactory::GetForProfile( | 405 extensions::CommandServiceFactory::GetForProfile(browser_->profile()); |
337 panel_->browser()->profile()); | |
338 extensions::Command browser_action_command; | 406 extensions::Command browser_action_command; |
339 if (command_service->GetBrowserActionCommand( | 407 if (command_service->GetBrowserActionCommand( |
340 extension_->id(), | 408 extension_->id(), |
341 extensions::CommandService::ACTIVE_ONLY, | 409 extensions::CommandService::ACTIVE_ONLY, |
342 &browser_action_command, | 410 &browser_action_command, |
343 NULL)) { | 411 NULL)) { |
344 keybinding_.reset(new ui::Accelerator( | 412 keybinding_.reset(new ui::Accelerator( |
345 browser_action_command.accelerator())); | 413 browser_action_command.accelerator())); |
346 panel_->GetFocusManager()->RegisterAccelerator( | 414 GetFocusManager()->RegisterAccelerator( |
347 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); | 415 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); |
348 } | 416 } |
349 } | 417 } |
350 | 418 |
351 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { | 419 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { |
352 if (!keybinding_.get() || !panel_->GetFocusManager()) | 420 if (!keybinding_.get() || !GetFocusManager()) |
353 return; | 421 return; |
354 | 422 |
355 extensions::CommandService* command_service = | 423 extensions::CommandService* command_service = |
356 extensions::CommandServiceFactory::GetForProfile( | 424 extensions::CommandServiceFactory::GetForProfile(browser_->profile()); |
357 panel_->browser()->profile()); | |
358 | 425 |
359 extensions::Command browser_action_command; | 426 extensions::Command browser_action_command; |
360 if (!only_if_active || !command_service->GetBrowserActionCommand( | 427 if (!only_if_active || !command_service->GetBrowserActionCommand( |
361 extension_->id(), | 428 extension_->id(), |
362 extensions::CommandService::ACTIVE_ONLY, | 429 extensions::CommandService::ACTIVE_ONLY, |
363 &browser_action_command, | 430 &browser_action_command, |
364 NULL)) { | 431 NULL)) { |
365 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 432 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
366 } | 433 } |
367 } | 434 } |
368 | 435 |
436 string16 BrowserActionButton::GetTextForTooltip() { | |
437 int tab_id = delegate_->GetCurrentTabId(); | |
438 if (tab_id < 0) | |
439 return string16(); | |
369 | 440 |
370 //////////////////////////////////////////////////////////////////////////////// | 441 // If the browser action name is empty, show the extension name instead. |
371 // BrowserActionView | 442 std::string name = browser_action()->GetTitle(tab_id); |
372 | 443 return UTF8ToUTF16(name.empty() ? extension()->name() : name); |
373 BrowserActionView::BrowserActionView(const Extension* extension, | |
374 BrowserActionsContainer* panel) | |
375 : panel_(panel) { | |
376 button_ = new BrowserActionButton(extension, panel); | |
377 button_->set_drag_controller(panel_); | |
378 AddChildView(button_); | |
379 button_->UpdateState(); | |
380 } | 444 } |
381 | 445 |
382 BrowserActionView::~BrowserActionView() { | 446 void BrowserActionButton::ShowContextMenuImpl() { |
383 RemoveChildView(button_); | 447 if (!extension()->ShowConfigureContextMenus()) |
384 button_->Destroy(); | 448 return; |
449 | |
450 SetButtonPushed(); | |
451 | |
452 // Reconstructs the menu every time because the menu's contents are dynamic. | |
453 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( | |
454 new ExtensionContextMenuModel(extension(), browser_)); | |
455 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); | |
456 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); | |
Peter Kasting
2012/07/26 20:37:17
MenuRunner objects should never be created on the
yefimt
2012/07/31 00:10:11
Done.
| |
457 | |
458 context_menu_ = menu_runner.GetMenu(); | |
459 gfx::Point screen_loc; | |
460 views::View::ConvertPointToScreen(this, &screen_loc); | |
461 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), | |
462 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == | |
463 views::MenuRunner::MENU_DELETED) | |
464 return; | |
465 | |
466 SetButtonNotPushed(); | |
467 context_menu_ = NULL; | |
385 } | 468 } |
386 | |
387 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | |
388 int tab_id = panel_->GetCurrentTabId(); | |
389 | |
390 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); | |
391 if (icon.isNull()) | |
392 icon = button_->default_icon(); | |
393 | |
394 // Dim the icon if our button is disabled. | |
395 if (!button_->IsEnabled(tab_id)) | |
396 icon = MakeTransparent(icon); | |
397 | |
398 gfx::Canvas* canvas = | |
399 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | |
400 | |
401 if (tab_id >= 0) { | |
402 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | |
403 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | |
404 } | |
405 | |
406 return canvas; | |
407 } | |
408 | |
409 void BrowserActionView::Layout() { | |
410 // We can't rely on button_->GetPreferredSize() here because that's not set | |
411 // correctly until the first call to | |
412 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | |
413 // called before that when the initial bounds are set (and then not after, | |
414 // since the bounds don't change). So instead of setting the height from the | |
415 // button's preferred size, we use IconHeight(), since that's how big the | |
416 // button should be regardless of what it's displaying. | |
417 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), | |
418 BrowserActionsContainer::IconHeight()); | |
419 } | |
420 | |
421 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | |
422 state->name = l10n_util::GetStringUTF16( | |
423 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | |
424 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | |
425 } | |
426 | |
427 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | |
428 View::PaintChildren(canvas); | |
429 ExtensionAction* action = button()->browser_action(); | |
430 int tab_id = panel_->GetCurrentTabId(); | |
431 if (tab_id >= 0) | |
432 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | |
433 } | |
OLD | NEW |