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/views/controls/menu/menu_model_adapter.h" | 24 #include "ui/views/controls/menu/menu_model_adapter.h" |
24 #include "ui/views/controls/menu/menu_runner.h" | 25 #include "ui/views/controls/menu/menu_runner.h" |
25 | 26 |
26 using extensions::Extension; | 27 using extensions::Extension; |
27 | 28 |
29 namespace { | |
30 | |
31 SkBitmap TransparentizeImage(const SkBitmap& image) { | |
sky
2012/07/16 15:43:46
How about MakeTransparent and add a description.
Yoyo Zhou
2012/07/16 17:45:45
Done.
| |
32 // Same parameters as for disabled extension icons. | |
33 // 40% lightness matches Mac. | |
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 | |
28 //////////////////////////////////////////////////////////////////////////////// | 44 //////////////////////////////////////////////////////////////////////////////// |
29 // BrowserActionButton | 45 // BrowserActionButton |
30 | 46 |
31 BrowserActionButton::BrowserActionButton(const Extension* extension, | 47 BrowserActionButton::BrowserActionButton(const Extension* extension, |
32 BrowserActionsContainer* panel) | 48 BrowserActionsContainer* panel) |
33 : ALLOW_THIS_IN_INITIALIZER_LIST( | 49 : ALLOW_THIS_IN_INITIALIZER_LIST( |
34 MenuButton(this, string16(), NULL, false)), | 50 MenuButton(this, string16(), NULL, false)), |
35 browser_action_(extension->browser_action()), | 51 browser_action_(extension->browser_action()), |
36 extension_(extension), | 52 extension_(extension), |
37 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 53 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 // Call back to UpdateState() because a more specific icon might have been set | 129 // Call back to UpdateState() because a more specific icon might have been set |
114 // while the load was outstanding. | 130 // while the load was outstanding. |
115 UpdateState(); | 131 UpdateState(); |
116 } | 132 } |
117 | 133 |
118 void BrowserActionButton::UpdateState() { | 134 void BrowserActionButton::UpdateState() { |
119 int tab_id = panel_->GetCurrentTabId(); | 135 int tab_id = panel_->GetCurrentTabId(); |
120 if (tab_id < 0) | 136 if (tab_id < 0) |
121 return; | 137 return; |
122 | 138 |
139 if (!IsEnabled(tab_id)) { | |
140 SetState(views::CustomButton::BS_DISABLED); | |
141 } else { | |
142 SetState(menu_visible_ ? | |
143 views::CustomButton::BS_NORMAL : | |
144 views::CustomButton::BS_PUSHED); | |
145 } | |
146 | |
123 SkBitmap icon(browser_action()->GetIcon(tab_id)); | 147 SkBitmap icon(browser_action()->GetIcon(tab_id)); |
124 if (icon.isNull()) | 148 if (icon.isNull()) |
125 icon = default_icon_; | 149 icon = default_icon_; |
126 if (!icon.isNull()) { | 150 if (!icon.isNull()) { |
151 // if (!enabled()) | |
sky
2012/07/16 15:43:46
Remove commented out line.
Yoyo Zhou
2012/07/16 17:45:45
Done.
| |
152 if (!browser_action()->GetIsVisible(tab_id)) | |
153 icon = TransparentizeImage(icon); | |
127 SkPaint paint; | 154 SkPaint paint; |
128 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | 155 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 156 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
130 | 157 |
131 SkBitmap bg; | 158 SkBitmap bg; |
132 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 159 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, |
133 SkBitmap::kARGB_8888_Config); | 160 SkBitmap::kARGB_8888_Config); |
134 SkCanvas bg_canvas(bg); | 161 SkCanvas bg_canvas(bg); |
135 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), | 162 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), |
136 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); | 163 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); |
(...skipping 17 matching lines...) Expand all Loading... | |
154 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | 181 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); |
155 SetPushedIcon(bg_p); | 182 SetPushedIcon(bg_p); |
156 } | 183 } |
157 | 184 |
158 // If the browser action name is empty, show the extension name instead. | 185 // If the browser action name is empty, show the extension name instead. |
159 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 186 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
160 if (name.empty()) | 187 if (name.empty()) |
161 name = UTF8ToUTF16(extension()->name()); | 188 name = UTF8ToUTF16(extension()->name()); |
162 SetTooltipText(name); | 189 SetTooltipText(name); |
163 SetAccessibleName(name); | 190 SetAccessibleName(name); |
191 | |
164 parent()->SchedulePaint(); | 192 parent()->SchedulePaint(); |
165 } | 193 } |
166 | 194 |
167 bool BrowserActionButton::IsPopup() { | 195 bool BrowserActionButton::IsPopup() { |
168 int tab_id = panel_->GetCurrentTabId(); | 196 int tab_id = panel_->GetCurrentTabId(); |
169 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); | 197 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); |
170 } | 198 } |
171 | 199 |
172 GURL BrowserActionButton::GetPopupUrl() { | 200 GURL BrowserActionButton::GetPopupUrl() { |
173 int tab_id = panel_->GetCurrentTabId(); | 201 int tab_id = panel_->GetCurrentTabId(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
290 void BrowserActionButton::SetButtonPushed() { | 318 void BrowserActionButton::SetButtonPushed() { |
291 SetState(views::CustomButton::BS_PUSHED); | 319 SetState(views::CustomButton::BS_PUSHED); |
292 menu_visible_ = true; | 320 menu_visible_ = true; |
293 } | 321 } |
294 | 322 |
295 void BrowserActionButton::SetButtonNotPushed() { | 323 void BrowserActionButton::SetButtonNotPushed() { |
296 SetState(views::CustomButton::BS_NORMAL); | 324 SetState(views::CustomButton::BS_NORMAL); |
297 menu_visible_ = false; | 325 menu_visible_ = false; |
298 } | 326 } |
299 | 327 |
328 bool BrowserActionButton::IsEnabled(int tab_id) const { | |
329 return browser_action_->GetIsVisible(tab_id); | |
330 } | |
331 | |
300 BrowserActionButton::~BrowserActionButton() { | 332 BrowserActionButton::~BrowserActionButton() { |
301 } | 333 } |
302 | 334 |
303 void BrowserActionButton::MaybeRegisterExtensionCommand() { | 335 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
304 extensions::CommandService* command_service = | 336 extensions::CommandService* command_service = |
305 extensions::CommandServiceFactory::GetForProfile( | 337 extensions::CommandServiceFactory::GetForProfile( |
306 panel_->browser()->profile()); | 338 panel_->browser()->profile()); |
307 extensions::Command browser_action_command; | 339 extensions::Command browser_action_command; |
308 if (command_service->GetBrowserActionCommand( | 340 if (command_service->GetBrowserActionCommand( |
309 extension_->id(), | 341 extension_->id(), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
353 button_->Destroy(); | 385 button_->Destroy(); |
354 } | 386 } |
355 | 387 |
356 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 388 gfx::Canvas* BrowserActionView::GetIconWithBadge() { |
357 int tab_id = panel_->GetCurrentTabId(); | 389 int tab_id = panel_->GetCurrentTabId(); |
358 | 390 |
359 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); | 391 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); |
360 if (icon.isNull()) | 392 if (icon.isNull()) |
361 icon = button_->default_icon(); | 393 icon = button_->default_icon(); |
362 | 394 |
395 // Gray out the icon if our button is disabled. | |
396 if (!button_->IsEnabled(tab_id)) | |
397 icon = TransparentizeImage(icon); | |
398 | |
363 gfx::Canvas* canvas = | 399 gfx::Canvas* canvas = |
364 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); | 400 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false); |
365 | 401 |
366 if (tab_id >= 0) { | 402 if (tab_id >= 0) { |
367 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | 403 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); |
368 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | 404 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); |
369 } | 405 } |
370 | 406 |
371 return canvas; | 407 return canvas; |
372 } | 408 } |
(...skipping 16 matching lines...) Expand all Loading... | |
389 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 425 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
390 } | 426 } |
391 | 427 |
392 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 428 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
393 View::PaintChildren(canvas); | 429 View::PaintChildren(canvas); |
394 ExtensionAction* action = button()->browser_action(); | 430 ExtensionAction* action = button()->browser_action(); |
395 int tab_id = panel_->GetCurrentTabId(); | 431 int tab_id = panel_->GetCurrentTabId(); |
396 if (tab_id >= 0) | 432 if (tab_id >= 0) |
397 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 433 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
398 } | 434 } |
OLD | NEW |