Files
__mahjong/components/render/ios/RenderIOS.swift
T

56 lines
2.0 KiB
Swift

import SwiftUI
import UIKit
// The prebuilt raylib iOS framework owns the app entry point (main()/
// AppDelegate/SceneDelegate in rcore_ios.c), so our SwiftUI @main shell is
// not used on iOS. Instead the C bridge calls rcios_present_overlay() from
// ios_ready(), and we stack a transparent window hosting ChkgfxView above
// raylib's window.
//
// ChkgfxComponent/ChkgfxVM live here because the component lifecycle is
// otherwise driven by the (iOS-unavailable) SwiftUI app shell.
enum RenderIOS {
static let chkgfx = ChkgfxComponent()
static let chkgfxVM = ChkgfxVM()
// Inert on iOS (the raylib framework drives the scene), but the generated
// dialect refers to them, so keep them alive.
static let render = RenderComponent()
static let renderVM = RenderVM()
}
@_cdecl("rcios_present_overlay")
public func rciosPresentOverlay() {
otherSetupConsoleLogging(RenderIOS.chkgfx.ctrl, "Chkgfx")
RenderIOS.chkgfx.setup()
DispatchQueue.main.async {
guard let scene = UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene })
.first(where: { $0.activationState == .foregroundActive })
?? UIApplication.shared.connectedScenes
.compactMap({ $0 as? UIWindowScene }).first
else { return }
let root = ChkgfxView(RenderIOS.chkgfxVM)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
let hosting = UIHostingController(rootView: root)
hosting.view.backgroundColor = .clear
let overlay = UIWindow(windowScene: scene)
overlay.rootViewController = hosting
overlay.backgroundColor = .clear
overlay.windowLevel = .alert + 1
overlay.isUserInteractionEnabled = false
overlay.isHidden = false
OverlayWindow.retained = overlay
}
}
// UIWindow is not retained by UIKit once we drop our reference, so hold it.
private enum OverlayWindow {
static var retained: UIWindow?
}