Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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/wm/maximize_mode/maximize_mode_controller.h" | 5 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
| 6 | 6 |
| 7 #include "ash/accelerators/accelerator_controller.h" | 7 #include "ash/accelerators/accelerator_controller.h" |
| 8 #include "ash/accelerators/accelerator_table.h" | 8 #include "ash/accelerators/accelerator_table.h" |
| 9 #include "ash/accelerometer/accelerometer_controller.h" | 9 #include "ash/accelerometer/accelerometer_controller.h" |
| 10 #include "ash/ash_switches.h" | 10 #include "ash/ash_switches.h" |
| 11 #include "ash/display/display_manager.h" | 11 #include "ash/display/display_manager.h" |
| 12 #include "ash/shell.h" | 12 #include "ash/shell.h" |
| 13 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h" | 13 #include "ash/wm/maximize_mode/maximize_mode_window_manager.h" |
| 14 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h" | 14 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard.h" |
| 15 #include "base/auto_reset.h" | 15 #include "base/auto_reset.h" |
| 16 #include "base/command_line.h" | 16 #include "base/command_line.h" |
| 17 #include "base/metrics/histogram.h" | 17 #include "base/metrics/histogram.h" |
| 18 #include "base/time/default_tick_clock.h" | |
| 19 #include "base/time/tick_clock.h" | |
| 20 #include "chromeos/dbus/dbus_thread_manager.h" | |
|
jonross
2014/07/25 13:39:06
#if defined this as well.
bruthig
2014/07/25 15:20:42
Done.
| |
| 18 #include "ui/base/accelerators/accelerator.h" | 21 #include "ui/base/accelerators/accelerator.h" |
| 19 #include "ui/events/event.h" | 22 #include "ui/events/event.h" |
| 20 #include "ui/events/event_handler.h" | 23 #include "ui/events/event_handler.h" |
| 21 #include "ui/events/keycodes/keyboard_codes.h" | 24 #include "ui/events/keycodes/keyboard_codes.h" |
| 22 #include "ui/gfx/vector3d_f.h" | 25 #include "ui/gfx/vector3d_f.h" |
| 23 | 26 |
| 24 #if defined(USE_X11) | 27 #if defined(USE_X11) |
| 25 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h" | 28 #include "ash/wm/maximize_mode/scoped_disable_internal_mouse_and_keyboard_x11.h" |
| 26 #endif | 29 #endif |
| 27 | 30 |
| 28 namespace ash { | 31 namespace ash { |
| 29 | 32 |
| 30 namespace { | 33 namespace { |
| 31 | 34 |
| 32 // The hinge angle at which to enter maximize mode. | 35 // The hinge angle at which to enter maximize mode. |
| 33 const float kEnterMaximizeModeAngle = 200.0f; | 36 const float kEnterMaximizeModeAngle = 200.0f; |
| 34 | 37 |
| 35 // The angle at which to exit maximize mode, this is specifically less than the | 38 // The angle at which to exit maximize mode, this is specifically less than the |
| 36 // angle to enter maximize mode to prevent rapid toggling when near the angle. | 39 // angle to enter maximize mode to prevent rapid toggling when near the angle. |
| 37 const float kExitMaximizeModeAngle = 160.0f; | 40 const float kExitMaximizeModeAngle = 160.0f; |
| 38 | 41 |
| 39 // When the lid is fully open 360 degrees, the accelerometer readings can | 42 // Defines a range for which accelerometer readings are considered accurate. |
| 40 // occasionally appear as though the lid is almost closed. If the lid appears | 43 // When the lid is near open (or near closed) the acceleromter readings may be |
| 41 // near closed but the device is on we assume it is an erroneous reading from | 44 // inaccurate and a lid that is fully open may appear to be near closed (and |
| 42 // it being open 360 degrees. | 45 // vice versa). |
| 43 const float kFullyOpenAngleErrorTolerance = 20.0f; | 46 const float kMinStableAngle = 20.0f; |
| 47 const float kMaxStableAngle = 340.0f; | |
| 48 | |
| 49 // The time duration to consider the lid to be recently opened. | |
| 50 // This is used to prevent entering maximize mode if an erroneous accelerometer | |
| 51 // reading makes the lid appear to be fully open when the user is opening the | |
| 52 // lid from a closed position. | |
| 53 base::TimeDelta kLidRecentlyOpenedTolerance = base::TimeDelta::FromSeconds(2); | |
|
jonross
2014/07/25 13:39:06
Make this a const.
bruthig
2014/07/25 15:20:42
Done.
| |
| 44 | 54 |
| 45 // When the device approaches vertical orientation (i.e. portrait orientation) | 55 // When the device approaches vertical orientation (i.e. portrait orientation) |
| 46 // the accelerometers for the base and lid approach the same values (i.e. | 56 // the accelerometers for the base and lid approach the same values (i.e. |
| 47 // gravity pointing in the direction of the hinge). When this happens we cannot | 57 // gravity pointing in the direction of the hinge). When this happens we cannot |
| 48 // compute the hinge angle reliably and must turn ignore accelerometer readings. | 58 // compute the hinge angle reliably and must turn ignore accelerometer readings. |
| 49 // This is the minimum acceleration perpendicular to the hinge under which to | 59 // This is the minimum acceleration perpendicular to the hinge under which to |
| 50 // detect hinge angle. | 60 // detect hinge angle. |
| 51 const float kHingeAngleDetectionThreshold = 0.25f; | 61 const float kHingeAngleDetectionThreshold = 0.25f; |
| 52 | 62 |
| 53 // The maximum deviation from the acceleration expected due to gravity under | 63 // The maximum deviation from the acceleration expected due to gravity under |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 137 | 147 |
| 138 #endif // OS_CHROMEOS | 148 #endif // OS_CHROMEOS |
| 139 | 149 |
| 140 } // namespace | 150 } // namespace |
| 141 | 151 |
| 142 MaximizeModeController::MaximizeModeController() | 152 MaximizeModeController::MaximizeModeController() |
| 143 : rotation_locked_(false), | 153 : rotation_locked_(false), |
| 144 have_seen_accelerometer_data_(false), | 154 have_seen_accelerometer_data_(false), |
| 145 in_set_screen_rotation_(false), | 155 in_set_screen_rotation_(false), |
| 146 user_rotation_(gfx::Display::ROTATE_0), | 156 user_rotation_(gfx::Display::ROTATE_0), |
| 147 last_touchview_transition_time_(base::Time::Now()) { | 157 last_touchview_transition_time_(base::Time::Now()), |
| 158 last_lid_open_time_(), | |
| 159 tick_clock_(new base::DefaultTickClock()), | |
| 160 lid_is_closed_(false) { | |
| 148 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); | 161 Shell::GetInstance()->accelerometer_controller()->AddObserver(this); |
| 149 Shell::GetInstance()->AddShellObserver(this); | 162 Shell::GetInstance()->AddShellObserver(this); |
| 163 #if defined(OS_CHROMEOS) | |
| 164 chromeos::DBusThreadManager::Get()-> | |
| 165 GetPowerManagerClient()->AddObserver(this); | |
| 166 #endif // OS_CHROMEOS | |
| 150 } | 167 } |
| 151 | 168 |
| 152 MaximizeModeController::~MaximizeModeController() { | 169 MaximizeModeController::~MaximizeModeController() { |
| 153 Shell::GetInstance()->RemoveShellObserver(this); | 170 Shell::GetInstance()->RemoveShellObserver(this); |
| 154 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); | 171 Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
| 172 #if defined(OS_CHROMEOS) | |
| 173 chromeos::DBusThreadManager::Get()-> | |
| 174 GetPowerManagerClient()->RemoveObserver(this); | |
| 175 #endif // OS_CHROMEOS | |
| 155 } | 176 } |
| 156 | 177 |
| 157 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { | 178 void MaximizeModeController::SetRotationLocked(bool rotation_locked) { |
| 158 if (rotation_locked_ == rotation_locked) | 179 if (rotation_locked_ == rotation_locked) |
| 159 return; | 180 return; |
| 160 rotation_locked_ = rotation_locked; | 181 rotation_locked_ = rotation_locked; |
| 161 FOR_EACH_OBSERVER(Observer, observers_, | 182 FOR_EACH_OBSERVER(Observer, observers_, |
| 162 OnRotationLockChanged(rotation_locked_)); | 183 OnRotationLockChanged(rotation_locked_)); |
| 163 } | 184 } |
| 164 | 185 |
| (...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 232 if (user_rotation != current_rotation_) { | 253 if (user_rotation != current_rotation_) { |
| 233 // A user may change other display configuration settings. When the user | 254 // A user may change other display configuration settings. When the user |
| 234 // does change the rotation setting, then lock rotation to prevent the | 255 // does change the rotation setting, then lock rotation to prevent the |
| 235 // accelerometer from erasing their change. | 256 // accelerometer from erasing their change. |
| 236 SetRotationLocked(true); | 257 SetRotationLocked(true); |
| 237 user_rotation_ = user_rotation; | 258 user_rotation_ = user_rotation; |
| 238 current_rotation_ = user_rotation; | 259 current_rotation_ = user_rotation; |
| 239 } | 260 } |
| 240 } | 261 } |
| 241 | 262 |
| 263 #if defined(OS_CHROMEOS) | |
| 264 void MaximizeModeController::LidEventReceived(bool open, | |
| 265 const base::TimeTicks& time) { | |
| 266 if (open) | |
| 267 last_lid_open_time_ = time; | |
| 268 lid_is_closed_ = !open; | |
| 269 LeaveMaximizeMode(); | |
| 270 } | |
| 271 | |
| 272 void MaximizeModeController::SuspendImminent() { | |
| 273 RecordTouchViewStateTransition(); | |
| 274 } | |
| 275 | |
| 276 void MaximizeModeController::SuspendDone( | |
| 277 const base::TimeDelta& sleep_duration) { | |
| 278 last_touchview_transition_time_ = base::Time::Now(); | |
| 279 // A lid open event won't always occur when coming out of a suspend state. | |
| 280 lid_is_closed_ = false; | |
| 281 } | |
| 282 #endif // OS_CHROMEOS | |
| 283 | |
| 242 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, | 284 void MaximizeModeController::HandleHingeRotation(const gfx::Vector3dF& base, |
| 243 const gfx::Vector3dF& lid) { | 285 const gfx::Vector3dF& lid) { |
| 244 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); | 286 static const gfx::Vector3dF hinge_vector(0.0f, 1.0f, 0.0f); |
| 245 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | |
| 246 // Ignore the component of acceleration parallel to the hinge for the purposes | 287 // Ignore the component of acceleration parallel to the hinge for the purposes |
| 247 // of hinge angle calculation. | 288 // of hinge angle calculation. |
| 248 gfx::Vector3dF base_flattened(base); | 289 gfx::Vector3dF base_flattened(base); |
| 249 gfx::Vector3dF lid_flattened(lid); | 290 gfx::Vector3dF lid_flattened(lid); |
| 250 base_flattened.set_y(0.0f); | 291 base_flattened.set_y(0.0f); |
| 251 lid_flattened.set_y(0.0f); | 292 lid_flattened.set_y(0.0f); |
| 252 | 293 |
| 253 // As the hinge approaches a vertical angle, the base and lid accelerometers | 294 // As the hinge approaches a vertical angle, the base and lid accelerometers |
| 254 // approach the same values making any angle calculations highly inaccurate. | 295 // approach the same values making any angle calculations highly inaccurate. |
| 255 // Bail out early when it is too close. | 296 // Bail out early when it is too close. |
| 256 if (base_flattened.Length() < kHingeAngleDetectionThreshold || | 297 if (base_flattened.Length() < kHingeAngleDetectionThreshold || |
| 257 lid_flattened.Length() < kHingeAngleDetectionThreshold) { | 298 lid_flattened.Length() < kHingeAngleDetectionThreshold) { |
| 258 return; | 299 return; |
| 259 } | 300 } |
| 260 | 301 |
| 261 // Compute the angle between the base and the lid. | 302 // Compute the angle between the base and the lid. |
| 262 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, | 303 float angle = ClockwiseAngleBetweenVectorsInDegrees(base_flattened, |
| 263 lid_flattened, hinge_vector); | 304 lid_flattened, hinge_vector); |
| 264 | |
| 265 // Toggle maximize mode on or off when corresponding thresholds are passed. | 305 // Toggle maximize mode on or off when corresponding thresholds are passed. |
| 266 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager | 306 // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager |
| 267 // such that observations of state changes occur after the change and shell | 307 // such that observations of state changes occur after the change and shell |
| 268 // has fewer states to track. | 308 // has fewer states to track. |
| 269 if (maximize_mode_engaged && | 309 if (lid_is_closed_) { |
| 270 angle > kFullyOpenAngleErrorTolerance && | |
| 271 angle < kExitMaximizeModeAngle) { | |
| 272 LeaveMaximizeMode(); | 310 LeaveMaximizeMode(); |
| 273 } else if (!maximize_mode_engaged && | 311 } else if (angle > kMinStableAngle && |
| 274 angle > kEnterMaximizeModeAngle) { | 312 angle < kMaxStableAngle) { |
| 275 EnterMaximizeMode(); | 313 // Clear the last_lid_open_time_ for a stable reading so that there is less |
| 314 // chance of a delay if the lid is moved from the close state to the fully | |
| 315 // open state very quickly. | |
| 316 last_lid_open_time_ = base::TimeTicks(); | |
| 317 if (angle < kExitMaximizeModeAngle) { | |
| 318 LeaveMaximizeMode(); | |
| 319 } else if (angle > kEnterMaximizeModeAngle) { | |
| 320 EnterMaximizeMode(); | |
| 321 } | |
| 322 } else if (angle > kMaxStableAngle) { | |
| 323 if (WasLidOpenedRecently()) { | |
| 324 LeaveMaximizeMode(); | |
| 325 } else { | |
| 326 EnterMaximizeMode(); | |
| 327 } | |
|
jonross
2014/07/25 13:39:06
I'd rather the new logic be introduced into the pr
bruthig
2014/07/25 15:20:42
Done.
| |
| 276 } | 328 } |
| 277 } | 329 } |
| 278 | 330 |
| 279 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { | 331 void MaximizeModeController::HandleScreenRotation(const gfx::Vector3dF& lid) { |
| 280 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); | 332 bool maximize_mode_engaged = IsMaximizeModeWindowManagerEnabled(); |
| 281 | 333 |
| 282 // TODO(jonross): track the updated rotation angle even when locked. So that | 334 // TODO(jonross): track the updated rotation angle even when locked. So that |
| 283 // when rotation lock is removed the accelerometer rotation can be applied | 335 // when rotation lock is removed the accelerometer rotation can be applied |
| 284 // without waiting for the next update. | 336 // without waiting for the next update. |
| 285 if (!maximize_mode_engaged || rotation_locked_) | 337 if (!maximize_mode_engaged || rotation_locked_) |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 343 DisplayManager* display_manager, | 395 DisplayManager* display_manager, |
| 344 gfx::Display::Rotation rotation) { | 396 gfx::Display::Rotation rotation) { |
| 345 base::AutoReset<bool> auto_in_set_screen_rotation( | 397 base::AutoReset<bool> auto_in_set_screen_rotation( |
| 346 &in_set_screen_rotation_, true); | 398 &in_set_screen_rotation_, true); |
| 347 current_rotation_ = rotation; | 399 current_rotation_ = rotation; |
| 348 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), | 400 display_manager->SetDisplayRotation(gfx::Display::InternalDisplayId(), |
| 349 rotation); | 401 rotation); |
| 350 } | 402 } |
| 351 | 403 |
| 352 void MaximizeModeController::EnterMaximizeMode() { | 404 void MaximizeModeController::EnterMaximizeMode() { |
| 405 if (IsMaximizeModeWindowManagerEnabled()) | |
| 406 return; | |
| 353 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 407 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 354 current_rotation_ = user_rotation_ = display_manager-> | 408 current_rotation_ = user_rotation_ = display_manager-> |
| 355 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 409 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
| 356 EnableMaximizeModeWindowManager(true); | 410 EnableMaximizeModeWindowManager(true); |
| 357 #if defined(USE_X11) | 411 #if defined(USE_X11) |
| 358 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); | 412 event_blocker_.reset(new ScopedDisableInternalMouseAndKeyboardX11); |
| 359 #endif | 413 #endif |
| 360 #if defined(OS_CHROMEOS) | 414 #if defined(OS_CHROMEOS) |
| 361 event_handler_.reset(new ScreenshotActionHandler); | 415 event_handler_.reset(new ScreenshotActionHandler); |
| 362 #endif | 416 #endif |
| 363 Shell::GetInstance()->display_controller()->AddObserver(this); | 417 Shell::GetInstance()->display_controller()->AddObserver(this); |
| 364 } | 418 } |
| 365 | 419 |
| 366 void MaximizeModeController::LeaveMaximizeMode() { | 420 void MaximizeModeController::LeaveMaximizeMode() { |
| 421 if (!IsMaximizeModeWindowManagerEnabled()) | |
| 422 return; | |
| 367 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); | 423 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 368 gfx::Display::Rotation current_rotation = display_manager-> | 424 gfx::Display::Rotation current_rotation = display_manager-> |
| 369 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); | 425 GetDisplayInfo(gfx::Display::InternalDisplayId()).rotation(); |
| 370 if (current_rotation != user_rotation_) | 426 if (current_rotation != user_rotation_) |
| 371 SetDisplayRotation(display_manager, user_rotation_); | 427 SetDisplayRotation(display_manager, user_rotation_); |
| 372 rotation_locked_ = false; | 428 rotation_locked_ = false; |
| 373 EnableMaximizeModeWindowManager(false); | 429 EnableMaximizeModeWindowManager(false); |
| 374 event_blocker_.reset(); | 430 event_blocker_.reset(); |
| 375 event_handler_.reset(); | 431 event_handler_.reset(); |
| 376 } | 432 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 377 | |
| 378 void MaximizeModeController::OnSuspend() { | |
| 379 RecordTouchViewStateTransition(); | |
| 380 } | |
| 381 | |
| 382 void MaximizeModeController::OnResume() { | |
| 383 last_touchview_transition_time_ = base::Time::Now(); | |
| 384 } | 433 } |
| 385 | 434 |
| 386 // Called after maximize mode has started, windows might still animate though. | 435 // Called after maximize mode has started, windows might still animate though. |
| 387 void MaximizeModeController::OnMaximizeModeStarted() { | 436 void MaximizeModeController::OnMaximizeModeStarted() { |
| 388 RecordTouchViewStateTransition(); | 437 RecordTouchViewStateTransition(); |
| 389 } | 438 } |
| 390 | 439 |
| 391 // Called after maximize mode has ended, windows might still be returning to | 440 // Called after maximize mode has ended, windows might still be returning to |
| 392 // their original position. | 441 // their original position. |
| 393 void MaximizeModeController::OnMaximizeModeEnded() { | 442 void MaximizeModeController::OnMaximizeModeEnded() { |
| (...skipping 27 matching lines...) Expand all Loading... | |
| 421 base::TimeDelta total_runtime = total_touchview_time_ + | 470 base::TimeDelta total_runtime = total_touchview_time_ + |
| 422 total_non_touchview_time_; | 471 total_non_touchview_time_; |
| 423 if (total_runtime.InSeconds() > 0) { | 472 if (total_runtime.InSeconds() > 0) { |
| 424 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", | 473 UMA_HISTOGRAM_PERCENTAGE("Ash.TouchView.TouchViewActivePercentage", |
| 425 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); | 474 100 * total_touchview_time_.InSeconds() / total_runtime.InSeconds()); |
| 426 } | 475 } |
| 427 } | 476 } |
| 428 Shell::GetInstance()->display_controller()->RemoveObserver(this); | 477 Shell::GetInstance()->display_controller()->RemoveObserver(this); |
| 429 } | 478 } |
| 430 | 479 |
| 480 bool MaximizeModeController::WasLidOpenedRecently() const { | |
| 481 if (last_lid_open_time_.is_null()) | |
| 482 return false; | |
| 483 | |
| 484 base::TimeTicks now = tick_clock_->NowTicks(); | |
| 485 DCHECK(now >= last_lid_open_time_); | |
| 486 base::TimeDelta elapsed_time = now - last_lid_open_time_; | |
| 487 return elapsed_time <= kLidRecentlyOpenedTolerance; | |
| 488 } | |
| 489 | |
| 490 void MaximizeModeController::SetTickClockForTest(base::TickClock* tick_clock) { | |
| 491 DCHECK(tick_clock_); | |
| 492 tick_clock_.reset(tick_clock); | |
| 493 } | |
| 494 | |
| 431 } // namespace ash | 495 } // namespace ash |
| OLD | NEW |