Index: chrome/browser/ui/views/browser_action_view.cc |
diff --git a/chrome/browser/ui/views/browser_action_view.cc b/chrome/browser/ui/views/browser_action_view.cc |
index dd2eeb776233b68c577f19c503deb54bac4d821f..ce54c0f34b8c0f75bfea813d304b82e14dc6de78 100644 |
--- a/chrome/browser/ui/views/browser_action_view.cc |
+++ b/chrome/browser/ui/views/browser_action_view.cc |
@@ -20,7 +20,10 @@ |
#include "ui/base/l10n/l10n_util.h" |
#include "ui/base/resource/resource_bundle.h" |
#include "ui/gfx/canvas.h" |
-#include "ui/gfx/skbitmap_operations.h" |
+#include "ui/gfx/image/canvas_image_source.h" |
+#include "ui/gfx/image/image_skia.h" |
+#include "ui/gfx/image/image_skia_operations.h" |
+#include "ui/gfx/image/image_skia_source.h" |
#include "ui/views/controls/menu/menu_model_adapter.h" |
#include "ui/views/controls/menu/menu_runner.h" |
@@ -28,16 +31,130 @@ using extensions::Extension; |
namespace { |
-// Return a more transparent |image|, with 25% of its original opacity. |
-SkBitmap MakeTransparent(const SkBitmap& image) { |
- SkBitmap alpha; |
- alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); |
- alpha.allocPixels(); |
- alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); |
+// CanvasImageSource for creating an browser action icon image with the provided |
+// background. |
+class IconWithBackgroundImageSource : public gfx::CanvasImageSource { |
+ public: |
+ IconWithBackgroundImageSource(const gfx::ImageSkia& icon, |
+ const gfx::ImageSkia& background) |
+ : gfx::CanvasImageSource(background.size(), false), |
+ icon_(icon), |
+ background_(background) { |
+ } |
+ |
+ private: |
+ virtual ~IconWithBackgroundImageSource() {} |
+ |
+ virtual void Draw(gfx::Canvas* canvas) OVERRIDE { |
+ SkPaint paint; |
+ paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
+ |
+ // We center the icon on the background. |
+ canvas->DrawImageInt(icon_, |
+ (background_.width() - icon_.width()) / 2, |
+ (background_.height() - icon_.height()) / 2, |
+ paint); |
+ } |
+ |
+ virtual gfx::ImageSkiaRep GetImageForScale( |
+ ui::ScaleFactor scale_factor) OVERRIDE { |
+ gfx::Canvas canvas(background_.GetRepresentation(scale_factor), is_opaque_); |
+ Draw(&canvas); |
+ 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.
|
+ } |
+ |
+ gfx::ImageSkia icon_; |
+ gfx::ImageSkia background_; |
+}; |
+ |
+// ImageSource for creating an image to be used as alpha in CreateMaskedImage |
+// image skia operation while creating transparent image. |
+class TransparentAlphaImageSource : public gfx::ImageSkiaSource { |
+ public: |
+ TransparentAlphaImageSource(size_t width, size_t height) |
+ : width_(width), |
+ height_(height) { |
+ } |
+ |
pkotwicz
2012/08/07 22:31:22
Nit: destructor
tbarzic
2012/08/08 02:09:29
Done.
|
+ private: |
+ virtual ~TransparentAlphaImageSource() {} |
+ |
+ virtual gfx::ImageSkiaRep GetImageForScale( |
+ ui::ScaleFactor scale_factor) OVERRIDE { |
+ SkBitmap alpha; |
+ const float scale = ui::GetScaleFactorScale(scale_factor); |
+ alpha.setConfig(SkBitmap::kARGB_8888_Config, |
+ static_cast<int>(width_ * scale), |
+ static_cast<int>(height_ * scale)); |
+ alpha.allocPixels(); |
+ alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); |
+ return gfx::ImageSkiaRep(alpha, scale_factor); |
+ } |
- return SkBitmapOperations::CreateMaskedBitmap(image, alpha); |
+ // Icon width in DIP. |
+ size_t width_; |
+ // Icon height in DIP. |
+ size_t height_; |
+}; |
+ |
+// Return a more transparent |image|, with 25% of its original opacity. |
+gfx::ImageSkia MakeTransparent(const gfx::ImageSkia& image) { |
+ gfx::ImageSkia alpha( |
+ new TransparentAlphaImageSource(image.width(), image.height()), |
+ gfx::Size(image.width(), image.height())); |
+ return gfx::ImageSkiaOperations::CreateMaskedImage(image, alpha); |
} |
+// CanvasImageSource for creating browser action icon with a badge. |
+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.
|
+ public: |
+ ActionBadgeImageSource(gfx::ImageSkia icon, |
+ bool button_enabled, |
+ const std::string& text, |
+ const SkColor& text_color, |
+ const SkColor& background_color) |
+ : gfx::CanvasImageSource(icon.size(), false), |
+ icon_(icon), |
+ button_enabled_(button_enabled), |
+ text_(text), |
+ text_color_(text_color), |
+ background_color_(background_color) { |
+ } |
+ |
pkotwicz
2012/08/07 22:31:22
Nit: destructor
tbarzic
2012/08/08 02:09:29
Done.
|
+ private: |
+ virtual ~ActionBadgeImageSource() {} |
+ |
+ virtual void Draw(gfx::Canvas* canvas) OVERRIDE { |
+ // Draw a badge on the provided browser action icon's canvas. |
+ gfx::Rect bounds(size_.width(), |
+ size_.height() + ToolbarView::kVertSpacing); |
+ ExtensionAction::DoPaintBadge(canvas, bounds, text_, text_color_, |
+ background_color_, size_.width()); |
+ } |
+ |
+ virtual gfx::ImageSkiaRep GetImageForScale( |
+ ui::ScaleFactor scale_factor) OVERRIDE { |
+ gfx::ImageSkia icon = icon_; |
+ if (!button_enabled_) |
+ icon = MakeTransparent(icon); |
+ |
+ gfx::Canvas canvas(icon.GetRepresentation(scale_factor), is_opaque_); |
+ Draw(&canvas); |
+ return canvas.ExtractImageRep(); |
+ } |
+ |
+ // Browser action icon image. |
+ gfx::ImageSkia icon_; |
+ // Whether the browser actions button is enabled. |
+ bool button_enabled_; |
+ // Text to be displayed on the badge. |
+ std::string text_; |
+ // Color of badge text. |
+ SkColor text_color_; |
+ // Color of the badge. |
+ SkColor background_color_; |
+}; |
+ |
} // namespace |
//////////////////////////////////////////////////////////////////////////////// |
@@ -148,7 +265,8 @@ void BrowserActionButton::ShowContextMenuForView(View* source, |
void BrowserActionButton::OnImageLoaded(const gfx::Image& image, |
const std::string& extension_id, |
int index) { |
- browser_action_->CacheIcon(browser_action_->default_icon_path(), image); |
+ browser_action_->CacheIcon(browser_action_->default_icon_path(), |
+ *image.ToImageSkia()); |
// Call back to UpdateState() because a more specific icon might have been set |
// while the load was outstanding. |
@@ -168,39 +286,24 @@ void BrowserActionButton::UpdateState() { |
views::CustomButton::BS_NORMAL); |
} |
- SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); |
+ gfx::ImageSkia icon(browser_action()->GetIcon(tab_id)); |
+ |
if (!icon.isNull()) { |
if (!browser_action()->GetIsVisible(tab_id)) |
icon = MakeTransparent(icon); |
- SkPaint paint; |
- paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
- SkBitmap bg; |
- rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, |
- SkBitmap::kARGB_8888_Config); |
- SkCanvas bg_canvas(bg); |
- bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), |
- SkIntToScalar((bg.height() - icon.height()) / 2), &paint); |
- SetIcon(bg); |
- |
- SkBitmap bg_h; |
- rb.GetBitmapNamed(IDR_BROWSER_ACTION_H)->copyTo(&bg_h, |
- SkBitmap::kARGB_8888_Config); |
- SkCanvas bg_h_canvas(bg_h); |
- bg_h_canvas.drawBitmap(icon, |
- SkIntToScalar((bg_h.width() - icon.width()) / 2), |
- SkIntToScalar((bg_h.height() - icon.height()) / 2), &paint); |
- SetHoverIcon(bg_h); |
- |
- SkBitmap bg_p; |
- rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, |
- SkBitmap::kARGB_8888_Config); |
- SkCanvas bg_p_canvas(bg_p); |
- bg_p_canvas.drawBitmap(icon, |
- SkIntToScalar((bg_p.width() - icon.width()) / 2), |
- SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); |
- SetPushedIcon(bg_p); |
+ gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
+ SetIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg), |
+ bg.size())); |
+ |
+ gfx::ImageSkia bg_h = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
+ SetHoverIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg_h), |
+ bg_h.size())); |
+ |
+ gfx::ImageSkia bg_p = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
+ SetPushedIcon(gfx::ImageSkia(new IconWithBackgroundImageSource(icon, bg_p), |
+ bg_p.size())); |
} |
// If the browser action name is empty, show the extension name instead. |
@@ -363,7 +466,6 @@ void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { |
} |
} |
- |
//////////////////////////////////////////////////////////////////////////////// |
// BrowserActionView |
@@ -381,25 +483,23 @@ BrowserActionView::~BrowserActionView() { |
button_->Destroy(); |
} |
-gfx::Canvas* BrowserActionView::GetIconWithBadge() { |
+gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
int tab_id = panel_->GetCurrentTabId(); |
- SkBitmap icon = *button_->extension()->browser_action()->GetIcon( |
- tab_id).ToSkBitmap(); |
+ const ExtensionAction* action = button_->extension()->browser_action(); |
- // Dim the icon if our button is disabled. |
- if (!button_->IsEnabled(tab_id)) |
- icon = MakeTransparent(icon); |
+ gfx::ImageSkia icon = action->GetIcon(tab_id); |
- gfx::Canvas* canvas = |
- new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); |
- |
- if (tab_id >= 0) { |
- gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); |
- button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); |
- } |
- |
- return canvas; |
+ if (tab_id < 0) |
+ return icon; |
+ |
+ return gfx::ImageSkia( |
+ new ActionBadgeImageSource(action->GetIcon(tab_id), |
+ button_->IsEnabled(tab_id), |
+ action->GetBadgeText(tab_id), |
+ action->GetBadgeTextColor(tab_id), |
+ action->GetBadgeBackgroundColor(tab_id)), |
+ icon.size()); |
} |
void BrowserActionView::Layout() { |