2023-02-27 05:39:07 +00:00
|
|
|
import Combine
|
2023-01-23 17:43:48 +00:00
|
|
|
import UIKit
|
|
|
|
|
2023-12-03 11:43:15 +00:00
|
|
|
@Observable
|
|
|
|
public class SceneDelegate: NSObject, UIWindowSceneDelegate, Sendable {
|
2023-01-23 17:43:48 +00:00
|
|
|
public var window: UIWindow?
|
2023-12-19 08:51:20 +00:00
|
|
|
#if os(visionOS)
|
2024-02-14 11:48:14 +00:00
|
|
|
public private(set) var windowWidth: CGFloat = 0
|
|
|
|
public private(set) var windowHeight: CGFloat = 0
|
2023-12-19 08:51:20 +00:00
|
|
|
#else
|
2024-02-14 11:48:14 +00:00
|
|
|
public private(set) var windowWidth: CGFloat = UIScreen.main.bounds.size.width
|
|
|
|
public private(set) var windowHeight: CGFloat = UIScreen.main.bounds.size.height
|
2023-12-19 08:51:20 +00:00
|
|
|
#endif
|
2023-12-18 07:22:59 +00:00
|
|
|
|
2023-01-23 17:43:48 +00:00
|
|
|
public func scene(_ scene: UIScene,
|
2023-01-25 12:02:28 +00:00
|
|
|
willConnectTo _: UISceneSession,
|
|
|
|
options _: UIScene.ConnectionOptions)
|
|
|
|
{
|
2023-01-23 17:43:48 +00:00
|
|
|
guard let windowScene = scene as? UIWindowScene else { return }
|
2023-01-25 12:02:28 +00:00
|
|
|
window = windowScene.keyWindow
|
2023-12-18 07:22:59 +00:00
|
|
|
|
2023-10-23 17:12:25 +00:00
|
|
|
#if targetEnvironment(macCatalyst)
|
2023-11-01 17:58:44 +00:00
|
|
|
if let titlebar = windowScene.titlebar {
|
|
|
|
titlebar.titleVisibility = .hidden
|
|
|
|
titlebar.toolbar = nil
|
|
|
|
}
|
2023-10-23 17:12:25 +00:00
|
|
|
#endif
|
2023-01-23 17:43:48 +00:00
|
|
|
}
|
2023-12-03 11:43:15 +00:00
|
|
|
|
2023-12-18 07:22:59 +00:00
|
|
|
override public init() {
|
2023-12-03 11:43:15 +00:00
|
|
|
super.init()
|
2023-12-19 08:51:20 +00:00
|
|
|
#if os(visionOS)
|
2024-02-14 11:48:14 +00:00
|
|
|
windowWidth = window?.bounds.size.width ?? 0
|
|
|
|
windowHeight = window?.bounds.size.height ?? 0
|
2023-12-19 08:51:20 +00:00
|
|
|
#else
|
2024-02-14 11:48:14 +00:00
|
|
|
windowWidth = window?.bounds.size.width ?? UIScreen.main.bounds.size.width
|
|
|
|
windowHeight = window?.bounds.size.height ?? UIScreen.main.bounds.size.height
|
2023-12-19 08:51:20 +00:00
|
|
|
#endif
|
2023-12-03 11:43:15 +00:00
|
|
|
Self.observedSceneDelegate.insert(self)
|
|
|
|
_ = Self.observer // just for activating the lazy static property
|
|
|
|
}
|
|
|
|
|
|
|
|
deinit {
|
|
|
|
Task { @MainActor in
|
|
|
|
Self.observedSceneDelegate.remove(self)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private static var observedSceneDelegate: Set<SceneDelegate> = []
|
|
|
|
private static let observer = Task {
|
|
|
|
while true {
|
|
|
|
try? await Task.sleep(for: .seconds(0.1))
|
|
|
|
for delegate in observedSceneDelegate {
|
2023-12-19 08:51:20 +00:00
|
|
|
#if os(visionOS)
|
2024-02-14 11:48:14 +00:00
|
|
|
let newWidth = delegate.window?.bounds.size.width ?? 0
|
|
|
|
if delegate.windowWidth != newWidth {
|
|
|
|
delegate.windowWidth = newWidth
|
|
|
|
}
|
|
|
|
let newHeight = delegate.window?.bounds.size.height ?? 0
|
|
|
|
if delegate.windowHeight != newHeight {
|
|
|
|
delegate.windowHeight = newHeight
|
|
|
|
}
|
2023-12-19 08:51:20 +00:00
|
|
|
#else
|
2024-02-14 11:48:14 +00:00
|
|
|
let newWidth = delegate.window?.bounds.size.width ?? UIScreen.main.bounds.size.width
|
|
|
|
if delegate.windowWidth != newWidth {
|
|
|
|
delegate.windowWidth = newWidth
|
|
|
|
}
|
|
|
|
let newHeight = delegate.window?.bounds.size.height ?? UIScreen.main.bounds.size.height
|
|
|
|
if delegate.windowHeight != newHeight {
|
|
|
|
delegate.windowHeight = newHeight
|
|
|
|
}
|
2023-12-19 08:51:20 +00:00
|
|
|
#endif
|
2023-12-03 11:43:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-01-23 17:43:48 +00:00
|
|
|
}
|