Skip to content

Commit fdbf6aa

Browse files
committed
iPad: when creating a new window, take over and close the previous one
1 parent 19c0072 commit fdbf6aa

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Mini vMac/DefaultSceneDelegate.swift

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import UIKit
1010

1111
class DefaultSceneDelegate: UIResponder, UIWindowSceneDelegate {
12-
var window: UIWindow?
12+
var window: UIWindow? // keep window reference to be able to set background colour before destroying
1313

1414
func scene(_ scene: UIScene, willConnectTo session: UISceneSession, options connectionOptions: UIScene.ConnectionOptions) {
1515
guard let windowScene = scene as? UIWindowScene else {
@@ -24,17 +24,32 @@ class DefaultSceneDelegate: UIResponder, UIWindowSceneDelegate {
2424
windowScene.sizeRestrictions?.maximumSize = size
2525

2626
window = UIWindow(windowScene: windowScene)
27-
let rootViewController = appDelegate.window?.rootViewController ?? UIStoryboard(name: "Main", bundle: .main).instantiateInitialViewController()
28-
2927
if let window {
3028
appDelegate.window = window
31-
window.rootViewController = rootViewController
29+
window.rootViewController = UIStoryboard(name: "Main", bundle: .main).instantiateInitialViewController()
3230
window.makeKeyAndVisible()
3331
}
32+
self.destroyOtherSessions(not: session)
33+
}
34+
35+
private func destroyOtherSessions(not session: UISceneSession) {
36+
let app = UIApplication.shared
37+
let options = UIWindowSceneDestructionRequestOptions()
38+
options.windowDismissalAnimation = .decline
39+
for otherSession in app.openSessions.filter({ $0 != session && $0.configuration.name == "Default"}) {
40+
if let window = (otherSession.scene as? UIWindowScene)?.windows.first {
41+
window.rootViewController?.view.removeFromSuperview()
42+
window.backgroundColor = .darkGray
43+
app.requestSceneSessionRefresh(otherSession)
44+
}
45+
app.requestSceneSessionDestruction(otherSession, options: options)
46+
// window will remain visible until window switcher is dismissed!
47+
}
3448
}
3549

3650
func sceneDidEnterBackground(_ scene: UIScene) {
37-
if UserDefaults.standard.bool(forKey: "runInBackground") == false {
51+
let app = UIApplication.shared
52+
if UserDefaults.standard.bool(forKey: "runInBackground") == false && app.connectedScenes.filter({ $0 != scene && $0.session.configuration.name == "Default"}).isEmpty {
3853
AppDelegate.emulator.isRunning = false
3954
}
4055
}

0 commit comments

Comments
 (0)