OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 "ash/tooltips/tooltip_controller.h" | 5 #include "ash/tooltips/tooltip_controller.h" |
6 | 6 |
7 #include <vector> | 7 #include <vector> |
8 | 8 |
9 #include "ash/ash_switches.h" | 9 #include "ash/ash_switches.h" |
10 #include "ash/shell.h" | 10 #include "ash/shell.h" |
(...skipping 167 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 | 178 |
179 //////////////////////////////////////////////////////////////////////////////// | 179 //////////////////////////////////////////////////////////////////////////////// |
180 // TooltipController public: | 180 // TooltipController public: |
181 | 181 |
182 TooltipController::TooltipController( | 182 TooltipController::TooltipController( |
183 aura::client::DragDropClient* drag_drop_client) | 183 aura::client::DragDropClient* drag_drop_client) |
184 : drag_drop_client_(drag_drop_client), | 184 : drag_drop_client_(drag_drop_client), |
185 tooltip_window_(NULL), | 185 tooltip_window_(NULL), |
186 tooltip_window_at_mouse_press_(NULL), | 186 tooltip_window_at_mouse_press_(NULL), |
187 mouse_pressed_(false), | 187 mouse_pressed_(false), |
188 tooltip_(new Tooltip), | |
189 tooltips_enabled_(true) { | 188 tooltips_enabled_(true) { |
190 tooltip_timer_.Start(FROM_HERE, | 189 tooltip_timer_.Start(FROM_HERE, |
191 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | 190 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
192 this, &TooltipController::TooltipTimerFired); | 191 this, &TooltipController::TooltipTimerFired); |
193 DCHECK(drag_drop_client_); | 192 DCHECK(drag_drop_client_); |
194 } | 193 } |
195 | 194 |
196 TooltipController::~TooltipController() { | 195 TooltipController::~TooltipController() { |
197 if (tooltip_window_) | 196 if (tooltip_window_) |
198 tooltip_window_->RemoveObserver(this); | 197 tooltip_window_->RemoveObserver(this); |
199 } | 198 } |
200 | 199 |
200 TooltipController::Tooltip* TooltipController::GetTooltip() { | |
sky
2012/07/25 15:02:43
Make position match header.
oshima
2012/07/25 15:17:26
Done.
| |
201 if (!tooltip_.get()) | |
202 tooltip_.reset(new Tooltip); | |
203 return tooltip_.get(); | |
204 } | |
205 | |
201 void TooltipController::UpdateTooltip(aura::Window* target) { | 206 void TooltipController::UpdateTooltip(aura::Window* target) { |
202 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | 207 // If tooltip is visible, we may want to hide it. If it is not, we are ok. |
203 if (tooltip_window_ == target && tooltip_->IsVisible()) | 208 if (tooltip_window_ == target && GetTooltip()->IsVisible()) |
204 UpdateIfRequired(); | 209 UpdateIfRequired(); |
205 | 210 |
206 // If we had stopped the tooltip timer for some reason, we must restart it if | 211 // If we had stopped the tooltip timer for some reason, we must restart it if |
207 // there is a change in the tooltip. | 212 // there is a change in the tooltip. |
208 if (!tooltip_timer_.IsRunning()) { | 213 if (!tooltip_timer_.IsRunning()) { |
209 if (tooltip_window_ != target || (tooltip_window_ && | 214 if (tooltip_window_ != target || (tooltip_window_ && |
210 tooltip_text_ != aura::client::GetTooltipText(tooltip_window_))) { | 215 tooltip_text_ != aura::client::GetTooltipText(tooltip_window_))) { |
211 tooltip_timer_.Start(FROM_HERE, | 216 tooltip_timer_.Start(FROM_HERE, |
212 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | 217 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), |
213 this, &TooltipController::TooltipTimerFired); | 218 this, &TooltipController::TooltipTimerFired); |
(...skipping 30 matching lines...) Expand all Loading... | |
244 tooltip_window_->RemoveObserver(this); | 249 tooltip_window_->RemoveObserver(this); |
245 tooltip_window_ = target; | 250 tooltip_window_ = target; |
246 tooltip_window_->AddObserver(this); | 251 tooltip_window_->AddObserver(this); |
247 } | 252 } |
248 curr_mouse_loc_ = event->location(); | 253 curr_mouse_loc_ = event->location(); |
249 if (tooltip_timer_.IsRunning()) | 254 if (tooltip_timer_.IsRunning()) |
250 tooltip_timer_.Reset(); | 255 tooltip_timer_.Reset(); |
251 | 256 |
252 // We update the tooltip if it is visible, or if we force-hid it due to a | 257 // We update the tooltip if it is visible, or if we force-hid it due to a |
253 // mouse press. | 258 // mouse press. |
254 if (tooltip_->IsVisible() || tooltip_window_at_mouse_press_) | 259 if (GetTooltip()->IsVisible() || tooltip_window_at_mouse_press_) |
255 UpdateIfRequired(); | 260 UpdateIfRequired(); |
256 break; | 261 break; |
257 case ui::ET_MOUSE_PRESSED: | 262 case ui::ET_MOUSE_PRESSED: |
258 mouse_pressed_ = true; | 263 mouse_pressed_ = true; |
259 tooltip_window_at_mouse_press_ = target; | 264 tooltip_window_at_mouse_press_ = target; |
260 if (target) | 265 if (target) |
261 tooltip_text_at_mouse_press_ = aura::client::GetTooltipText(target); | 266 tooltip_text_at_mouse_press_ = aura::client::GetTooltipText(target); |
262 tooltip_->Hide(); | 267 GetTooltip()->Hide(); |
263 break; | 268 break; |
264 case ui::ET_MOUSE_RELEASED: | 269 case ui::ET_MOUSE_RELEASED: |
265 mouse_pressed_ = false; | 270 mouse_pressed_ = false; |
266 break; | 271 break; |
267 case ui::ET_MOUSE_CAPTURE_CHANGED: | 272 case ui::ET_MOUSE_CAPTURE_CHANGED: |
268 // We will not received a mouse release, so reset mouse pressed state. | 273 // We will not received a mouse release, so reset mouse pressed state. |
269 mouse_pressed_ = false; | 274 mouse_pressed_ = false; |
270 case ui::ET_MOUSEWHEEL: | 275 case ui::ET_MOUSEWHEEL: |
271 // Hide the tooltip for click, release, drag, wheel events. | 276 // Hide the tooltip for click, release, drag, wheel events. |
272 if (tooltip_->IsVisible()) | 277 if (GetTooltip()->IsVisible()) |
273 tooltip_->Hide(); | 278 GetTooltip()->Hide(); |
274 break; | 279 break; |
275 default: | 280 default: |
276 break; | 281 break; |
277 } | 282 } |
278 return false; | 283 return false; |
279 } | 284 } |
280 | 285 |
281 ui::TouchStatus TooltipController::PreHandleTouchEvent( | 286 ui::TouchStatus TooltipController::PreHandleTouchEvent( |
282 aura::Window* target, | 287 aura::Window* target, |
283 aura::TouchEvent* event) { | 288 aura::TouchEvent* event) { |
284 // TODO(varunjain): need to properly implement tooltips for | 289 // TODO(varunjain): need to properly implement tooltips for |
285 // touch events. | 290 // touch events. |
286 // Hide the tooltip for touch events. | 291 // Hide the tooltip for touch events. |
287 if (tooltip_->IsVisible()) | 292 if (GetTooltip()->IsVisible()) |
288 tooltip_->Hide(); | 293 GetTooltip()->Hide(); |
289 if (tooltip_window_) | 294 if (tooltip_window_) |
290 tooltip_window_->RemoveObserver(this); | 295 tooltip_window_->RemoveObserver(this); |
291 tooltip_window_ = NULL; | 296 tooltip_window_ = NULL; |
292 return ui::TOUCH_STATUS_UNKNOWN; | 297 return ui::TOUCH_STATUS_UNKNOWN; |
293 } | 298 } |
294 | 299 |
295 ui::GestureStatus TooltipController::PreHandleGestureEvent( | 300 ui::GestureStatus TooltipController::PreHandleGestureEvent( |
296 aura::Window* target, | 301 aura::Window* target, |
297 aura::GestureEvent* event) { | 302 aura::GestureEvent* event) { |
298 return ui::GESTURE_STATUS_UNKNOWN; | 303 return ui::GESTURE_STATUS_UNKNOWN; |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
387 } | 392 } |
388 } | 393 } |
389 *text = result; | 394 *text = result; |
390 } | 395 } |
391 | 396 |
392 void TooltipController::TooltipTimerFired() { | 397 void TooltipController::TooltipTimerFired() { |
393 UpdateIfRequired(); | 398 UpdateIfRequired(); |
394 } | 399 } |
395 | 400 |
396 void TooltipController::TooltipShownTimerFired() { | 401 void TooltipController::TooltipShownTimerFired() { |
397 tooltip_->Hide(); | 402 GetTooltip()->Hide(); |
398 | 403 |
399 // Since the user presumably no longer needs the tooltip, we also stop the | 404 // Since the user presumably no longer needs the tooltip, we also stop the |
400 // tooltip timer so that tooltip does not pop back up. We will restart this | 405 // tooltip timer so that tooltip does not pop back up. We will restart this |
401 // timer if the tooltip changes (see UpdateTooltip()). | 406 // timer if the tooltip changes (see UpdateTooltip()). |
402 tooltip_timer_.Stop(); | 407 tooltip_timer_.Stop(); |
403 } | 408 } |
404 | 409 |
405 void TooltipController::UpdateIfRequired() { | 410 void TooltipController::UpdateIfRequired() { |
406 if (!tooltips_enabled_ || mouse_pressed_ || IsDragDropInProgress() || | 411 if (!tooltips_enabled_ || mouse_pressed_ || IsDragDropInProgress() || |
407 !aura::Env::GetInstance()->cursor_manager()->cursor_visible()) { | 412 !aura::Env::GetInstance()->cursor_manager()->cursor_visible()) { |
408 tooltip_->Hide(); | 413 GetTooltip()->Hide(); |
409 return; | 414 return; |
410 } | 415 } |
411 | 416 |
412 string16 tooltip_text; | 417 string16 tooltip_text; |
413 if (tooltip_window_) | 418 if (tooltip_window_) |
414 tooltip_text = aura::client::GetTooltipText(tooltip_window_); | 419 tooltip_text = aura::client::GetTooltipText(tooltip_window_); |
415 | 420 |
416 // If the user pressed a mouse button. We will hide the tooltip and not show | 421 // If the user pressed a mouse button. We will hide the tooltip and not show |
417 // it until there is a change in the tooltip. | 422 // it until there is a change in the tooltip. |
418 if (tooltip_window_at_mouse_press_) { | 423 if (tooltip_window_at_mouse_press_) { |
419 if (tooltip_window_ == tooltip_window_at_mouse_press_ && | 424 if (tooltip_window_ == tooltip_window_at_mouse_press_ && |
420 tooltip_text == tooltip_text_at_mouse_press_) { | 425 tooltip_text == tooltip_text_at_mouse_press_) { |
421 tooltip_->Hide(); | 426 GetTooltip()->Hide(); |
422 return; | 427 return; |
423 } | 428 } |
424 tooltip_window_at_mouse_press_ = NULL; | 429 tooltip_window_at_mouse_press_ = NULL; |
425 } | 430 } |
426 | 431 |
427 // We add the !tooltip_->IsVisible() below because when we come here from | 432 // We add the !GetTooltip()->IsVisible() below because when we come here from |
428 // TooltipTimerFired(), the tooltip_text may not have changed but we still | 433 // TooltipTimerFired(), the tooltip_text may not have changed but we still |
429 // want to update the tooltip because the timer has fired. | 434 // want to update the tooltip because the timer has fired. |
430 // If we come here from UpdateTooltip(), we have already checked for tooltip | 435 // If we come here from UpdateTooltip(), we have already checked for tooltip |
431 // visibility and this check below will have no effect. | 436 // visibility and this check below will have no effect. |
432 if (tooltip_text_ != tooltip_text || !tooltip_->IsVisible()) { | 437 if (tooltip_text_ != tooltip_text || !GetTooltip()->IsVisible()) { |
433 tooltip_shown_timer_.Stop(); | 438 tooltip_shown_timer_.Stop(); |
434 tooltip_text_ = tooltip_text; | 439 tooltip_text_ = tooltip_text; |
435 if (tooltip_text_.empty()) { | 440 if (tooltip_text_.empty()) { |
436 tooltip_->Hide(); | 441 GetTooltip()->Hide(); |
437 } else { | 442 } else { |
438 string16 tooltip_text(tooltip_text_); | 443 string16 tooltip_text(tooltip_text_); |
439 gfx::Point widget_loc = curr_mouse_loc_; | 444 gfx::Point widget_loc = curr_mouse_loc_; |
440 widget_loc = widget_loc.Add( | 445 widget_loc = widget_loc.Add( |
441 tooltip_window_->GetBoundsInScreen().origin()); | 446 tooltip_window_->GetBoundsInScreen().origin()); |
442 tooltip_->SetText(tooltip_text, widget_loc); | 447 GetTooltip()->SetText(tooltip_text, widget_loc); |
443 tooltip_->Show(); | 448 GetTooltip()->Show(); |
444 tooltip_shown_timer_.Start(FROM_HERE, | 449 tooltip_shown_timer_.Start(FROM_HERE, |
445 base::TimeDelta::FromMilliseconds(kTooltipShownTimeoutMs), | 450 base::TimeDelta::FromMilliseconds(kTooltipShownTimeoutMs), |
446 this, &TooltipController::TooltipShownTimerFired); | 451 this, &TooltipController::TooltipShownTimerFired); |
447 } | 452 } |
448 } | 453 } |
449 } | 454 } |
450 | 455 |
451 bool TooltipController::IsTooltipVisible() { | 456 bool TooltipController::IsTooltipVisible() { |
452 return tooltip_->IsVisible(); | 457 return GetTooltip()->IsVisible(); |
453 } | 458 } |
454 | 459 |
455 bool TooltipController::IsDragDropInProgress() { | 460 bool TooltipController::IsDragDropInProgress() { |
456 return drag_drop_client_->IsDragDropInProgress(); | 461 return drag_drop_client_->IsDragDropInProgress(); |
457 } | 462 } |
458 | 463 |
459 } // namespace internal | 464 } // namespace internal |
460 } // namespace ash | 465 } // namespace ash |
OLD | NEW |