This commit is contained in:
Михаил Капелько committed 2026-10-22 15:55:54 +03:00
1 parent 0af2d75754
commit aca30bfe6a
1 file changed
+33 -14
+33 -14
View File
@@ -2,10 +2,17 @@ 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.
// AppDelegate/SceneDelegate in rcore_ios.c). Its SceneDelegate creates the
// window, installs GameViewController as the root and only then calls
// ios_ready(), so by the time rcios_present_overlay() runs there is already a
// key window with a GL-backed root view.
//
// We deliberately do NOT create a second UIWindow: that would sit at a fixed
// windowLevel and break alerts/buttons later. Instead we add the SwiftUI
// content as a child UIViewController of raylib's root view controller, so:
// - buttons receive touches through the normal responder chain;
// - .alert / sheets present correctly in the same window;
// - rotation and geometry changes apply to both layers at once.
//
// ChkgfxComponent/ChkgfxVM live here because the component lifecycle is
// otherwise driven by the (iOS-unavailable) SwiftUI app shell.
@@ -32,24 +39,36 @@ public func rciosPresentOverlay() {
.compactMap({ $0 as? UIWindowScene }).first
else { return }
// raylib's SceneDelegate made its window key before calling ios_ready().
guard let window = scene.windows.first(where: { $0.isKeyWindow })
?? scene.windows.first,
let host = window.rootViewController
else { return }
let root = ChkgfxView(RenderIOS.chkgfxVM)
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
let hosting = UIHostingController(rootView: root)
hosting.view.backgroundColor = .clear
// The UI is a passive title for now. Once it gains buttons, drop this
// line; touches will then stay in the SwiftUI layer over the whole
// screen instead of reaching raylib's GameViewController.
hosting.view.isUserInteractionEnabled = false
let overlay = UIWindow(windowScene: scene)
overlay.rootViewController = hosting
overlay.backgroundColor = .clear
overlay.windowLevel = .alert + 1
overlay.isUserInteractionEnabled = false
overlay.isHidden = false
host.addChild(hosting)
hosting.view.frame = host.view.bounds
hosting.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
host.view.addSubview(hosting.view)
hosting.didMove(toParent: host)
OverlayWindow.retained = overlay
// addChild retains the hosting controller for as long as it stays
// attached, so no extra strong reference is needed here.
OverlayHost.retained = hosting
}
}
// UIWindow is not retained by UIKit once we drop our reference, so hold it.
private enum OverlayWindow {
static var retained: UIWindow?
// Optional handle for later use (e.g. presenting an alert from the hosting
// controller). The parent view controller keeps it alive while attached.
private enum OverlayHost {
static weak var retained: UIViewController?
}