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/image/image_skia.h" |
| 23 #include "ui/gfx/skbitmap_operations.h" | 23 #include "ui/gfx/image/image_skia_operations.h" |
| 24 #include "ui/gfx/image/image_skia_source.h" | |
| 24 #include "ui/views/controls/menu/menu_model_adapter.h" | 25 #include "ui/views/controls/menu/menu_model_adapter.h" |
| 25 #include "ui/views/controls/menu/menu_runner.h" | 26 #include "ui/views/controls/menu/menu_runner.h" |
| 26 | 27 |
| 27 using extensions::Extension; | 28 using extensions::Extension; |
| 28 | 29 |
| 29 namespace { | 30 namespace { |
| 30 | 31 |
| 32 // ImageSource which creates a partially transparent image mask. | |
| 33 class TransparencyMaskImageSource : public gfx::ImageSkiaSource { | |
| 34 public: | |
| 35 explicit TransparencyMaskImageSource(const gfx::Size& size) | |
| 36 : width_(size.width()), | |
| 37 height_(size.height()) { | |
| 38 } | |
| 39 | |
| 40 virtual ~TransparencyMaskImageSource() {} | |
| 41 | |
| 42 private: | |
| 43 virtual gfx::ImageSkiaRep GetImageForScale( | |
| 44 ui::ScaleFactor scale_factor) OVERRIDE { | |
| 45 SkBitmap alpha; | |
| 46 const float scale = ui::GetScaleFactorScale(scale_factor); | |
| 47 alpha.setConfig(SkBitmap::kARGB_8888_Config, | |
| 48 static_cast<int>(width_ * scale), | |
| 49 static_cast<int>(height_ * scale)); | |
| 50 alpha.allocPixels(); | |
| 51 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
| 52 return gfx::ImageSkiaRep(alpha, scale_factor); | |
| 53 } | |
| 54 | |
| 55 // Icon width in DIP. | |
| 56 const int width_; | |
| 57 // Icon height in DIP. | |
| 58 const int height_; | |
| 59 | |
| 60 DISALLOW_COPY_AND_ASSIGN(TransparencyMaskImageSource); | |
| 61 }; | |
| 62 | |
| 31 // Return a more transparent |image|, with 25% of its original opacity. | 63 // Return a more transparent |image|, with 25% of its original opacity. |
| 32 SkBitmap MakeTransparent(const SkBitmap& image) { | 64 gfx::ImageSkia MakeTransparent(const gfx::ImageSkia& image) { |
| 33 SkBitmap alpha; | 65 gfx::ImageSkia alpha(new TransparencyMaskImageSource(image.size()), |
| 34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | 66 image.size()); |
| 35 alpha.allocPixels(); | 67 return gfx::ImageSkiaOperations::CreateMaskedImage(image, alpha); |
|
Jeffrey Yasskin
2012/08/09 21:39:04
Why do a 2-step transformation instead of directly
tbarzic
2012/08/10 06:24:08
Done. moved to image_skia_operations
| |
| 36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
| 37 | |
| 38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | |
| 39 } | 68 } |
| 40 | 69 |
| 41 } // namespace | 70 } // namespace |
| 42 | 71 |
| 43 //////////////////////////////////////////////////////////////////////////////// | 72 //////////////////////////////////////////////////////////////////////////////// |
| 44 // BrowserActionButton | 73 // BrowserActionButton |
| 45 | 74 |
| 46 BrowserActionButton::BrowserActionButton(const Extension* extension, | 75 BrowserActionButton::BrowserActionButton(const Extension* extension, |
| 47 BrowserActionsContainer* panel) | 76 BrowserActionsContainer* panel) |
| 48 : ALLOW_THIS_IN_INITIALIZER_LIST( | 77 : ALLOW_THIS_IN_INITIALIZER_LIST( |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 161 return; | 190 return; |
| 162 | 191 |
| 163 if (!IsEnabled(tab_id)) { | 192 if (!IsEnabled(tab_id)) { |
| 164 SetState(views::CustomButton::BS_DISABLED); | 193 SetState(views::CustomButton::BS_DISABLED); |
| 165 } else { | 194 } else { |
| 166 SetState(menu_visible_ ? | 195 SetState(menu_visible_ ? |
| 167 views::CustomButton::BS_PUSHED : | 196 views::CustomButton::BS_PUSHED : |
| 168 views::CustomButton::BS_NORMAL); | 197 views::CustomButton::BS_NORMAL); |
| 169 } | 198 } |
| 170 | 199 |
| 171 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); | 200 gfx::ImageSkia icon = *browser_action()->GetIcon(tab_id).ToImageSkia(); |
| 201 | |
| 172 if (!icon.isNull()) { | 202 if (!icon.isNull()) { |
| 173 if (!browser_action()->GetIsVisible(tab_id)) | 203 if (!browser_action()->GetIsVisible(tab_id)) |
| 174 icon = MakeTransparent(icon); | 204 icon = MakeTransparent(icon); |
| 175 SkPaint paint; | 205 |
| 176 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | |
| 177 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 206 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
| 178 | 207 |
| 179 SkBitmap bg; | 208 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
| 180 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 209 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
| 181 SkBitmap::kARGB_8888_Config); | |
| 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 | 210 |
| 187 SkBitmap bg_h; | 211 gfx::ImageSkia bg_h = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
| 188 rb.GetBitmapNamed(IDR_BROWSER_ACTION_H)->copyTo(&bg_h, | 212 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h, icon)); |
| 189 SkBitmap::kARGB_8888_Config); | |
| 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 | 213 |
| 196 SkBitmap bg_p; | 214 gfx::ImageSkia bg_p = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
| 197 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, | 215 SetPushedIcon( |
| 198 SkBitmap::kARGB_8888_Config); | 216 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p, icon)); |
| 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 } | 217 } |
| 205 | 218 |
| 206 // If the browser action name is empty, show the extension name instead. | 219 // If the browser action name is empty, show the extension name instead. |
| 207 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 220 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
| 208 if (name.empty()) | 221 if (name.empty()) |
| 209 name = UTF8ToUTF16(extension()->name()); | 222 name = UTF8ToUTF16(extension()->name()); |
| 210 SetTooltipText(name); | 223 SetTooltipText(name); |
| 211 SetAccessibleName(name); | 224 SetAccessibleName(name); |
| 212 | 225 |
| 213 parent()->SchedulePaint(); | 226 parent()->SchedulePaint(); |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 356 extensions::Command browser_action_command; | 369 extensions::Command browser_action_command; |
| 357 if (!only_if_active || !command_service->GetBrowserActionCommand( | 370 if (!only_if_active || !command_service->GetBrowserActionCommand( |
| 358 extension_->id(), | 371 extension_->id(), |
| 359 extensions::CommandService::ACTIVE_ONLY, | 372 extensions::CommandService::ACTIVE_ONLY, |
| 360 &browser_action_command, | 373 &browser_action_command, |
| 361 NULL)) { | 374 NULL)) { |
| 362 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 375 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
| 363 } | 376 } |
| 364 } | 377 } |
| 365 | 378 |
| 366 | |
| 367 //////////////////////////////////////////////////////////////////////////////// | 379 //////////////////////////////////////////////////////////////////////////////// |
| 368 // BrowserActionView | 380 // BrowserActionView |
| 369 | 381 |
| 370 BrowserActionView::BrowserActionView(const Extension* extension, | 382 BrowserActionView::BrowserActionView(const Extension* extension, |
| 371 BrowserActionsContainer* panel) | 383 BrowserActionsContainer* panel) |
| 372 : panel_(panel) { | 384 : panel_(panel) { |
| 373 button_ = new BrowserActionButton(extension, panel); | 385 button_ = new BrowserActionButton(extension, panel); |
| 374 button_->set_drag_controller(panel_); | 386 button_->set_drag_controller(panel_); |
| 375 AddChildView(button_); | 387 AddChildView(button_); |
| 376 button_->UpdateState(); | 388 button_->UpdateState(); |
| 377 } | 389 } |
| 378 | 390 |
| 379 BrowserActionView::~BrowserActionView() { | 391 BrowserActionView::~BrowserActionView() { |
| 380 RemoveChildView(button_); | 392 RemoveChildView(button_); |
| 381 button_->Destroy(); | 393 button_->Destroy(); |
| 382 } | 394 } |
| 383 | 395 |
| 384 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 396 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
| 385 int tab_id = panel_->GetCurrentTabId(); | 397 int tab_id = panel_->GetCurrentTabId(); |
| 386 | 398 |
| 387 SkBitmap icon = *button_->extension()->browser_action()->GetIcon( | 399 const ExtensionAction* action = button_->extension()->browser_action(); |
| 388 tab_id).ToSkBitmap(); | 400 gfx::Size spacing(0, ToolbarView::kVertSpacing); |
| 389 | 401 gfx::ImageSkia icon = *action->GetIcon(tab_id).ToImageSkia(); |
| 390 // Dim the icon if our button is disabled. | |
| 391 if (!button_->IsEnabled(tab_id)) | 402 if (!button_->IsEnabled(tab_id)) |
| 392 icon = MakeTransparent(icon); | 403 icon = MakeTransparent(icon); |
| 393 | 404 return action->GetIconWithBadge(icon, tab_id, spacing); |
| 394 gfx::Canvas* canvas = | |
| 395 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | |
| 396 | |
| 397 if (tab_id >= 0) { | |
| 398 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | |
| 399 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | |
| 400 } | |
| 401 | |
| 402 return canvas; | |
| 403 } | 405 } |
| 404 | 406 |
| 405 void BrowserActionView::Layout() { | 407 void BrowserActionView::Layout() { |
| 406 // We can't rely on button_->GetPreferredSize() here because that's not set | 408 // We can't rely on button_->GetPreferredSize() here because that's not set |
| 407 // correctly until the first call to | 409 // correctly until the first call to |
| 408 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 410 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
| 409 // called before that when the initial bounds are set (and then not after, | 411 // 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 | 412 // 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 | 413 // button's preferred size, we use IconHeight(), since that's how big the |
| 412 // button should be regardless of what it's displaying. | 414 // button should be regardless of what it's displaying. |
| 413 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), | 415 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), |
| 414 BrowserActionsContainer::IconHeight()); | 416 BrowserActionsContainer::IconHeight()); |
| 415 } | 417 } |
| 416 | 418 |
| 417 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | 419 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { |
| 418 state->name = l10n_util::GetStringUTF16( | 420 state->name = l10n_util::GetStringUTF16( |
| 419 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | 421 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); |
| 420 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 422 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
| 421 } | 423 } |
| 422 | 424 |
| 423 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 425 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
| 424 View::PaintChildren(canvas); | 426 View::PaintChildren(canvas); |
| 425 ExtensionAction* action = button()->browser_action(); | 427 ExtensionAction* action = button()->browser_action(); |
| 426 int tab_id = panel_->GetCurrentTabId(); | 428 int tab_id = panel_->GetCurrentTabId(); |
| 427 if (tab_id >= 0) | 429 if (tab_id >= 0) |
| 428 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 430 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
| 429 } | 431 } |
| OLD | NEW |