Disable view source for Developer Tools.
[chromium-blink-merge.git] / chrome / browser / ui / views / location_bar / script_bubble_icon_view.cc
blob0e3c8dca3f88481fa5265024b69aa0743b20e74c
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),
24 script_count_(0) {
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(
43 *icon,
44 requested_size,
45 gfx::Size(0, 2),
46 base::IntToString(script_count_),
47 SkColor(),
48 SkColorSetRGB(0, 170, 0),
49 extensions::ActionInfo::TYPE_PAGE),
50 requested_size);
52 SetImage(image);
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
66 // for buttons.
67 return true;
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());
79 return true;
81 return false;
84 void ScriptBubbleIconView::OnGestureEvent(ui::GestureEvent* event) {
85 if (event->type() == ui::ET_GESTURE_TAP) {
86 ShowScriptBubble(this, location_bar_delegate_->GetWebContents());
87 event->SetHandled();
91 void ScriptBubbleIconView::ShowScriptBubble(views::View* anchor_view,
92 WebContents* web_contents) {
93 ScriptBubbleView* script_bubble = new ScriptBubbleView(anchor_view,
94 web_contents);
95 views::BubbleDelegateView::CreateBubble(script_bubble)->Show();