Ver Fonte

scripting: properly create vm window on start

osy há 2 anos atrás
pai
commit
1b48c64967

+ 7 - 3
Platform/macOS/UTMDataExtension.swift

@@ -19,7 +19,7 @@ import Carbon.HIToolbox
 
 @available(macOS 11, *)
 extension UTMData {
-    @MainActor func run(vm: UTMVirtualMachine) {
+    @MainActor func run(vm: UTMVirtualMachine, startImmediately: Bool = true) {
         var window: Any? = vmWindows[vm]
         if window == nil {
             let close = { (notification: Notification) -> Void in
@@ -59,10 +59,14 @@ extension UTMData {
             vm.delegate = unwrappedWindow
             unwrappedWindow.showWindow(nil)
             unwrappedWindow.window!.makeMain()
-            unwrappedWindow.requestAutoStart()
+            if startImmediately {
+                unwrappedWindow.requestAutoStart()
+            }
         } else if let unwrappedWindow = window as? VMHeadlessSessionState {
             vmWindows[vm] = unwrappedWindow
-            unwrappedWindow.start()
+            if startImmediately {
+                vm.requestVmStart()
+            }
         } else {
             logger.critical("Failed to create window controller.")
         }

+ 5 - 5
Platform/macOS/VMHeadlessSessionState.swift

@@ -41,10 +41,11 @@ extension VMHeadlessSessionState: UTMVirtualMachineDelegate {
             vmState = state
             if state == .vmStarted {
                 hasStarted = true
+                didStart()
             }
             if state == .vmStopped {
                 if hasStarted {
-                    stop() // graceful exit
+                    didStop() // graceful exit
                 }
                 hasStarted = false
             }
@@ -57,19 +58,18 @@ extension VMHeadlessSessionState: UTMVirtualMachineDelegate {
             NotificationCenter.default.post(name: .vmSessionError, object: nil, userInfo: ["Session": self, "Message": message])
             if !hasStarted {
                 // if we got an error and haven't started, then cleanup
-                stop()
+                didStop()
             }
         }
     }
 }
 
 extension VMHeadlessSessionState {
-    func start() {
+    private func didStart() {
         NotificationCenter.default.post(name: .vmSessionCreated, object: nil, userInfo: ["Session": self])
-        vm.requestVmStart()
     }
     
-    func stop() {
+    private func didStop() {
         NotificationCenter.default.post(name: .vmSessionEnded, object: nil, userInfo: ["Session": self])
     }
 }

+ 1 - 0
Scripting/UTMScriptingVirtualMachineImpl.swift

@@ -122,6 +122,7 @@ class UTMScriptingVirtualMachineImpl: NSObject {
     
     @objc func start(_ command: NSScriptCommand) {
         withScriptCommand(command) { [self] in
+            data.run(vm: vm, startImmediately: false)
             if vm.state == .vmStopped {
                 try await vm.vmStart()
             } else if vm.state == .vmPaused {