feat: support locking screen orientation, closes #860 (#1034)

This commit is contained in:
Huang Xin 2025-05-05 16:55:50 +08:00 committed by GitHub
parent 6299ea09b7
commit 5e04f6ae03
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
37 changed files with 375 additions and 21 deletions

View file

@ -12,6 +12,7 @@ import android.view.WindowManager
import android.view.WindowInsetsController
import android.graphics.Color
import android.webkit.WebView
import android.content.pm.ActivityInfo
import android.graphics.fonts.SystemFonts
import android.graphics.fonts.Font
import androidx.core.view.WindowCompat
@ -56,6 +57,11 @@ class InterceptKeysRequestArgs {
var backKey: Boolean? = null
}
@InvokeArg
class LockScreenOrientationRequestArgs {
var orientation: String? = null
}
interface KeyDownInterceptor {
fun interceptVolumeKeys(enabled: Boolean)
fun interceptBackKey(enabled: Boolean)
@ -326,4 +332,20 @@ class NativeBridgePlugin(private val activity: Activity): Plugin(activity) {
}
invoke.resolve()
}
@Command
fun lock_screen_orientation(invoke: Invoke) {
val args = invoke.parseArgs(LockScreenOrientationRequestArgs::class.java)
val orientation = args.orientation ?: "auto"
when (orientation) {
"portrait" -> activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
"landscape" -> activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
"auto" -> activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
else -> {
invoke.reject("Invalid orientation mode")
return
}
}
invoke.resolve()
}
}

View file

@ -8,6 +8,7 @@ const COMMANDS: &[&str] = &[
"get_status_bar_height",
"get_sys_fonts_list",
"intercept_keys",
"lock_screen_orientation",
];
fn main() {

View file

@ -44,6 +44,10 @@ class InterceptKeysRequestArgs: Decodable {
let volumeKeys: Bool?
}
class LockScreenOrientationRequestArgs: Decodable {
let orientation: String?
}
class VolumeKeyHandler: NSObject {
private var audioSession: AVAudioSession?
private var originalVolume: Float = 0.0
@ -150,6 +154,9 @@ class VolumeKeyHandler: NSObject {
class NativeBridgePlugin: Plugin {
private var webView: WKWebView?
private var authSession: ASWebAuthenticationSession?
private var isOrientationLocked = false
private var currentOrientationMask: UIInterfaceOrientationMask = .all
private var orientationObserver: NSObjectProtocol?
@objc public override func load(webview: WKWebView) {
self.webView = webview
@ -174,6 +181,10 @@ class NativeBridgePlugin: Plugin {
if volumeKeyHandler != nil {
activateVolumeKeyInterception()
}
if orientationObserver != nil {
self.setupOrientationObserver()
}
}
@objc func appDidEnterBackground() {
@ -338,6 +349,75 @@ class NativeBridgePlugin: Plugin {
invoke.reject(error.localizedDescription)
}
}
@objc public func lock_screen_orientation(_ invoke: Invoke) throws {
guard let args = try? invoke.parseArgs(LockScreenOrientationRequestArgs.self) else {
return invoke.reject("Invalid arguments")
}
DispatchQueue.main.async {
UIDevice.current.beginGeneratingDeviceOrientationNotifications()
let orientation = args.orientation ?? "auto"
switch orientation.lowercased() {
case "portrait":
self.isOrientationLocked = true
self.currentOrientationMask = .portrait
self.forceOrientation(.portrait)
self.setupOrientationObserver()
case "landscape":
self.isOrientationLocked = true
self.currentOrientationMask = .landscape
self.forceOrientation(.landscapeRight)
self.setupOrientationObserver()
case "auto":
self.isOrientationLocked = false
self.currentOrientationMask = .all
default:
invoke.reject("Invalid orientation mode")
return
}
invoke.resolve()
}
}
private func forceOrientation(_ orientation: UIInterfaceOrientation) {
if #available(iOS 16.0, *) {
if let windowScene = UIApplication.shared.connectedScenes.first as? UIWindowScene {
let orientationMask: UIInterfaceOrientationMask =
orientation.isPortrait ? .portrait : .landscape
for window in windowScene.windows {
if let rootVC = window.rootViewController {
rootVC.setNeedsUpdateOfSupportedInterfaceOrientations()
}
}
windowScene.requestGeometryUpdate(.iOS(interfaceOrientations: orientationMask)) { error in
print("Orientation update error: \(error.localizedDescription)")
}
}
} else {
UIDevice.current.setValue(orientation.rawValue, forKey: "orientation")
UIViewController.attemptRotationToDeviceOrientation()
}
}
private func setupOrientationObserver() {
orientationObserver = NotificationCenter.default.addObserver(
forName: UIDevice.orientationDidChangeNotification,
object: nil,
queue: .main
) { [weak self] _ in
guard let self = self, self.isOrientationLocked else { return }
if self.currentOrientationMask == .portrait {
self.forceOrientation(.portrait)
} else if self.currentOrientationMask == .landscape {
self.forceOrientation(.landscapeRight)
}
}
}
}
@_cdecl("init_plugin_native_bridge")

View file

@ -0,0 +1,13 @@
# Automatically generated - DO NOT EDIT!
"$schema" = "../../schemas/schema.json"
[[permission]]
identifier = "allow-lock-screen-orientation"
description = "Enables the lock_screen_orientation command without any pre-configured scope."
commands.allow = ["lock_screen_orientation"]
[[permission]]
identifier = "deny-lock-screen-orientation"
description = "Denies the lock_screen_orientation command without any pre-configured scope."
commands.deny = ["lock_screen_orientation"]

View file

@ -13,6 +13,7 @@ Default permissions for the plugin
- `allow-get-status-bar-height`
- `allow-get-sys-fonts-list`
- `allow-intercept-keys`
- `allow-lock-screen-orientation`
## Permission Table
@ -208,6 +209,32 @@ Denies the intercept_keys command without any pre-configured scope.
<tr>
<td>
`native-bridge:allow-lock-screen-orientation`
</td>
<td>
Enables the lock_screen_orientation command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`native-bridge:deny-lock-screen-orientation`
</td>
<td>
Denies the lock_screen_orientation command without any pre-configured scope.
</td>
</tr>
<tr>
<td>
`native-bridge:allow-set-system-ui-visibility`
</td>

View file

@ -10,4 +10,5 @@ permissions = [
"allow-get-status-bar-height",
"allow-get-sys-fonts-list",
"allow-intercept-keys",
"allow-lock-screen-orientation",
]

View file

@ -378,6 +378,18 @@
"const": "deny-intercept-keys",
"markdownDescription": "Denies the intercept_keys command without any pre-configured scope."
},
{
"description": "Enables the lock_screen_orientation command without any pre-configured scope.",
"type": "string",
"const": "allow-lock-screen-orientation",
"markdownDescription": "Enables the lock_screen_orientation command without any pre-configured scope."
},
{
"description": "Denies the lock_screen_orientation command without any pre-configured scope.",
"type": "string",
"const": "deny-lock-screen-orientation",
"markdownDescription": "Denies the lock_screen_orientation command without any pre-configured scope."
},
{
"description": "Enables the set_system_ui_visibility command without any pre-configured scope.",
"type": "string",
@ -403,10 +415,10 @@
"markdownDescription": "Denies the use_background_audio command without any pre-configured scope."
},
{
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`",
"description": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`",
"type": "string",
"const": "default",
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`"
"markdownDescription": "Default permissions for the plugin\n#### This default permission set includes:\n\n- `allow-auth-with-safari`\n- `allow-auth-with-custom-tab`\n- `allow-copy-uri-to-path`\n- `allow-use-background-audio`\n- `allow-install-package`\n- `allow-set-system-ui-visibility`\n- `allow-get-status-bar-height`\n- `allow-get-sys-fonts-list`\n- `allow-intercept-keys`\n- `allow-lock-screen-orientation`"
}
]
}

View file

@ -73,3 +73,11 @@ pub(crate) async fn intercept_keys<R: Runtime>(
) -> Result<()> {
app.native_bridge().intercept_keys(payload)
}
#[command]
pub(crate) async fn lock_screen_orientation<R: Runtime>(
app: AppHandle<R>,
payload: LockScreenOrientationRequest,
) -> Result<()> {
app.native_bridge().lock_screen_orientation(payload)
}

View file

@ -61,4 +61,11 @@ impl<R: Runtime> NativeBridge<R> {
pub fn intercept_keys(&self, _payload: InterceptKeysRequest) -> crate::Result<()> {
Err(crate::Error::UnsupportedPlatformError)
}
pub fn lock_screen_orientation(
&self,
_payload: LockScreenOrientationRequest,
) -> crate::Result<()> {
Err(crate::Error::UnsupportedPlatformError)
}
}

View file

@ -46,6 +46,7 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
commands::get_status_bar_height,
commands::get_sys_fonts_list,
commands::intercept_keys,
commands::lock_screen_orientation,
])
.setup(|app, api| {
#[cfg(mobile)]

View file

@ -101,3 +101,14 @@ impl<R: Runtime> NativeBridge<R> {
.map_err(Into::into)
}
}
impl<R: Runtime> NativeBridge<R> {
pub fn lock_screen_orientation(
&self,
payload: LockScreenOrientationRequest,
) -> crate::Result<()> {
self.0
.run_mobile_plugin("lock_screen_orientation", payload)
.map_err(Into::into)
}
}

View file

@ -80,3 +80,9 @@ pub struct InterceptKeysRequest {
pub volume_keys: Option<bool>,
pub back_key: Option<bool>,
}
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct LockScreenOrientationRequest {
pub orientation: String,
}