1 // Copyright 2013 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/bubble_icon_view.h"
7 #include "chrome/browser/command_updater.h"
8 #include "ui/accessibility/ax_view_state.h"
9 #include "ui/events/event.h"
10 #include "ui/views/bubble/bubble_delegate.h"
12 BubbleIconView::BubbleIconView(CommandUpdater
* command_updater
, int command_id
)
13 : command_updater_(command_updater
),
14 command_id_(command_id
),
15 suppress_mouse_released_action_(false) {
16 SetAccessibilityFocusable(true);
19 BubbleIconView::~BubbleIconView() {
22 bool BubbleIconView::IsBubbleShowing() const {
23 // If the bubble is being destroyed, it's considered showing though it may be
24 // already invisible currently.
25 return GetBubble() != NULL
;
28 void BubbleIconView::GetAccessibleState(ui::AXViewState
* state
) {
29 views::ImageView::GetAccessibleState(state
);
30 state
->role
= ui::AX_ROLE_BUTTON
;
33 bool BubbleIconView::GetTooltipText(const gfx::Point
& p
,
34 base::string16
* tooltip
) const {
35 if (IsBubbleShowing())
38 return views::ImageView::GetTooltipText(p
, tooltip
);
41 bool BubbleIconView::OnMousePressed(const ui::MouseEvent
& event
) {
42 // If the bubble is showing then don't reshow it when the mouse is released.
43 suppress_mouse_released_action_
= IsBubbleShowing();
45 // We want to show the bubble on mouse release; that is the standard behavior
50 void BubbleIconView::OnMouseReleased(const ui::MouseEvent
& event
) {
51 // If this is the second click on this view then the bubble was showing on the
52 // mouse pressed event and is hidden now. Prevent the bubble from reshowing by
53 // doing nothing here.
54 if (suppress_mouse_released_action_
) {
55 suppress_mouse_released_action_
= false;
59 if (event
.IsOnlyLeftMouseButton() && HitTestPoint(event
.location()))
60 ExecuteCommand(EXECUTE_SOURCE_MOUSE
);
63 bool BubbleIconView::OnKeyPressed(const ui::KeyEvent
& event
) {
64 if (event
.key_code() == ui::VKEY_SPACE
||
65 event
.key_code() == ui::VKEY_RETURN
) {
66 ExecuteCommand(EXECUTE_SOURCE_KEYBOARD
);
72 void BubbleIconView::OnGestureEvent(ui::GestureEvent
* event
) {
73 if (event
->type() == ui::ET_GESTURE_TAP
) {
74 ExecuteCommand(EXECUTE_SOURCE_GESTURE
);
79 void BubbleIconView::ExecuteCommand(ExecuteSource source
) {
82 command_updater_
->ExecuteCommand(command_id_
);
85 void BubbleIconView::OnBoundsChanged(const gfx::Rect
& previous_bounds
) {
86 views::BubbleDelegateView
* bubble
= GetBubble();
88 bubble
->OnAnchorBoundsChanged();