Chromium Code Reviews| 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/gfx/image/canvas_image_source.h" |
| 24 #include "ui/gfx/image/image_skia.h" | |
| 25 #include "ui/gfx/image/image_skia_operations.h" | |
| 26 #include "ui/gfx/image/image_skia_source.h" | |
| 24 #include "ui/views/controls/menu/menu_model_adapter.h" | 27 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 25 #include "ui/views/controls/menu/menu_runner.h" | 28 #include "ui/views/controls/menu/menu_runner.h" |
| 26 | 29 |
| 27 using extensions::Extension; | 30 using extensions::Extension; |
| 28 | 31 |
| 29 namespace { | 32 namespace { |
| 30 | 33 |
| 34 // CanvasImageSource for creating an browser action icon image with the provided | |
| 35 // background. | |
| 36 class IconWithBackgroundImageSource : public gfx::CanvasImageSource { | |
| 37 public: | |
| 38 IconWithBackgroundImageSource(const gfx::ImageSkia& icon, | |
| 39 const gfx::ImageSkia& background) | |
| 40 : gfx::CanvasImageSource(background.size(), false), | |
| 41 icon_(icon), | |
| 42 background_(background) { | |
| 43 } | |
| 44 | |
| 45 private: | |
| 46 virtual ~IconWithBackgroundImageSource() {} | |
| 47 | |
| 48 virtual void Draw(gfx::Canvas* canvas) OVERRIDE { | |
| 49 SkPaint paint; | |
| 50 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | |
| 51 | |
| 52 // We center the icon on the background. | |
| 53 canvas->DrawImageInt(icon_, | |
| 54 (background_.width() - icon_.width()) / 2, | |
| 55 (background_.height() - icon_.height()) / 2, | |
| 56 paint); | |
| 57 } | |
| 58 | |
| 59 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 60 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 61 gfx::Canvas canvas(background_.GetRepresentation(scale_factor), is_opaque_); | |
| 62 Draw(&canvas); | |
| 63 return canvas.ExtractImageRep(); | |
|
pkotwicz
2012/08/07 22:31:22
A couple comments.
It feels like we could introduc
tbarzic
2012/08/08 02:09:29
Done.
| |
| 64 } | |
| 65 | |
| 66 gfx::ImageSkia icon_; | |
| 67 gfx::ImageSkia background_; | |
| 68 }; | |
| 69 | |
| 70 // ImageSource for creating an image to be used as alpha in CreateMaskedImage | |
| 71 // image skia operation while creating transparent image. | |
| 72 class TransparentAlphaImageSource : public gfx::ImageSkiaSource { | |
| 73 public: | |
| 74 TransparentAlphaImageSource(size_t width, size_t height) | |
| 75 : width_(width), | |
| 76 height_(height) { | |
| 77 } | |
| 78 | |
|
pkotwicz
2012/08/07 22:31:22
Nit: destructor
tbarzic
2012/08/08 02:09:29
Done.
| |
| 79 private: | |
| 80 virtual ~TransparentAlphaImageSource() {} | |
| 81 | |
| 82 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 83 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 84 SkBitmap alpha; | |
| 85 const float scale = ui::GetScaleFactorScale(scale_factor); | |
| 86 alpha.setConfig(SkBitmap::kARGB_8888_Config, | |
| 87 static_cast<int>(width_ * scale), | |
| 88 static_cast<int>(height_ * scale)); | |
| 89 alpha.allocPixels(); | |
| 90 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
| 91 return gfx::ImageSkiaRep(alpha, scale_factor); | |
| 92 } | |
| 93 | |
| 94 // Icon width in DIP. | |
| 95 size_t width_; | |
| 96 // Icon height in DIP. | |
| 97 size_t height_; | |
| 98 }; | |
| 99 | |
| 31 // Return a more transparent |image|, with 25% of its original opacity. | 100 // Return a more transparent |image|, with 25% of its original opacity. |
| 32 SkBitmap MakeTransparent(const SkBitmap& image) { | 101 gfx::ImageSkia MakeTransparent(const gfx::ImageSkia& image) { |
| 33 SkBitmap alpha; | 102 gfx::ImageSkia alpha( |
| 34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | 103 new TransparentAlphaImageSource(image.width(), image.height()), |
| 35 alpha.allocPixels(); | 104 gfx::Size(image.width(), image.height())); |
| 36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | 105 return gfx::ImageSkiaOperations::CreateMaskedImage(image, alpha); |
| 106 } | |
| 37 | 107 |
| 38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | 108 // CanvasImageSource for creating browser action icon with a badge. |
| 39 } | 109 class ActionBadgeImageSource : public gfx::CanvasImageSource { |
|
pkotwicz
2012/08/07 22:31:22
Can you move this to new function ExtensionAction:
tbarzic
2012/08/08 02:09:29
Done.
| |
| 110 public: | |
| 111 ActionBadgeImageSource(gfx::ImageSkia icon, | |
| 112 bool button_enabled, | |
| 113 const std::string& text, | |
| 114 const SkColor& text_color, | |
| 115 const SkColor& background_color) | |
| 116 : gfx::CanvasImageSource(icon.size(), false), | |
| 117 icon_(icon), | |
| 118 button_enabled_(button_enabled), | |
| 119 text_(text), | |
| 120 text_color_(text_color), | |
| 121 background_color_(background_color) { | |
| 122 } | |
| 123 | |
|
pkotwicz
2012/08/07 22:31:22
Nit: destructor
tbarzic
2012/08/08 02:09:29
Done.
| |
| 124 private: | |
| 125 virtual ~ActionBadgeImageSource() {} | |
| 126 | |
| 127 virtual void Draw(gfx::Canvas* canvas) OVERRIDE { | |
| 128 // Draw a badge on the provided browser action icon's canvas. | |
| 129 gfx::Rect bounds(size_.width(), | |
| 130 size_.height() + ToolbarView::kVertSpacing); | |
| 131 ExtensionAction::DoPaintBadge(canvas, bounds, text_, text_color_, | |
| 132 background_color_, size_.width()); | |
| 133 } | |
| 134 | |
| 135 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 136 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 137 gfx::ImageSkia icon = icon_; | |
| 138 if (!button_enabled_) | |
| 139 icon = MakeTransparent(icon); | |
| 140 | |
| 141 gfx::Canvas canvas(icon.GetRepresentation(scale_factor), is_opaque_); | |
| 142 Draw(&canvas); | |
| 143 return canvas.ExtractImageRep(); | |
| 144 } | |
| 145 | |
| 146 // Browser action icon image. | |
| 147 gfx::ImageSkia icon_; | |
| 148 // Whether the browser actions button is enabled. | |
| 149 bool button_enabled_; | |
| 150 // Text to be displayed on the badge. | |
| 151 std::string text_; | |
| 152 // Color of badge text. | |
| 153 SkColor text_color_; | |
| 154 // Color of the badge. | |
| 155 SkColor background_color_; | |
| 156 }; | |
| 40 | 157 |
| 41 } // namespace | 158 } // namespace |
| 42 | 159 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 160 //////////////////////////////////////////////////////////////////////////////// |
| 44 // BrowserActionButton | 161 // BrowserActionButton |
| 45 | 162 |
| 46 BrowserActionButton::BrowserActionButton(const Extension* extension, | 163 BrowserActionButton::BrowserActionButton(const Extension* extension, |
| 47 BrowserActionsContainer* panel) | 164 BrowserActionsContainer* panel) |
| 48 : ALLOW_THIS_IN_INITIALIZER_LIST( | 165 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| 49 MenuButton(this, string16(), NULL, false)), | 166 MenuButton(this, string16(), NULL, false)), |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 141 views::MenuRunner::MENU_DELETED) | 258 views::MenuRunner::MENU_DELETED) |
| 142 return; | 259 return; |
| 143 | 260 |
| 144 SetButtonNotPushed(); | 261 SetButtonNotPushed(); |
| 145 context_menu_ = NULL; | 262 context_menu_ = NULL; |
| 146 } | 263 } |
| 147 | 264 |
| 148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, | 265 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, |
| 149 const std::string& extension_id, | 266 const std::string& extension_id, |
| 150 int index) { | 267 int index) { |
| 151 browser_action_->CacheIcon(browser_action_->default_icon_path(), image); | 268 browser_action_->CacheIcon(browser_action_->default_icon_path(), |
| 269 *image.ToImageSkia()); | |
| 152 | 270 |
| 153 // Call back to UpdateState() because a more specific icon might have been set | 271 // Call back to UpdateState() because a more specific icon might have been set |
| 154 // while the load was outstanding. | 272 // while the load was outstanding. |
| 155 UpdateState(); | 273 UpdateState(); |
| 156 } | 274 } |
| 157 | 275 |
| 158 void BrowserActionButton::UpdateState() { | 276 void BrowserActionButton::UpdateState() { |
| 159 int tab_id = panel_->GetCurrentTabId(); | 277 int tab_id = panel_->GetCurrentTabId(); |
| 160 if (tab_id < 0) | 278 if (tab_id < 0) |
| 161 return; | 279 return; |
| 162 | 280 |
| 163 if (!IsEnabled(tab_id)) { | 281 if (!IsEnabled(tab_id)) { |
| 164 SetState(views::CustomButton::BS_DISABLED); | 282 SetState(views::CustomButton::BS_DISABLED); |
| 165 } else { | 283 } else { |
| 166 SetState(menu_visible_ ? | 284 SetState(menu_visible_ ? |
| 167 views::CustomButton::BS_PUSHED : | 285 views::CustomButton::BS_PUSHED : |
| 168 views::CustomButton::BS_NORMAL); | 286 views::CustomButton::BS_NORMAL); |
| 169 } | 287 } |
| 170 | 288 |
| 171 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); | 289 gfx::ImageSkia icon(browser_action()->GetIcon(tab_id)); |
| 290 | |
| 172 if (!icon.isNull()) { | 291 if (!icon.isNull()) { |
| 173 if (!browser_action()->GetIsVisible(tab_id)) | 292 if (!browser_action()->GetIsVisible(tab_id)) |
| 174 icon = MakeTransparent(icon); | 293 icon = MakeTransparent(icon); |
| 175 SkPaint paint; | |
| 176 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | |
| 177 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 294 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 178 | 295 |
| 179 SkBitmap bg; | 296 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| 180 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 297 SetIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg), |
| 181 SkBitmap::kARGB_8888_Config); | 298 bg.size())); |
| 182 SkCanvas bg_canvas(bg); | |
| 183 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), | |
| 184 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); | |
| 185 SetIcon(bg); | |
| 186 | 299 |
| 187 SkBitmap bg_h; | 300 gfx::ImageSkia bg_h = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
| 188 rb.GetBitmapNamed(IDR_BROWSER_ACTION_H)->copyTo(&bg_h, | 301 SetHoverIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg_h), |
| 189 SkBitmap::kARGB_8888_Config); | 302 bg_h.size())); |
| 190 SkCanvas bg_h_canvas(bg_h); | |
| 191 bg_h_canvas.drawBitmap(icon, | |
| 192 SkIntToScalar((bg_h.width() - icon.width()) / 2), | |
| 193 SkIntToScalar((bg_h.height() - icon.height()) / 2), &paint); | |
| 194 SetHoverIcon(bg_h); | |
| 195 | 303 |
| 196 SkBitmap bg_p; | 304 gfx::ImageSkia bg_p = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
| 197 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, | 305 SetPushedIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg_p), |
| 198 SkBitmap::kARGB_8888_Config); | 306 bg_p.size())); |
| 199 SkCanvas bg_p_canvas(bg_p); | |
| 200 bg_p_canvas.drawBitmap(icon, | |
| 201 SkIntToScalar((bg_p.width() - icon.width()) / 2), | |
| 202 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | |
| 203 SetPushedIcon(bg_p); | |
| 204 } | 307 } |
| 205 | 308 |
| 206 // If the browser action name is empty, show the extension name instead. | 309 // If the browser action name is empty, show the extension name instead. |
| 207 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 310 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
| 208 if (name.empty()) | 311 if (name.empty()) |
| 209 name = UTF8ToUTF16(extension()->name()); | 312 name = UTF8ToUTF16(extension()->name()); |
| 210 SetTooltipText(name); | 313 SetTooltipText(name); |
| 211 SetAccessibleName(name); | 314 SetAccessibleName(name); |
| 212 | 315 |
| 213 parent()->SchedulePaint(); | 316 parent()->SchedulePaint(); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 extensions::Command browser_action_command; | 459 extensions::Command browser_action_command; |
| 357 if (!only_if_active || !command_service->GetBrowserActionCommand( | 460 if (!only_if_active || !command_service->GetBrowserActionCommand( |
| 358 extension_->id(), | 461 extension_->id(), |
| 359 extensions::CommandService::ACTIVE_ONLY, | 462 extensions::CommandService::ACTIVE_ONLY, |
| 360 &browser_action_command, | 463 &browser_action_command, |
| 361 NULL)) { | 464 NULL)) { |
| 362 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 465 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 363 } | 466 } |
| 364 } | 467 } |
| 365 | 468 |
| 366 | |
| 367 //////////////////////////////////////////////////////////////////////////////// | 469 //////////////////////////////////////////////////////////////////////////////// |
| 368 // BrowserActionView | 470 // BrowserActionView |
| 369 | 471 |
| 370 BrowserActionView::BrowserActionView(const Extension* extension, | 472 BrowserActionView::BrowserActionView(const Extension* extension, |
| 371 BrowserActionsContainer* panel) | 473 BrowserActionsContainer* panel) |
| 372 : panel_(panel) { | 474 : panel_(panel) { |
| 373 button_ = new BrowserActionButton(extension, panel); | 475 button_ = new BrowserActionButton(extension, panel); |
| 374 button_->set_drag_controller(panel_); | 476 button_->set_drag_controller(panel_); |
| 375 AddChildView(button_); | 477 AddChildView(button_); |
| 376 button_->UpdateState(); | 478 button_->UpdateState(); |
| 377 } | 479 } |
| 378 | 480 |
| 379 BrowserActionView::~BrowserActionView() { | 481 BrowserActionView::~BrowserActionView() { |
| 380 RemoveChildView(button_); | 482 RemoveChildView(button_); |
| 381 button_->Destroy(); | 483 button_->Destroy(); |
| 382 } | 484 } |
| 383 | 485 |
| 384 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 486 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
| 385 int tab_id = panel_->GetCurrentTabId(); | 487 int tab_id = panel_->GetCurrentTabId(); |
| 386 | 488 |
| 387 SkBitmap icon = *button_->extension()->browser_action()->GetIcon( | 489 const ExtensionAction* action = button_->extension()->browser_action(); |
| 388 tab_id).ToSkBitmap(); | |
| 389 | 490 |
| 390 // Dim the icon if our button is disabled. | 491 gfx::ImageSkia icon = action->GetIcon(tab_id); |
| 391 if (!button_->IsEnabled(tab_id)) | |
| 392 icon = MakeTransparent(icon); | |
| 393 | 492 |
| 394 gfx::Canvas* canvas = | 493 if (tab_id < 0) |
| 395 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | 494 return icon; |
| 396 | 495 |
| 397 if (tab_id >= 0) { | 496 return gfx::ImageSkia( |
| 398 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | 497 new ActionBadgeImageSource(action->GetIcon(tab_id), |
| 399 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | 498 button_->IsEnabled(tab_id), |
| 400 } | 499 action->GetBadgeText(tab_id), |
| 401 | 500 action->GetBadgeTextColor(tab_id), |
| 402 return canvas; | 501 action->GetBadgeBackgroundColor(tab_id)), |
| 502 icon.size()); | |
| 403 } | 503 } |
| 404 | 504 |
| 405 void BrowserActionView::Layout() { | 505 void BrowserActionView::Layout() { |
| 406 // We can't rely on button_->GetPreferredSize() here because that's not set | 506 // We can't rely on button_->GetPreferredSize() here because that's not set |
| 407 // correctly until the first call to | 507 // correctly until the first call to |
| 408 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 508 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
| 409 // called before that when the initial bounds are set (and then not after, | 509 // called before that when the initial bounds are set (and then not after, |
| 410 // since the bounds don't change). So instead of setting the height from the | 510 // since the bounds don't change). So instead of setting the height from the |
| 411 // button's preferred size, we use IconHeight(), since that's how big the | 511 // button's preferred size, we use IconHeight(), since that's how big the |
| 412 // button should be regardless of what it's displaying. | 512 // button should be regardless of what it's displaying. |
| 413 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), | 513 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), |
| 414 BrowserActionsContainer::IconHeight()); | 514 BrowserActionsContainer::IconHeight()); |
| 415 } | 515 } |
| 416 | 516 |
| 417 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | 517 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 418 state->name = l10n_util::GetStringUTF16( | 518 state->name = l10n_util::GetStringUTF16( |
| 419 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | 519 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); |
| 420 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 520 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
| 421 } | 521 } |
| 422 | 522 |
| 423 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 523 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
| 424 View::PaintChildren(canvas); | 524 View::PaintChildren(canvas); |
| 425 ExtensionAction* action = button()->browser_action(); | 525 ExtensionAction* action = button()->browser_action(); |
| 426 int tab_id = panel_->GetCurrentTabId(); | 526 int tab_id = panel_->GetCurrentTabId(); |
| 427 if (tab_id >= 0) | 527 if (tab_id >= 0) |
| 428 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 528 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
| 429 } | 529 } |
| OLD | NEW |