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