Index: ash/wm/maximize_mode/maximize_mode_controller.cc |
diff --git a/ash/wm/maximize_mode/maximize_mode_controller.cc b/ash/wm/maximize_mode/maximize_mode_controller.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..7b8981b2f714c020cd9df3b2700152722f2e1fa4 |
--- /dev/null |
+++ b/ash/wm/maximize_mode/maximize_mode_controller.cc |
@@ -0,0 +1,89 @@ |
+// Copyright 2014 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "ash/wm/maximize_mode/maximize_mode_controller.h" |
+ |
+#include "ash/accelerometer/accelerometer_controller.h" |
+#include "ash/shell.h" |
+#include "ui/gfx/vector3d_f.h" |
+ |
+namespace ash { |
+namespace internal { |
+ |
+namespace { |
+ |
+// The hinge angle at which to enter maximize mode. |
+const float kEnterMaximizeModeAngle = 200.0f; |
+ |
+// The angle at which to exit maximize mode, this is specifically less than the |
+// angle to enter maximize mode to prevent rapid toggling when near the angle. |
+const float kExitMaximizeModeAngle = 160.0f; |
+ |
+// When the lid is fully open 360 degrees, the accelerometer readings can |
+// occasionally appear as though the lid is almost closed. If the lid appears |
+// near closed but the device is on we assume it is an erroneous reading from |
+// it being open 360 degrees. |
+const float kFullyOpenAngleErrorTolerance = 10.0f; |
+ |
+// When the device approaches vertical orientation (i.e. portrait orientation) |
+// the accelerometers for the base and lid approach the same values (i.e. |
+// gravity pointing in the direction of the hinge). When this happens we cannot |
+// compute the hinge angle reliably and must turn ignore accelerometer readings. |
+// This is the angle from vertical under which we will not compute a hinge |
+// angle. |
+const float kHingeAxisAlignedThreshold = 15.0f; |
+ |
+const gfx::Vector3dF kHingeVector(0.0f, 1.0f, 0.0f); |
oshima
2014/04/01 22:29:08
won't this create static initializer?
|
+const float kRadiansToDegrees = 180.0f / 3.14159265f; |
+ |
+} // namespace |
+ |
+MaximizeModeController::MaximizeModeController() { |
+ Shell::GetInstance()->accelerometer_controller()->AddObserver(this); |
Mr4D (OOO till 08-26)
2014/04/01 22:05:50
This was fired periodically and not on state chang
flackr
2014/04/02 21:24:30
Yes, this is always fired periodically.
|
+} |
+ |
+MaximizeModeController::~MaximizeModeController() { |
+ Shell::GetInstance()->accelerometer_controller()->RemoveObserver(this); |
+} |
+ |
+void MaximizeModeController::OnAccelerometerUpdated( |
+ const gfx::Vector3dF& base, |
+ const gfx::Vector3dF& lid) { |
+ // As the hinge approaches a vertical angle, the base and lid accelerometers |
+ // approach the same values making any angle calculations highly inaccurate. |
+ // Bail out early when it is too close. |
+ float hinge_angle = acos( |
+ gfx::DotProduct(base, kHingeVector) / base.Length()) * kRadiansToDegrees; |
Mr4D (OOO till 08-26)
2014/04/01 22:05:50
Just checking - What was the frequency for the eve
oshima
2014/04/01 22:29:08
I agree with Stefan's concern. I guess it's alread
flackr
2014/04/02 21:24:30
Even before I added a separate enter and exit thre
oshima
2014/04/02 22:11:30
I think the mode flag should eventually move here
flackr
2014/04/03 02:52:14
I agree, that is what the TODO on line 73 is for.
|
+ if (hinge_angle < kHingeAxisAlignedThreshold || |
+ hinge_angle > 180.0f - kHingeAxisAlignedThreshold) |
+ return; |
+ |
+ // Compute the angle between the base and the lid. |
Mr4D (OOO till 08-26)
2014/04/01 22:05:50
You might want to pull this out into a separate fu
flackr
2014/04/02 21:24:30
Good call, made two helpers for shortest angle and
|
+ float dot_product = gfx::DotProduct(base, lid); |
+ float angle = acos(dot_product / base.Length() / lid.Length()) * |
+ kRadiansToDegrees; |
+ gfx::Vector3dF cross(base); |
+ cross.Cross(lid); |
+ float sign = gfx::DotProduct(cross, kHingeVector); |
+ if (sign > 0.0f) |
+ angle = 360.0f - angle; |
+ |
+ // Toggle maximize mode on or off when corresponding thresholds are passed. |
+ // TODO(flackr): Make MaximizeModeController own the MaximizeModeWindowManager |
+ // such that observations of state changes occur after the change and shell |
+ // has fewer states to track. |
+ bool maximize_mode_engaged = |
+ Shell::GetInstance()->IsMaximizeModeWindowManagerEnabled(); |
+ if (maximize_mode_engaged && |
+ angle > kFullyOpenAngleErrorTolerance && |
+ angle < kExitMaximizeModeAngle) { |
+ Shell::GetInstance()->EnableMaximizeModeWindowManager(false); |
+ } else if (!maximize_mode_engaged && |
+ angle > kEnterMaximizeModeAngle) { |
+ Shell::GetInstance()->EnableMaximizeModeWindowManager(true); |
+ } |
+} |
+ |
+} // namespace internal |
+} // namespace ash |