visionOS: add “run in background” setting and respect it

This commit is contained in:
Jesús A. Álvarez 2024-02-17 14:52:50 +01:00
parent fca6943bfe
commit 2ccef4e3a6
3 changed files with 18 additions and 1 deletions

View File

@ -334,16 +334,20 @@ NSString *DocumentsChangedNotification = @"documentsChanged";
} }
} }
if ([self sceneWithName:@"Default"] == nil) { if ([self sceneWithName:@"Default"] == nil) {
[[AppDelegate sharedEmulator] setRunning:YES];
return [UISceneConfiguration configurationWithName:@"Default" sessionRole:UIWindowSceneSessionRoleApplication]; return [UISceneConfiguration configurationWithName:@"Default" sessionRole:UIWindowSceneSessionRoleApplication];
} }
return nil; return nil;
} }
- (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions { - (void)application:(UIApplication *)application didDiscardSceneSessions:(NSSet<UISceneSession *> *)sceneSessions {
// if only keyboard is left, close it too
if ([self sceneWithName:@"Default"] == nil) { if ([self sceneWithName:@"Default"] == nil) {
if ([[NSUserDefaults standardUserDefaults] boolForKey:@"runInBackground"] == NO) {
[[AppDelegate sharedEmulator] setRunning:NO];
}
UIScene *keyboardScene = [self sceneWithName:@"Keyboard"]; UIScene *keyboardScene = [self sceneWithName:@"Keyboard"];
if (keyboardScene != nil) { if (keyboardScene != nil) {
// if only keyboard is left, close it too
[application requestSceneSessionDestruction:keyboardScene.session options:nil errorHandler:nil]; [application requestSceneSessionDestruction:keyboardScene.session options:nil errorHandler:nil];
} }
} }

View File

@ -32,4 +32,14 @@ class DefaultSceneDelegate: UIResponder, UIWindowSceneDelegate {
window.makeKeyAndVisible() window.makeKeyAndVisible()
} }
} }
func sceneDidEnterBackground(_ scene: UIScene) {
if UserDefaults.standard.bool(forKey: "runInBackground") == false {
AppDelegate.emulator.isRunning = false
}
}
func sceneDidBecomeActive(_ scene: UIScene) {
AppDelegate.emulator.isRunning = true
}
} }

View File

@ -25,6 +25,7 @@ struct SettingsMenu: View {
struct SpeedMenu: View { struct SpeedMenu: View {
@AppStorage("speedValue") var currentSpeed: EmulatorSpeed = .speed1x @AppStorage("speedValue") var currentSpeed: EmulatorSpeed = .speed1x
@AppStorage("runInBackground") var runInBackground: Bool = false
private var currentSpeedImage: String { private var currentSpeedImage: String {
switch currentSpeed { switch currentSpeed {
case .speed1x: case .speed1x:
@ -54,6 +55,8 @@ struct SpeedMenu: View {
SpeedButton(label: "16x", speed: .speed16x) SpeedButton(label: "16x", speed: .speed16x)
SpeedButton(label: "32x", speed: .speed32x) SpeedButton(label: "32x", speed: .speed32x)
SpeedButton(label: "Unlimited", speed: .speedAllOut) SpeedButton(label: "Unlimited", speed: .speedAllOut)
Divider()
Toggle("Run in Background", isOn: $runInBackground)
}.menuOrder(.fixed) }.menuOrder(.fixed)
} }
} }