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