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/toolbar_view.h" | 13 #include "chrome/browser/ui/views/toolbar_view.h" |
14 #include "chrome/common/chrome_notification_types.h" | 14 #include "chrome/common/chrome_notification_types.h" |
15 #include "chrome/common/extensions/extension.h" | 15 #include "chrome/common/extensions/extension.h" |
16 #include "chrome/common/extensions/extension_manifest_constants.h" | 16 #include "chrome/common/extensions/extension_manifest_constants.h" |
17 #include "grit/generated_resources.h" | 17 #include "grit/generated_resources.h" |
18 #include "grit/theme_resources.h" | 18 #include "grit/theme_resources.h" |
19 #include "ui/base/accessibility/accessible_view_state.h" | 19 #include "ui/base/accessibility/accessible_view_state.h" |
20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
21 #include "ui/base/resource/resource_bundle.h" | 21 #include "ui/base/resource/resource_bundle.h" |
22 #include "ui/gfx/canvas.h" | 22 #include "ui/gfx/canvas.h" |
| 23 #include "ui/gfx/skbitmap_operations.h" |
23 #include "ui/views/controls/menu/menu_model_adapter.h" | 24 #include "ui/views/controls/menu/menu_model_adapter.h" |
24 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
25 | 26 |
26 using extensions::Extension; | 27 using extensions::Extension; |
27 | 28 |
| 29 namespace { |
| 30 |
| 31 // Return a more transparent |image|, with 25% of its original opacity. |
| 32 SkBitmap MakeTransparent(const SkBitmap& image) { |
| 33 SkBitmap alpha; |
| 34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); |
| 35 alpha.allocPixels(); |
| 36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); |
| 37 |
| 38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
28 //////////////////////////////////////////////////////////////////////////////// | 43 //////////////////////////////////////////////////////////////////////////////// |
29 // BrowserActionButton | 44 // BrowserActionButton |
30 | 45 |
31 BrowserActionButton::BrowserActionButton(const Extension* extension, | 46 BrowserActionButton::BrowserActionButton(const Extension* extension, |
32 BrowserActionsContainer* panel) | 47 BrowserActionsContainer* panel) |
33 : ALLOW_THIS_IN_INITIALIZER_LIST( | 48 : ALLOW_THIS_IN_INITIALIZER_LIST( |
34 MenuButton(this, string16(), NULL, false)), | 49 MenuButton(this, string16(), NULL, false)), |
35 browser_action_(extension->browser_action()), | 50 browser_action_(extension->browser_action()), |
36 extension_(extension), | 51 extension_(extension), |
37 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 // Call back to UpdateState() because a more specific icon might have been set | 128 // Call back to UpdateState() because a more specific icon might have been set |
114 // while the load was outstanding. | 129 // while the load was outstanding. |
115 UpdateState(); | 130 UpdateState(); |
116 } | 131 } |
117 | 132 |
118 void BrowserActionButton::UpdateState() { | 133 void BrowserActionButton::UpdateState() { |
119 int tab_id = panel_->GetCurrentTabId(); | 134 int tab_id = panel_->GetCurrentTabId(); |
120 if (tab_id < 0) | 135 if (tab_id < 0) |
121 return; | 136 return; |
122 | 137 |
| 138 if (!IsEnabled(tab_id)) { |
| 139 SetState(views::CustomButton::BS_DISABLED); |
| 140 } else { |
| 141 SetState(menu_visible_ ? |
| 142 views::CustomButton::BS_NORMAL : |
| 143 views::CustomButton::BS_PUSHED); |
| 144 } |
| 145 |
123 SkBitmap icon(browser_action()->GetIcon(tab_id)); | 146 SkBitmap icon(browser_action()->GetIcon(tab_id)); |
124 if (icon.isNull()) | 147 if (icon.isNull()) |
125 icon = default_icon_; | 148 icon = default_icon_; |
126 if (!icon.isNull()) { | 149 if (!icon.isNull()) { |
| 150 if (!browser_action()->GetIsVisible(tab_id)) |
| 151 icon = MakeTransparent(icon); |
127 SkPaint paint; | 152 SkPaint paint; |
128 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | 153 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 154 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
130 | 155 |
131 SkBitmap bg; | 156 SkBitmap bg; |
132 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 157 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, |
133 SkBitmap::kARGB_8888_Config); | 158 SkBitmap::kARGB_8888_Config); |
134 SkCanvas bg_canvas(bg); | 159 SkCanvas bg_canvas(bg); |
135 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), | 160 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), |
136 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); | 161 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); |
(...skipping 17 matching lines...) Expand all Loading... |
154 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | 179 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); |
155 SetPushedIcon(bg_p); | 180 SetPushedIcon(bg_p); |
156 } | 181 } |
157 | 182 |
158 // If the browser action name is empty, show the extension name instead. | 183 // If the browser action name is empty, show the extension name instead. |
159 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 184 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
160 if (name.empty()) | 185 if (name.empty()) |
161 name = UTF8ToUTF16(extension()->name()); | 186 name = UTF8ToUTF16(extension()->name()); |
162 SetTooltipText(name); | 187 SetTooltipText(name); |
163 SetAccessibleName(name); | 188 SetAccessibleName(name); |
| 189 |
164 parent()->SchedulePaint(); | 190 parent()->SchedulePaint(); |
165 } | 191 } |
166 | 192 |
167 bool BrowserActionButton::IsPopup() { | 193 bool BrowserActionButton::IsPopup() { |
168 int tab_id = panel_->GetCurrentTabId(); | 194 int tab_id = panel_->GetCurrentTabId(); |
169 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); | 195 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); |
170 } | 196 } |
171 | 197 |
172 GURL BrowserActionButton::GetPopupUrl() { | 198 GURL BrowserActionButton::GetPopupUrl() { |
173 int tab_id = panel_->GetCurrentTabId(); | 199 int tab_id = panel_->GetCurrentTabId(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
290 void BrowserActionButton::SetButtonPushed() { | 316 void BrowserActionButton::SetButtonPushed() { |
291 SetState(views::CustomButton::BS_PUSHED); | 317 SetState(views::CustomButton::BS_PUSHED); |
292 menu_visible_ = true; | 318 menu_visible_ = true; |
293 } | 319 } |
294 | 320 |
295 void BrowserActionButton::SetButtonNotPushed() { | 321 void BrowserActionButton::SetButtonNotPushed() { |
296 SetState(views::CustomButton::BS_NORMAL); | 322 SetState(views::CustomButton::BS_NORMAL); |
297 menu_visible_ = false; | 323 menu_visible_ = false; |
298 } | 324 } |
299 | 325 |
| 326 bool BrowserActionButton::IsEnabled(int tab_id) const { |
| 327 return browser_action_->GetIsVisible(tab_id); |
| 328 } |
| 329 |
300 BrowserActionButton::~BrowserActionButton() { | 330 BrowserActionButton::~BrowserActionButton() { |
301 } | 331 } |
302 | 332 |
303 void BrowserActionButton::MaybeRegisterExtensionCommand() { | 333 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
304 extensions::CommandService* command_service = | 334 extensions::CommandService* command_service = |
305 extensions::CommandServiceFactory::GetForProfile( | 335 extensions::CommandServiceFactory::GetForProfile( |
306 panel_->browser()->profile()); | 336 panel_->browser()->profile()); |
307 extensions::Command browser_action_command; | 337 extensions::Command browser_action_command; |
308 if (command_service->GetBrowserActionCommand( | 338 if (command_service->GetBrowserActionCommand( |
309 extension_->id(), | 339 extension_->id(), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
353 button_->Destroy(); | 383 button_->Destroy(); |
354 } | 384 } |
355 | 385 |
356 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 386 gfx::Canvas* BrowserActionView::GetIconWithBadge() { |
357 int tab_id = panel_->GetCurrentTabId(); | 387 int tab_id = panel_->GetCurrentTabId(); |
358 | 388 |
359 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); | 389 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); |
360 if (icon.isNull()) | 390 if (icon.isNull()) |
361 icon = button_->default_icon(); | 391 icon = button_->default_icon(); |
362 | 392 |
| 393 // Dim the icon if our button is disabled. |
| 394 if (!button_->IsEnabled(tab_id)) |
| 395 icon = MakeTransparent(icon); |
| 396 |
363 gfx::Canvas* canvas = | 397 gfx::Canvas* canvas = |
364 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | 398 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); |
365 | 399 |
366 if (tab_id >= 0) { | 400 if (tab_id >= 0) { |
367 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | 401 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); |
368 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | 402 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); |
369 } | 403 } |
370 | 404 |
371 return canvas; | 405 return canvas; |
372 } | 406 } |
(...skipping 16 matching lines...) Expand all Loading... |
389 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 423 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
390 } | 424 } |
391 | 425 |
392 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 426 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
393 View::PaintChildren(canvas); | 427 View::PaintChildren(canvas); |
394 ExtensionAction* action = button()->browser_action(); | 428 ExtensionAction* action = button()->browser_action(); |
395 int tab_id = panel_->GetCurrentTabId(); | 429 int tab_id = panel_->GetCurrentTabId(); |
396 if (tab_id >= 0) | 430 if (tab_id >= 0) |
397 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 431 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
398 } | 432 } |
OLD | NEW |