Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(262)

Side by Side Diff: chrome/browser/ui/views/browser_action_view.cc

Issue 10533086: Action box menu (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Action box menu Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
(...skipping 23 matching lines...) Expand all
34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height()); 34 alpha.setConfig(SkBitmap::kARGB_8888_Config, image.width(), image.height());
35 alpha.allocPixels(); 35 alpha.allocPixels();
36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0)); 36 alpha.eraseColor(SkColorSetARGB(64, 0, 0, 0));
37 37
38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha); 38 return SkBitmapOperations::CreateMaskedBitmap(image, alpha);
39 } 39 }
40 40
41 } // namespace 41 } // namespace
42 42
43 //////////////////////////////////////////////////////////////////////////////// 43 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionView
45
46 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const {
47 return true;
48 }
49
50 bool BrowserActionView::Delegate::NeedToShowTooltip() const {
51 return true;
52 }
53
54 BrowserActionView::BrowserActionView(const Extension* extension,
55 Browser* browser,
56 BrowserActionView::Delegate* delegate)
57 : browser_(browser),
58 delegate_(delegate),
59 button_(NULL),
60 extension_(extension) {
61 }
62
63 BrowserActionView::~BrowserActionView() {
64 }
65
66 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
67 int tab_id = delegate_->GetCurrentTabId();
68
69 SkBitmap icon =
70 *button_->extension()->browser_action()->GetIcon(tab_id).ToSkBitmap();
71
72 // Dim the icon if our button is disabled.
73 if (!button_->IsEnabled(tab_id))
74 icon = MakeTransparent(icon);
75
76 gfx::Canvas* canvas =
77 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
78
79 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
80 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
81
82 return canvas;
83 }
84
85 void BrowserActionView::Layout() {
86 // We can't rely on button_->GetPreferredSize() here because that's not set
87 // correctly until the first call to
88 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
89 // called before that when the initial bounds are set (and then not after,
90 // since the bounds don't change). So instead of setting the height from the
91 // button's preferred size, we use IconHeight(), since that's how big the
92 // button should be regardless of what it's displaying.
93 gfx::Point offset = delegate_->GetViewContentOffset();
94 button_->SetBounds(offset.x(), offset.y(), width() - offset.x(),
95 BrowserActionsContainer::IconHeight());
96 }
97
98 void BrowserActionView::ViewHierarchyChanged(bool is_add,
99 View* parent,
100 View* child) {
101 if (is_add && (child == this)) {
102 button_ = new BrowserActionButton(extension_, browser_, delegate_);
103 button_->set_drag_controller(delegate_);
104
105 AddChildView(button_);
106 button_->UpdateState();
107 }
108 }
109
110 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
111 state->name = l10n_util::GetStringUTF16(
112 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
113 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
114 }
115
116 gfx::Size BrowserActionView::GetPreferredSize() {
117 return gfx::Size(BrowserActionsContainer::IconWidth(false),
118 BrowserActionsContainer::IconHeight());
119 }
120
121 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
122 View::PaintChildren(canvas);
123 ExtensionAction* action = button()->browser_action();
124 int tab_id = delegate_->GetCurrentTabId();
125 if (tab_id >= 0)
126 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
127 }
128
129 ////////////////////////////////////////////////////////////////////////////////
44 // BrowserActionButton 130 // BrowserActionButton
45 131
46 BrowserActionButton::BrowserActionButton(const Extension* extension, 132 BrowserActionButton::BrowserActionButton(const Extension* extension,
47 BrowserActionsContainer* panel) 133 Browser* browser,
134 BrowserActionView::Delegate* delegate)
48 : ALLOW_THIS_IN_INITIALIZER_LIST( 135 : ALLOW_THIS_IN_INITIALIZER_LIST(
49 MenuButton(this, string16(), NULL, false)), 136 MenuButton(this, string16(), NULL, false)),
137 browser_(browser),
50 browser_action_(extension->browser_action()), 138 browser_action_(extension->browser_action()),
51 extension_(extension), 139 extension_(extension),
52 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), 140 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
53 panel_(panel), 141 delegate_(delegate),
54 context_menu_(NULL) { 142 context_menu_(NULL) {
55 set_border(NULL); 143 set_border(NULL);
56 set_alignment(TextButton::ALIGN_CENTER); 144 set_alignment(TextButton::ALIGN_CENTER);
57 set_context_menu_controller(this); 145 set_context_menu_controller(this);
58 146
59 // No UpdateState() here because View hierarchy not setup yet. Our parent 147 // No UpdateState() here because View hierarchy not setup yet. Our parent
60 // should call UpdateState() after creation. 148 // should call UpdateState() after creation.
61 149
150 content::NotificationSource notification_source =
151 content::Source<Profile>(browser_->profile()->GetOriginalProfile());
62 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 152 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
63 content::Source<ExtensionAction>(browser_action_)); 153 content::Source<ExtensionAction>(browser_action_));
64 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 154 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
65 content::Source<Profile>( 155 notification_source);
66 panel_->profile()->GetOriginalProfile()));
67 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 156 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
68 content::Source<Profile>( 157 notification_source);
69 panel_->profile()->GetOriginalProfile()));
70 } 158 }
71 159
72 void BrowserActionButton::Destroy() { 160 void BrowserActionButton::Destroy() {
73 MaybeUnregisterExtensionCommand(false); 161 MaybeUnregisterExtensionCommand(false);
74 162
75 if (context_menu_) { 163 if (context_menu_) {
76 context_menu_->Cancel(); 164 context_menu_->Cancel();
77 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 165 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
78 } else { 166 } else {
79 delete this; 167 delete this;
(...skipping 30 matching lines...) Expand all
110 return true; 198 return true;
111 } 199 }
112 200
113 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState* state) { 201 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState* state) {
114 views::MenuButton::GetAccessibleState(state); 202 views::MenuButton::GetAccessibleState(state);
115 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON; 203 state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
116 } 204 }
117 205
118 void BrowserActionButton::ButtonPressed(views::Button* sender, 206 void BrowserActionButton::ButtonPressed(views::Button* sender,
119 const views::Event& event) { 207 const views::Event& event) {
120 panel_->OnBrowserActionExecuted(this); 208 delegate_->OnBrowserActionExecuted(this);
121 } 209 }
122 210
123 void BrowserActionButton::ShowContextMenuForView(View* source, 211 void BrowserActionButton::ShowContextMenuForView(View* source,
124 const gfx::Point& point) { 212 const gfx::Point& point) {
125 if (!extension()->ShowConfigureContextMenus()) 213 if (!extension()->ShowConfigureContextMenus())
126 return; 214 return;
127 215
128 SetButtonPushed(); 216 SetButtonPushed();
129 217
130 // Reconstructs the menu every time because the menu's contents are dynamic. 218 // Reconstructs the menu every time because the menu's contents are dynamic.
131 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( 219 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
132 new ExtensionContextMenuModel(extension(), panel_->browser())); 220 new ExtensionContextMenuModel(extension(), browser_));
133 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); 221 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
134 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); 222 menu_runner_.reset(new views::MenuRunner(menu_model_adapter.CreateMenu()));
135 223
136 context_menu_ = menu_runner.GetMenu(); 224 context_menu_ = menu_runner_->GetMenu();
137 gfx::Point screen_loc; 225 gfx::Point screen_loc;
138 views::View::ConvertPointToScreen(this, &screen_loc); 226 views::View::ConvertPointToScreen(this, &screen_loc);
139 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), 227 if (menu_runner_->RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
140 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == 228 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
141 views::MenuRunner::MENU_DELETED) 229 views::MenuRunner::MENU_DELETED) {
142 return; 230 return;
231 }
143 232
233 menu_runner_.reset();
144 SetButtonNotPushed(); 234 SetButtonNotPushed();
145 context_menu_ = NULL; 235 context_menu_ = NULL;
146 } 236 }
147 237
148 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, 238 void BrowserActionButton::OnImageLoaded(const gfx::Image& image,
149 const std::string& extension_id, 239 const std::string& extension_id,
150 int index) { 240 int index) {
151 browser_action_->CacheIcon(browser_action_->default_icon_path(), image); 241 browser_action_->CacheIcon(browser_action_->default_icon_path(), image);
152 242
153 // Call back to UpdateState() because a more specific icon might have been set 243 // Call back to UpdateState() because a more specific icon might have been set
154 // while the load was outstanding. 244 // while the load was outstanding.
155 UpdateState(); 245 UpdateState();
156 } 246 }
157 247
158 void BrowserActionButton::UpdateState() { 248 void BrowserActionButton::UpdateState() {
159 int tab_id = panel_->GetCurrentTabId(); 249 int tab_id = delegate_->GetCurrentTabId();
160 if (tab_id < 0) 250 if (tab_id < 0)
161 return; 251 return;
162 252
253 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates());
254
163 if (!IsEnabled(tab_id)) { 255 if (!IsEnabled(tab_id)) {
164 SetState(views::CustomButton::BS_DISABLED); 256 SetState(views::CustomButton::BS_DISABLED);
165 } else { 257 } else {
166 SetState(menu_visible_ ? 258 SetState(menu_visible_ ?
167 views::CustomButton::BS_PUSHED : 259 views::CustomButton::BS_PUSHED :
168 views::CustomButton::BS_NORMAL); 260 views::CustomButton::BS_NORMAL);
169 } 261 }
170 262
171 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap()); 263 SkBitmap icon(*browser_action()->GetIcon(tab_id).ToSkBitmap());
172 if (!icon.isNull()) { 264 if (!icon.isNull()) {
(...skipping 24 matching lines...) Expand all
197 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, 289 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p,
198 SkBitmap::kARGB_8888_Config); 290 SkBitmap::kARGB_8888_Config);
199 SkCanvas bg_p_canvas(bg_p); 291 SkCanvas bg_p_canvas(bg_p);
200 bg_p_canvas.drawBitmap(icon, 292 bg_p_canvas.drawBitmap(icon,
201 SkIntToScalar((bg_p.width() - icon.width()) / 2), 293 SkIntToScalar((bg_p.width() - icon.width()) / 2),
202 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); 294 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint);
203 SetPushedIcon(bg_p); 295 SetPushedIcon(bg_p);
204 } 296 }
205 297
206 // If the browser action name is empty, show the extension name instead. 298 // If the browser action name is empty, show the extension name instead.
207 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); 299 std::string title = browser_action()->GetTitle(tab_id);
208 if (name.empty()) 300 string16 name = UTF8ToUTF16(title.empty() ? extension()->name() : title);
209 name = UTF8ToUTF16(extension()->name()); 301 SetTooltipText(delegate_->NeedToShowTooltip() ? name : string16());
210 SetTooltipText(name);
211 SetAccessibleName(name); 302 SetAccessibleName(name);
212 303
213 parent()->SchedulePaint(); 304 parent()->SchedulePaint();
214 } 305 }
215 306
216 bool BrowserActionButton::IsPopup() { 307 bool BrowserActionButton::IsPopup() {
217 int tab_id = panel_->GetCurrentTabId(); 308 int tab_id = delegate_->GetCurrentTabId();
218 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 309 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id);
219 } 310 }
220 311
221 GURL BrowserActionButton::GetPopupUrl() { 312 GURL BrowserActionButton::GetPopupUrl() {
222 int tab_id = panel_->GetCurrentTabId(); 313 int tab_id = delegate_->GetCurrentTabId();
223 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 314 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id);
224 } 315 }
225 316
226 void BrowserActionButton::Observe(int type, 317 void BrowserActionButton::Observe(int type,
227 const content::NotificationSource& source, 318 const content::NotificationSource& source,
228 const content::NotificationDetails& details) { 319 const content::NotificationDetails& details) {
229 switch (type) { 320 switch (type) {
230 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 321 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED:
231 UpdateState(); 322 UpdateState();
232 // The browser action may have become visible/hidden so we need to make 323 // The browser action may have become visible/hidden so we need to make
233 // sure the state gets updated. 324 // sure the state gets updated.
234 panel_->OnBrowserActionVisibilityChanged(); 325 delegate_->OnBrowserActionVisibilityChanged();
235 break; 326 break;
236 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 327 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED:
237 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 328 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
238 std::pair<const std::string, const std::string>* payload = 329 std::pair<const std::string, const std::string>* payload =
239 content::Details<std::pair<const std::string, const std::string> >( 330 content::Details<std::pair<const std::string, const std::string> >(
240 details).ptr(); 331 details).ptr();
241 if (extension_->id() == payload->first && 332 if (extension_->id() == payload->first &&
242 payload->second == 333 payload->second ==
243 extension_manifest_values::kBrowserActionCommandEvent) { 334 extension_manifest_values::kBrowserActionCommandEvent) {
244 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 335 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED)
245 MaybeRegisterExtensionCommand(); 336 MaybeRegisterExtensionCommand();
246 else 337 else
247 MaybeUnregisterExtensionCommand(true); 338 MaybeUnregisterExtensionCommand(true);
248 } 339 }
249 break; 340 break;
250 } 341 }
251 default: 342 default:
252 NOTREACHED(); 343 NOTREACHED();
253 break; 344 break;
254 } 345 }
255 } 346 }
256 347
257 bool BrowserActionButton::Activate() { 348 bool BrowserActionButton::Activate() {
258 if (!IsPopup()) 349 if (!IsPopup())
259 return true; 350 return true;
260 351
261 panel_->OnBrowserActionExecuted(this); 352 delegate_->OnBrowserActionExecuted(this);
262 353
263 // TODO(erikkay): Run a nested modal loop while the mouse is down to 354 // TODO(erikkay): Run a nested modal loop while the mouse is down to
264 // enable menu-like drag-select behavior. 355 // enable menu-like drag-select behavior.
265 356
266 // The return value of this method is returned via OnMousePressed. 357 // The return value of this method is returned via OnMousePressed.
267 // We need to return false here since we're handing off focus to another 358 // We need to return false here since we're handing off focus to another
268 // widget/view, and true will grab it right back and try to send events 359 // widget/view, and true will grab it right back and try to send events
269 // to us. 360 // to us.
270 return false; 361 return false;
271 } 362 }
(...skipping 28 matching lines...) Expand all
300 TextButton::OnMouseExited(event); 391 TextButton::OnMouseExited(event);
301 } 392 }
302 393
303 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) { 394 bool BrowserActionButton::OnKeyReleased(const views::KeyEvent& event) {
304 return IsPopup() ? MenuButton::OnKeyReleased(event) 395 return IsPopup() ? MenuButton::OnKeyReleased(event)
305 : TextButton::OnKeyReleased(event); 396 : TextButton::OnKeyReleased(event);
306 } 397 }
307 398
308 bool BrowserActionButton::AcceleratorPressed( 399 bool BrowserActionButton::AcceleratorPressed(
309 const ui::Accelerator& accelerator) { 400 const ui::Accelerator& accelerator) {
310 panel_->OnBrowserActionExecuted(this); 401 delegate_->OnBrowserActionExecuted(this);
311 return true; 402 return true;
312 } 403 }
313 404
314 void BrowserActionButton::SetButtonPushed() { 405 void BrowserActionButton::SetButtonPushed() {
315 SetState(views::CustomButton::BS_PUSHED); 406 SetState(views::CustomButton::BS_PUSHED);
316 menu_visible_ = true; 407 menu_visible_ = true;
317 } 408 }
318 409
319 void BrowserActionButton::SetButtonNotPushed() { 410 void BrowserActionButton::SetButtonNotPushed() {
320 SetState(views::CustomButton::BS_NORMAL); 411 SetState(views::CustomButton::BS_NORMAL);
321 menu_visible_ = false; 412 menu_visible_ = false;
322 } 413 }
323 414
324 bool BrowserActionButton::IsEnabled(int tab_id) const { 415 bool BrowserActionButton::IsEnabled(int tab_id) const {
325 return browser_action_->GetIsVisible(tab_id); 416 return browser_action_->GetIsVisible(tab_id);
326 } 417 }
327 418
328 BrowserActionButton::~BrowserActionButton() { 419 BrowserActionButton::~BrowserActionButton() {
329 } 420 }
330 421
331 void BrowserActionButton::MaybeRegisterExtensionCommand() { 422 void BrowserActionButton::MaybeRegisterExtensionCommand() {
332 extensions::CommandService* command_service = 423 extensions::CommandService* command_service =
333 extensions::CommandServiceFactory::GetForProfile( 424 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
334 panel_->browser()->profile());
335 extensions::Command browser_action_command; 425 extensions::Command browser_action_command;
336 if (command_service->GetBrowserActionCommand( 426 if (command_service->GetBrowserActionCommand(
337 extension_->id(), 427 extension_->id(),
338 extensions::CommandService::ACTIVE_ONLY, 428 extensions::CommandService::ACTIVE_ONLY,
339 &browser_action_command, 429 &browser_action_command,
340 NULL)) { 430 NULL)) {
341 keybinding_.reset(new ui::Accelerator( 431 keybinding_.reset(new ui::Accelerator(
342 browser_action_command.accelerator())); 432 browser_action_command.accelerator()));
343 panel_->GetFocusManager()->RegisterAccelerator( 433 GetFocusManager()->RegisterAccelerator(
344 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 434 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this);
345 } 435 }
346 } 436 }
347 437
348 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 438 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) {
349 if (!keybinding_.get() || !panel_->GetFocusManager()) 439 if (!keybinding_.get() || !GetFocusManager())
350 return; 440 return;
351 441
352 extensions::CommandService* command_service = 442 extensions::CommandService* command_service =
353 extensions::CommandServiceFactory::GetForProfile( 443 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
354 panel_->browser()->profile());
355 444
356 extensions::Command browser_action_command; 445 extensions::Command browser_action_command;
357 if (!only_if_active || !command_service->GetBrowserActionCommand( 446 if (!only_if_active || !command_service->GetBrowserActionCommand(
358 extension_->id(), 447 extension_->id(),
359 extensions::CommandService::ACTIVE_ONLY, 448 extensions::CommandService::ACTIVE_ONLY,
360 &browser_action_command, 449 &browser_action_command,
361 NULL)) { 450 NULL)) {
362 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 451 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this);
363 } 452 }
364 } 453 }
365
366
367 ////////////////////////////////////////////////////////////////////////////////
368 // BrowserActionView
369
370 BrowserActionView::BrowserActionView(const Extension* extension,
371 BrowserActionsContainer* panel)
372 : panel_(panel) {
373 button_ = new BrowserActionButton(extension, panel);
374 button_->set_drag_controller(panel_);
375 AddChildView(button_);
376 button_->UpdateState();
377 }
378
379 BrowserActionView::~BrowserActionView() {
380 RemoveChildView(button_);
381 button_->Destroy();
382 }
383
384 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
385 int tab_id = panel_->GetCurrentTabId();
386
387 SkBitmap icon = *button_->extension()->browser_action()->GetIcon(
388 tab_id).ToSkBitmap();
389
390 // Dim the icon if our button is disabled.
391 if (!button_->IsEnabled(tab_id))
392 icon = MakeTransparent(icon);
393
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 }
404
405 void BrowserActionView::Layout() {
406 // We can't rely on button_->GetPreferredSize() here because that's not set
407 // correctly until the first call to
408 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
409 // 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
411 // button's preferred size, we use IconHeight(), since that's how big the
412 // button should be regardless of what it's displaying.
413 button_->SetBounds(0, ToolbarView::kVertSpacing, width(),
414 BrowserActionsContainer::IconHeight());
415 }
416
417 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
418 state->name = l10n_util::GetStringUTF16(
419 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
420 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
421 }
422
423 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
424 View::PaintChildren(canvas);
425 ExtensionAction* action = button()->browser_action();
426 int tab_id = panel_->GetCurrentTabId();
427 if (tab_id >= 0)
428 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
429 }
OLDNEW
« no previous file with comments | « chrome/browser/ui/views/browser_action_view.h ('k') | chrome/browser/ui/views/browser_actions_container.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698