mirror of
https://github.com/stablyai/orca.git
synced 2026-11-16 02:47:34 +00:00
* fix(mobile): keep iPad page headers clear of the window controls The reserved-regions probe reported the window's plain safe area, which on iPadOS 26+ windowed mode does not cover the window controls. On iOS 27.1+ pages take their top inset from that value, so the host header sat under the controls while native screens, which get the corner-adapted inset, cleared them. * fix(mobile): only raise the page's top inset for window controls
159 lines
5.7 KiB
Swift
159 lines
5.7 KiB
Swift
import ExpoModulesCore
|
|
import UIKit
|
|
|
|
// Why: iPhone Duo reserves part of the window for its fold (when partially folded) and its
|
|
// camera, and neither shows up in the safe area. React Native has no view of either, so the
|
|
// window's active regions are reported here in window points, the root view's coordinates.
|
|
public class OrcaReservedRegionsModule: Module {
|
|
private var probe: UIView?
|
|
private var keyWindowObserver: NSObjectProtocol?
|
|
|
|
public func definition() -> ModuleDefinition {
|
|
Name("OrcaReservedRegions")
|
|
|
|
Events("onChange")
|
|
|
|
OnStartObserving {
|
|
DispatchQueue.main.async { [weak self] in
|
|
guard let self else { return }
|
|
self.installProbe()
|
|
// A reconnected scene brings a new window; the probe follows it.
|
|
self.keyWindowObserver = NotificationCenter.default.addObserver(
|
|
forName: UIWindow.didBecomeKeyNotification, object: nil, queue: .main
|
|
) { [weak self] _ in
|
|
self?.installProbe()
|
|
}
|
|
}
|
|
}
|
|
|
|
OnStopObserving {
|
|
DispatchQueue.main.async { [weak self] in
|
|
guard let self else { return }
|
|
if let keyWindowObserver = self.keyWindowObserver {
|
|
NotificationCenter.default.removeObserver(keyWindowObserver)
|
|
}
|
|
self.keyWindowObserver = nil
|
|
self.probe?.removeFromSuperview()
|
|
self.probe = nil
|
|
}
|
|
}
|
|
}
|
|
|
|
private func installProbe() {
|
|
guard let window = UIApplication.shared.delegate?.window ?? nil, probe?.window !== window else {
|
|
return
|
|
}
|
|
probe?.removeFromSuperview()
|
|
probe = nil
|
|
#if canImport(UIKit, _version: 9127.0.85)
|
|
// 9127.0.85 is UIKit in the iOS 27.1 SDK, the first with reserved regions; older Xcodes skip this.
|
|
if #available(iOS 27.1, *) {
|
|
let probe = ReservedRegionsProbe(frame: window.bounds)
|
|
probe.onChange = { [weak self] payload in
|
|
self?.sendEvent("onChange", payload)
|
|
}
|
|
window.insertSubview(probe, at: 0)
|
|
self.probe = probe
|
|
}
|
|
#endif
|
|
}
|
|
}
|
|
|
|
#if canImport(UIKit, _version: 9127.0.85)
|
|
/// Invisible full-window view: it is laid out on every window resize, safe-area change and keyboard
|
|
/// move, and UIKit lays it out again when the reserved regions it read change (a fold that keeps the size).
|
|
@available(iOS 27.1, *)
|
|
private final class ReservedRegionsProbe: UIView {
|
|
var onChange: (([String: Any]) -> Void)?
|
|
private var reported: ReservedRegionsSnapshot?
|
|
/// The keyboard's latest end frame in screen coordinates; nil until the keyboard first moves.
|
|
private var keyboardFrame: CGRect?
|
|
|
|
override init(frame: CGRect) {
|
|
super.init(frame: frame)
|
|
autoresizingMask = [.flexibleWidth, .flexibleHeight]
|
|
isUserInteractionEnabled = false
|
|
isAccessibilityElement = false
|
|
// Not keyboardLayoutGuide: constraints on it crash UIKit's split keyboard on an unfolded Duo.
|
|
NotificationCenter.default.addObserver(
|
|
self, selector: #selector(keyboardWillChangeFrame(_:)),
|
|
name: UIResponder.keyboardWillChangeFrameNotification, object: nil)
|
|
}
|
|
|
|
required init?(coder: NSCoder) {
|
|
fatalError("init(coder:) is not supported")
|
|
}
|
|
|
|
@objc private func keyboardWillChangeFrame(_ notification: Notification) {
|
|
keyboardFrame = notification.userInfo?[UIResponder.keyboardFrameEndUserInfoKey] as? CGRect
|
|
setNeedsLayout()
|
|
}
|
|
|
|
override func safeAreaInsetsDidChange() {
|
|
super.safeAreaInsetsDidChange()
|
|
setNeedsLayout()
|
|
}
|
|
|
|
override func layoutSubviews() {
|
|
super.layoutSubviews()
|
|
guard let window else { return }
|
|
var keyboardInset: CGFloat = 0
|
|
if let keyboardFrame {
|
|
let overlap = window.convert(keyboardFrame, from: window.screen.coordinateSpace)
|
|
.intersection(window.bounds)
|
|
keyboardInset = overlap.isNull ? 0 : overlap.height
|
|
}
|
|
// Same top as the root's (OrcaWindowControlsInset), so pages clear iPad window controls too.
|
|
var safeAreaInsets = window.safeAreaInsets
|
|
safeAreaInsets.top = max(
|
|
safeAreaInsets.top, window.edgeInsets(for: .safeArea(cornerAdaptation: .vertical)).top)
|
|
let snapshot = ReservedRegionsSnapshot(
|
|
divisions: window.reservedRegions(kind: .division).map(\.frame),
|
|
hasFoldDivision: !window.reservedRegions(kind: .division, options: .includeInactive).isEmpty,
|
|
occlusions: window.reservedRegions(kind: .occlusion).map(\.frame),
|
|
windowBounds: window.bounds,
|
|
windowSafeAreaInsets: safeAreaInsets,
|
|
keyboardInset: keyboardInset
|
|
)
|
|
// The first layout always reports, so a listener starts from the real state.
|
|
guard snapshot != reported else { return }
|
|
reported = snapshot
|
|
onChange?(snapshot.payload)
|
|
}
|
|
}
|
|
|
|
private struct ReservedRegionsSnapshot: Equatable {
|
|
let divisions: [CGRect]
|
|
let hasFoldDivision: Bool
|
|
let occlusions: [CGRect]
|
|
let windowBounds: CGRect
|
|
let windowSafeAreaInsets: UIEdgeInsets
|
|
let keyboardInset: CGFloat
|
|
|
|
var payload: [String: Any] {
|
|
[
|
|
"divisions": divisions.map(Self.rectPayload),
|
|
"hasFoldDivision": hasFoldDivision,
|
|
"occlusions": occlusions.map(Self.rectPayload),
|
|
"windowSafeArea": [
|
|
"bounds": Self.rectPayload(windowBounds),
|
|
"insets": [
|
|
"top": Double(windowSafeAreaInsets.top),
|
|
"right": Double(windowSafeAreaInsets.right),
|
|
"bottom": Double(windowSafeAreaInsets.bottom),
|
|
"left": Double(windowSafeAreaInsets.left),
|
|
],
|
|
],
|
|
"keyboardInset": Double(keyboardInset),
|
|
]
|
|
}
|
|
|
|
private static func rectPayload(_ rect: CGRect) -> [String: Double] {
|
|
[
|
|
"x": Double(rect.minX), "y": Double(rect.minY), "width": Double(rect.width),
|
|
"height": Double(rect.height),
|
|
]
|
|
}
|
|
}
|
|
#endif
|