Chromium Code Reviews| 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 "ui/views/corewm/tooltip_controller.h" | 5 #include "ui/views/corewm/tooltip_controller.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 #include <vector> | 10 #include <vector> |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 23 #include "ui/gfx/font.h" | 23 #include "ui/gfx/font.h" |
| 24 #include "ui/gfx/geometry/rect.h" | 24 #include "ui/gfx/geometry/rect.h" |
| 25 #include "ui/gfx/text_elider.h" | 25 #include "ui/gfx/text_elider.h" |
| 26 #include "ui/views/corewm/tooltip.h" | 26 #include "ui/views/corewm/tooltip.h" |
| 27 #include "ui/views/widget/tooltip_manager.h" | 27 #include "ui/views/widget/tooltip_manager.h" |
| 28 | 28 |
| 29 namespace views { | 29 namespace views { |
| 30 namespace corewm { | 30 namespace corewm { |
| 31 namespace { | 31 namespace { |
| 32 | 32 |
| 33 const int kTooltipTimeoutMs = 500; | |
| 34 const int kDefaultTooltipShownTimeoutMs = 10000; | 33 const int kDefaultTooltipShownTimeoutMs = 10000; |
| 35 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 36 // Drawing a long word in tooltip is very slow on Windows. crbug.com/513693 | 35 // Drawing a long word in tooltip is very slow on Windows. crbug.com/513693 |
| 37 const size_t kMaxTooltipLength = 1024; | 36 const size_t kMaxTooltipLength = 1024; |
| 38 #else | 37 #else |
| 39 const size_t kMaxTooltipLength = 2048; | 38 const size_t kMaxTooltipLength = 2048; |
| 40 #endif | 39 #endif |
| 41 | 40 |
| 42 // Returns true if |target| is a valid window to get the tooltip from. | 41 // Returns true if |target| is a valid window to get the tooltip from. |
| 43 // |event_target| is the original target from the event and |target| the window | 42 // |event_target| is the original target from the event and |target| the window |
| (...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 122 } // namespace | 121 } // namespace |
| 123 | 122 |
| 124 //////////////////////////////////////////////////////////////////////////////// | 123 //////////////////////////////////////////////////////////////////////////////// |
| 125 // TooltipController public: | 124 // TooltipController public: |
| 126 | 125 |
| 127 TooltipController::TooltipController(std::unique_ptr<Tooltip> tooltip) | 126 TooltipController::TooltipController(std::unique_ptr<Tooltip> tooltip) |
| 128 : tooltip_window_(NULL), | 127 : tooltip_window_(NULL), |
| 129 tooltip_id_(NULL), | 128 tooltip_id_(NULL), |
| 130 tooltip_window_at_mouse_press_(NULL), | 129 tooltip_window_at_mouse_press_(NULL), |
| 131 tooltip_(std::move(tooltip)), | 130 tooltip_(std::move(tooltip)), |
| 132 tooltips_enabled_(true) { | 131 tooltips_enabled_(true) {} |
| 133 tooltip_timer_.Start(FROM_HERE, | |
| 134 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | |
| 135 this, &TooltipController::TooltipTimerFired); | |
| 136 } | |
| 137 | 132 |
| 138 TooltipController::~TooltipController() { | 133 TooltipController::~TooltipController() { |
| 139 if (tooltip_window_) | 134 if (tooltip_window_) |
| 140 tooltip_window_->RemoveObserver(this); | 135 tooltip_window_->RemoveObserver(this); |
| 141 } | 136 } |
| 142 | 137 |
| 143 int TooltipController::GetMaxWidth(const gfx::Point& location) const { | 138 int TooltipController::GetMaxWidth(const gfx::Point& location) const { |
| 144 return tooltip_->GetMaxWidth(location); | 139 return tooltip_->GetMaxWidth(location); |
| 145 } | 140 } |
| 146 | 141 |
| 147 void TooltipController::UpdateTooltip(aura::Window* target) { | 142 void TooltipController::UpdateTooltip(aura::Window* target) { |
| 148 // If tooltip is visible, we may want to hide it. If it is not, we are ok. | 143 // If tooltip is visible, we may want to hide it. If it is not, we are ok. |
| 149 if (tooltip_window_ == target && tooltip_->IsVisible()) | 144 if (tooltip_window_ == target && tooltip_->IsVisible()) |
| 150 UpdateIfRequired(); | 145 UpdateIfRequired(); |
| 151 | 146 |
| 152 // Reset |tooltip_window_at_mouse_press_| if the moving within the same window | 147 // Reset |tooltip_window_at_mouse_press_| if the moving within the same window |
| 153 // but over a region that has different tooltip text. By resetting | 148 // but over a region that has different tooltip text. By resetting |
| 154 // |tooltip_window_at_mouse_press_| we ensure the next time the timer fires | 149 // |tooltip_window_at_mouse_press_| we ensure the next time the timer fires |
| 155 // we'll requery for the tooltip text. | 150 // we'll requery for the tooltip text. |
| 156 // This handles the case of clicking on a view, moving within the same window | 151 // This handles the case of clicking on a view, moving within the same window |
| 157 // but over a different view, than back to the original. | 152 // but over a different view, than back to the original. |
| 158 if (tooltip_window_at_mouse_press_ && | 153 if (tooltip_window_at_mouse_press_ && |
| 159 target == tooltip_window_at_mouse_press_ && | 154 target == tooltip_window_at_mouse_press_ && |
| 160 aura::client::GetTooltipText(target) != tooltip_text_at_mouse_press_) { | 155 aura::client::GetTooltipText(target) != tooltip_text_at_mouse_press_) { |
| 161 tooltip_window_at_mouse_press_ = NULL; | 156 tooltip_window_at_mouse_press_ = NULL; |
| 162 } | 157 } |
| 163 | |
| 164 // If we had stopped the tooltip timer for some reason, we must restart it if | |
| 165 // there is a change in the tooltip. | |
| 166 if (!tooltip_timer_.IsRunning()) { | |
| 167 if (tooltip_window_ != target || (tooltip_window_ && | |
| 168 tooltip_text_ != aura::client::GetTooltipText(tooltip_window_))) { | |
| 169 tooltip_timer_.Start(FROM_HERE, | |
| 170 base::TimeDelta::FromMilliseconds(kTooltipTimeoutMs), | |
| 171 this, &TooltipController::TooltipTimerFired); | |
| 172 } | |
| 173 } | |
| 174 } | 158 } |
| 175 | 159 |
| 176 void TooltipController::SetTooltipShownTimeout(aura::Window* target, | 160 void TooltipController::SetTooltipShownTimeout(aura::Window* target, |
| 177 int timeout_in_ms) { | 161 int timeout_in_ms) { |
| 178 tooltip_shown_timeout_map_[target] = timeout_in_ms; | 162 tooltip_shown_timeout_map_[target] = timeout_in_ms; |
| 179 } | 163 } |
| 180 | 164 |
| 181 void TooltipController::SetTooltipsEnabled(bool enable) { | 165 void TooltipController::SetTooltipsEnabled(bool enable) { |
| 182 if (tooltips_enabled_ == enable) | 166 if (tooltips_enabled_ == enable) |
| 183 return; | 167 return; |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 204 curr_mouse_loc_ = event->location(); | 188 curr_mouse_loc_ = event->location(); |
| 205 aura::Window* target = NULL; | 189 aura::Window* target = NULL; |
| 206 // Avoid a call to display::Screen::GetWindowAtScreenPoint() since it can | 190 // Avoid a call to display::Screen::GetWindowAtScreenPoint() since it can |
| 207 // be very expensive on X11 in cases when the tooltip is hidden anyway. | 191 // be very expensive on X11 in cases when the tooltip is hidden anyway. |
| 208 if (tooltips_enabled_ && | 192 if (tooltips_enabled_ && |
| 209 !aura::Env::GetInstance()->IsMouseButtonDown() && | 193 !aura::Env::GetInstance()->IsMouseButtonDown() && |
| 210 !IsDragDropInProgress()) { | 194 !IsDragDropInProgress()) { |
| 211 target = GetTooltipTarget(*event, &curr_mouse_loc_); | 195 target = GetTooltipTarget(*event, &curr_mouse_loc_); |
| 212 } | 196 } |
| 213 SetTooltipWindow(target); | 197 SetTooltipWindow(target); |
| 214 if (tooltip_timer_.IsRunning()) | |
| 215 tooltip_timer_.Reset(); | |
| 216 | 198 |
| 217 if (tooltip_->IsVisible()) | 199 if (tooltip_->IsVisible() || tooltip_window_ != target || |
| 200 (tooltip_window_ && | |
| 201 tooltip_text_ != aura::client::GetTooltipText(tooltip_window_))) | |
|
brucedawson
2017/01/06 20:45:23
I don't understand why this logic has changed. Can
chengx
2017/01/06 20:58:19
Previously, mouse event handlers update tooltip on
| |
| 218 UpdateIfRequired(); | 202 UpdateIfRequired(); |
| 219 break; | 203 break; |
| 220 } | 204 } |
| 221 case ui::ET_MOUSE_PRESSED: | 205 case ui::ET_MOUSE_PRESSED: |
| 222 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) { | 206 if ((event->flags() & ui::EF_IS_NON_CLIENT) == 0) { |
| 223 aura::Window* target = static_cast<aura::Window*>(event->target()); | 207 aura::Window* target = static_cast<aura::Window*>(event->target()); |
| 224 // We don't get a release for non-client areas. | 208 // We don't get a release for non-client areas. |
| 225 tooltip_window_at_mouse_press_ = target; | 209 tooltip_window_at_mouse_press_ = target; |
| 226 if (target) | 210 if (target) |
| 227 tooltip_text_at_mouse_press_ = aura::client::GetTooltipText(target); | 211 tooltip_text_at_mouse_press_ = aura::client::GetTooltipText(target); |
| 228 } | 212 } |
| 229 tooltip_->Hide(); | 213 tooltip_->Hide(); |
| 230 break; | 214 break; |
| 231 case ui::ET_MOUSEWHEEL: | 215 case ui::ET_MOUSEWHEEL: |
| 232 // Hide the tooltip for click, release, drag, wheel events. | 216 // Hide the tooltip for click, release, drag, wheel events. |
| 233 if (tooltip_->IsVisible()) | 217 if (tooltip_->IsVisible()) |
| 234 tooltip_->Hide(); | 218 tooltip_->Hide(); |
| 235 | |
| 236 // Don't reshow the tooltip during scroll. | |
| 237 if (tooltip_timer_.IsRunning()) | |
| 238 tooltip_timer_.Reset(); | |
| 239 break; | 219 break; |
| 240 default: | 220 default: |
| 241 break; | 221 break; |
| 242 } | 222 } |
| 243 } | 223 } |
| 244 | 224 |
| 245 void TooltipController::OnTouchEvent(ui::TouchEvent* event) { | 225 void TooltipController::OnTouchEvent(ui::TouchEvent* event) { |
| 246 // TODO(varunjain): need to properly implement tooltips for | 226 // TODO(varunjain): need to properly implement tooltips for |
| 247 // touch events. | 227 // touch events. |
| 248 // Hide the tooltip for touch events. | 228 // Hide the tooltip for touch events. |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 259 if (tooltip_window_ == window) { | 239 if (tooltip_window_ == window) { |
| 260 tooltip_->Hide(); | 240 tooltip_->Hide(); |
| 261 tooltip_shown_timeout_map_.erase(tooltip_window_); | 241 tooltip_shown_timeout_map_.erase(tooltip_window_); |
| 262 tooltip_window_ = NULL; | 242 tooltip_window_ = NULL; |
| 263 } | 243 } |
| 264 } | 244 } |
| 265 | 245 |
| 266 //////////////////////////////////////////////////////////////////////////////// | 246 //////////////////////////////////////////////////////////////////////////////// |
| 267 // TooltipController private: | 247 // TooltipController private: |
| 268 | 248 |
| 269 void TooltipController::TooltipTimerFired() { | |
| 270 UpdateIfRequired(); | |
| 271 } | |
| 272 | |
| 273 void TooltipController::TooltipShownTimerFired() { | 249 void TooltipController::TooltipShownTimerFired() { |
| 274 tooltip_->Hide(); | 250 tooltip_->Hide(); |
| 275 | |
| 276 // Since the user presumably no longer needs the tooltip, we also stop the | |
| 277 // tooltip timer so that tooltip does not pop back up. We will restart this | |
| 278 // timer if the tooltip changes (see UpdateTooltip()). | |
| 279 tooltip_timer_.Stop(); | |
| 280 } | 251 } |
| 281 | 252 |
| 282 void TooltipController::UpdateIfRequired() { | 253 void TooltipController::UpdateIfRequired() { |
| 283 if (!tooltips_enabled_ || | 254 if (!tooltips_enabled_ || |
| 284 aura::Env::GetInstance()->IsMouseButtonDown() || | 255 aura::Env::GetInstance()->IsMouseButtonDown() || |
| 285 IsDragDropInProgress() || !IsCursorVisible()) { | 256 IsDragDropInProgress() || !IsCursorVisible()) { |
| 286 tooltip_->Hide(); | 257 tooltip_->Hide(); |
| 287 return; | 258 return; |
| 288 } | 259 } |
| 289 | 260 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 378 return; | 349 return; |
| 379 if (tooltip_window_) | 350 if (tooltip_window_) |
| 380 tooltip_window_->RemoveObserver(this); | 351 tooltip_window_->RemoveObserver(this); |
| 381 tooltip_window_ = target; | 352 tooltip_window_ = target; |
| 382 if (tooltip_window_) | 353 if (tooltip_window_) |
| 383 tooltip_window_->AddObserver(this); | 354 tooltip_window_->AddObserver(this); |
| 384 } | 355 } |
| 385 | 356 |
| 386 } // namespace corewm | 357 } // namespace corewm |
| 387 } // namespace views | 358 } // namespace views |
| OLD | NEW |