| 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/event.h" | 20 #include "ui/base/event.h" |
| 21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
| 22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
| 23 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/image/image_skia.h" |
| 24 #include "ui/gfx/skbitmap_operations.h" | 24 #include "ui/gfx/image/image_skia_operations.h" |
| 25 #include "ui/gfx/image/image_skia_source.h" |
| 25 #include "ui/views/controls/menu/menu_model_adapter.h" | 26 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 26 #include "ui/views/controls/menu/menu_runner.h" | 27 #include "ui/views/controls/menu/menu_runner.h" |
| 27 | 28 |
| 28 using extensions::Extension; | 29 using extensions::Extension; |
| 29 | 30 |
| 30 namespace { | |
| 31 | |
| 32 // Return a more transparent |image|, with 25% of its original opacity. | |
| 33 SkBitmap MakeTransparent(const SkBitmap& image) { | |
| 34 SkBitmap alpha; | |
| 35 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | |
| 36 alpha.allocPixels(); | |
| 37 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
| 38 | |
| 39 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | |
| 40 } | |
| 41 | |
| 42 } // namespace | |
| 43 | |
| 44 //////////////////////////////////////////////////////////////////////////////// | 31 //////////////////////////////////////////////////////////////////////////////// |
| 45 // BrowserActionView | 32 // BrowserActionView |
| 46 | 33 |
| 47 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const { | 34 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const { |
| 48 return true; | 35 return true; |
| 49 } | 36 } |
| 50 | 37 |
| 51 bool BrowserActionView::Delegate::NeedToShowTooltip() const { | 38 bool BrowserActionView::Delegate::NeedToShowTooltip() const { |
| 52 return true; | 39 return true; |
| 53 } | 40 } |
| 54 | 41 |
| 55 BrowserActionView::BrowserActionView(const Extension* extension, | 42 BrowserActionView::BrowserActionView(const Extension* extension, |
| 56 Browser* browser, | 43 Browser* browser, |
| 57 BrowserActionView::Delegate* delegate) | 44 BrowserActionView::Delegate* delegate) |
| 58 : browser_(browser), | 45 : browser_(browser), |
| 59 delegate_(delegate), | 46 delegate_(delegate), |
| 60 button_(NULL), | 47 button_(NULL), |
| 61 extension_(extension) { | 48 extension_(extension) { |
| 62 } | 49 } |
| 63 | 50 |
| 64 BrowserActionView::~BrowserActionView() { | 51 BrowserActionView::~BrowserActionView() { |
| 65 } | 52 } |
| 66 | 53 |
| 67 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 54 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
| 68 int tab_id = delegate_->GetCurrentTabId(); | 55 int tab_id = delegate_->GetCurrentTabId(); |
| 69 | 56 |
| 70 SkBitmap icon = | 57 const ExtensionAction* action = button_->extension()->browser_action(); |
| 71 *button_->extension()->browser_action()->GetIcon(tab_id).ToSkBitmap(); | 58 gfx::Size spacing(0, ToolbarView::kVertSpacing); |
| 72 | 59 gfx::ImageSkia icon = *action->GetIcon(tab_id).ToImageSkia(); |
| 73 // Dim the icon if our button is disabled. | |
| 74 if (!button_->IsEnabled(tab_id)) | 60 if (!button_->IsEnabled(tab_id)) |
| 75 icon = MakeTransparent(icon); | 61 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| 76 | 62 return action->GetIconWithBadge(icon, tab_id, spacing); |
| 77 gfx::Canvas* canvas = | |
| 78 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | |
| 79 | |
| 80 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | |
| 81 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | |
| 82 | |
| 83 return canvas; | |
| 84 } | 63 } |
| 85 | 64 |
| 86 void BrowserActionView::Layout() { | 65 void BrowserActionView::Layout() { |
| 87 // We can't rely on button_->GetPreferredSize() here because that's not set | 66 // We can't rely on button_->GetPreferredSize() here because that's not set |
| 88 // correctly until the first call to | 67 // correctly until the first call to |
| 89 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 68 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
| 90 // called before that when the initial bounds are set (and then not after, | 69 // called before that when the initial bounds are set (and then not after, |
| 91 // since the bounds don't change). So instead of setting the height from the | 70 // since the bounds don't change). So instead of setting the height from the |
| 92 // button's preferred size, we use IconHeight(), since that's how big the | 71 // button's preferred size, we use IconHeight(), since that's how big the |
| 93 // button should be regardless of what it's displaying. | 72 // button should be regardless of what it's displaying. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 254 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); | 233 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); |
| 255 | 234 |
| 256 if (!IsEnabled(tab_id)) { | 235 if (!IsEnabled(tab_id)) { |
| 257 SetState(views::CustomButton::BS_DISABLED); | 236 SetState(views::CustomButton::BS_DISABLED); |
| 258 } else { | 237 } else { |
| 259 SetState(menu_visible_ ? | 238 SetState(menu_visible_ ? |
| 260 views::CustomButton::BS_PUSHED : | 239 views::CustomButton::BS_PUSHED : |
| 261 views::CustomButton::BS_NORMAL); | 240 views::CustomButton::BS_NORMAL); |
| 262 } | 241 } |
| 263 | 242 |
| 264 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); | 243 gfx::ImageSkia icon = *browser_action()->GetIcon(tab_id).ToImageSkia(); |
| 244 |
| 265 if (!icon.isNull()) { | 245 if (!icon.isNull()) { |
| 266 if (!browser_action()->GetIsVisible(tab_id)) | 246 if (!browser_action()->GetIsVisible(tab_id)) |
| 267 icon = MakeTransparent(icon); | 247 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
| 268 SkPaint paint; | 248 |
| 269 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | |
| 270 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 249 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 271 | 250 |
| 272 SkBitmap bg; | 251 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| 273 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 252 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
| 274 SkBitmap::kARGB_8888_Config); | |
| 275 SkCanvas bg_canvas(bg); | |
| 276 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), | |
| 277 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); | |
| 278 SetIcon(bg); | |
| 279 | 253 |
| 280 SkBitmap bg_h; | 254 gfx::ImageSkia bg_h = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
| 281 rb.GetBitmapNamed(IDR_BROWSER_ACTION_H)->copyTo(&bg_h, | 255 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h, icon)); |
| 282 SkBitmap::kARGB_8888_Config); | |
| 283 SkCanvas bg_h_canvas(bg_h); | |
| 284 bg_h_canvas.drawBitmap(icon, | |
| 285 SkIntToScalar((bg_h.width() - icon.width()) / 2), | |
| 286 SkIntToScalar((bg_h.height() - icon.height()) / 2), &paint); | |
| 287 SetHoverIcon(bg_h); | |
| 288 | 256 |
| 289 SkBitmap bg_p; | 257 gfx::ImageSkia bg_p = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
| 290 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, | 258 SetPushedIcon( |
| 291 SkBitmap::kARGB_8888_Config); | 259 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p, icon)); |
| 292 SkCanvas bg_p_canvas(bg_p); | |
| 293 bg_p_canvas.drawBitmap(icon, | |
| 294 SkIntToScalar((bg_p.width() - icon.width()) / 2), | |
| 295 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | |
| 296 SetPushedIcon(bg_p); | |
| 297 } | 260 } |
| 298 | 261 |
| 299 // If the browser action name is empty, show the extension name instead. | 262 // If the browser action name is empty, show the extension name instead. |
| 300 std::string title = browser_action()->GetTitle(tab_id); | 263 std::string title = browser_action()->GetTitle(tab_id); |
| 301 string16 name = UTF8ToUTF16(title.empty() ? extension()->name() : title); | 264 string16 name = UTF8ToUTF16(title.empty() ? extension()->name() : title); |
| 302 SetTooltipText(delegate_->NeedToShowTooltip() ? name : string16()); | 265 SetTooltipText(delegate_->NeedToShowTooltip() ? name : string16()); |
| 303 SetAccessibleName(name); | 266 SetAccessibleName(name); |
| 304 | 267 |
| 305 parent()->SchedulePaint(); | 268 parent()->SchedulePaint(); |
| 306 } | 269 } |
| (...skipping 138 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 445 | 408 |
| 446 extensions::Command browser_action_command; | 409 extensions::Command browser_action_command; |
| 447 if (!only_if_active || !command_service->GetBrowserActionCommand( | 410 if (!only_if_active || !command_service->GetBrowserActionCommand( |
| 448 extension_->id(), | 411 extension_->id(), |
| 449 extensions::CommandService::ACTIVE_ONLY, | 412 extensions::CommandService::ACTIVE_ONLY, |
| 450 &browser_action_command, | 413 &browser_action_command, |
| 451 NULL)) { | 414 NULL)) { |
| 452 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 415 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 453 } | 416 } |
| 454 } | 417 } |
| OLD | NEW |