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 for creating an image to be used as alpha in CreateMaskedImage | |
33 // image skia operation while creating transparent image. | |
pkotwicz
2012/08/08 15:58:50
Nit: How about "ImageSource which creates a partia
tbarzic
2012/08/08 18:53:51
Done.
| |
34 class TransparencyMaskImageSource : public gfx::ImageSkiaSource { | |
35 public: | |
36 explicit TransparencyMaskImageSource(const gfx::Size& size) | |
37 : width_(size.width()), | |
38 height_(size.height()) { | |
39 } | |
40 | |
41 virtual ~TransparencyMaskImageSource() {} | |
42 | |
43 private: | |
44 virtual gfx::ImageSkiaRep GetImageForScale( | |
45 ui::ScaleFactor scale_factor) OVERRIDE { | |
46 SkBitmap alpha; | |
47 const float scale = ui::GetScaleFactorScale(scale_factor); | |
48 alpha.setConfig(SkBitmap::kARGB_8888_Config, | |
49 static_cast<int>(width_ * scale), | |
50 static_cast<int>(height_ * scale)); | |
51 alpha.allocPixels(); | |
52 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
53 return gfx::ImageSkiaRep(alpha, scale_factor); | |
54 } | |
55 | |
56 // Icon width in DIP. | |
57 size_t width_; | |
sky
2012/08/08 16:55:45
gfx::Size uses int, not size_t. Also, make these c
tbarzic
2012/08/08 18:53:51
Done.
| |
58 // Icon height in DIP. | |
59 size_t height_; | |
60 | |
61 DISALLOW_COPY_AND_ASSIGN(TransparencyMaskImageSource); | |
62 }; | |
63 | |
31 // Return a more transparent |image|, with 25% of its original opacity. | 64 // Return a more transparent |image|, with 25% of its original opacity. |
32 SkBitmap MakeTransparent(const SkBitmap& image) { | 65 gfx::ImageSkia MakeTransparent(const gfx::ImageSkia& image) { |
33 SkBitmap alpha; | 66 gfx::ImageSkia alpha(new TransparencyMaskImageSource(image.size()), |
34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | 67 image.size()); |
35 alpha.allocPixels(); | 68 return gfx::ImageSkiaOperations::CreateMaskedImage(image, alpha); |
36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); | |
37 | |
38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | |
39 } | 69 } |
40 | 70 |
41 } // namespace | 71 } // namespace |
42 | 72 |
43 //////////////////////////////////////////////////////////////////////////////// | 73 //////////////////////////////////////////////////////////////////////////////// |
44 // BrowserActionButton | 74 // BrowserActionButton |
45 | 75 |
46 BrowserActionButton::BrowserActionButton(const Extension* extension, | 76 BrowserActionButton::BrowserActionButton(const Extension* extension, |
47 BrowserActionsContainer* panel) | 77 BrowserActionsContainer* panel) |
48 : ALLOW_THIS_IN_INITIALIZER_LIST( | 78 : ALLOW_THIS_IN_INITIALIZER_LIST( |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
141 views::MenuRunner::MENU_DELETED) | 171 views::MenuRunner::MENU_DELETED) |
142 return; | 172 return; |
143 | 173 |
144 SetButtonNotPushed(); | 174 SetButtonNotPushed(); |
145 context_menu_ = NULL; | 175 context_menu_ = NULL; |
146 } | 176 } |
147 | 177 |
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, | 178 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, |
149 const std::string& extension_id, | 179 const std::string& extension_id, |
150 int index) { | 180 int index) { |
151 browser_action_->CacheIcon(browser_action_->default_icon_path(), image); | 181 browser_action_->CacheIcon(browser_action_->default_icon_path(), |
182 *image.ToImageSkia()); | |
152 | 183 |
153 // Call back to UpdateState() because a more specific icon might have been set | 184 // Call back to UpdateState() because a more specific icon might have been set |
154 // while the load was outstanding. | 185 // while the load was outstanding. |
155 UpdateState(); | 186 UpdateState(); |
156 } | 187 } |
157 | 188 |
158 void BrowserActionButton::UpdateState() { | 189 void BrowserActionButton::UpdateState() { |
159 int tab_id = panel_->GetCurrentTabId(); | 190 int tab_id = panel_->GetCurrentTabId(); |
160 if (tab_id < 0) | 191 if (tab_id < 0) |
161 return; | 192 return; |
162 | 193 |
163 if (!IsEnabled(tab_id)) { | 194 if (!IsEnabled(tab_id)) { |
164 SetState(views::CustomButton::BS_DISABLED); | 195 SetState(views::CustomButton::BS_DISABLED); |
165 } else { | 196 } else { |
166 SetState(menu_visible_ ? | 197 SetState(menu_visible_ ? |
167 views::CustomButton::BS_PUSHED : | 198 views::CustomButton::BS_PUSHED : |
168 views::CustomButton::BS_NORMAL); | 199 views::CustomButton::BS_NORMAL); |
169 } | 200 } |
170 | 201 |
171 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); | 202 gfx::ImageSkia icon(browser_action()->GetIcon(tab_id)); |
203 | |
172 if (!icon.isNull()) { | 204 if (!icon.isNull()) { |
173 if (!browser_action()->GetIsVisible(tab_id)) | 205 if (!browser_action()->GetIsVisible(tab_id)) |
174 icon = MakeTransparent(icon); | 206 icon = MakeTransparent(icon); |
175 SkPaint paint; | 207 |
176 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | |
177 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 208 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
178 | 209 |
179 SkBitmap bg; | 210 gfx::ImageSkia bg = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION); |
180 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 211 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 | 212 |
187 SkBitmap bg_h; | 213 gfx::ImageSkia bg_h = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
188 rb.GetBitmapNamed(IDR_BROWSER_ACTION_H)->copyTo(&bg_h, | 214 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 | 215 |
196 SkBitmap bg_p; | 216 gfx::ImageSkia bg_p = *rb.GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
197 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, | 217 SetPushedIcon( |
198 SkBitmap::kARGB_8888_Config); | 218 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 } | 219 } |
205 | 220 |
206 // If the browser action name is empty, show the extension name instead. | 221 // If the browser action name is empty, show the extension name instead. |
207 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 222 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
208 if (name.empty()) | 223 if (name.empty()) |
209 name = UTF8ToUTF16(extension()->name()); | 224 name = UTF8ToUTF16(extension()->name()); |
210 SetTooltipText(name); | 225 SetTooltipText(name); |
211 SetAccessibleName(name); | 226 SetAccessibleName(name); |
212 | 227 |
213 parent()->SchedulePaint(); | 228 parent()->SchedulePaint(); |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
356 extensions::Command browser_action_command; | 371 extensions::Command browser_action_command; |
357 if (!only_if_active || !command_service->GetBrowserActionCommand( | 372 if (!only_if_active || !command_service->GetBrowserActionCommand( |
358 extension_->id(), | 373 extension_->id(), |
359 extensions::CommandService::ACTIVE_ONLY, | 374 extensions::CommandService::ACTIVE_ONLY, |
360 &browser_action_command, | 375 &browser_action_command, |
361 NULL)) { | 376 NULL)) { |
362 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 377 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
363 } | 378 } |
364 } | 379 } |
365 | 380 |
366 | |
367 //////////////////////////////////////////////////////////////////////////////// | 381 //////////////////////////////////////////////////////////////////////////////// |
368 // BrowserActionView | 382 // BrowserActionView |
369 | 383 |
370 BrowserActionView::BrowserActionView(const Extension* extension, | 384 BrowserActionView::BrowserActionView(const Extension* extension, |
371 BrowserActionsContainer* panel) | 385 BrowserActionsContainer* panel) |
372 : panel_(panel) { | 386 : panel_(panel) { |
373 button_ = new BrowserActionButton(extension, panel); | 387 button_ = new BrowserActionButton(extension, panel); |
374 button_->set_drag_controller(panel_); | 388 button_->set_drag_controller(panel_); |
375 AddChildView(button_); | 389 AddChildView(button_); |
376 button_->UpdateState(); | 390 button_->UpdateState(); |
377 } | 391 } |
378 | 392 |
379 BrowserActionView::~BrowserActionView() { | 393 BrowserActionView::~BrowserActionView() { |
380 RemoveChildView(button_); | 394 RemoveChildView(button_); |
381 button_->Destroy(); | 395 button_->Destroy(); |
382 } | 396 } |
383 | 397 |
384 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 398 gfx::ImageSkia BrowserActionView::GetIconWithBadge() { |
385 int tab_id = panel_->GetCurrentTabId(); | 399 int tab_id = panel_->GetCurrentTabId(); |
386 | 400 |
387 SkBitmap icon = *button_->extension()->browser_action()->GetIcon( | 401 const ExtensionAction* action = button_->extension()->browser_action(); |
388 tab_id).ToSkBitmap(); | 402 gfx::Size spacing(0, ToolbarView::kVertSpacing); |
389 | 403 gfx::ImageSkia icon = action->GetIcon(tab_id); |
390 // Dim the icon if our button is disabled. | |
391 if (!button_->IsEnabled(tab_id)) | 404 if (!button_->IsEnabled(tab_id)) |
392 icon = MakeTransparent(icon); | 405 icon = MakeTransparent(icon); |
393 | 406 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 } | 407 } |
404 | 408 |
405 void BrowserActionView::Layout() { | 409 void BrowserActionView::Layout() { |
406 // We can't rely on button_->GetPreferredSize() here because that's not set | 410 // We can't rely on button_->GetPreferredSize() here because that's not set |
407 // correctly until the first call to | 411 // correctly until the first call to |
408 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be | 412 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be |
409 // called before that when the initial bounds are set (and then not after, | 413 // 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 | 414 // 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 | 415 // button's preferred size, we use IconHeight(), since that's how big the |
412 // button should be regardless of what it's displaying. | 416 // button should be regardless of what it's displaying. |
413 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), | 417 button_->SetBounds(0, ToolbarView::kVertSpacing, width(), |
414 BrowserActionsContainer::IconHeight()); | 418 BrowserActionsContainer::IconHeight()); |
415 } | 419 } |
416 | 420 |
417 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { | 421 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) { |
418 state->name = l10n_util::GetStringUTF16( | 422 state->name = l10n_util::GetStringUTF16( |
419 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); | 423 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION); |
420 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 424 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
421 } | 425 } |
422 | 426 |
423 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 427 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
424 View::PaintChildren(canvas); | 428 View::PaintChildren(canvas); |
425 ExtensionAction* action = button()->browser_action(); | 429 ExtensionAction* action = button()->browser_action(); |
426 int tab_id = panel_->GetCurrentTabId(); | 430 int tab_id = panel_->GetCurrentTabId(); |
427 if (tab_id >= 0) | 431 if (tab_id >= 0) |
428 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 432 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
429 } | 433 } |
OLD | NEW |