Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(34)

Side by Side Diff: views/widget/tooltip_manager_views.cc

Issue 8371024: Enable tooltips for aura. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: minor changes Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« ui/aura/window.h ('K') | « views/widget/tooltip_manager_views.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "views/widget/tooltip_manager_views.h" 5 #include "views/widget/tooltip_manager_views.h"
6 6
7 #if defined(USE_X11) 7 #if defined(USE_X11)
8 #include <X11/Xlib.h> 8 #include <X11/Xlib.h>
9 #include <X11/extensions/XInput2.h> 9 #include <X11/extensions/XInput2.h>
10 #endif 10 #endif
11 11
12 #if defined(OS_WIN) 12 #if defined(OS_WIN)
13 #include <windowsx.h> 13 #include <windowsx.h>
14 #endif 14 #endif
15 15
16 #include "base/logging.h" 16 #include "base/logging.h"
17 #include "base/time.h" 17 #include "base/time.h"
18 #include "base/utf_string_conversions.h" 18 #include "base/utf_string_conversions.h"
19 #include "third_party/skia/include/core/SkColor.h" 19 #include "third_party/skia/include/core/SkColor.h"
20 #if defined(USE_AURA)
21 #include "ui/aura/desktop.h"
22 #include "ui/aura/event.h"
23 #include "ui/aura/window.h"
24 #endif
20 #include "ui/base/resource/resource_bundle.h" 25 #include "ui/base/resource/resource_bundle.h"
21 #include "ui/gfx/font.h" 26 #include "ui/gfx/font.h"
22 #include "ui/gfx/screen.h" 27 #include "ui/gfx/screen.h"
23 #include "views/background.h" 28 #include "views/background.h"
24 #include "views/border.h" 29 #include "views/border.h"
25 #include "views/events/event.h" 30 #include "views/events/event.h"
26 #include "views/focus/focus_manager.h" 31 #include "views/focus/focus_manager.h"
27 #include "views/view.h" 32 #include "views/view.h"
28 #include "views/widget/native_widget.h" 33 #include "views/widget/native_widget.h"
29 #include "views/widget/root_view.h"
30 34
31 #if defined(USE_WAYLAND) 35 #if defined(USE_WAYLAND)
32 #include "ui/wayland/events/wayland_event.h" 36 #include "ui/wayland/events/wayland_event.h"
33 #endif 37 #endif
34 38
35 namespace { 39 namespace {
36 SkColor kTooltipBackground = 0xFF7F7F00; 40 SkColor kTooltipBackground = 0xFF7F7F00;
37 int kTooltipTimeoutMs = 500; 41 int kTooltipTimeoutMs = 500;
38 42
39 // FIXME: get cursor offset from actual cursor size. 43 // FIXME: get cursor offset from actual cursor size.
(...skipping 22 matching lines...) Expand all
62 // We always display the tooltip inside the root view. So the max width is 66 // We always display the tooltip inside the root view. So the max width is
63 // the width of the view. 67 // the width of the view.
64 gfx::Rect monitor_bounds = 68 gfx::Rect monitor_bounds =
65 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y)); 69 gfx::Screen::GetMonitorAreaNearestPoint(gfx::Point(x, y));
66 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror 70 // GtkLabel (gtk_label_ensure_layout) forces wrapping at this size. We mirror
67 // the size here otherwise tooltips wider than the size used by gtklabel end 71 // the size here otherwise tooltips wider than the size used by gtklabel end
68 // up with extraneous empty lines. 72 // up with extraneous empty lines.
69 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2; 73 return monitor_bounds.width() == 0 ? 800 : (monitor_bounds.width() + 1) / 2;
70 } 74 }
71 75
72 TooltipManagerViews::TooltipManagerViews(internal::RootView* root_view) 76 TooltipManagerViews::TooltipManagerViews(views::View* root_view)
73 : root_view_(root_view), 77 : root_view_(root_view),
74 tooltip_view_(NULL) { 78 tooltip_view_(NULL) {
75 tooltip_label_.set_background( 79 tooltip_label_.set_background(
76 views::Background::CreateSolidBackground(kTooltipBackground)); 80 views::Background::CreateSolidBackground(kTooltipBackground));
77 tooltip_widget_.reset(CreateTooltip()); 81 tooltip_widget_.reset(CreateTooltip());
78 tooltip_widget_->SetContentsView(&tooltip_label_); 82 tooltip_widget_->SetContentsView(&tooltip_label_);
79 tooltip_widget_->Activate(); 83 tooltip_widget_->Activate();
80 tooltip_widget_->SetAlwaysOnTop(true); 84 tooltip_widget_->SetAlwaysOnTop(true);
81 tooltip_timer_.Start(FROM_HERE, 85 tooltip_timer_.Start(FROM_HERE,
82 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), 86 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs),
(...skipping 25 matching lines...) Expand all
108 112
109 #if defined(USE_WAYLAND) 113 #if defined(USE_WAYLAND)
110 base::MessagePumpObserver::EventStatus TooltipManagerViews::WillProcessEvent( 114 base::MessagePumpObserver::EventStatus TooltipManagerViews::WillProcessEvent(
111 ui::WaylandEvent* event) { 115 ui::WaylandEvent* event) {
112 if (event->type == ui::WAYLAND_MOTION) 116 if (event->type == ui::WAYLAND_MOTION)
113 OnMouseMoved(event->motion.x, event->motion.y); 117 OnMouseMoved(event->motion.x, event->motion.y);
114 return base::MessagePumpObserver::EVENT_CONTINUE; 118 return base::MessagePumpObserver::EVENT_CONTINUE;
115 } 119 }
116 #elif defined(USE_X11) 120 #elif defined(USE_X11)
117 base::EventStatus TooltipManagerViews::WillProcessEvent( 121 base::EventStatus TooltipManagerViews::WillProcessEvent(
118 const base::NativeEvent& event) { 122 const base::NativeEvent& native_event) {
119 XGenericEventCookie* cookie = &event->xcookie; 123 if (!ui::IsMouseEvent(native_event))
120 if (cookie->evtype == XI_Motion) { 124 return base::EVENT_CONTINUE;
121 const XIDeviceEvent* xievent = static_cast<XIDeviceEvent*>(cookie->data); 125 #if defined(USE_AURA)
122 OnMouseMoved(static_cast<int>(xievent->event_x), 126 aura::MouseEvent event(native_event);
123 static_cast<int>(xievent->event_y)); 127 #else
128 MouseEvent event(native_event);
129 #endif
130 switch (event.type()) {
131 case ui::ET_MOUSE_MOVED:
132 OnMouseMoved(event.x(), event.y());
133 default:
134 break;
124 } 135 }
125 return base::EVENT_CONTINUE; 136 return base::EVENT_CONTINUE;
126 } 137 }
127 138
128 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { 139 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) {
129 } 140 }
130 #elif defined(OS_WIN) 141 #elif defined(OS_WIN)
131 base::EventStatus TooltipManagerViews::WillProcessEvent( 142 base::EventStatus TooltipManagerViews::WillProcessEvent(
132 const base::NativeEvent& event) { 143 const base::NativeEvent& event) {
133 if (event.message == WM_MOUSEMOVE) 144 if (event.message == WM_MOUSEMOVE)
134 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam)); 145 OnMouseMoved(GET_X_LPARAM(event.lParam), GET_Y_LPARAM(event.lParam));
135 return base::EVENT_CONTINUE; 146 return base::EVENT_CONTINUE;
136 } 147 }
137 148
138 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) { 149 void TooltipManagerViews::DidProcessEvent(const base::NativeEvent& event) {
139 } 150 }
140 #endif 151 #endif
141 152
142 void TooltipManagerViews::TooltipTimerFired() { 153 void TooltipManagerViews::TooltipTimerFired() {
143 if (tooltip_widget_->IsVisible()) { 154 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
144 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
145 } else {
146 tooltip_view_ = GetViewForTooltip(curr_mouse_pos_.x(), curr_mouse_pos_.y(),
147 false);
148 Update();
149 }
150 } 155 }
151 156
152 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) { 157 View* TooltipManagerViews::GetViewForTooltip(int x, int y, bool for_keyboard) {
153 View* view = NULL; 158 View* view = NULL;
154 if (!for_keyboard) { 159 if (!for_keyboard) {
155 view = root_view_->GetEventHandlerForPoint(gfx::Point(x, y)); 160 view = root_view_->GetEventHandlerForPoint(gfx::Point(x, y));
156 } else { 161 } else {
157 FocusManager* focus_manager = root_view_->GetFocusManager(); 162 FocusManager* focus_manager = root_view_->GetFocusManager();
158 if (focus_manager) 163 if (focus_manager)
159 view = focus_manager->GetFocusedView(); 164 view = focus_manager->GetFocusedView();
160 } 165 }
161 return view; 166 return view;
162 } 167 }
163 168
164 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) { 169 void TooltipManagerViews::UpdateIfRequired(int x, int y, bool for_keyboard) {
165 View* view = GetViewForTooltip(x, y, for_keyboard); 170 View* view = GetViewForTooltip(x, y, for_keyboard);
166 string16 tooltip_text; 171 string16 tooltip_text;
167 if (view) 172 if (view)
168 view->GetTooltipText(gfx::Point(x, y), &tooltip_text); 173 view->GetTooltipText(gfx::Point(x, y), &tooltip_text);
169 174
175 #if defined(USE_AURA)
176 // In aura, and aura::Window can also have a tooltip. If the view doesnot have
177 // a tooltip, we must also check for the aura::Window underneath the cursor.
178 if (tooltip_text.empty()) {
179 aura::Window* root = reinterpret_cast<aura::Window*>(
180 root_view_->GetWidget()->GetNativeView());
181 aura::Window* window = root->GetEventHandlerForPoint(gfx::Point(x, y));
182 if (window)
183 tooltip_text = window->tooltip();
184 }
185 #endif
186
170 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) { 187 if (tooltip_view_ != view || tooltip_text_ != tooltip_text) {
171 tooltip_view_ = view; 188 tooltip_view_ = view;
189 tooltip_text_ = tooltip_text;
172 Update(); 190 Update();
173 } 191 }
174 } 192 }
175 193
176 void TooltipManagerViews::Update() { 194 void TooltipManagerViews::Update() {
177 if (!tooltip_view_ ||
178 !tooltip_view_->GetTooltipText(gfx::Point(), &tooltip_text_))
179 tooltip_text_.clear();
180
181 if (tooltip_text_.empty()) { 195 if (tooltip_text_.empty()) {
182 tooltip_widget_->Hide(); 196 tooltip_widget_->Hide();
183 } else { 197 } else {
184 int max_width, line_count; 198 int max_width, line_count;
185 string16 tooltip_text(tooltip_text_); 199 string16 tooltip_text(tooltip_text_);
186 TrimTooltipToFit(&tooltip_text, &max_width, &line_count, 200 TrimTooltipToFit(&tooltip_text, &max_width, &line_count,
187 curr_mouse_pos_.x(), curr_mouse_pos_.y()); 201 curr_mouse_pos_.x(), curr_mouse_pos_.y());
188 tooltip_label_.SetText(tooltip_text); 202 tooltip_label_.SetText(tooltip_text);
189 203
190 SetTooltipBounds(curr_mouse_pos_, max_width, 204 SetTooltipBounds(curr_mouse_pos_, max_width,
(...skipping 11 matching lines...) Expand all
202 216
203 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY); 217 tooltip_rect.Offset(kCursorOffsetX, kCursorOffsetY);
204 gfx::Rect monitor_bounds = 218 gfx::Rect monitor_bounds =
205 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin()); 219 gfx::Screen::GetMonitorAreaNearestPoint(tooltip_rect.origin());
206 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds)); 220 tooltip_widget_->SetBounds(tooltip_rect.AdjustToFit(monitor_bounds));
207 } 221 }
208 222
209 Widget* TooltipManagerViews::CreateTooltip() { 223 Widget* TooltipManagerViews::CreateTooltip() {
210 Widget* widget = new Widget; 224 Widget* widget = new Widget;
211 Widget::InitParams params; 225 Widget::InitParams params;
226 // For aura, since we set the type to TOOLTIP_TYPE, the widget will get
227 // auto-parented to the MenuAndTooltipsContainer.
212 params.type = Widget::InitParams::TYPE_TOOLTIP; 228 params.type = Widget::InitParams::TYPE_TOOLTIP;
213 params.keep_on_top = true; 229 params.keep_on_top = true;
214 params.accept_events = false; 230 params.accept_events = false;
215 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET; 231 params.ownership = Widget::InitParams::WIDGET_OWNS_NATIVE_WIDGET;
216 widget->Init(params); 232 widget->Init(params);
217 widget->SetOpacity(0x00); 233 widget->SetOpacity(0x00);
218 return widget; 234 return widget;
219 } 235 }
220 236
221 void TooltipManagerViews::OnMouseMoved(int x, int y) { 237 void TooltipManagerViews::OnMouseMoved(int x, int y) {
222 if (tooltip_timer_.IsRunning()) 238 if (tooltip_timer_.IsRunning())
223 tooltip_timer_.Reset(); 239 tooltip_timer_.Reset();
224 curr_mouse_pos_.SetPoint(x, y); 240 curr_mouse_pos_.SetPoint(x, y);
225 241
226 // If tooltip is visible, we may want to hide it. If it is not, we are ok. 242 // If tooltip is visible, we may want to hide it. If it is not, we are ok.
227 if (tooltip_widget_->IsVisible()) 243 if (tooltip_widget_->IsVisible())
228 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false); 244 UpdateIfRequired(curr_mouse_pos_.x(), curr_mouse_pos_.y(), false);
229 } 245 }
230 246
231 } // namespace views 247 } // namespace views
OLDNEW
« ui/aura/window.h ('K') | « views/widget/tooltip_manager_views.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698