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

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, 5 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"
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/browser_action_view.h"
13 #include "chrome/browser/ui/views/toolbar_view.h" 14 #include "chrome/browser/ui/views/toolbar_view.h"
14 #include "chrome/common/chrome_notification_types.h" 15 #include "chrome/common/chrome_notification_types.h"
15 #include "chrome/common/extensions/extension.h" 16 #include "chrome/common/extensions/extension.h"
16 #include "chrome/common/extensions/extension_manifest_constants.h" 17 #include "chrome/common/extensions/extension_manifest_constants.h"
17 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
18 #include "grit/theme_resources.h" 19 #include "grit/theme_resources.h"
19 #include "ui/base/accessibility/accessible_view_state.h" 20 #include "ui/base/accessibility/accessible_view_state.h"
20 #include "ui/base/l10n/l10n_util.h" 21 #include "ui/base/l10n/l10n_util.h"
21 #include "ui/base/resource/resource_bundle.h" 22 #include "ui/base/resource/resource_bundle.h"
22 #include "ui/gfx/canvas.h" 23 #include "ui/gfx/canvas.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
28 //////////////////////////////////////////////////////////////////////////////// 29 ////////////////////////////////////////////////////////////////////////////////
30 // BrowserActionView
31
32 BrowserActionView::BrowserActionView(const Extension* extension,
33 Browser* browser,
34 BrowserActionView::Delegate* delegate)
35 : browser_(browser),
36 delegate_(delegate) {
37 button_ = new BrowserActionButton(extension, browser_, delegate_);
38 button_->set_drag_controller(delegate_);
39 }
40
41 BrowserActionView::~BrowserActionView() {
42 }
43
44 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
45 int tab_id = delegate_->GetCurrentTabId();
46
47 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id);
48 if (icon.isNull())
49 icon = button_->default_icon();
50
51 gfx::Canvas* canvas =
52 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
53
54 if (tab_id >= 0) {
55 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
56 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
57 }
58
59 return canvas;
60 }
61
62 void BrowserActionView::Layout() {
63 // We can't rely on button_->GetPreferredSize() here because that's not set
64 // correctly until the first call to
65 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
66 // called before that when the initial bounds are set (and then not after,
67 // since the bounds don't change). So instead of setting the height from the
68 // button's preferred size, we use IconHeight(), since that's how big the
69 // button should be regardless of what it's displaying.
70 gfx::Size size = delegate_->GetViewContentOffset();
71 button_->SetBounds(size.width(), size.height(), width(),
72 BrowserActionsContainer::IconHeight());
73 }
74
75 void BrowserActionView::ViewHierarchyChanged(bool is_add,
76 View* parent,
77 View* child) {
78 if (is_add && (child == this)) {
79 AddChildView(button_);
80 button_->UpdateState();
81 }
82 }
83
84 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
85 state->name = l10n_util::GetStringUTF16(
86 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
87 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
88 }
89
90 gfx::Size BrowserActionView::GetPreferredSize() {
91 return gfx::Size(BrowserActionsContainer::IconWidth(false),
92 BrowserActionsContainer::IconHeight());
93 }
94
95 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
96 View::PaintChildren(canvas);
97 ExtensionAction* action = button()->browser_action();
98 int tab_id = delegate_->GetCurrentTabId();
99 if (tab_id >= 0)
100 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
101 }
102
103 ////////////////////////////////////////////////////////////////////////////////
29 // BrowserActionButton 104 // BrowserActionButton
30 105
31 BrowserActionButton::BrowserActionButton(const Extension* extension, 106 BrowserActionButton::BrowserActionButton(const Extension* extension,
32 BrowserActionsContainer* panel) 107 Browser* browser,
108 BrowserActionView::Delegate* delegate)
33 : ALLOW_THIS_IN_INITIALIZER_LIST( 109 : ALLOW_THIS_IN_INITIALIZER_LIST(
34 MenuButton(this, string16(), NULL, false)), 110 MenuButton(this, string16(), NULL, false)),
111 browser_(browser),
35 browser_action_(extension->browser_action()), 112 browser_action_(extension->browser_action()),
36 extension_(extension), 113 extension_(extension),
37 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)), 114 ALLOW_THIS_IN_INITIALIZER_LIST(tracker_(this)),
38 panel_(panel), 115 delegate_(delegate),
39 context_menu_(NULL) { 116 context_menu_(NULL),
117 disable_tooltip_(false) {
40 set_border(NULL); 118 set_border(NULL);
41 set_alignment(TextButton::ALIGN_CENTER); 119 set_alignment(TextButton::ALIGN_CENTER);
42 120
43 // No UpdateState() here because View hierarchy not setup yet. Our parent 121 // No UpdateState() here because View hierarchy not setup yet. Our parent
44 // should call UpdateState() after creation. 122 // should call UpdateState() after creation.
45 123
124 Profile* original_profile = browser_->profile()->GetOriginalProfile();
46 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, 125 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED,
47 content::Source<ExtensionAction>(browser_action_)); 126 content::Source<ExtensionAction>(browser_action_));
48 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, 127 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED,
49 content::Source<Profile>( 128 content::Source<Profile>(original_profile));
50 panel_->profile()->GetOriginalProfile()));
51 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED, 129 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED,
52 content::Source<Profile>( 130 content::Source<Profile>(original_profile));
53 panel_->profile()->GetOriginalProfile()));
54 } 131 }
55 132
56 void BrowserActionButton::Destroy() { 133 void BrowserActionButton::Destroy() {
57 MaybeUnregisterExtensionCommand(false); 134 MaybeUnregisterExtensionCommand(false);
58 135
59 if (context_menu_) { 136 if (context_menu_) {
60 context_menu_->Cancel(); 137 context_menu_->Cancel();
61 MessageLoop::current()->DeleteSoon(FROM_HERE, this); 138 MessageLoop::current()->DeleteSoon(FROM_HERE, this);
62 } else { 139 } else {
63 delete this; 140 delete this;
(...skipping 30 matching lines...) Expand all
94 171
95 bool BrowserActionButton::CanHandleAccelerators() const { 172 bool BrowserActionButton::CanHandleAccelerators() const {
96 // View::CanHandleAccelerators() checks to see if the view is visible before 173 // View::CanHandleAccelerators() checks to see if the view is visible before
97 // allowing it to process accelerators. This is not appropriate for browser 174 // allowing it to process accelerators. This is not appropriate for browser
98 // actions buttons, which can be hidden inside the overflow area. 175 // actions buttons, which can be hidden inside the overflow area.
99 return true; 176 return true;
100 } 177 }
101 178
102 void BrowserActionButton::ButtonPressed(views::Button* sender, 179 void BrowserActionButton::ButtonPressed(views::Button* sender,
103 const views::Event& event) { 180 const views::Event& event) {
104 panel_->OnBrowserActionExecuted(this); 181 delegate_->OnBrowserActionExecuted(this);
105 } 182 }
106 183
107 void BrowserActionButton::OnImageLoaded(const gfx::Image& image, 184 void BrowserActionButton::OnImageLoaded(const gfx::Image& image,
108 const std::string& extension_id, 185 const std::string& extension_id,
109 int index) { 186 int index) {
110 if (!image.IsEmpty()) 187 if (!image.IsEmpty())
111 default_icon_ = *image.ToSkBitmap(); 188 default_icon_ = *image.ToSkBitmap();
112 189
113 // Call back to UpdateState() because a more specific icon might have been set 190 // Call back to UpdateState() because a more specific icon might have been set
114 // while the load was outstanding. 191 // while the load was outstanding.
115 UpdateState(); 192 UpdateState();
116 } 193 }
117 194
118 void BrowserActionButton::UpdateState() { 195 void BrowserActionButton::UpdateState() {
119 int tab_id = panel_->GetCurrentTabId(); 196 int tab_id = delegate_->GetCurrentTabId();
120 if (tab_id < 0) 197 if (tab_id < 0)
121 return; 198 return;
122 199
123 SkBitmap icon(browser_action()->GetIcon(tab_id)); 200 SkBitmap icon(browser_action()->GetIcon(tab_id));
124 if (icon.isNull()) 201 if (icon.isNull())
125 icon = default_icon_; 202 icon = default_icon_;
126 if (!icon.isNull()) { 203 if (!icon.isNull()) {
127 SkPaint paint; 204 SkPaint paint;
128 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); 205 paint.setXfermode(SkXfermode::Create(SkXfermode::kSrcOver_Mode));
129 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance(); 206 ui::ResourceBundle& rb = ui::ResourceBundle::GetSharedInstance();
(...skipping 18 matching lines...) Expand all
148 SkBitmap bg_p; 225 SkBitmap bg_p;
149 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p, 226 rb.GetBitmapNamed(IDR_BROWSER_ACTION_P)->copyTo(&bg_p,
150 SkBitmap::kARGB_8888_Config); 227 SkBitmap::kARGB_8888_Config);
151 SkCanvas bg_p_canvas(bg_p); 228 SkCanvas bg_p_canvas(bg_p);
152 bg_p_canvas.drawBitmap(icon, 229 bg_p_canvas.drawBitmap(icon,
153 SkIntToScalar((bg_p.width() - icon.width()) / 2), 230 SkIntToScalar((bg_p.width() - icon.width()) / 2),
154 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint); 231 SkIntToScalar((bg_p.height() - icon.height()) / 2), &paint);
155 SetPushedIcon(bg_p); 232 SetPushedIcon(bg_p);
156 } 233 }
157 234
158 // If the browser action name is empty, show the extension name instead. 235 string16 name = GetTextForTooltip();
159 string16 name = UTF8ToUTF16(browser_action()->GetTitle(tab_id)); 236 SetTooltipText(disable_tooltip_ ? string16() : name);
160 if (name.empty())
161 name = UTF8ToUTF16(extension()->name());
162 SetTooltipText(name);
163 SetAccessibleName(name); 237 SetAccessibleName(name);
164 parent()->SchedulePaint(); 238 parent()->SchedulePaint();
165 } 239 }
166 240
167 bool BrowserActionButton::IsPopup() { 241 bool BrowserActionButton::IsPopup() {
168 int tab_id = panel_->GetCurrentTabId(); 242 int tab_id = delegate_->GetCurrentTabId();
169 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); 243 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id);
170 } 244 }
171 245
172 GURL BrowserActionButton::GetPopupUrl() { 246 GURL BrowserActionButton::GetPopupUrl() {
173 int tab_id = panel_->GetCurrentTabId(); 247 int tab_id = delegate_->GetCurrentTabId();
174 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id); 248 return (tab_id < 0) ? GURL() : browser_action_->GetPopupUrl(tab_id);
175 } 249 }
176 250
177 void BrowserActionButton::Observe(int type, 251 void BrowserActionButton::Observe(int type,
178 const content::NotificationSource& source, 252 const content::NotificationSource& source,
179 const content::NotificationDetails& details) { 253 const content::NotificationDetails& details) {
180 switch (type) { 254 switch (type) {
181 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED: 255 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED:
182 UpdateState(); 256 UpdateState();
183 // The browser action may have become visible/hidden so we need to make 257 // The browser action may have become visible/hidden so we need to make
184 // sure the state gets updated. 258 // sure the state gets updated.
185 panel_->OnBrowserActionVisibilityChanged(); 259 delegate_->OnBrowserActionVisibilityChanged();
186 break; 260 break;
187 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED: 261 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED:
188 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: { 262 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED: {
189 std::pair<const std::string, const std::string>* payload = 263 std::pair<const std::string, const std::string>* payload =
190 content::Details<std::pair<const std::string, const std::string> >( 264 content::Details<std::pair<const std::string, const std::string> >(
191 details).ptr(); 265 details).ptr();
192 if (extension_->id() == payload->first && 266 if (extension_->id() == payload->first &&
193 payload->second == 267 payload->second ==
194 extension_manifest_values::kBrowserActionKeybindingEvent) { 268 extension_manifest_values::kBrowserActionKeybindingEvent) {
195 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED) 269 if (type == chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED)
196 MaybeRegisterExtensionCommand(); 270 MaybeRegisterExtensionCommand();
197 else 271 else
198 MaybeUnregisterExtensionCommand(true); 272 MaybeUnregisterExtensionCommand(true);
199 } 273 }
200 break; 274 break;
201 } 275 }
202 default: 276 default:
203 NOTREACHED(); 277 NOTREACHED();
204 break; 278 break;
205 } 279 }
206 } 280 }
207 281
208 bool BrowserActionButton::Activate() { 282 bool BrowserActionButton::Activate() {
209 if (!IsPopup()) 283 if (!IsPopup())
210 return true; 284 return true;
211 285
212 panel_->OnBrowserActionExecuted(this); 286 delegate_->OnBrowserActionExecuted(this);
213 287
214 // TODO(erikkay): Run a nested modal loop while the mouse is down to 288 // TODO(erikkay): Run a nested modal loop while the mouse is down to
215 // enable menu-like drag-select behavior. 289 // enable menu-like drag-select behavior.
216 290
217 // The return value of this method is returned via OnMousePressed. 291 // The return value of this method is returned via OnMousePressed.
218 // We need to return false here since we're handing off focus to another 292 // We need to return false here since we're handing off focus to another
219 // widget/view, and true will grab it right back and try to send events 293 // widget/view, and true will grab it right back and try to send events
220 // to us. 294 // to us.
221 return false; 295 return false;
222 } 296 }
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
258 332
259 void BrowserActionButton::ShowContextMenu(const gfx::Point& p, 333 void BrowserActionButton::ShowContextMenu(const gfx::Point& p,
260 bool is_mouse_gesture) { 334 bool is_mouse_gesture) {
261 if (!extension()->ShowConfigureContextMenus()) 335 if (!extension()->ShowConfigureContextMenus())
262 return; 336 return;
263 337
264 SetButtonPushed(); 338 SetButtonPushed();
265 339
266 // Reconstructs the menu every time because the menu's contents are dynamic. 340 // Reconstructs the menu every time because the menu's contents are dynamic.
267 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_( 341 scoped_refptr<ExtensionContextMenuModel> context_menu_contents_(
268 new ExtensionContextMenuModel(extension(), panel_->browser())); 342 new ExtensionContextMenuModel(extension(), browser_));
269 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get()); 343 views::MenuModelAdapter menu_model_adapter(context_menu_contents_.get());
270 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu()); 344 views::MenuRunner menu_runner(menu_model_adapter.CreateMenu());
271 345
272 context_menu_ = menu_runner.GetMenu(); 346 context_menu_ = menu_runner.GetMenu();
273 gfx::Point screen_loc; 347 gfx::Point screen_loc;
274 views::View::ConvertPointToScreen(this, &screen_loc); 348 views::View::ConvertPointToScreen(this, &screen_loc);
275 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()), 349 if (menu_runner.RunMenuAt(GetWidget(), NULL, gfx::Rect(screen_loc, size()),
276 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) == 350 views::MenuItemView::TOPLEFT, views::MenuRunner::HAS_MNEMONICS) ==
277 views::MenuRunner::MENU_DELETED) 351 views::MenuRunner::MENU_DELETED)
278 return; 352 return;
279 353
280 SetButtonNotPushed(); 354 SetButtonNotPushed();
281 context_menu_ = NULL; 355 context_menu_ = NULL;
282 } 356 }
283 357
284 bool BrowserActionButton::AcceleratorPressed( 358 bool BrowserActionButton::AcceleratorPressed(
285 const ui::Accelerator& accelerator) { 359 const ui::Accelerator& accelerator) {
286 panel_->OnBrowserActionExecuted(this); 360 delegate_->OnBrowserActionExecuted(this);
287 return true; 361 return true;
288 } 362 }
289 363
290 void BrowserActionButton::SetButtonPushed() { 364 void BrowserActionButton::SetButtonPushed() {
291 SetState(views::CustomButton::BS_PUSHED); 365 SetState(views::CustomButton::BS_PUSHED);
292 menu_visible_ = true; 366 menu_visible_ = true;
293 } 367 }
294 368
295 void BrowserActionButton::SetButtonNotPushed() { 369 void BrowserActionButton::SetButtonNotPushed() {
296 SetState(views::CustomButton::BS_NORMAL); 370 SetState(views::CustomButton::BS_NORMAL);
297 menu_visible_ = false; 371 menu_visible_ = false;
298 } 372 }
299 373
374 void BrowserActionButton::SetTooltipDisabled(bool disable_tooltip) {
375 disable_tooltip_ = disable_tooltip;
376 SetTooltipText(disable_tooltip_ ? string16() : GetTextForTooltip());
377 }
378
300 BrowserActionButton::~BrowserActionButton() { 379 BrowserActionButton::~BrowserActionButton() {
301 } 380 }
302 381
303 void BrowserActionButton::MaybeRegisterExtensionCommand() { 382 void BrowserActionButton::MaybeRegisterExtensionCommand() {
304 extensions::CommandService* command_service = 383 extensions::CommandService* command_service =
305 extensions::CommandServiceFactory::GetForProfile( 384 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
306 panel_->browser()->profile());
307 extensions::Command browser_action_command; 385 extensions::Command browser_action_command;
308 if (command_service->GetBrowserActionCommand( 386 if (command_service->GetBrowserActionCommand(
309 extension_->id(), 387 extension_->id(),
310 extensions::CommandService::ACTIVE_ONLY, 388 extensions::CommandService::ACTIVE_ONLY,
311 &browser_action_command, 389 &browser_action_command,
312 NULL)) { 390 NULL)) {
313 keybinding_.reset(new ui::Accelerator( 391 keybinding_.reset(new ui::Accelerator(
314 browser_action_command.accelerator())); 392 browser_action_command.accelerator()));
315 panel_->GetFocusManager()->RegisterAccelerator( 393 GetFocusManager()->RegisterAccelerator(
316 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this); 394 *keybinding_.get(), ui::AcceleratorManager::kHighPriority, this);
317 } 395 }
318 } 396 }
319 397
320 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) { 398 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active) {
321 if (!keybinding_.get() || !panel_->GetFocusManager()) 399 if (!keybinding_.get() || !GetFocusManager())
322 return; 400 return;
323 401
324 extensions::CommandService* command_service = 402 extensions::CommandService* command_service =
325 extensions::CommandServiceFactory::GetForProfile( 403 extensions::CommandServiceFactory::GetForProfile(browser_->profile());
326 panel_->browser()->profile());
327 404
328 extensions::Command browser_action_command; 405 extensions::Command browser_action_command;
329 if (!only_if_active || !command_service->GetBrowserActionCommand( 406 if (!only_if_active || !command_service->GetBrowserActionCommand(
330 extension_->id(), 407 extension_->id(),
331 extensions::CommandService::ACTIVE_ONLY, 408 extensions::CommandService::ACTIVE_ONLY,
332 &browser_action_command, 409 &browser_action_command,
333 NULL)) { 410 NULL)) {
334 panel_->GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); 411 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this);
335 } 412 }
336 } 413 }
337 414
415 string16 BrowserActionButton::GetTextForTooltip() {
416 int tab_id = delegate_->GetCurrentTabId();
417 if (tab_id < 0)
418 return string16();
338 419
339 //////////////////////////////////////////////////////////////////////////////// 420 // If the browser action name is empty, show the extension name instead.
340 // BrowserActionView 421 std::string name = browser_action()->GetTitle(tab_id);
341 422 return UTF8ToUTF16(name.empty() ? extension()->name() : name);
342 BrowserActionView::BrowserActionView(const Extension* extension,
343 BrowserActionsContainer* panel)
344 : panel_(panel) {
345 button_ = new BrowserActionButton(extension, panel);
346 button_->set_drag_controller(panel_);
347 AddChildView(button_);
348 button_->UpdateState();
349 } 423 }
350
351 BrowserActionView::~BrowserActionView() {
352 RemoveChildView(button_);
353 button_->Destroy();
354 }
355
356 gfx::Canvas* BrowserActionView::GetIconWithBadge() {
357 int tab_id = panel_->GetCurrentTabId();
358
359 SkBitmap icon = button_->extension()->browser_action()->GetIcon(tab_id);
360 if (icon.isNull())
361 icon = button_->default_icon();
362
363 gfx::Canvas* canvas =
364 new gfx::Canvas(gfx::ImageSkiaRep(icon, ui::SCALE_FACTOR_100P), false);
365
366 if (tab_id >= 0) {
367 gfx::Rect bounds(icon.width(), icon.height() + ToolbarView::kVertSpacing);
368 button_->extension()->browser_action()->PaintBadge(canvas, bounds, tab_id);
369 }
370
371 return canvas;
372 }
373
374 void BrowserActionView::Layout() {
375 // We can't rely on button_->GetPreferredSize() here because that's not set
376 // correctly until the first call to
377 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
378 // called before that when the initial bounds are set (and then not after,
379 // since the bounds don't change). So instead of setting the height from the
380 // button's preferred size, we use IconHeight(), since that's how big the
381 // button should be regardless of what it's displaying.
382 button_->SetBounds(0, ToolbarView::kVertSpacing, width(),
383 BrowserActionsContainer::IconHeight());
384 }
385
386 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState* state) {
387 state->name = l10n_util::GetStringUTF16(
388 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION);
389 state->role = ui::AccessibilityTypes::ROLE_GROUPING;
390 }
391
392 void BrowserActionView::PaintChildren(gfx::Canvas* canvas) {
393 View::PaintChildren(canvas);
394 ExtensionAction* action = button()->browser_action();
395 int tab_id = panel_->GetCurrentTabId();
396 if (tab_id >= 0)
397 action->PaintBadge(canvas, gfx::Rect(width(), height()), tab_id);
398 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698