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 "grit/theme_resources_standard.h" | 19 #include "grit/theme_resources_standard.h" |
20 #include "ui/base/accessibility/accessible_view_state.h" | 20 #include "ui/base/accessibility/accessible_view_state.h" |
21 #include "ui/base/l10n/l10n_util.h" | 21 #include "ui/base/l10n/l10n_util.h" |
22 #include "ui/base/resource/resource_bundle.h" | 22 #include "ui/base/resource/resource_bundle.h" |
23 #include "ui/gfx/canvas.h" | 23 #include "ui/gfx/canvas.h" |
24 #include "ui/gfx/skbitmap_operations.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 |
30 namespace { | |
31 | |
32 SkBitmap TransparentizeImage(const SkBitmap& image) { | |
33 // Same parameters as for disabled extension icons. | |
34 // 40% lightness matches Mac. | |
35 SkBitmap alpha; | |
36 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); | |
37 alpha.allocPixels(); | |
38 alpha.eraseColor(0x40000000); | |
not at google - send to devlin
2012/07/12 01:07:29
0x40 isn't 40% alpha, it's 25% alpha? Or am I goin
Yoyo Zhou
2012/07/12 01:33:27
Yup, 25% alpha. I was trying to find some good SkC
| |
39 | |
40 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); | |
41 } | |
42 | |
43 } // namespace | |
44 | |
29 //////////////////////////////////////////////////////////////////////////////// | 45 //////////////////////////////////////////////////////////////////////////////// |
30 // BrowserActionButton | 46 // BrowserActionButton |
31 | 47 |
32 BrowserActionButton::BrowserActionButton(const Extension* extension, | 48 BrowserActionButton::BrowserActionButton(const Extension* extension, |
33 BrowserActionsContainer* panel) | 49 BrowserActionsContainer* panel) |
34 : ALLOW_THIS_IN_INITIALIZER_LIST( | 50 : ALLOW_THIS_IN_INITIALIZER_LIST( |
35 MenuButton(this, string16(), NULL, false)), | 51 MenuButton(this, string16(), NULL, false)), |
36 browser_action_(extension->browser_action()), | 52 browser_action_(extension->browser_action()), |
37 extension_(extension), | 53 extension_(extension), |
38 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), | 54 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
114 // Call back to UpdateState() because a more specific icon might have been set | 130 // Call back to UpdateState() because a more specific icon might have been set |
115 // while the load was outstanding. | 131 // while the load was outstanding. |
116 UpdateState(); | 132 UpdateState(); |
117 } | 133 } |
118 | 134 |
119 void BrowserActionButton::UpdateState() { | 135 void BrowserActionButton::UpdateState() { |
120 int tab_id = panel_->GetCurrentTabId(); | 136 int tab_id = panel_->GetCurrentTabId(); |
121 if (tab_id < 0) | 137 if (tab_id < 0) |
122 return; | 138 return; |
123 | 139 |
140 SetState(IsEnabled(tab_id) ? | |
141 views::CustomButton::BS_NORMAL : | |
not at google - send to devlin
2012/07/12 01:07:29
This should be PUSHED If it's pushed; can use the
Yoyo Zhou
2012/07/12 01:33:27
Done.
| |
142 views::CustomButton::BS_DISABLED); | |
143 | |
124 SkBitmap icon(browser_action()->GetIcon(tab_id)); | 144 SkBitmap icon(browser_action()->GetIcon(tab_id)); |
125 if (icon.isNull()) | 145 if (icon.isNull()) |
126 icon = default_icon_; | 146 icon = default_icon_; |
127 if (!icon.isNull()) { | 147 if (!icon.isNull()) { |
148 // if (!enabled()) | |
149 if (!browser_action()->GetIsVisible(tab_id)) | |
150 icon = TransparentizeImage(icon); | |
128 SkPaint paint; | 151 SkPaint paint; |
129 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); | 152 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
130 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); | 153 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); |
131 | 154 |
132 SkBitmap bg; | 155 SkBitmap bg; |
133 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, | 156 rb.GetBitmapNamed(IDR_BROWSER_ACTION)->copyTo(&bg, |
134 SkBitmap::kARGB_8888_Config); | 157 SkBitmap::kARGB_8888_Config); |
135 SkCanvas bg_canvas(bg); | 158 SkCanvas bg_canvas(bg); |
136 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), | 159 bg_canvas.drawBitmap(icon, SkIntToScalar((bg.width() - icon.width()) / 2), |
137 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); | 160 SkIntToScalar((bg.height() - icon.height()) / 2), &paint); |
(...skipping 17 matching lines...) Expand all Loading... | |
155 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); | 178 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); |
156 SetPushedIcon(bg_p); | 179 SetPushedIcon(bg_p); |
157 } | 180 } |
158 | 181 |
159 // If the browser action name is empty, show the extension name instead. | 182 // If the browser action name is empty, show the extension name instead. |
160 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); | 183 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); |
161 if (name.empty()) | 184 if (name.empty()) |
162 name = UTF8ToUTF16(extension()->name()); | 185 name = UTF8ToUTF16(extension()->name()); |
163 SetTooltipText(name); | 186 SetTooltipText(name); |
164 SetAccessibleName(name); | 187 SetAccessibleName(name); |
188 | |
165 parent()->SchedulePaint(); | 189 parent()->SchedulePaint(); |
166 } | 190 } |
167 | 191 |
168 bool BrowserActionButton::IsPopup() { | 192 bool BrowserActionButton::IsPopup() { |
169 int tab_id = panel_->GetCurrentTabId(); | 193 int tab_id = panel_->GetCurrentTabId(); |
170 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); | 194 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); |
171 } | 195 } |
172 | 196 |
173 GURL BrowserActionButton::GetPopupUrl() { | 197 GURL BrowserActionButton::GetPopupUrl() { |
174 int tab_id = panel_->GetCurrentTabId(); | 198 int tab_id = panel_->GetCurrentTabId(); |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
291 void BrowserActionButton::SetButtonPushed() { | 315 void BrowserActionButton::SetButtonPushed() { |
292 SetState(views::CustomButton::BS_PUSHED); | 316 SetState(views::CustomButton::BS_PUSHED); |
293 menu_visible_ = true; | 317 menu_visible_ = true; |
294 } | 318 } |
295 | 319 |
296 void BrowserActionButton::SetButtonNotPushed() { | 320 void BrowserActionButton::SetButtonNotPushed() { |
297 SetState(views::CustomButton::BS_NORMAL); | 321 SetState(views::CustomButton::BS_NORMAL); |
298 menu_visible_ = false; | 322 menu_visible_ = false; |
299 } | 323 } |
300 | 324 |
325 bool BrowserActionButton::IsEnabled(int tab_id) const { | |
326 return browser_action_->GetIsVisible(tab_id); | |
327 } | |
328 | |
301 BrowserActionButton::~BrowserActionButton() { | 329 BrowserActionButton::~BrowserActionButton() { |
302 } | 330 } |
303 | 331 |
304 void BrowserActionButton::MaybeRegisterExtensionCommand() { | 332 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
305 extensions::CommandService* command_service = | 333 extensions::CommandService* command_service = |
306 extensions::CommandServiceFactory::GetForProfile( | 334 extensions::CommandServiceFactory::GetForProfile( |
307 panel_->browser()->profile()); | 335 panel_->browser()->profile()); |
308 extensions::Command browser_action_command; | 336 extensions::Command browser_action_command; |
309 if (command_service->GetBrowserActionCommand( | 337 if (command_service->GetBrowserActionCommand( |
310 extension_->id(), | 338 extension_->id(), |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
354 button_->Destroy(); | 382 button_->Destroy(); |
355 } | 383 } |
356 | 384 |
357 gfx::Canvas* BrowserActionView::GetIconWithBadge() { | 385 gfx::Canvas* BrowserActionView::GetIconWithBadge() { |
358 int tab_id = panel_->GetCurrentTabId(); | 386 int tab_id = panel_->GetCurrentTabId(); |
359 | 387 |
360 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); | 388 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id); |
361 if (icon.isNull()) | 389 if (icon.isNull()) |
362 icon = button_->default_icon(); | 390 icon = button_->default_icon(); |
363 | 391 |
392 // Gray out the icon if our button is disabled. | |
393 if (!button_->IsEnabled(tab_id)) | |
394 icon = TransparentizeImage(icon); | |
395 | |
364 gfx::Canvas* canvas = new gfx::Canvas(icon, false); | 396 gfx::Canvas* canvas = new gfx::Canvas(icon, false); |
365 | 397 |
366 if (tab_id >= 0) { | 398 if (tab_id >= 0) { |
367 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); | 399 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing); |
368 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); | 400 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id); |
369 } | 401 } |
370 | 402 |
371 return canvas; | 403 return canvas; |
372 } | 404 } |
373 | 405 |
(...skipping 15 matching lines...) Expand all Loading... | |
389 state->role = ui::AccessibilityTypes::ROLE_GROUPING; | 421 state->role = ui::AccessibilityTypes::ROLE_GROUPING; |
390 } | 422 } |
391 | 423 |
392 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { | 424 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) { |
393 View::PaintChildren(canvas); | 425 View::PaintChildren(canvas); |
394 ExtensionAction* action = button()->browser_action(); | 426 ExtensionAction* action = button()->browser_action(); |
395 int tab_id = panel_->GetCurrentTabId(); | 427 int tab_id = panel_->GetCurrentTabId(); |
396 if (tab_id >= 0) | 428 if (tab_id >= 0) |
397 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); | 429 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id); |
398 } | 430 } |
OLD | NEW |