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/autofill/tooltip_icon.h"
7 #include "base/basictypes.h"
8 #include "base/timer/timer.h"
9 #include "chrome/browser/ui/views/autofill/info_bubble.h"
10 #include "grit/theme_resources.h"
11 #include "ui/accessibility/ax_view_state.h"
12 #include "ui/base/resource/resource_bundle.h"
13 #include "ui/views/bubble/bubble_frame_view.h"
14 #include "ui/views/mouse_watcher_view_host.h"
15 #include "ui/views/painter.h"
21 gfx::Insets
GetPreferredInsets(const views::View
* view
) {
22 gfx::Size pref_size
= view
->GetPreferredSize();
23 gfx::Rect local_bounds
= view
->GetLocalBounds();
24 gfx::Point origin
= local_bounds
.CenterPoint();
25 origin
.Offset(-pref_size
.width() / 2, -pref_size
.height() / 2);
26 return gfx::Insets(origin
.y(),
28 local_bounds
.bottom() - (origin
.y() + pref_size
.height()),
29 local_bounds
.right() - (origin
.x() + pref_size
.width()));
32 // An info bubble with some extra positioning magic for tooltip icons.
33 class TooltipBubble
: public InfoBubble
{
35 TooltipBubble(views::View
* anchor
, const base::string16
& message
)
36 : InfoBubble(anchor
, message
) {}
37 ~TooltipBubble() override
{}
41 gfx::Rect
GetAnchorRect() const override
{
42 gfx::Rect bounds
= views::BubbleDelegateView::GetAnchorRect();
43 bounds
.Inset(GetPreferredInsets(anchor()));
48 DISALLOW_COPY_AND_ASSIGN(TooltipBubble
);
53 TooltipIcon::TooltipIcon(const base::string16
& tooltip
)
57 bubble_arrow_(views::BubbleBorder::TOP_RIGHT
),
59 ChangeImageTo(IDR_AUTOFILL_TOOLTIP_ICON
);
62 TooltipIcon::~TooltipIcon() {
67 const char TooltipIcon::kViewClassName
[] = "autofill/TooltipIcon";
69 const char* TooltipIcon::GetClassName() const {
70 return TooltipIcon::kViewClassName
;
73 void TooltipIcon::OnMouseEntered(const ui::MouseEvent
& event
) {
75 show_timer_
.Start(FROM_HERE
, base::TimeDelta::FromMilliseconds(150), this,
76 &TooltipIcon::ShowBubble
);
79 void TooltipIcon::OnMouseExited(const ui::MouseEvent
& event
) {
83 void TooltipIcon::OnGestureEvent(ui::GestureEvent
* event
) {
84 if (event
->type() == ui::ET_GESTURE_TAP
) {
90 void TooltipIcon::GetAccessibleState(ui::AXViewState
* state
) {
91 state
->name
= tooltip_
;
94 void TooltipIcon::MouseMovedOutOfHost() {
95 if (IsMouseHovered()) {
96 mouse_watcher_
->Start();
100 mouse_inside_
= false;
104 void TooltipIcon::ChangeImageTo(int idr
) {
105 ui::ResourceBundle
& rb
= ui::ResourceBundle::GetSharedInstance();
106 SetImage(rb
.GetImageNamed(idr
).ToImageSkia());
109 void TooltipIcon::ShowBubble() {
113 ChangeImageTo(IDR_AUTOFILL_TOOLTIP_ICON_H
);
115 bubble_
= new TooltipBubble(this, tooltip_
);
116 bubble_
->set_arrow(bubble_arrow_
);
117 // When shown due to a gesture event, close on deactivate (i.e. don't use
119 bubble_
->set_can_activate(!mouse_inside_
);
122 observer_
.Add(bubble_
->GetWidget());
125 views::View
* frame
= bubble_
->GetWidget()->non_client_view()->frame_view();
126 scoped_ptr
<views::MouseWatcherHost
> host(
127 new views::MouseWatcherViewHost(frame
, gfx::Insets()));
128 mouse_watcher_
.reset(new views::MouseWatcher(host
.release(), this));
129 mouse_watcher_
->Start();
133 void TooltipIcon::HideBubble() {
138 void TooltipIcon::OnWidgetDestroyed(views::Widget
* widget
) {
139 observer_
.Remove(widget
);
141 ChangeImageTo(IDR_AUTOFILL_TOOLTIP_ICON
);
142 mouse_watcher_
.reset();
146 } // namespace autofill