mainWindowScene

This commit is contained in:
xaoxuu 2022-09-20 17:11:45 +08:00
parent b62c027cd5
commit 238ffa1055
4 changed files with 15 additions and 3 deletions

View File

@ -21,7 +21,7 @@ class AlertWindow: Window {
}
let w: AlertWindow
if #available(iOS 13.0, *) {
if let scene = config.windowScene ?? UIApplication.shared.windows.last?.windowScene {
if let scene = config.windowScene ?? UIWindowScene.mainWindowScene {
w = .init(windowScene: scene)
} else {
w = .init(frame: UIScreen.main.bounds)

View File

@ -52,3 +52,15 @@ class Window: UIWindow {
}
}
@available(iOS 13.0, *)
extension UIWindowScene {
static var mainWindowScene: UIWindowScene? {
UIApplication.shared.connectedScenes.first(where: { scene in
guard let ws = scene as? UIWindowScene else { return false }
return ws.activationState == .foregroundActive
}) as? UIWindowScene
}
}

View File

@ -16,7 +16,7 @@ class SheetWindow: Window {
init(sheet: Sheet) {
self.sheet = sheet
if #available(iOS 13.0, *) {
if let scene = sheet.config.windowScene ?? UIApplication.shared.windows.last?.windowScene {
if let scene = sheet.config.windowScene ?? UIWindowScene.mainWindowScene {
super.init(windowScene: scene)
} else {
super.init(frame: UIScreen.main.bounds)

View File

@ -19,7 +19,7 @@ class ToastWindow: Window {
self.toast = toast
super.init(frame: .zero)
if #available(iOS 13.0, *) {
windowScene = toast.config.windowScene ?? UIApplication.shared.windows.last?.windowScene
windowScene = toast.config.windowScene ?? UIWindowScene.mainWindowScene
}
toast.window = self
windowLevel = .init(rawValue: UIWindow.Level.alert.rawValue + 1000)