OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "ash/display/screen_orientation_delegate_chromeos.h" |
| 6 |
| 7 #include "ash/display/display_info.h" |
| 8 #include "ash/display/display_manager.h" |
| 9 #include "ash/shell.h" |
| 10 #include "ash/wm/maximize_mode/maximize_mode_controller.h" |
| 11 #include "content/public/browser/screen_orientation_provider.h" |
| 12 #include "content/public/browser/web_contents.h" |
| 13 #include "ui/aura/window.h" |
| 14 #include "ui/gfx/display.h" |
| 15 #include "ui/gfx/geometry/size.h" |
| 16 |
| 17 namespace { |
| 18 |
| 19 blink::WebScreenOrientationLockType GetDisplayNaturalOrientation() { |
| 20 ash::DisplayManager* display_manager = |
| 21 ash::Shell::GetInstance()->display_manager(); |
| 22 if (display_manager->HasInternalDisplay()) { |
| 23 ash::DisplayInfo info = |
| 24 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()); |
| 25 gfx::Display::Rotation rotation = info.rotation(); |
| 26 gfx::Size size = info.size_in_pixel(); |
| 27 if (((rotation == gfx::Display::ROTATE_0 || |
| 28 rotation == gfx::Display::ROTATE_180) && |
| 29 size.height() >= size.width()) || |
| 30 ((rotation == gfx::Display::ROTATE_90 || |
| 31 rotation == gfx::Display::ROTATE_270) && |
| 32 size.height() < size.width())) { |
| 33 return blink::WebScreenOrientationLockPortrait; |
| 34 } |
| 35 } |
| 36 return blink::WebScreenOrientationLockLandscape; |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
| 41 namespace ash { |
| 42 |
| 43 ScreenOrientationDelegate::ScreenOrientationDelegate() |
| 44 : locking_window_(NULL), |
| 45 natural_orientation_(GetDisplayNaturalOrientation()) { |
| 46 content::ScreenOrientationProvider::SetDelegate(this); |
| 47 } |
| 48 |
| 49 ScreenOrientationDelegate::~ScreenOrientationDelegate() { |
| 50 content::ScreenOrientationProvider::SetDelegate(NULL); |
| 51 } |
| 52 |
| 53 bool ScreenOrientationDelegate::FullScreenRequired( |
| 54 content::WebContents* web_contents) { |
| 55 return true; |
| 56 } |
| 57 |
| 58 bool ScreenOrientationDelegate::Lock( |
| 59 content::WebContents* web_contents, |
| 60 blink::WebScreenOrientationLockType lock_orientation) { |
| 61 aura::Window* requesting_window = web_contents->GetNativeView(); |
| 62 |
| 63 // TODO(jonross): Make ScreenOrientationDelegate responsible for rotation |
| 64 // lock. Have MaximizeModeController, and TrayRotationLock both use it |
| 65 // instead. |
| 66 MaximizeModeController* controller = |
| 67 Shell::GetInstance()->maximize_mode_controller(); |
| 68 |
| 69 // TODO(jonross): Track one rotation lock per window. When the active window |
| 70 // changes apply any corresponding rotation lock. |
| 71 if (!locking_window_) { |
| 72 // Exit on user set rotation lock. |
| 73 if (controller->rotation_locked()) |
| 74 return false; |
| 75 locking_window_ = requesting_window; |
| 76 } else if (requesting_window != locking_window_) { |
| 77 return false; |
| 78 } |
| 79 |
| 80 switch (lock_orientation) { |
| 81 case blink::WebScreenOrientationLockAny: |
| 82 controller->SetRotationLocked(false); |
| 83 locking_window_ = NULL; |
| 84 break; |
| 85 case blink::WebScreenOrientationLockDefault: |
| 86 NOTREACHED(); |
| 87 break; |
| 88 case blink::WebScreenOrientationLockPortraitPrimary: |
| 89 LockRotationToPrimaryOrientation(blink::WebScreenOrientationLockPortrait); |
| 90 break; |
| 91 case blink::WebScreenOrientationLockPortrait: |
| 92 LockToRotationMatchingOrientation(lock_orientation); |
| 93 break; |
| 94 case blink::WebScreenOrientationLockPortraitSecondary: |
| 95 LockRotationToSecondaryOrientation( |
| 96 blink::WebScreenOrientationLockPortrait); |
| 97 break; |
| 98 case blink::WebScreenOrientationLockLandscapeSecondary: |
| 99 LockRotationToSecondaryOrientation( |
| 100 blink::WebScreenOrientationLockLandscape); |
| 101 break; |
| 102 case blink::WebScreenOrientationLockLandscapePrimary: |
| 103 LockRotationToPrimaryOrientation( |
| 104 blink::WebScreenOrientationLockLandscape); |
| 105 break; |
| 106 case blink::WebScreenOrientationLockLandscape: |
| 107 LockToRotationMatchingOrientation(lock_orientation); |
| 108 break; |
| 109 case blink::WebScreenOrientationLockNatural: |
| 110 controller->LockRotation(gfx::Display::ROTATE_0); |
| 111 break; |
| 112 default: |
| 113 NOTREACHED(); |
| 114 break; |
| 115 } |
| 116 return true; |
| 117 } |
| 118 |
| 119 bool ScreenOrientationDelegate::ScreenOrientationProviderSupported() { |
| 120 return Shell::GetInstance() |
| 121 ->maximize_mode_controller() |
| 122 ->IsMaximizeModeWindowManagerEnabled(); |
| 123 } |
| 124 |
| 125 void ScreenOrientationDelegate::Unlock(content::WebContents* web_contents) { |
| 126 aura::Window* requesting_window = web_contents->GetNativeView(); |
| 127 if (requesting_window != locking_window_) |
| 128 return; |
| 129 locking_window_ = NULL; |
| 130 Shell::GetInstance()->maximize_mode_controller()->SetRotationLocked(false); |
| 131 } |
| 132 |
| 133 void ScreenOrientationDelegate::LockRotationToPrimaryOrientation( |
| 134 blink::WebScreenOrientationLockType lock_orientation) { |
| 135 Shell::GetInstance()->maximize_mode_controller()->LockRotation( |
| 136 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_0 |
| 137 : gfx::Display::ROTATE_90); |
| 138 } |
| 139 |
| 140 void ScreenOrientationDelegate::LockRotationToSecondaryOrientation( |
| 141 blink::WebScreenOrientationLockType lock_orientation) { |
| 142 Shell::GetInstance()->maximize_mode_controller()->LockRotation( |
| 143 natural_orientation_ == lock_orientation ? gfx::Display::ROTATE_180 |
| 144 : gfx::Display::ROTATE_270); |
| 145 } |
| 146 |
| 147 void ScreenOrientationDelegate::LockToRotationMatchingOrientation( |
| 148 blink::WebScreenOrientationLockType lock_orientation) { |
| 149 // TODO(jonross): Update MaximizeModeController to allow rotation between |
| 150 // two angles of an orientation (e.g. from ROTATE_0 to ROTATE_180, and from |
| 151 // ROTATE_90 to ROTATE_270) |
| 152 DisplayManager* display_manager = Shell::GetInstance()->display_manager(); |
| 153 if (!display_manager->HasInternalDisplay()) |
| 154 return; |
| 155 |
| 156 gfx::Display::Rotation rotation = |
| 157 display_manager->GetDisplayInfo(gfx::Display::InternalDisplayId()) |
| 158 .rotation(); |
| 159 MaximizeModeController* controller = |
| 160 Shell::GetInstance()->maximize_mode_controller(); |
| 161 if (natural_orientation_ == lock_orientation) { |
| 162 if (rotation == gfx::Display::ROTATE_0 || |
| 163 rotation == gfx::Display::ROTATE_180) { |
| 164 controller->SetRotationLocked(true); |
| 165 } else { |
| 166 controller->LockRotation(gfx::Display::ROTATE_0); |
| 167 } |
| 168 } else { |
| 169 if (rotation == gfx::Display::ROTATE_90 || |
| 170 rotation == gfx::Display::ROTATE_270) { |
| 171 controller->SetRotationLocked(true); |
| 172 } else { |
| 173 controller->LockRotation(gfx::Display::ROTATE_90); |
| 174 } |
| 175 } |
| 176 } |
| 177 |
| 178 } // namespace ash |
OLD | NEW |