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

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

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

Powered by Google App Engine
This is Rietveld 408576698