Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(108)

Side by Side Diff: ash/display/screen_orientation_controller_chromeos.cc

Issue 2134753004: mash: Partially migrate ScreenOrientationController to ash/common. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Sync and rebase. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/display/screen_orientation_controller_chromeos.h" 5 #include "ash/display/screen_orientation_controller_chromeos.h"
6 6
7 #include "ash/common/ash_switches.h" 7 #include "ash/common/ash_switches.h"
8 #include "ash/common/display/display_info.h" 8 #include "ash/common/display/display_info.h"
9 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h" 9 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
10 #include "ash/common/wm_shell.h" 10 #include "ash/common/wm_shell.h"
11 #include "ash/common/wm_window.h"
11 #include "ash/display/display_configuration_controller.h" 12 #include "ash/display/display_configuration_controller.h"
12 #include "ash/display/display_manager.h" 13 #include "ash/display/display_manager.h"
13 #include "ash/shell.h" 14 #include "ash/shell.h"
14 #include "base/auto_reset.h" 15 #include "base/auto_reset.h"
15 #include "base/command_line.h" 16 #include "base/command_line.h"
16 #include "chromeos/accelerometer/accelerometer_reader.h" 17 #include "chromeos/accelerometer/accelerometer_reader.h"
17 #include "chromeos/accelerometer/accelerometer_types.h" 18 #include "chromeos/accelerometer/accelerometer_types.h"
18 #include "ui/aura/window.h"
19 #include "ui/aura/window_observer.h"
20 #include "ui/chromeos/accelerometer/accelerometer_util.h" 19 #include "ui/chromeos/accelerometer/accelerometer_util.h"
21 #include "ui/display/display.h" 20 #include "ui/display/display.h"
22 #include "ui/gfx/geometry/size.h" 21 #include "ui/gfx/geometry/size.h"
23 #include "ui/wm/public/activation_client.h" 22
23 namespace ash {
24 24
25 namespace { 25 namespace {
26 26
27 // The angle which the screen has to be rotated past before the display will 27 // The angle which the screen has to be rotated past before the display will
28 // rotate to match it (i.e. 45.0f is no stickiness). 28 // rotate to match it (i.e. 45.0f is no stickiness).
29 const float kDisplayRotationStickyAngleDegrees = 60.0f; 29 const float kDisplayRotationStickyAngleDegrees = 60.0f;
30 30
31 // The minimum acceleration in m/s^2 in a direction required to trigger screen 31 // The minimum acceleration in m/s^2 in a direction required to trigger screen
32 // rotation. This prevents rapid toggling of rotation when the device is near 32 // rotation. This prevents rapid toggling of rotation when the device is near
33 // flat and there is very little screen aligned force on it. The value is 33 // flat and there is very little screen aligned force on it. The value is
34 // effectively the sine of the rise angle required times the acceleration due 34 // effectively the sine of the rise angle required times the acceleration due
35 // to gravity, with the current value requiring at least a 25 degree rise. 35 // to gravity, with the current value requiring at least a 25 degree rise.
36 const float kMinimumAccelerationScreenRotation = 4.2f; 36 const float kMinimumAccelerationScreenRotation = 4.2f;
37 37
38 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { 38 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() {
39 if (!display::Display::HasInternalDisplay()) 39 if (!display::Display::HasInternalDisplay())
40 return blink::WebScreenOrientationLockLandscape; 40 return blink::WebScreenOrientationLockLandscape;
41 41
42 ash::DisplayInfo info = 42 DisplayInfo info =
43 ash::Shell::GetInstance()->display_manager()->GetDisplayInfo( 43 WmShell::Get()->GetDisplayInfo(display::Display::InternalDisplayId());
44 display::Display::InternalDisplayId());
45 gfx::Size size = info.size_in_pixel(); 44 gfx::Size size = info.size_in_pixel();
46 switch (info.GetActiveRotation()) { 45 switch (info.GetActiveRotation()) {
47 case display::Display::ROTATE_0: 46 case display::Display::ROTATE_0:
48 case display::Display::ROTATE_180: 47 case display::Display::ROTATE_180:
49 return size.height() >= size.width() 48 return size.height() >= size.width()
50 ? blink::WebScreenOrientationLockPortrait 49 ? blink::WebScreenOrientationLockPortrait
51 : blink::WebScreenOrientationLockLandscape; 50 : blink::WebScreenOrientationLockLandscape;
52 case display::Display::ROTATE_90: 51 case display::Display::ROTATE_90:
53 case display::Display::ROTATE_270: 52 case display::Display::ROTATE_270:
54 return size.height() < size.width() 53 return size.height() < size.width()
55 ? blink::WebScreenOrientationLockPortrait 54 ? blink::WebScreenOrientationLockPortrait
56 : blink::WebScreenOrientationLockLandscape; 55 : blink::WebScreenOrientationLockLandscape;
57 } 56 }
58 NOTREACHED(); 57 NOTREACHED();
59 return blink::WebScreenOrientationLockLandscape; 58 return blink::WebScreenOrientationLockLandscape;
60 } 59 }
61 60
62 } // namespace 61 } // namespace
63 62
64 namespace ash {
65
66 ScreenOrientationController::ScreenOrientationController() 63 ScreenOrientationController::ScreenOrientationController()
67 : natural_orientation_(GetDisplayNaturalOrientation()), 64 : natural_orientation_(GetDisplayNaturalOrientation()),
68 ignore_display_configuration_updates_(false), 65 ignore_display_configuration_updates_(false),
69 rotation_locked_(false), 66 rotation_locked_(false),
70 rotation_locked_orientation_(blink::WebScreenOrientationLockAny), 67 rotation_locked_orientation_(blink::WebScreenOrientationLockAny),
71 user_rotation_(display::Display::ROTATE_0), 68 user_rotation_(display::Display::ROTATE_0),
72 current_rotation_(display::Display::ROTATE_0) { 69 current_rotation_(display::Display::ROTATE_0) {
73 WmShell::Get()->AddShellObserver(this); 70 WmShell::Get()->AddShellObserver(this);
74 } 71 }
75 72
76 ScreenOrientationController::~ScreenOrientationController() { 73 ScreenOrientationController::~ScreenOrientationController() {
77 WmShell::Get()->RemoveShellObserver(this); 74 WmShell::Get()->RemoveShellObserver(this);
78 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); 75 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
79 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); 76 WmShell::Get()->RemoveDisplayObserver(this);
80 Shell::GetInstance()->activation_client()->RemoveObserver(this); 77 WmShell::Get()->RemoveActivationObserver(this);
81 for (auto& windows : locking_windows_) 78 for (auto& windows : locking_windows_)
82 windows.first->RemoveObserver(this); 79 windows.first->RemoveObserver(this);
83 } 80 }
84 81
85 void ScreenOrientationController::AddObserver(Observer* observer) { 82 void ScreenOrientationController::AddObserver(Observer* observer) {
86 observers_.AddObserver(observer); 83 observers_.AddObserver(observer);
87 } 84 }
88 85
89 void ScreenOrientationController::RemoveObserver(Observer* observer) { 86 void ScreenOrientationController::RemoveObserver(Observer* observer) {
90 observers_.RemoveObserver(observer); 87 observers_.RemoveObserver(observer);
91 } 88 }
92 89
93 void ScreenOrientationController::LockOrientationForWindow( 90 void ScreenOrientationController::LockOrientationForWindow(
94 aura::Window* requesting_window, 91 WmWindow* requesting_window,
95 blink::WebScreenOrientationLockType lock_orientation) { 92 blink::WebScreenOrientationLockType lock_orientation) {
96 if (locking_windows_.empty()) 93 if (locking_windows_.empty())
97 Shell::GetInstance()->activation_client()->AddObserver(this); 94 WmShell::Get()->AddActivationObserver(this);
98 95
99 if (!requesting_window->HasObserver(this)) 96 if (!requesting_window->HasObserver(this))
100 requesting_window->AddObserver(this); 97 requesting_window->AddObserver(this);
101 locking_windows_[requesting_window] = lock_orientation; 98 locking_windows_[requesting_window] = lock_orientation;
102 99
103 ApplyLockForActiveWindow(); 100 ApplyLockForActiveWindow();
104 } 101 }
105 102
106 void ScreenOrientationController::UnlockOrientationForWindow( 103 void ScreenOrientationController::UnlockOrientationForWindow(WmWindow* window) {
107 aura::Window* window) {
108 locking_windows_.erase(window); 104 locking_windows_.erase(window);
109 if (locking_windows_.empty()) 105 if (locking_windows_.empty())
110 Shell::GetInstance()->activation_client()->RemoveObserver(this); 106 WmShell::Get()->RemoveActivationObserver(this);
111 window->RemoveObserver(this); 107 window->RemoveObserver(this);
112 ApplyLockForActiveWindow(); 108 ApplyLockForActiveWindow();
113 } 109 }
114 110
115 void ScreenOrientationController::UnlockAll() { 111 void ScreenOrientationController::UnlockAll() {
116 for (auto pair : locking_windows_) 112 for (auto pair : locking_windows_)
117 pair.first->RemoveObserver(this); 113 pair.first->RemoveObserver(this);
118 locking_windows_.clear(); 114 locking_windows_.clear();
119 Shell::GetInstance()->activation_client()->RemoveObserver(this); 115 WmShell::Get()->RemoveActivationObserver(this);
120 SetRotationLocked(false); 116 SetRotationLocked(false);
121 if (user_rotation_ != current_rotation_) 117 if (user_rotation_ != current_rotation_)
122 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER); 118 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER);
123 } 119 }
124 120
125 bool ScreenOrientationController::ScreenOrientationProviderSupported() const { 121 bool ScreenOrientationController::ScreenOrientationProviderSupported() const {
126 return WmShell::Get() 122 return WmShell::Get()
127 ->maximize_mode_controller() 123 ->maximize_mode_controller()
128 ->IsMaximizeModeWindowManagerEnabled() && 124 ->IsMaximizeModeWindowManagerEnabled() &&
129 !base::CommandLine::ForCurrentProcess()->HasSwitch( 125 !base::CommandLine::ForCurrentProcess()->HasSwitch(
(...skipping 23 matching lines...) Expand all
153 return; 149 return;
154 current_rotation_ = rotation; 150 current_rotation_ = rotation;
155 base::AutoReset<bool> auto_ignore_display_configuration_updates( 151 base::AutoReset<bool> auto_ignore_display_configuration_updates(
156 &ignore_display_configuration_updates_, true); 152 &ignore_display_configuration_updates_, true);
157 153
158 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation( 154 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation(
159 display::Display::InternalDisplayId(), rotation, source, 155 display::Display::InternalDisplayId(), rotation, source,
160 true /* user_action */); 156 true /* user_action */);
161 } 157 }
162 158
163 void ScreenOrientationController::OnWindowActivated( 159 void ScreenOrientationController::OnWindowActivated(WmWindow* gained_active,
164 aura::client::ActivationChangeObserver::ActivationReason reason, 160 WmWindow* lost_active) {
165 aura::Window* gained_active,
166 aura::Window* lost_active) {
167 ApplyLockForActiveWindow(); 161 ApplyLockForActiveWindow();
168 } 162 }
169 163
164 void ScreenOrientationController::OnWindowDestroying(WmWindow* window) {
165 UnlockOrientationForWindow(window);
166 }
167
170 // Currently contents::WebContents will only be able to lock rotation while 168 // Currently contents::WebContents will only be able to lock rotation while
171 // fullscreen. In this state a user cannot click on the tab strip to change. If 169 // fullscreen. In this state a user cannot click on the tab strip to change. If
172 // this becomes supported for non-fullscreen tabs then the following interferes 170 // this becomes supported for non-fullscreen tabs then the following interferes
173 // with TabDragController. OnWindowVisibilityChanged is called between a mouse 171 // with TabDragController. OnWindowVisibilityChanged is called between a mouse
174 // down and mouse up. The rotation this triggers leads to a coordinate space 172 // down and mouse up. The rotation this triggers leads to a coordinate space
175 // change in the middle of an event. Causes the tab to separate from the tab 173 // change in the middle of an event. Causes the tab to separate from the tab
176 // strip. 174 // strip.
177 void ScreenOrientationController::OnWindowVisibilityChanged( 175 void ScreenOrientationController::OnWindowVisibilityChanged(WmWindow* window,
178 aura::Window* window, 176 bool visible) {
179 bool visible) {
180 if (locking_windows_.find(window) == locking_windows_.end()) 177 if (locking_windows_.find(window) == locking_windows_.end())
181 return; 178 return;
182 ApplyLockForActiveWindow(); 179 ApplyLockForActiveWindow();
183 } 180 }
184 181
185 void ScreenOrientationController::OnWindowDestroying(aura::Window* window) {
186 UnlockOrientationForWindow(window);
187 }
188
189 void ScreenOrientationController::OnAccelerometerUpdated( 182 void ScreenOrientationController::OnAccelerometerUpdated(
190 scoped_refptr<const chromeos::AccelerometerUpdate> update) { 183 scoped_refptr<const chromeos::AccelerometerUpdate> update) {
191 if (rotation_locked_ && !CanRotateInLockedState()) 184 if (rotation_locked_ && !CanRotateInLockedState())
192 return; 185 return;
193 if (!update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN)) 186 if (!update->has(chromeos::ACCELEROMETER_SOURCE_SCREEN))
194 return; 187 return;
195 // Ignore the reading if it appears unstable. The reading is considered 188 // Ignore the reading if it appears unstable. The reading is considered
196 // unstable if it deviates too much from gravity 189 // unstable if it deviates too much from gravity
197 if (ui::IsAccelerometerReadingStable(*update, 190 if (ui::IsAccelerometerReadingStable(*update,
198 chromeos::ACCELEROMETER_SOURCE_SCREEN)) { 191 chromeos::ACCELEROMETER_SOURCE_SCREEN)) {
199 HandleScreenRotation(update->get(chromeos::ACCELEROMETER_SOURCE_SCREEN)); 192 HandleScreenRotation(update->get(chromeos::ACCELEROMETER_SOURCE_SCREEN));
200 } 193 }
201 } 194 }
202 195
203 void ScreenOrientationController::OnDisplayConfigurationChanged() { 196 void ScreenOrientationController::OnDisplayConfigurationChanged() {
204 if (ignore_display_configuration_updates_) 197 if (ignore_display_configuration_updates_)
205 return; 198 return;
206 if (!display::Display::HasInternalDisplay()) 199 if (!display::Display::HasInternalDisplay())
207 return; 200 return;
208 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
209 display::Display::Rotation user_rotation = 201 display::Display::Rotation user_rotation =
210 display_manager->GetDisplayInfo(display::Display::InternalDisplayId()) 202 WmShell::Get()
203 ->GetDisplayInfo(display::Display::InternalDisplayId())
211 .GetActiveRotation(); 204 .GetActiveRotation();
212 if (user_rotation != current_rotation_) { 205 if (user_rotation != current_rotation_) {
213 // A user may change other display configuration settings. When the user 206 // A user may change other display configuration settings. When the user
214 // does change the rotation setting, then lock rotation to prevent the 207 // does change the rotation setting, then lock rotation to prevent the
215 // accelerometer from erasing their change. 208 // accelerometer from erasing their change.
216 SetRotationLocked(true); 209 SetRotationLocked(true);
217 user_rotation_ = current_rotation_ = user_rotation; 210 user_rotation_ = current_rotation_ = user_rotation;
218 } 211 }
219 } 212 }
220 213
221 void ScreenOrientationController::OnMaximizeModeStarted() { 214 void ScreenOrientationController::OnMaximizeModeStarted() {
222 // Do not exit early, as the internal display can be determined after Maximize 215 // Do not exit early, as the internal display can be determined after Maximize
223 // Mode has started. (chrome-os-partner:38796) 216 // Mode has started. (chrome-os-partner:38796)
224 // Always start observing. 217 // Always start observing.
225 if (display::Display::HasInternalDisplay()) { 218 if (display::Display::HasInternalDisplay()) {
226 current_rotation_ = user_rotation_ = 219 current_rotation_ = user_rotation_ =
227 Shell::GetInstance() 220 WmShell::Get()
228 ->display_manager()
229 ->GetDisplayInfo(display::Display::InternalDisplayId()) 221 ->GetDisplayInfo(display::Display::InternalDisplayId())
230 .GetActiveRotation(); 222 .GetActiveRotation();
231 } 223 }
232 if (!rotation_locked_) 224 if (!rotation_locked_)
233 LoadDisplayRotationProperties(); 225 LoadDisplayRotationProperties();
234 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); 226 chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
235 Shell::GetInstance()->window_tree_host_manager()->AddObserver(this); 227 WmShell::Get()->AddDisplayObserver(this);
236 } 228 }
237 229
238 void ScreenOrientationController::OnMaximizeModeEnded() { 230 void ScreenOrientationController::OnMaximizeModeEnded() {
239 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); 231 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
240 Shell::GetInstance()->window_tree_host_manager()->RemoveObserver(this); 232 WmShell::Get()->RemoveDisplayObserver(this);
241 if (current_rotation_ != user_rotation_) 233 if (current_rotation_ != user_rotation_)
242 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER); 234 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER);
243 } 235 }
244 236
245 void ScreenOrientationController::LockRotation( 237 void ScreenOrientationController::LockRotation(
246 display::Display::Rotation rotation, 238 display::Display::Rotation rotation,
247 display::Display::RotationSource source) { 239 display::Display::RotationSource source) {
248 SetRotationLocked(true); 240 SetRotationLocked(true);
249 SetDisplayRotation(rotation, source); 241 SetDisplayRotation(rotation, source);
250 } 242 }
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
302 ? display::Display::ROTATE_180 294 ? display::Display::ROTATE_180
303 : display::Display::ROTATE_270, 295 : display::Display::ROTATE_270,
304 display::Display::ROTATION_SOURCE_ACTIVE); 296 display::Display::ROTATION_SOURCE_ACTIVE);
305 } 297 }
306 298
307 void ScreenOrientationController::LockToRotationMatchingOrientation( 299 void ScreenOrientationController::LockToRotationMatchingOrientation(
308 blink::WebScreenOrientationLockType lock_orientation) { 300 blink::WebScreenOrientationLockType lock_orientation) {
309 if (!display::Display::HasInternalDisplay()) 301 if (!display::Display::HasInternalDisplay())
310 return; 302 return;
311 303
312 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
313 display::Display::Rotation rotation = 304 display::Display::Rotation rotation =
314 display_manager->GetDisplayInfo(display::Display::InternalDisplayId()) 305 WmShell::Get()
306 ->GetDisplayInfo(display::Display::InternalDisplayId())
315 .GetActiveRotation(); 307 .GetActiveRotation();
316 if (natural_orientation_ == lock_orientation) { 308 if (natural_orientation_ == lock_orientation) {
317 if (rotation == display::Display::ROTATE_0 || 309 if (rotation == display::Display::ROTATE_0 ||
318 rotation == display::Display::ROTATE_180) { 310 rotation == display::Display::ROTATE_180) {
319 SetRotationLocked(true); 311 SetRotationLocked(true);
320 } else { 312 } else {
321 LockRotation(display::Display::ROTATE_0, 313 LockRotation(display::Display::ROTATE_0,
322 display::Display::ROTATION_SOURCE_ACTIVE); 314 display::Display::ROTATION_SOURCE_ACTIVE);
323 } 315 }
324 } else { 316 } else {
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 void ScreenOrientationController::LoadDisplayRotationProperties() { 379 void ScreenOrientationController::LoadDisplayRotationProperties() {
388 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); 380 DisplayManager* display_manager = Shell::GetInstance()->display_manager();
389 if (!display_manager->registered_internal_display_rotation_lock()) 381 if (!display_manager->registered_internal_display_rotation_lock())
390 return; 382 return;
391 SetDisplayRotation(display_manager->registered_internal_display_rotation(), 383 SetDisplayRotation(display_manager->registered_internal_display_rotation(),
392 display::Display::ROTATION_SOURCE_ACCELEROMETER); 384 display::Display::ROTATION_SOURCE_ACCELEROMETER);
393 SetRotationLocked(true); 385 SetRotationLocked(true);
394 } 386 }
395 387
396 void ScreenOrientationController::ApplyLockForActiveWindow() { 388 void ScreenOrientationController::ApplyLockForActiveWindow() {
397 aura::Window* active_window = 389 WmWindow* active_window = WmShell::Get()->GetActiveWindow();
398 Shell::GetInstance()->activation_client()->GetActiveWindow();
399 if (active_window) { 390 if (active_window) {
400 for (auto const& windows : locking_windows_) { 391 for (auto const& windows : locking_windows_) {
401 if (windows.first->TargetVisibility() && 392 if (windows.first->GetTargetVisibility() &&
402 active_window->Contains(windows.first)) { 393 active_window->Contains(windows.first)) {
403 LockRotationToOrientation(windows.second); 394 LockRotationToOrientation(windows.second);
404 return; 395 return;
405 } 396 }
406 } 397 }
407 } 398 }
408 SetRotationLocked(false); 399 SetRotationLocked(false);
409 } 400 }
410 401
411 bool ScreenOrientationController::IsRotationAllowedInLockedState( 402 bool ScreenOrientationController::IsRotationAllowedInLockedState(
(...skipping 15 matching lines...) Expand all
427 } 418 }
428 419
429 bool ScreenOrientationController::CanRotateInLockedState() { 420 bool ScreenOrientationController::CanRotateInLockedState() {
430 return rotation_locked_orientation_ == 421 return rotation_locked_orientation_ ==
431 blink::WebScreenOrientationLockLandscape || 422 blink::WebScreenOrientationLockLandscape ||
432 rotation_locked_orientation_ == 423 rotation_locked_orientation_ ==
433 blink::WebScreenOrientationLockPortrait; 424 blink::WebScreenOrientationLockPortrait;
434 } 425 }
435 426
436 } // namespace ash 427 } // namespace ash
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698