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