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
3 // found in the LICENSE file.
5 #include "chrome/browser/ui/views/browser_action_view.h"
7 #include "base/utf_string_conversions.h"
8 #include "chrome/browser/extensions/api/commands/command_service.h"
9 #include "chrome/browser/extensions/extension_action.h"
10 #include "chrome/browser/extensions/extension_action_manager.h"
11 #include "chrome/browser/extensions/extension_context_menu_model.h"
12 #include "chrome/browser/profiles/profile.h"
13 #include "chrome/browser/ui/browser.h"
14 #include "chrome/browser/ui/views/browser_actions_container.h"
15 #include "chrome/browser/ui/views/toolbar_view.h"
16 #include "chrome/common/chrome_notification_types.h"
17 #include "chrome/common/extensions/extension.h"
18 #include "chrome/common/extensions/extension_manifest_constants.h"
19 #include "grit/generated_resources.h"
20 #include "grit/theme_resources.h"
21 #include "ui/base/accessibility/accessible_view_state.h"
22 #include "ui/base/events/event.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/base/resource/resource_bundle.h"
25 #include "ui/gfx/image/image_skia.h"
26 #include "ui/gfx/image/image_skia_operations.h"
27 #include "ui/gfx/image/image_skia_source.h"
28 #include "ui/views/controls/menu/menu_runner.h"
30 using extensions::Extension
;
32 ////////////////////////////////////////////////////////////////////////////////
35 bool BrowserActionView::Delegate::NeedToShowMultipleIconStates() const {
39 bool BrowserActionView::Delegate::NeedToShowTooltip() const {
43 BrowserActionView::BrowserActionView(const Extension
* extension
,
45 BrowserActionView::Delegate
* delegate
)
49 extension_(extension
) {
50 button_
= new BrowserActionButton(extension_
, browser_
, delegate_
);
51 button_
->set_drag_controller(delegate_
);
52 button_
->set_owned_by_client();
53 AddChildView(button_
);
54 button_
->UpdateState();
57 BrowserActionView::~BrowserActionView() {
61 gfx::ImageSkia
BrowserActionView::GetIconWithBadge() {
62 int tab_id
= delegate_
->GetCurrentTabId();
64 const ExtensionAction
* action
=
65 extensions::ExtensionActionManager::Get(browser_
->profile())->
66 GetBrowserAction(*button_
->extension());
67 gfx::Size
spacing(0, ToolbarView::kVertSpacing
);
68 gfx::ImageSkia icon
= *button_
->icon_factory().GetIcon(tab_id
).ToImageSkia();
69 if (!button_
->IsEnabled(tab_id
))
70 icon
= gfx::ImageSkiaOperations::CreateTransparentImage(icon
, .25);
71 return action
->GetIconWithBadge(icon
, tab_id
, spacing
);
74 void BrowserActionView::Layout() {
75 // We can't rely on button_->GetPreferredSize() here because that's not set
76 // correctly until the first call to
77 // BrowserActionsContainer::RefreshBrowserActionViews(), whereas this can be
78 // called before that when the initial bounds are set (and then not after,
79 // since the bounds don't change). So instead of setting the height from the
80 // button's preferred size, we use IconHeight(), since that's how big the
81 // button should be regardless of what it's displaying.
82 gfx::Point offset
= delegate_
->GetViewContentOffset();
83 button_
->SetBounds(offset
.x(), offset
.y(), width() - offset
.x(),
84 BrowserActionsContainer::IconHeight());
87 void BrowserActionView::GetAccessibleState(ui::AccessibleViewState
* state
) {
88 state
->name
= l10n_util::GetStringUTF16(
89 IDS_ACCNAME_EXTENSIONS_BROWSER_ACTION
);
90 state
->role
= ui::AccessibilityTypes::ROLE_GROUPING
;
93 gfx::Size
BrowserActionView::GetPreferredSize() {
94 return gfx::Size(BrowserActionsContainer::IconWidth(false),
95 BrowserActionsContainer::IconHeight());
98 void BrowserActionView::PaintChildren(gfx::Canvas
* canvas
) {
99 View::PaintChildren(canvas
);
100 ExtensionAction
* action
= button()->browser_action();
101 int tab_id
= delegate_
->GetCurrentTabId();
103 action
->PaintBadge(canvas
, GetLocalBounds(), tab_id
);
106 ////////////////////////////////////////////////////////////////////////////////
107 // BrowserActionButton
109 BrowserActionButton::BrowserActionButton(const Extension
* extension
,
111 BrowserActionView::Delegate
* delegate
)
112 : ALLOW_THIS_IN_INITIALIZER_LIST(
113 MenuButton(this, string16(), NULL
, false)),
116 extensions::ExtensionActionManager::Get(browser
->profile())->
117 GetBrowserAction(*extension
)),
118 extension_(extension
),
119 ALLOW_THIS_IN_INITIALIZER_LIST(
120 icon_factory_(browser
->profile(), extension
, browser_action_
, this)),
123 called_registered_extension_command_(false) {
125 set_alignment(TextButton::ALIGN_CENTER
);
126 set_context_menu_controller(this);
128 // No UpdateState() here because View hierarchy not setup yet. Our parent
129 // should call UpdateState() after creation.
131 content::NotificationSource notification_source
=
132 content::Source
<Profile
>(browser_
->profile()->GetOriginalProfile());
133 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED
,
134 content::Source
<ExtensionAction
>(browser_action_
));
135 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
,
136 notification_source
);
137 registrar_
.Add(this, chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED
,
138 notification_source
);
141 void BrowserActionButton::Destroy() {
142 MaybeUnregisterExtensionCommand(false);
145 context_menu_
->Cancel();
146 MessageLoop::current()->DeleteSoon(FROM_HERE
, this);
152 void BrowserActionButton::ViewHierarchyChanged(
153 bool is_add
, View
* parent
, View
* child
) {
155 if (is_add
&& !called_registered_extension_command_
&& GetFocusManager()) {
156 MaybeRegisterExtensionCommand();
157 called_registered_extension_command_
= true;
160 MenuButton::ViewHierarchyChanged(is_add
, parent
, child
);
163 bool BrowserActionButton::CanHandleAccelerators() const {
164 // View::CanHandleAccelerators() checks to see if the view is visible before
165 // allowing it to process accelerators. This is not appropriate for browser
166 // actions buttons, which can be hidden inside the overflow area.
170 void BrowserActionButton::GetAccessibleState(ui::AccessibleViewState
* state
) {
171 views::MenuButton::GetAccessibleState(state
);
172 state
->role
= ui::AccessibilityTypes::ROLE_PUSHBUTTON
;
175 void BrowserActionButton::ButtonPressed(views::Button
* sender
,
176 const ui::Event
& event
) {
177 delegate_
->OnBrowserActionExecuted(this);
180 void BrowserActionButton::ShowContextMenuForView(View
* source
,
181 const gfx::Point
& point
) {
182 if (!extension()->ShowConfigureContextMenus())
187 // Reconstructs the menu every time because the menu's contents are dynamic.
188 scoped_refptr
<ExtensionContextMenuModel
> context_menu_contents_(
189 new ExtensionContextMenuModel(extension(), browser_
, delegate_
));
190 menu_runner_
.reset(new views::MenuRunner(context_menu_contents_
.get()));
192 context_menu_
= menu_runner_
->GetMenu();
193 gfx::Point screen_loc
;
194 views::View::ConvertPointToScreen(this, &screen_loc
);
195 if (menu_runner_
->RunMenuAt(GetWidget(), NULL
, gfx::Rect(screen_loc
, size()),
196 views::MenuItemView::TOPLEFT
, views::MenuRunner::HAS_MNEMONICS
|
197 views::MenuRunner::CONTEXT_MENU
) ==
198 views::MenuRunner::MENU_DELETED
) {
202 menu_runner_
.reset();
203 SetButtonNotPushed();
204 context_menu_
= NULL
;
207 void BrowserActionButton::UpdateState() {
208 int tab_id
= delegate_
->GetCurrentTabId();
212 SetShowMultipleIconStates(delegate_
->NeedToShowMultipleIconStates());
214 if (!IsEnabled(tab_id
)) {
215 SetState(views::CustomButton::STATE_DISABLED
);
217 SetState(menu_visible_
?
218 views::CustomButton::STATE_PRESSED
:
219 views::CustomButton::STATE_NORMAL
);
222 gfx::ImageSkia icon
= *icon_factory_
.GetIcon(tab_id
).ToImageSkia();
224 if (!icon
.isNull()) {
225 if (!browser_action()->GetIsVisible(tab_id
))
226 icon
= gfx::ImageSkiaOperations::CreateTransparentImage(icon
, .25);
228 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
230 gfx::ImageSkia bg
= *rb
.GetImageSkiaNamed(IDR_BROWSER_ACTION
);
231 SetIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg
, icon
));
233 gfx::ImageSkia bg_h
= *rb
.GetImageSkiaNamed(IDR_BROWSER_ACTION_H
);
234 SetHoverIcon(gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_h
, icon
));
236 gfx::ImageSkia bg_p
= *rb
.GetImageSkiaNamed(IDR_BROWSER_ACTION_P
);
238 gfx::ImageSkiaOperations::CreateSuperimposedImage(bg_p
, icon
));
241 // If the browser action name is empty, show the extension name instead.
242 std::string title
= browser_action()->GetTitle(tab_id
);
243 string16 name
= UTF8ToUTF16(title
.empty() ? extension()->name() : title
);
244 SetTooltipText(delegate_
->NeedToShowTooltip() ? name
: string16());
245 SetAccessibleName(name
);
247 parent()->SchedulePaint();
250 bool BrowserActionButton::IsPopup() {
251 int tab_id
= delegate_
->GetCurrentTabId();
252 return (tab_id
< 0) ? false : browser_action_
->HasPopup(tab_id
);
255 GURL
BrowserActionButton::GetPopupUrl() {
256 int tab_id
= delegate_
->GetCurrentTabId();
257 return (tab_id
< 0) ? GURL() : browser_action_
->GetPopupUrl(tab_id
);
260 void BrowserActionButton::Observe(int type
,
261 const content::NotificationSource
& source
,
262 const content::NotificationDetails
& details
) {
264 case chrome::NOTIFICATION_EXTENSION_BROWSER_ACTION_UPDATED
:
266 // The browser action may have become visible/hidden so we need to make
267 // sure the state gets updated.
268 delegate_
->OnBrowserActionVisibilityChanged();
270 case chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
:
271 case chrome::NOTIFICATION_EXTENSION_COMMAND_REMOVED
: {
272 std::pair
<const std::string
, const std::string
>* payload
=
273 content::Details
<std::pair
<const std::string
, const std::string
> >(
275 if (extension_
->id() == payload
->first
&&
277 extension_manifest_values::kBrowserActionCommandEvent
) {
278 if (type
== chrome::NOTIFICATION_EXTENSION_COMMAND_ADDED
)
279 MaybeRegisterExtensionCommand();
281 MaybeUnregisterExtensionCommand(true);
291 void BrowserActionButton::OnIconUpdated() {
295 bool BrowserActionButton::Activate() {
299 delegate_
->OnBrowserActionExecuted(this);
301 // TODO(erikkay): Run a nested modal loop while the mouse is down to
302 // enable menu-like drag-select behavior.
304 // The return value of this method is returned via OnMousePressed.
305 // We need to return false here since we're handing off focus to another
306 // widget/view, and true will grab it right back and try to send events
311 bool BrowserActionButton::OnMousePressed(const ui::MouseEvent
& event
) {
312 if (!event
.IsRightMouseButton()) {
313 return IsPopup() ? MenuButton::OnMousePressed(event
) :
314 TextButton::OnMousePressed(event
);
317 // See comments in MenuButton::Activate() as to why this is needed.
318 SetMouseHandler(NULL
);
320 ShowContextMenu(gfx::Point(), true);
324 void BrowserActionButton::OnMouseReleased(const ui::MouseEvent
& event
) {
325 if (IsPopup() || context_menu_
) {
326 // TODO(erikkay) this never actually gets called (probably because of the
328 MenuButton::OnMouseReleased(event
);
330 TextButton::OnMouseReleased(event
);
334 void BrowserActionButton::OnMouseExited(const ui::MouseEvent
& event
) {
335 if (IsPopup() || context_menu_
)
336 MenuButton::OnMouseExited(event
);
338 TextButton::OnMouseExited(event
);
341 bool BrowserActionButton::OnKeyReleased(const ui::KeyEvent
& event
) {
342 return IsPopup() ? MenuButton::OnKeyReleased(event
) :
343 TextButton::OnKeyReleased(event
);
346 void BrowserActionButton::OnGestureEvent(ui::GestureEvent
* event
) {
348 MenuButton::OnGestureEvent(event
);
350 TextButton::OnGestureEvent(event
);
353 bool BrowserActionButton::AcceleratorPressed(
354 const ui::Accelerator
& accelerator
) {
355 delegate_
->OnBrowserActionExecuted(this);
359 void BrowserActionButton::SetButtonPushed() {
360 SetState(views::CustomButton::STATE_PRESSED
);
361 menu_visible_
= true;
364 void BrowserActionButton::SetButtonNotPushed() {
365 SetState(views::CustomButton::STATE_NORMAL
);
366 menu_visible_
= false;
369 bool BrowserActionButton::IsEnabled(int tab_id
) const {
370 return browser_action_
->GetIsVisible(tab_id
);
373 gfx::ImageSkia
BrowserActionButton::GetIconForTest() {
377 BrowserActionButton::~BrowserActionButton() {
380 void BrowserActionButton::MaybeRegisterExtensionCommand() {
381 extensions::CommandService
* command_service
=
382 extensions::CommandService::Get(browser_
->profile());
383 extensions::Command browser_action_command
;
384 if (command_service
->GetBrowserActionCommand(
386 extensions::CommandService::ACTIVE_ONLY
,
387 &browser_action_command
,
389 keybinding_
.reset(new ui::Accelerator(
390 browser_action_command
.accelerator()));
391 GetFocusManager()->RegisterAccelerator(
392 *keybinding_
.get(), ui::AcceleratorManager::kHighPriority
, this);
396 void BrowserActionButton::MaybeUnregisterExtensionCommand(bool only_if_active
) {
397 if (!keybinding_
.get() || !GetFocusManager())
400 extensions::CommandService
* command_service
=
401 extensions::CommandService::Get(browser_
->profile());
403 extensions::Command browser_action_command
;
404 if (!only_if_active
|| !command_service
->GetBrowserActionCommand(
406 extensions::CommandService::ACTIVE_ONLY
,
407 &browser_action_command
,
409 GetFocusManager()->UnregisterAccelerator(*keybinding_
.get(), this);