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

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

Issue 2777163003: Remember User locked rotation (Closed)
Patch Set: Created 3 years, 9 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/wm/maximize_mode/maximize_mode_controller.h" 8 #include "ash/common/wm/maximize_mode/maximize_mode_controller.h"
9 #include "ash/common/wm/mru_window_tracker.h" 9 #include "ash/common/wm/mru_window_tracker.h"
10 #include "ash/common/wm/window_state.h" 10 #include "ash/common/wm/window_state.h"
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 case display::Display::ROTATE_90: 54 case display::Display::ROTATE_90:
55 case display::Display::ROTATE_270: 55 case display::Display::ROTATE_270:
56 return size.height() < size.width() 56 return size.height() < size.width()
57 ? blink::WebScreenOrientationLockPortrait 57 ? blink::WebScreenOrientationLockPortrait
58 : blink::WebScreenOrientationLockLandscape; 58 : blink::WebScreenOrientationLockLandscape;
59 } 59 }
60 NOTREACHED(); 60 NOTREACHED();
61 return blink::WebScreenOrientationLockLandscape; 61 return blink::WebScreenOrientationLockLandscape;
62 } 62 }
63 63
64 blink::WebScreenOrientationLockType RotationToOrientation(
65 display::Display::Rotation rotation) {
66 blink::WebScreenOrientationLockType natural = GetDisplayNaturalOrientation();
67 if (natural == blink::WebScreenOrientationLockLandscape) {
68 switch (rotation) {
69 case display::Display::ROTATE_0:
70 return blink::WebScreenOrientationLockLandscapePrimary;
71 case display::Display::ROTATE_90:
72 return blink::WebScreenOrientationLockPortraitPrimary;
73 case display::Display::ROTATE_180:
74 return blink::WebScreenOrientationLockLandscapeSecondary;
75 case display::Display::ROTATE_270:
76 return blink::WebScreenOrientationLockPortraitSecondary;
77 }
78 } else { // Natural portrait
79 switch (rotation) {
80 case display::Display::ROTATE_0:
81 return blink::WebScreenOrientationLockPortraitPrimary;
82 case display::Display::ROTATE_90:
83 return blink::WebScreenOrientationLockLandscapePrimary;
84 case display::Display::ROTATE_180:
85 return blink::WebScreenOrientationLockPortraitSecondary;
86 case display::Display::ROTATE_270:
87 return blink::WebScreenOrientationLockLandscapeSecondary;
88 }
89 }
90 NOTREACHED();
91 return blink::WebScreenOrientationLockAny;
92 }
93
94 // Returns the rotation that matches the orientation type.
95 // Returns ROTATE_0 if the given orientation is ANY, which is used
96 // to indicate that user didn't lock orientation.
97 display::Display::Rotation OrientationToRotation(
98 blink::WebScreenOrientationLockType orientation) {
99 blink::WebScreenOrientationLockType natural = GetDisplayNaturalOrientation();
100 if (orientation == blink::WebScreenOrientationLockAny) {
101 return display::Display::ROTATE_0;
102 }
103
104 if (natural == blink::WebScreenOrientationLockLandscape) {
105 switch (orientation) {
106 case blink::WebScreenOrientationLockLandscapePrimary:
107 return display::Display::ROTATE_0;
108 case blink::WebScreenOrientationLockPortraitPrimary:
109 return display::Display::ROTATE_90;
110 case blink::WebScreenOrientationLockLandscapeSecondary:
111 return display::Display::ROTATE_180;
112 case blink::WebScreenOrientationLockPortraitSecondary:
113 return display::Display::ROTATE_270;
114 default:
115 break;
116 }
117 } else { // Natural portrait
118 switch (orientation) {
119 case blink::WebScreenOrientationLockPortraitPrimary:
120 return display::Display::ROTATE_0;
121 case blink::WebScreenOrientationLockLandscapePrimary:
122 return display::Display::ROTATE_90;
123 case blink::WebScreenOrientationLockPortraitSecondary:
124 return display::Display::ROTATE_180;
125 case blink::WebScreenOrientationLockLandscapeSecondary:
126 return display::Display::ROTATE_270;
127 default:
128 break;
129 }
130 }
131 NOTREACHED() << orientation;
132 return display::Display::ROTATE_0;
133 }
134
135 // Returns the locked orientation that matches the application
136 // requested orientation, or the application orientation itself
137 // if it didn't match.
138 blink::WebScreenOrientationLockType ResolveOrientationLock(
139 blink::WebScreenOrientationLockType app_requested,
140 blink::WebScreenOrientationLockType lock) {
141 if (app_requested == blink::WebScreenOrientationLockAny ||
142 (app_requested == blink::WebScreenOrientationLockLandscape &&
143 (lock == blink::WebScreenOrientationLockLandscapePrimary ||
144 lock == blink::WebScreenOrientationLockLandscapeSecondary)) ||
145 (app_requested == blink::WebScreenOrientationLockPortrait &&
146 (lock == blink::WebScreenOrientationLockPortraitPrimary ||
147 lock == blink::WebScreenOrientationLockPortraitSecondary))) {
148 return lock;
149 }
150 return app_requested;
151 }
152
64 } // namespace 153 } // namespace
65 154
66 ScreenOrientationController::ScreenOrientationController() 155 ScreenOrientationController::ScreenOrientationController()
67 : natural_orientation_(GetDisplayNaturalOrientation()), 156 : natural_orientation_(GetDisplayNaturalOrientation()),
68 ignore_display_configuration_updates_(false), 157 ignore_display_configuration_updates_(false),
69 rotation_locked_(false), 158 rotation_locked_(false),
70 rotation_locked_orientation_(blink::WebScreenOrientationLockAny), 159 rotation_locked_orientation_(blink::WebScreenOrientationLockAny),
71 user_rotation_(display::Display::ROTATE_0), 160 user_rotation_(display::Display::ROTATE_0),
72 current_rotation_(display::Display::ROTATE_0) { 161 current_rotation_(display::Display::ROTATE_0) {
73 WmShell::Get()->AddShellObserver(this); 162 WmShell::Get()->AddShellObserver(this);
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
123 212
124 bool ScreenOrientationController::ScreenOrientationProviderSupported() const { 213 bool ScreenOrientationController::ScreenOrientationProviderSupported() const {
125 return WmShell::Get() 214 return WmShell::Get()
126 ->maximize_mode_controller() 215 ->maximize_mode_controller()
127 ->IsMaximizeModeWindowManagerEnabled() && 216 ->IsMaximizeModeWindowManagerEnabled() &&
128 !base::CommandLine::ForCurrentProcess()->HasSwitch( 217 !base::CommandLine::ForCurrentProcess()->HasSwitch(
129 switches::kAshDisableScreenOrientationLock); 218 switches::kAshDisableScreenOrientationLock);
130 } 219 }
131 220
132 void ScreenOrientationController::ToggleUserRotationLock() { 221 void ScreenOrientationController::ToggleUserRotationLock() {
222 if (!display::Display::HasInternalDisplay())
223 return;
224
133 if (user_rotation_locked()) { 225 if (user_rotation_locked()) {
134 user_locked_orientation_ = blink::WebScreenOrientationLockAny; 226 user_locked_orientation_ = blink::WebScreenOrientationLockAny;
135 } else { 227 } else {
136 display::Display::Rotation current_rotation = 228 display::Display::Rotation current_rotation =
137 WmShell::Get() 229 WmShell::Get()
138 ->GetDisplayInfo(display::Display::InternalDisplayId()) 230 ->GetDisplayInfo(display::Display::InternalDisplayId())
139 .GetActiveRotation(); 231 .GetActiveRotation();
140 blink::WebScreenOrientationLockType natural = 232 user_locked_orientation_ = RotationToOrientation(current_rotation);
141 GetDisplayNaturalOrientation();
142 if (natural == blink::WebScreenOrientationLockLandscape) {
143 switch (current_rotation) {
144 case display::Display::ROTATE_0:
145 user_locked_orientation_ =
146 blink::WebScreenOrientationLockLandscapePrimary;
147 break;
148 case display::Display::ROTATE_90:
149 user_locked_orientation_ =
150 blink::WebScreenOrientationLockPortraitPrimary;
151 break;
152 case display::Display::ROTATE_180:
153 user_locked_orientation_ =
154 blink::WebScreenOrientationLockLandscapeSecondary;
155 break;
156 case display::Display::ROTATE_270:
157 user_locked_orientation_ =
158 blink::WebScreenOrientationLockPortraitSecondary;
159 break;
160 }
161 } else { // Natural portrait
162 switch (current_rotation) {
163 case display::Display::ROTATE_0:
164 user_locked_orientation_ =
165 blink::WebScreenOrientationLockPortraitPrimary;
166 break;
167 case display::Display::ROTATE_90:
168 user_locked_orientation_ =
169 blink::WebScreenOrientationLockLandscapePrimary;
170 break;
171 case display::Display::ROTATE_180:
172 user_locked_orientation_ =
173 blink::WebScreenOrientationLockPortraitSecondary;
174 break;
175 case display::Display::ROTATE_270:
176 user_locked_orientation_ =
177 blink::WebScreenOrientationLockLandscapeSecondary;
178 break;
179 }
180 }
181 } 233 }
234 base::AutoReset<bool> auto_ignore_display_configuration_updates(
235 &ignore_display_configuration_updates_, true);
236 Shell::GetInstance()->display_manager()->RegisterDisplayRotationProperties(
237 user_rotation_locked(), OrientationToRotation(user_locked_orientation_));
238
182 ApplyLockForActiveWindow(); 239 ApplyLockForActiveWindow();
183
184 for (auto& observer : observers_) 240 for (auto& observer : observers_)
185 observer.OnUserRotationLockChanged(); 241 observer.OnUserRotationLockChanged();
186 } 242 }
187 243
188 void ScreenOrientationController::OnWindowActivated(WmWindow* gained_active, 244 void ScreenOrientationController::OnWindowActivated(WmWindow* gained_active,
189 WmWindow* lost_active) { 245 WmWindow* lost_active) {
190 ApplyLockForActiveWindow(); 246 ApplyLockForActiveWindow();
191 } 247 }
192 248
193 void ScreenOrientationController::OnWindowDestroying(aura::Window* window) { 249 void ScreenOrientationController::OnWindowDestroying(aura::Window* window) {
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
251 if (display::Display::HasInternalDisplay()) { 307 if (display::Display::HasInternalDisplay()) {
252 current_rotation_ = user_rotation_ = 308 current_rotation_ = user_rotation_ =
253 WmShell::Get() 309 WmShell::Get()
254 ->GetDisplayInfo(display::Display::InternalDisplayId()) 310 ->GetDisplayInfo(display::Display::InternalDisplayId())
255 .GetActiveRotation(); 311 .GetActiveRotation();
256 } 312 }
257 if (!rotation_locked_) 313 if (!rotation_locked_)
258 LoadDisplayRotationProperties(); 314 LoadDisplayRotationProperties();
259 chromeos::AccelerometerReader::GetInstance()->AddObserver(this); 315 chromeos::AccelerometerReader::GetInstance()->AddObserver(this);
260 WmShell::Get()->AddDisplayObserver(this); 316 WmShell::Get()->AddDisplayObserver(this);
317
318 if (!display::Display::HasInternalDisplay())
319 return;
320 ApplyLockForActiveWindow();
321 for (auto& observer : observers_)
322 observer.OnUserRotationLockChanged();
261 } 323 }
262 324
263 void ScreenOrientationController::OnMaximizeModeEnded() { 325 void ScreenOrientationController::OnMaximizeModeEnded() {
264 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this); 326 chromeos::AccelerometerReader::GetInstance()->RemoveObserver(this);
265 WmShell::Get()->RemoveDisplayObserver(this); 327 WmShell::Get()->RemoveDisplayObserver(this);
328 if (!display::Display::HasInternalDisplay())
329 return;
266 if (current_rotation_ != user_rotation_) 330 if (current_rotation_ != user_rotation_)
267 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER); 331 SetDisplayRotation(user_rotation_, display::Display::ROTATION_SOURCE_USER);
332 for (auto& observer : observers_)
333 observer.OnUserRotationLockChanged();
268 } 334 }
269 335
270 void ScreenOrientationController::SetDisplayRotation( 336 void ScreenOrientationController::SetDisplayRotation(
271 display::Display::Rotation rotation, 337 display::Display::Rotation rotation,
272 display::Display::RotationSource source) { 338 display::Display::RotationSource source) {
273 if (!display::Display::HasInternalDisplay()) 339 if (!display::Display::HasInternalDisplay())
274 return; 340 return;
275 current_rotation_ = rotation; 341 current_rotation_ = rotation;
276 base::AutoReset<bool> auto_ignore_display_configuration_updates( 342 base::AutoReset<bool> auto_ignore_display_configuration_updates(
277 &ignore_display_configuration_updates_, true); 343 &ignore_display_configuration_updates_, true);
278 344
279 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation( 345 Shell::GetInstance()->display_configuration_controller()->SetDisplayRotation(
280 display::Display::InternalDisplayId(), rotation, source); 346 display::Display::InternalDisplayId(), rotation, source);
281 } 347 }
282 348
283 void ScreenOrientationController::SetRotationLockedInternal( 349 void ScreenOrientationController::SetRotationLockedInternal(
284 bool rotation_locked) { 350 bool rotation_locked) {
285 if (rotation_locked_ == rotation_locked) 351 if (rotation_locked_ == rotation_locked)
286 return; 352 return;
287 rotation_locked_ = rotation_locked; 353 rotation_locked_ = rotation_locked;
288 if (!rotation_locked_) 354 if (!rotation_locked_)
289 rotation_locked_orientation_ = blink::WebScreenOrientationLockAny; 355 rotation_locked_orientation_ = blink::WebScreenOrientationLockAny;
290 if (!display::Display::HasInternalDisplay())
291 return;
292 base::AutoReset<bool> auto_ignore_display_configuration_updates(
293 &ignore_display_configuration_updates_, true);
294 Shell::GetInstance()->display_manager()->RegisterDisplayRotationProperties(
295 rotation_locked_, current_rotation_);
296 } 356 }
297 357
298 void ScreenOrientationController::LockRotation( 358 void ScreenOrientationController::LockRotation(
299 display::Display::Rotation rotation, 359 display::Display::Rotation rotation,
300 display::Display::RotationSource source) { 360 display::Display::RotationSource source) {
301 SetRotationLockedInternal(true); 361 SetRotationLockedInternal(true);
302 SetDisplayRotation(rotation, source); 362 SetDisplayRotation(rotation, source);
303 } 363 }
304 364
305 void ScreenOrientationController::LockRotationToOrientation( 365 void ScreenOrientationController::LockRotationToOrientation(
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after
435 SetDisplayRotation(new_rotation, 495 SetDisplayRotation(new_rotation,
436 display::Display::ROTATION_SOURCE_ACCELEROMETER); 496 display::Display::ROTATION_SOURCE_ACCELEROMETER);
437 } 497 }
438 } 498 }
439 499
440 void ScreenOrientationController::LoadDisplayRotationProperties() { 500 void ScreenOrientationController::LoadDisplayRotationProperties() {
441 display::DisplayManager* display_manager = 501 display::DisplayManager* display_manager =
442 Shell::GetInstance()->display_manager(); 502 Shell::GetInstance()->display_manager();
443 if (!display_manager->registered_internal_display_rotation_lock()) 503 if (!display_manager->registered_internal_display_rotation_lock())
444 return; 504 return;
445 SetDisplayRotation(display_manager->registered_internal_display_rotation(), 505 user_locked_orientation_ = RotationToOrientation(
446 display::Display::ROTATION_SOURCE_ACCELEROMETER); 506 display_manager->registered_internal_display_rotation());
447 SetRotationLockedInternal(true);
448 } 507 }
449 508
450 void ScreenOrientationController::ApplyLockForActiveWindow() { 509 void ScreenOrientationController::ApplyLockForActiveWindow() {
451 MruWindowTracker::WindowList mru_windows( 510 MruWindowTracker::WindowList mru_windows(
452 WmShell::Get()->mru_window_tracker()->BuildMruWindowList()); 511 WmShell::Get()->mru_window_tracker()->BuildMruWindowList());
453 512
454 for (WmWindow* window : mru_windows) { 513 for (WmWindow* window : mru_windows) {
455 if (!window->GetTargetVisibility()) 514 if (!window->GetTargetVisibility())
456 continue; 515 continue;
457 // TODO(oshima): If the application's orientation is landscape
458 // and user_locked_orientation_ is landscape primary or secondary,
459 // it should lock to the specific orientation. (and same for portrait).
460 for (auto const& pair : locking_windows_) { 516 for (auto const& pair : locking_windows_) {
461 if (pair.first->GetTargetVisibility() && window->Contains(pair.first)) { 517 if (pair.first->GetTargetVisibility() && window->Contains(pair.first)) {
462 LockRotationToOrientation(pair.second == 518 LockRotationToOrientation(
463 blink::WebScreenOrientationLockAny 519 ResolveOrientationLock(pair.second, user_locked_orientation_));
464 ? user_locked_orientation_
465 : pair.second);
466 return; 520 return;
467 } 521 }
468 } 522 }
469 // The default orientation for all chrome browser/apps windows is 523 // The default orientation for all chrome browser/apps windows is
470 // ANY, so use the user_locked_orientation_; 524 // ANY, so use the user_locked_orientation_;
471 if (window->GetTargetVisibility() && 525 if (window->GetTargetVisibility() &&
472 static_cast<AppType>(window->GetAppType()) != AppType::OTHERS) { 526 static_cast<AppType>(window->GetAppType()) != AppType::OTHERS) {
473 LockRotationToOrientation(user_locked_orientation_); 527 LockRotationToOrientation(user_locked_orientation_);
474 return; 528 return;
475 } 529 }
(...skipping 20 matching lines...) Expand all
496 } 550 }
497 551
498 bool ScreenOrientationController::CanRotateInLockedState() { 552 bool ScreenOrientationController::CanRotateInLockedState() {
499 return rotation_locked_orientation_ == 553 return rotation_locked_orientation_ ==
500 blink::WebScreenOrientationLockLandscape || 554 blink::WebScreenOrientationLockLandscape ||
501 rotation_locked_orientation_ == 555 rotation_locked_orientation_ ==
502 blink::WebScreenOrientationLockPortrait; 556 blink::WebScreenOrientationLockPortrait;
503 } 557 }
504 558
505 } // namespace ash 559 } // namespace ash
OLDNEW
« no previous file with comments | « ash/display/display_manager_unittest.cc ('k') | ash/system/chromeos/rotation/tray_rotation_lock_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698