orientation

This commit is contained in:
agra
2026-04-22 16:22:45 +03:00
parent 3032442c31
commit 2113537078
14 changed files with 231 additions and 16 deletions

View File

@@ -86,7 +86,7 @@ public func ux_keyboard_anim_gen() -> Int32 {
// MARK: - Plugin
public class KeyboardPlugin: NSObject, FlutterPlugin {
public class KeyboardPlugin: NSObject, NativePlugin {
fileprivate static var shared: KeyboardPlugin?
fileprivate var wakeCallback: WakeCallback?
@@ -112,15 +112,9 @@ public class KeyboardPlugin: NSObject, FlutterPlugin {
fileprivate var animDuration: Double = 0
fileprivate var animGeneration: Int32 = 0
public static func register(with registrar: FlutterPluginRegistrar) {
let instance = KeyboardPlugin()
KeyboardPlugin.shared = instance
instance.startObserving()
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result(FlutterMethodNotImplemented)
public func register(with registrar: FlutterPluginRegistrar) {
KeyboardPlugin.shared = self
startObserving()
}
/// Wake Dart so it reads the height on its next frame

View File

@@ -0,0 +1,5 @@
import Flutter
public protocol NativePlugin {
func register(with registrar: FlutterPluginRegistrar)
}

View File

@@ -0,0 +1,34 @@
import CoreMotion
import Flutter
import UIKit
@_cdecl("ux_device_orientation")
public func ux_device_orientation() -> Int32 {
return SensorPlugin.shared?.currentIndex ?? 0
}
public class SensorPlugin: NSObject, NativePlugin {
fileprivate static var shared: SensorPlugin?
fileprivate var currentIndex: Int32 = 0
private let motion = CMMotionManager()
public func register(with registrar: FlutterPluginRegistrar) {
SensorPlugin.shared = self
guard motion.isDeviceMotionAvailable else { return }
motion.deviceMotionUpdateInterval = 1.0 / 10.0
motion.startDeviceMotionUpdates(to: .main) { [weak self] data, _ in
guard let self = self, let g = data?.gravity else { return }
if abs(g.z) > 0.8 { return }
let next: Int32
if abs(g.x) > abs(g.y) {
next = g.x < 0 ? 1 : 3
} else {
next = g.y < 0 ? 0 : 2
}
if next != self.currentIndex {
self.currentIndex = next
}
}
}
}

View File

@@ -0,0 +1,20 @@
import Flutter
import UIKit
public class UxPlugin: NSObject, FlutterPlugin {
private static var plugins: [NativePlugin] = []
public static func register(with registrar: FlutterPluginRegistrar) {
plugins = [
KeyboardPlugin(),
SensorPlugin(),
]
for plugin in plugins {
plugin.register(with: registrar)
}
}
public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
result(FlutterMethodNotImplemented)
}
}