Home | History | Annotate | Download | only in location_bar
      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.
      4 
      5 #include "chrome/browser/ui/views/location_bar/script_bubble_icon_view.h"
      6 
      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"
     18 
     19 using content::WebContents;
     20 
     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   set_accessibility_focusable(true);
     28   LocationBarView::InitTouchableLocationBarChildView(this);
     29 }
     30 
     31 ScriptBubbleIconView::~ScriptBubbleIconView() {
     32 }
     33 
     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.
     40 
     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);
     51 
     52   SetImage(image);
     53 }
     54 
     55 void ScriptBubbleIconView::Layout() {
     56   SetScriptCount(script_count_);
     57 }
     58 
     59 void ScriptBubbleIconView::GetAccessibleState(ui::AccessibleViewState* state) {
     60   ImageView::GetAccessibleState(state);
     61   state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
     62 }
     63 
     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;
     68 }
     69 
     70 void ScriptBubbleIconView::OnMouseReleased(const ui::MouseEvent& event) {
     71   if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location()))
     72     ShowScriptBubble(this, location_bar_delegate_->GetWebContents());
     73 }
     74 
     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;
     80   }
     81   return false;
     82 }
     83 
     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();
     88   }
     89 }
     90 
     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();
     96 }
     97