OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/views/autofill/tooltip_icon.h" | 5 #include "ui/views/bubble/tooltip_icon.h" |
6 | 6 |
7 #include "base/macros.h" | |
8 #include "base/timer/timer.h" | 7 #include "base/timer/timer.h" |
9 #include "chrome/browser/ui/views/autofill/info_bubble.h" | |
10 #include "ui/accessibility/ax_node_data.h" | 8 #include "ui/accessibility/ax_node_data.h" |
11 #include "ui/gfx/paint_vector_icon.h" | 9 #include "ui/gfx/paint_vector_icon.h" |
12 #include "ui/gfx/vector_icons_public.h" | 10 #include "ui/gfx/vector_icons_public.h" |
13 #include "ui/views/bubble/bubble_frame_view.h" | 11 #include "ui/views/bubble/bubble_frame_view.h" |
| 12 #include "ui/views/bubble/info_bubble.h" |
14 #include "ui/views/mouse_watcher_view_host.h" | 13 #include "ui/views/mouse_watcher_view_host.h" |
15 #include "ui/views/painter.h" | |
16 | 14 |
17 namespace autofill { | 15 namespace views { |
18 | |
19 namespace { | |
20 | |
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(), | |
27 origin.x(), | |
28 local_bounds.bottom() - (origin.y() + pref_size.height()), | |
29 local_bounds.right() - (origin.x() + pref_size.width())); | |
30 } | |
31 | |
32 // An info bubble with some extra positioning magic for tooltip icons. | |
33 class TooltipBubble : public InfoBubble { | |
34 public: | |
35 TooltipBubble(views::View* anchor, const base::string16& message) | |
36 : InfoBubble(anchor, message) {} | |
37 ~TooltipBubble() override {} | |
38 | |
39 protected: | |
40 // InfoBubble: | |
41 gfx::Rect GetAnchorRect() const override { | |
42 gfx::Rect bounds = views::BubbleDialogDelegateView::GetAnchorRect(); | |
43 bounds.Inset(GetPreferredInsets(anchor())); | |
44 return bounds; | |
45 } | |
46 | |
47 private: | |
48 DISALLOW_COPY_AND_ASSIGN(TooltipBubble); | |
49 }; | |
50 | |
51 } // namespace | |
52 | 16 |
53 TooltipIcon::TooltipIcon(const base::string16& tooltip) | 17 TooltipIcon::TooltipIcon(const base::string16& tooltip) |
54 : tooltip_(tooltip), | 18 : tooltip_(tooltip), |
55 mouse_inside_(false), | 19 mouse_inside_(false), |
56 bubble_(NULL), | 20 bubble_(nullptr), |
57 bubble_arrow_(views::BubbleBorder::TOP_RIGHT), | 21 preferred_width_(0), |
58 observer_(this) { | 22 observer_(this) { |
59 SetDrawAsHovered(false); | 23 SetDrawAsHovered(false); |
60 } | 24 } |
61 | 25 |
62 TooltipIcon::~TooltipIcon() { | 26 TooltipIcon::~TooltipIcon() { |
63 HideBubble(); | 27 HideBubble(); |
64 } | 28 } |
65 | 29 |
66 // static | |
67 const char TooltipIcon::kViewClassName[] = "autofill/TooltipIcon"; | |
68 | |
69 const char* TooltipIcon::GetClassName() const { | 30 const char* TooltipIcon::GetClassName() const { |
70 return TooltipIcon::kViewClassName; | 31 return "TooltipIcon"; |
71 } | 32 } |
72 | 33 |
73 void TooltipIcon::OnMouseEntered(const ui::MouseEvent& event) { | 34 void TooltipIcon::OnMouseEntered(const ui::MouseEvent& event) { |
74 mouse_inside_ = true; | 35 mouse_inside_ = true; |
75 show_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(150), this, | 36 show_timer_.Start(FROM_HERE, base::TimeDelta::FromMilliseconds(150), this, |
76 &TooltipIcon::ShowBubble); | 37 &TooltipIcon::ShowBubble); |
77 } | 38 } |
78 | 39 |
79 void TooltipIcon::OnMouseExited(const ui::MouseEvent& event) { | 40 void TooltipIcon::OnMouseExited(const ui::MouseEvent& event) { |
80 show_timer_.Stop(); | 41 show_timer_.Stop(); |
81 } | 42 } |
82 | 43 |
| 44 bool TooltipIcon::OnMousePressed(const ui::MouseEvent& event) { |
| 45 // Swallow the click so that the parent doesn't process it. |
| 46 return true; |
| 47 } |
| 48 |
83 void TooltipIcon::OnGestureEvent(ui::GestureEvent* event) { | 49 void TooltipIcon::OnGestureEvent(ui::GestureEvent* event) { |
84 if (event->type() == ui::ET_GESTURE_TAP) { | 50 if (event->type() == ui::ET_GESTURE_TAP) { |
85 ShowBubble(); | 51 ShowBubble(); |
86 event->SetHandled(); | 52 event->SetHandled(); |
87 } | 53 } |
88 } | 54 } |
89 | 55 |
90 void TooltipIcon::GetAccessibleNodeData(ui::AXNodeData* node_data) { | 56 void TooltipIcon::GetAccessibleNodeData(ui::AXNodeData* node_data) { |
91 node_data->SetName(tooltip_); | 57 node_data->SetName(tooltip_); |
92 } | 58 } |
93 | 59 |
94 void TooltipIcon::MouseMovedOutOfHost() { | 60 void TooltipIcon::MouseMovedOutOfHost() { |
95 if (IsMouseHovered()) { | 61 if (IsMouseHovered()) { |
96 mouse_watcher_->Start(); | 62 mouse_watcher_->Start(); |
97 return; | 63 return; |
98 } | 64 } |
99 | 65 |
100 mouse_inside_ = false; | 66 mouse_inside_ = false; |
101 HideBubble(); | 67 HideBubble(); |
102 } | 68 } |
103 | 69 |
104 void TooltipIcon::SetDrawAsHovered(bool hovered) { | 70 void TooltipIcon::SetDrawAsHovered(bool hovered) { |
105 SetImage(gfx::CreateVectorIcon(gfx::VectorIconId::HELP_OUTLINE, 18, | 71 SetImage(gfx::CreateVectorIcon(gfx::VectorIconId::INFO_OUTLINE, 18, |
106 hovered | 72 hovered |
107 ? SkColorSetARGB(0xBD, 0, 0, 0) | 73 ? SkColorSetARGB(0xBD, 0, 0, 0) |
108 : SkColorSetARGB(0xBD, 0x44, 0x44, 0x44))); | 74 : SkColorSetARGB(0xBD, 0x44, 0x44, 0x44))); |
109 } | 75 } |
110 | 76 |
111 void TooltipIcon::ShowBubble() { | 77 void TooltipIcon::ShowBubble() { |
112 if (bubble_) | 78 if (bubble_) |
113 return; | 79 return; |
114 | 80 |
115 SetDrawAsHovered(true); | 81 SetDrawAsHovered(true); |
116 | 82 |
117 bubble_ = new TooltipBubble(this, tooltip_); | 83 bubble_ = new InfoBubble(this, tooltip_); |
118 bubble_->set_arrow(bubble_arrow_); | 84 bubble_->set_preferred_width(preferred_width_); |
| 85 bubble_->set_arrow(BubbleBorder::TOP_RIGHT); |
119 // When shown due to a gesture event, close on deactivate (i.e. don't use | 86 // When shown due to a gesture event, close on deactivate (i.e. don't use |
120 // "focusless"). | 87 // "focusless"). |
121 bubble_->set_can_activate(!mouse_inside_); | 88 bubble_->set_can_activate(!mouse_inside_); |
122 | 89 |
123 bubble_->Show(); | 90 bubble_->Show(); |
124 observer_.Add(bubble_->GetWidget()); | 91 observer_.Add(bubble_->GetWidget()); |
125 | 92 |
126 if (mouse_inside_) { | 93 if (mouse_inside_) { |
127 views::View* frame = bubble_->GetWidget()->non_client_view()->frame_view(); | 94 View* frame = bubble_->GetWidget()->non_client_view()->frame_view(); |
128 std::unique_ptr<views::MouseWatcherHost> host( | 95 std::unique_ptr<MouseWatcherHost> host( |
129 new views::MouseWatcherViewHost(frame, gfx::Insets())); | 96 base::MakeUnique<MouseWatcherViewHost>(frame, gfx::Insets())); |
130 mouse_watcher_.reset(new views::MouseWatcher(host.release(), this)); | 97 mouse_watcher_ = base::MakeUnique<MouseWatcher>(host.release(), this); |
131 mouse_watcher_->Start(); | 98 mouse_watcher_->Start(); |
132 } | 99 } |
133 } | 100 } |
134 | 101 |
135 void TooltipIcon::HideBubble() { | 102 void TooltipIcon::HideBubble() { |
136 if (bubble_) | 103 if (bubble_) |
137 bubble_->Hide(); | 104 bubble_->Hide(); |
138 } | 105 } |
139 | 106 |
140 void TooltipIcon::OnWidgetDestroyed(views::Widget* widget) { | 107 void TooltipIcon::OnWidgetDestroyed(Widget* widget) { |
141 observer_.Remove(widget); | 108 observer_.Remove(widget); |
142 | 109 |
143 SetDrawAsHovered(false); | 110 SetDrawAsHovered(false); |
144 mouse_watcher_.reset(); | 111 mouse_watcher_.reset(); |
145 bubble_ = NULL; | 112 bubble_ = nullptr; |
146 } | 113 } |
147 | 114 |
148 } // namespace autofill | 115 } // namespace views |
OLD | NEW |