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/location_bar/script_bubble_icon_view.h"
7 #include "base/strings/string_number_conversions.h"
8 #include "chrome/browser/ui/view_ids.h"
9 #include "chrome/browser/ui/views/script_bubble_view.h"
10 #include "chrome/common/extensions/api/extension_action/action_info.h"
11 #include "chrome/common/icon_with_badge_image_source.h"
12 #include "grit/generated_resources.h"
13 #include "grit/theme_resources.h"
14 #include "ui/base/accessibility/accessible_view_state.h"
15 #include "ui/base/l10n/l10n_util.h"
16 #include "ui/base/resource/resource_bundle.h"
17 #include "ui/views/widget/widget.h"
19 using content::WebContents
;
21 ScriptBubbleIconView::ScriptBubbleIconView(
22 LocationBarView::Delegate
* location_bar_delegate
)
23 : location_bar_delegate_(location_bar_delegate
),
25 set_id(VIEW_ID_SCRIPT_BUBBLE
);
26 SetTooltipText(l10n_util::GetStringUTF16(IDS_TOOLTIP_SCRIPT_BUBBLE
));
27 SetAccessibilityFocusable(true);
28 LocationBarView::InitTouchableLocationBarChildView(this);
31 ScriptBubbleIconView::~ScriptBubbleIconView() {
34 void ScriptBubbleIconView::SetScriptCount(size_t script_count
) {
35 script_count_
= script_count
;
36 gfx::ImageSkia
* icon
=
37 ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
38 IDR_EXTENSIONS_SCRIPT_BUBBLE
);
39 gfx::Size
requested_size(19, 19); // Icon is only 16x16, too small to badge.
41 gfx::ImageSkia image
= gfx::ImageSkia(
42 new IconWithBadgeImageSource(
46 base::IntToString(script_count_
),
48 SkColorSetRGB(0, 170, 0),
49 extensions::ActionInfo::TYPE_PAGE
),
55 void ScriptBubbleIconView::Layout() {
56 SetScriptCount(script_count_
);
59 void ScriptBubbleIconView::GetAccessibleState(ui::AccessibleViewState
* state
) {
60 ImageView::GetAccessibleState(state
);
61 state
->role
= ui::AccessibilityTypes::ROLE_PUSHBUTTON
;
64 bool ScriptBubbleIconView::OnMousePressed(const ui::MouseEvent
& event
) {
65 // We want to show the bubble on mouse release; that is the standard behavior
70 void ScriptBubbleIconView::OnMouseReleased(const ui::MouseEvent
& event
) {
71 if (event
.IsOnlyLeftMouseButton() && HitTestPoint(event
.location()))
72 ShowScriptBubble(this, location_bar_delegate_
->GetWebContents());
75 bool ScriptBubbleIconView::OnKeyPressed(const ui::KeyEvent
& event
) {
76 if (event
.key_code() == ui::VKEY_SPACE
||
77 event
.key_code() == ui::VKEY_RETURN
) {
78 ShowScriptBubble(this, location_bar_delegate_
->GetWebContents());
84 void ScriptBubbleIconView::OnGestureEvent(ui::GestureEvent
* event
) {
85 if (event
->type() == ui::ET_GESTURE_TAP
) {
86 ShowScriptBubble(this, location_bar_delegate_
->GetWebContents());
91 void ScriptBubbleIconView::ShowScriptBubble(views::View
* anchor_view
,
92 WebContents
* web_contents
) {
93 ScriptBubbleView
* script_bubble
= new ScriptBubbleView(anchor_view
,
95 views::BubbleDelegateView::CreateBubble(script_bubble
)->Show();