Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(@capacitor/screen-orientation): Orientation 'landscape' lock uses current held landscape #2040

Open
wants to merge 6 commits into
base: 5.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ private int fromOrientationTypeToEnum(String orientationType) {
case "any":
return ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
case "landscape":
return ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE;
case "landscape-primary":
return ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;
case "landscape-secondary":
Expand Down
26 changes: 15 additions & 11 deletions screen-orientation/ios/Plugin/ScreenOrientation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,19 @@ public class ScreenOrientation: NSObject {
return fromDeviceOrientationToOrientationType(currentOrientation)
}

private func lockLegacy(_ orientation: Int) {
private func lockLegacy(_ orientation: Any) {
UIDevice.current.setValue(orientation, forKey: "orientation")
UINavigationController.attemptRotationToDeviceOrientation()
}

public func lock(_ orientationType: String, completion: @escaping (Error?) -> Void) {
DispatchQueue.main.async {
let orientation = self.fromOrientationTypeToInt(orientationType)
self.capViewController?.supportedOrientations = [orientation]
if let orientation = orientation as? Int {
self.capViewController?.supportedOrientations = [orientation]
} else {
self.capViewController?.supportedOrientations = orientation as! [Int]
}
let mask = self.fromOrientationTypeToMask(orientationType)
if #available(iOS 16.0, *) {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
Expand Down Expand Up @@ -84,12 +88,13 @@ public class ScreenOrientation: NSObject {
switch orientationType {
case "any":
return UIInterfaceOrientationMask.all
case "landscape", "landscape-primary":
case "landscape":
return UIInterfaceOrientationMask.landscape
case "landscape-primary":
return UIInterfaceOrientationMask.landscapeLeft
codepip55 marked this conversation as resolved.
Show resolved Hide resolved
case "landscape-secondary":
// UIInterfaceOrientationMask.landscapeRight is the same as UIDeviceOrientation.landscapeLeft
return UIInterfaceOrientationMask.landscapeRight
codepip55 marked this conversation as resolved.
Show resolved Hide resolved
case "landscape-secondary":
// UIInterfaceOrientationMask.landscapeLeft is the same as UIDeviceOrientation.landscapeRight
return UIInterfaceOrientationMask.landscapeLeft
case "portrait-secondary":
return UIInterfaceOrientationMask.portraitUpsideDown
default:
Expand All @@ -98,14 +103,13 @@ public class ScreenOrientation: NSObject {
}
}

private func fromOrientationTypeToInt(_ orientationType: String) -> Int {
private func fromOrientationTypeToInt(_ orientationType: String) -> Any {
switch orientationType {
case "any":
return UIInterfaceOrientation.unknown.rawValue
case "landscape", "landscape-primary":
// UIInterfaceOrientation.landscapeRight is the same as UIDeviceOrientation.landscapeLeft
codepip55 marked this conversation as resolved.
Show resolved Hide resolved
// @see https://developer.apple.com/documentation/uikit/uiinterfaceorientation/landscaperight
// @see https://developer.apple.com/documentation/uikit/uideviceorientation/landscapeleft
case "landscape":
return [UIInterfaceOrientation.landscapeLeft.rawValue, UIInterfaceOrientation.landscapeRight.rawValue]
case "landscape-primary":
return UIInterfaceOrientation.landscapeRight.rawValue
case "landscape-secondary":
// UIInterfaceOrientation.landscapeLeft is the same as UIDeviceOrientation.landscapeRight
Expand Down