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/star_view.h"
      6 
      7 #include "base/metrics/histogram.h"
      8 #include "base/strings/utf_string_conversions.h"
      9 #include "chrome/app/chrome_command_ids.h"
     10 #include "chrome/browser/bookmarks/bookmark_utils.h"
     11 #include "chrome/browser/command_updater.h"
     12 #include "chrome/browser/ui/view_ids.h"
     13 #include "chrome/browser/ui/views/browser_dialogs.h"
     14 #include "chrome/browser/ui/views/location_bar/location_bar_view.h"
     15 #include "grit/generated_resources.h"
     16 #include "grit/theme_resources.h"
     17 #include "ui/base/accessibility/accessible_view_state.h"
     18 #include "ui/base/events/event.h"
     19 #include "ui/base/l10n/l10n_util.h"
     20 #include "ui/base/resource/resource_bundle.h"
     21 
     22 StarView::StarView(CommandUpdater* command_updater)
     23     : command_updater_(command_updater),
     24       suppress_mouse_released_action_(false) {
     25   set_id(VIEW_ID_STAR_BUTTON);
     26   SetToggled(false);
     27   set_accessibility_focusable(true);
     28   LocationBarView::InitTouchableLocationBarChildView(this);
     29 }
     30 
     31 StarView::~StarView() {
     32 }
     33 
     34 void StarView::SetToggled(bool on) {
     35   SetTooltipText(l10n_util::GetStringUTF16(
     36       on ? IDS_TOOLTIP_STARRED : IDS_TOOLTIP_STAR));
     37   SetImage(ui::ResourceBundle::GetSharedInstance().GetImageSkiaNamed(
     38       on ? IDR_STAR_LIT : IDR_STAR));
     39 }
     40 
     41 void StarView::GetAccessibleState(ui::AccessibleViewState* state) {
     42   ImageView::GetAccessibleState(state);
     43   state->role = ui::AccessibilityTypes::ROLE_PUSHBUTTON;
     44 }
     45 
     46 bool StarView::GetTooltipText(const gfx::Point& p, string16* tooltip) const {
     47   // Don't show tooltip to distract user if BookmarkBubbleView is showing.
     48   if (chrome::IsBookmarkBubbleViewShowing())
     49     return false;
     50 
     51   return views::ImageView::GetTooltipText(p, tooltip);
     52 }
     53 
     54 bool StarView::OnMousePressed(const ui::MouseEvent& event) {
     55   // If the bookmark bubble is showing then don't reshow it when the mouse is
     56   // released.
     57   suppress_mouse_released_action_ = chrome::IsBookmarkBubbleViewShowing();
     58 
     59   // We want to show the bubble on mouse release; that is the standard behavior
     60   // for buttons.
     61   return true;
     62 }
     63 
     64 void StarView::OnMouseReleased(const ui::MouseEvent& event) {
     65   // If this is the second click on this view then the bookmark bubble was
     66   // showing on the mouse pressed event and is hidden now. Prevent the bubble
     67   // from reshowing by doing nothing here.
     68   if (suppress_mouse_released_action_) {
     69     suppress_mouse_released_action_ = false;
     70     return;
     71   }
     72 
     73   if (event.IsOnlyLeftMouseButton() && HitTestPoint(event.location())) {
     74     UMA_HISTOGRAM_ENUMERATION("Bookmarks.EntryPoint",
     75                               bookmark_utils::ENTRY_POINT_STAR_MOUSE,
     76                               bookmark_utils::ENTRY_POINT_LIMIT);
     77     command_updater_->ExecuteCommand(IDC_BOOKMARK_PAGE_FROM_STAR);
     78   }
     79 }
     80 
     81 bool StarView::OnKeyPressed(const ui::KeyEvent& event) {
     82   if (event.key_code() == ui::VKEY_SPACE ||
     83       event.key_code() == ui::VKEY_RETURN) {
     84     UMA_HISTOGRAM_ENUMERATION("Bookmarks.EntryPoint",
     85                               bookmark_utils::ENTRY_POINT_STAR_KEY,
     86                               bookmark_utils::ENTRY_POINT_LIMIT);
     87     command_updater_->ExecuteCommand(IDC_BOOKMARK_PAGE_FROM_STAR);
     88     return true;
     89   }
     90   return false;
     91 }
     92 
     93 void StarView::OnGestureEvent(ui::GestureEvent* event) {
     94   if (event->type() == ui::ET_GESTURE_TAP) {
     95     UMA_HISTOGRAM_ENUMERATION("Bookmarks.EntryPoint",
     96                               bookmark_utils::ENTRY_POINT_STAR_GESTURE,
     97                               bookmark_utils::ENTRY_POINT_LIMIT);
     98     command_updater_->ExecuteCommand(IDC_BOOKMARK_PAGE_FROM_STAR);
     99     event->SetHandled();
    100   }
    101 }
    102