OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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/toolbar/browser_action_view.h" | 5 #include "chrome/browser/ui/views/toolbar/browser_action_view.h" |
6 | 6 |
7 #include "base/strings/utf_string_conversions.h" | 7 #include "base/strings/utf_string_conversions.h" |
8 #include "chrome/browser/chrome_notification_types.h" | 8 #include "chrome/browser/chrome_notification_types.h" |
9 #include "chrome/browser/extensions/api/commands/command_service.h" | 9 #include "chrome/browser/extensions/api/commands/command_service.h" |
10 #include "chrome/browser/extensions/extension_action.h" | 10 #include "chrome/browser/extensions/extension_action.h" |
(...skipping 19 matching lines...) Expand all Loading... |
30 #include "ui/gfx/image/image_skia_operations.h" | 30 #include "ui/gfx/image/image_skia_operations.h" |
31 #include "ui/gfx/image/image_skia_source.h" | 31 #include "ui/gfx/image/image_skia_source.h" |
32 #include "ui/views/controls/menu/menu_item_view.h" | 32 #include "ui/views/controls/menu/menu_item_view.h" |
33 #include "ui/views/controls/menu/menu_runner.h" | 33 #include "ui/views/controls/menu/menu_runner.h" |
34 | 34 |
35 using extensions::Extension; | 35 using extensions::Extension; |
36 | 36 |
37 //////////////////////////////////////////////////////////////////////////////// | 37 //////////////////////////////////////////////////////////////////////////////// |
38 // BrowserActionView | 38 // BrowserActionView |
39 | 39 |
40 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const { | |
41 return true; | |
42 } | |
43 | |
44 bool BrowserActionView::Delegate::NeedToShowTooltip() const { | |
45 return true; | |
46 } | |
47 | |
48 BrowserActionView::BrowserActionView(const Extension* extension, | 40 BrowserActionView::BrowserActionView(const Extension* extension, |
49 Browser* browser, | 41 Browser* browser, |
50 BrowserActionView::Delegate* delegate) | 42 BrowserActionView::Delegate* delegate) |
51 : browser_(browser), | 43 : browser_(browser), |
52 delegate_(delegate), | 44 delegate_(delegate), |
53 button_(NULL), | 45 button_(NULL), |
54 extension_(extension) { | 46 extension_(extension) { |
55 set_id(VIEW_ID_BROWSER_ACTION); | 47 set_id(VIEW_ID_BROWSER_ACTION); |
56 button_ = new BrowserActionButton(extension_, browser_, delegate_); | 48 button_ = new BrowserActionButton(extension_, browser_, delegate_); |
57 button_->set_drag_controller(delegate_); | 49 button_->set_drag_controller(delegate_); |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 browser_action_( | 95 browser_action_( |
104 extensions::ExtensionActionManager::Get(browser->profile())-> | 96 extensions::ExtensionActionManager::Get(browser->profile())-> |
105 GetBrowserAction(*extension)), | 97 GetBrowserAction(*extension)), |
106 extension_(extension), | 98 extension_(extension), |
107 icon_factory_(browser->profile(), extension, browser_action_, this), | 99 icon_factory_(browser->profile(), extension, browser_action_, this), |
108 delegate_(delegate), | 100 delegate_(delegate), |
109 context_menu_(NULL), | 101 context_menu_(NULL), |
110 called_registered_extension_command_(false), | 102 called_registered_extension_command_(false), |
111 icon_observer_(NULL) { | 103 icon_observer_(NULL) { |
112 SetBorder(views::Border::NullBorder()); | 104 SetBorder(views::Border::NullBorder()); |
113 set_alignment(TextButton::ALIGN_CENTER); | 105 SetHorizontalAlignment(gfx::ALIGN_CENTER); |
114 set_context_menu_controller(this); | 106 set_context_menu_controller(this); |
115 | 107 |
116 // No UpdateState() here because View hierarchy not setup yet. Our parent | 108 // No UpdateState() here because View hierarchy not setup yet. Our parent |
117 // should call UpdateState() after creation. | 109 // should call UpdateState() after creation. |
118 | 110 |
119 content::NotificationSource notification_source = | 111 content::NotificationSource notification_source = |
120 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); | 112 content::Source<Profile>(browser_->profile()->GetOriginalProfile()); |
121 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, | 113 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED, |
122 content::Source<ExtensionAction>(browser_action_)); | 114 content::Source<ExtensionAction>(browser_action_)); |
123 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, | 115 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED, |
(...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 menu_runner_.reset(); | 196 menu_runner_.reset(); |
205 SetButtonNotPushed(); | 197 SetButtonNotPushed(); |
206 context_menu_ = NULL; | 198 context_menu_ = NULL; |
207 } | 199 } |
208 | 200 |
209 void BrowserActionButton::UpdateState() { | 201 void BrowserActionButton::UpdateState() { |
210 int tab_id = delegate_->GetCurrentTabId(); | 202 int tab_id = delegate_->GetCurrentTabId(); |
211 if (tab_id < 0) | 203 if (tab_id < 0) |
212 return; | 204 return; |
213 | 205 |
214 SetShowMultipleIconStates(delegate_->NeedToShowMultipleIconStates()); | |
215 | |
216 if (!IsEnabled(tab_id)) { | 206 if (!IsEnabled(tab_id)) { |
217 SetState(views::CustomButton::STATE_DISABLED); | 207 SetState(views::CustomButton::STATE_DISABLED); |
218 } else { | 208 } else { |
219 SetState(menu_visible_ ? | 209 SetState(menu_visible_ ? |
220 views::CustomButton::STATE_PRESSED : | 210 views::CustomButton::STATE_PRESSED : |
221 views::CustomButton::STATE_NORMAL); | 211 views::CustomButton::STATE_NORMAL); |
222 } | 212 } |
223 | 213 |
224 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); | 214 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); |
225 | 215 |
226 if (!icon.isNull()) { | 216 if (!icon.isNull()) { |
227 if (!browser_action()->GetIsVisible(tab_id)) | 217 if (!browser_action()->GetIsVisible(tab_id)) |
228 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 218 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
229 | 219 |
230 ThemeService* theme = | 220 ThemeService* theme = |
231 ThemeServiceFactory::GetForProfile(browser_->profile()); | 221 ThemeServiceFactory::GetForProfile(browser_->profile()); |
232 | 222 |
233 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); | 223 gfx::ImageSkia bg = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION); |
234 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); | 224 SetImage(views::Button::STATE_NORMAL, |
| 225 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg, icon)); |
235 | 226 |
236 gfx::ImageSkia bg_h = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_H); | 227 gfx::ImageSkia bg_h = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_H); |
237 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h, icon)); | 228 SetImage(views::Button::STATE_HOVERED, |
| 229 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h, icon)); |
238 | 230 |
239 gfx::ImageSkia bg_p = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_P); | 231 gfx::ImageSkia bg_p = *theme->GetImageSkiaNamed(IDR_BROWSER_ACTION_P); |
240 SetPushedIcon( | 232 SetImage(views::Button::STATE_PRESSED, |
241 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p, icon)); | 233 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p, icon)); |
242 } | 234 } |
243 | 235 |
244 // If the browser action name is empty, show the extension name instead. | 236 // If the browser action name is empty, show the extension name instead. |
245 std::string title = browser_action()->GetTitle(tab_id); | 237 std::string title = browser_action()->GetTitle(tab_id); |
246 base::string16 name = | 238 base::string16 name = |
247 base::UTF8ToUTF16(title.empty() ? extension()->name() : title); | 239 base::UTF8ToUTF16(title.empty() ? extension()->name() : title); |
248 SetTooltipText(delegate_->NeedToShowTooltip() ? name : base::string16()); | 240 SetTooltipText(name); |
249 SetAccessibleName(name); | 241 SetAccessibleName(name); |
250 | 242 |
251 parent()->SchedulePaint(); | 243 parent()->SchedulePaint(); |
252 } | 244 } |
253 | 245 |
254 bool BrowserActionButton::IsPopup() { | 246 bool BrowserActionButton::IsPopup() { |
255 int tab_id = delegate_->GetCurrentTabId(); | 247 int tab_id = delegate_->GetCurrentTabId(); |
256 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); | 248 return (tab_id < 0) ? false : browser_action_->HasPopup(tab_id); |
257 } | 249 } |
258 | 250 |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
313 // The return value of this method is returned via OnMousePressed. | 305 // The return value of this method is returned via OnMousePressed. |
314 // We need to return false here since we're handing off focus to another | 306 // We need to return false here since we're handing off focus to another |
315 // widget/view, and true will grab it right back and try to send events | 307 // widget/view, and true will grab it right back and try to send events |
316 // to us. | 308 // to us. |
317 return false; | 309 return false; |
318 } | 310 } |
319 | 311 |
320 bool BrowserActionButton::OnMousePressed(const ui::MouseEvent& event) { | 312 bool BrowserActionButton::OnMousePressed(const ui::MouseEvent& event) { |
321 if (!event.IsRightMouseButton()) { | 313 if (!event.IsRightMouseButton()) { |
322 return IsPopup() ? MenuButton::OnMousePressed(event) : | 314 return IsPopup() ? MenuButton::OnMousePressed(event) : |
323 TextButton::OnMousePressed(event); | 315 LabelButton::OnMousePressed(event); |
324 } | 316 } |
325 | 317 |
326 if (!views::View::ShouldShowContextMenuOnMousePress()) { | 318 if (!views::View::ShouldShowContextMenuOnMousePress()) { |
327 // See comments in MenuButton::Activate() as to why this is needed. | 319 // See comments in MenuButton::Activate() as to why this is needed. |
328 SetMouseHandler(NULL); | 320 SetMouseHandler(NULL); |
329 | 321 |
330 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); | 322 ShowContextMenu(gfx::Point(), ui::MENU_SOURCE_MOUSE); |
331 } | 323 } |
332 return false; | 324 return false; |
333 } | 325 } |
334 | 326 |
335 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent& event) { | 327 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent& event) { |
336 if (IsPopup() || context_menu_) { | 328 if (IsPopup() || context_menu_) { |
337 // TODO(erikkay) this never actually gets called (probably because of the | 329 // TODO(erikkay) this never actually gets called (probably because of the |
338 // loss of focus). | 330 // loss of focus). |
339 MenuButton::OnMouseReleased(event); | 331 MenuButton::OnMouseReleased(event); |
340 } else { | 332 } else { |
341 TextButton::OnMouseReleased(event); | 333 LabelButton::OnMouseReleased(event); |
342 } | 334 } |
343 } | 335 } |
344 | 336 |
345 void BrowserActionButton::OnMouseExited(const ui::MouseEvent& event) { | 337 void BrowserActionButton::OnMouseExited(const ui::MouseEvent& event) { |
346 if (IsPopup() || context_menu_) | 338 if (IsPopup() || context_menu_) |
347 MenuButton::OnMouseExited(event); | 339 MenuButton::OnMouseExited(event); |
348 else | 340 else |
349 TextButton::OnMouseExited(event); | 341 LabelButton::OnMouseExited(event); |
350 } | 342 } |
351 | 343 |
352 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent& event) { | 344 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent& event) { |
353 return IsPopup() ? MenuButton::OnKeyReleased(event) : | 345 return IsPopup() ? MenuButton::OnKeyReleased(event) : |
354 TextButton::OnKeyReleased(event); | 346 LabelButton::OnKeyReleased(event); |
355 } | 347 } |
356 | 348 |
357 void BrowserActionButton::OnGestureEvent(ui::GestureEvent* event) { | 349 void BrowserActionButton::OnGestureEvent(ui::GestureEvent* event) { |
358 if (IsPopup()) | 350 if (IsPopup()) |
359 MenuButton::OnGestureEvent(event); | 351 MenuButton::OnGestureEvent(event); |
360 else | 352 else |
361 TextButton::OnGestureEvent(event); | 353 LabelButton::OnGestureEvent(event); |
362 } | 354 } |
363 | 355 |
364 bool BrowserActionButton::AcceleratorPressed( | 356 bool BrowserActionButton::AcceleratorPressed( |
365 const ui::Accelerator& accelerator) { | 357 const ui::Accelerator& accelerator) { |
366 delegate_->OnBrowserActionExecuted(this); | 358 delegate_->OnBrowserActionExecuted(this); |
367 return true; | 359 return true; |
368 } | 360 } |
369 | 361 |
370 void BrowserActionButton::SetButtonPushed() { | 362 void BrowserActionButton::SetButtonPushed() { |
371 SetState(views::CustomButton::STATE_PRESSED); | 363 SetState(views::CustomButton::STATE_PRESSED); |
(...skipping 12 matching lines...) Expand all Loading... |
384 gfx::ImageSkia BrowserActionButton::GetIconWithBadge() { | 376 gfx::ImageSkia BrowserActionButton::GetIconWithBadge() { |
385 int tab_id = delegate_->GetCurrentTabId(); | 377 int tab_id = delegate_->GetCurrentTabId(); |
386 gfx::Size spacing(0, ToolbarView::kVertSpacing); | 378 gfx::Size spacing(0, ToolbarView::kVertSpacing); |
387 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); | 379 gfx::ImageSkia icon = *icon_factory_.GetIcon(tab_id).ToImageSkia(); |
388 if (!IsEnabled(tab_id)) | 380 if (!IsEnabled(tab_id)) |
389 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); | 381 icon = gfx::ImageSkiaOperations::CreateTransparentImage(icon, .25); |
390 return browser_action_->GetIconWithBadge(icon, tab_id, spacing); | 382 return browser_action_->GetIconWithBadge(icon, tab_id, spacing); |
391 } | 383 } |
392 | 384 |
393 gfx::ImageSkia BrowserActionButton::GetIconForTest() { | 385 gfx::ImageSkia BrowserActionButton::GetIconForTest() { |
394 return icon(); | 386 return GetImage(views::Button::STATE_NORMAL); |
395 } | 387 } |
396 | 388 |
397 BrowserActionButton::~BrowserActionButton() { | 389 BrowserActionButton::~BrowserActionButton() { |
398 } | 390 } |
399 | 391 |
400 void BrowserActionButton::MaybeRegisterExtensionCommand() { | 392 void BrowserActionButton::MaybeRegisterExtensionCommand() { |
401 extensions::CommandService* command_service = | 393 extensions::CommandService* command_service = |
402 extensions::CommandService::Get(browser_->profile()); | 394 extensions::CommandService::Get(browser_->profile()); |
403 extensions::Command browser_action_command; | 395 extensions::Command browser_action_command; |
404 if (command_service->GetBrowserActionCommand( | 396 if (command_service->GetBrowserActionCommand( |
(...skipping 18 matching lines...) Expand all Loading... |
423 extensions::Command browser_action_command; | 415 extensions::Command browser_action_command; |
424 if (!only_if_active || !command_service->GetBrowserActionCommand( | 416 if (!only_if_active || !command_service->GetBrowserActionCommand( |
425 extension_->id(), | 417 extension_->id(), |
426 extensions::CommandService::ACTIVE_ONLY, | 418 extensions::CommandService::ACTIVE_ONLY, |
427 &browser_action_command, | 419 &browser_action_command, |
428 NULL)) { | 420 NULL)) { |
429 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); | 421 GetFocusManager()->UnregisterAccelerator(*keybinding_.get(), this); |
430 keybinding_.reset(NULL); | 422 keybinding_.reset(NULL); |
431 } | 423 } |
432 } | 424 } |
OLD | NEW |