Prechádzať zdrojové kódy

macOS: support multiple VM windows

osy 5 rokov pred
rodič
commit
6852fb1d5a

+ 1 - 1
Platform/UTMData.swift

@@ -49,7 +49,7 @@ class UTMData: ObservableObject {
     }
     
     #if os(macOS)
-    var windowController: NSWindowController? = nil
+    var vmWindows: [UTMVirtualMachine: VMDisplayWindowController] = [:]
     #endif
     
     var fileManager: FileManager {

+ 8 - 2
Platform/macOS/Display/VMDisplayWindowController.swift

@@ -30,6 +30,7 @@ class VMDisplayWindowController: NSWindowController {
     @IBOutlet weak var sharedFolderToolbarItem: NSToolbarItem!
     
     var vm: UTMVirtualMachine!
+    var onClose: ((Notification) -> Void)?
     var vmMessage: String?
     var vmConfiguration: UTMConfiguration?
     var toolbarVisible: Bool = false // ignored
@@ -45,9 +46,10 @@ class VMDisplayWindowController: NSWindowController {
         self
     }
     
-    convenience init(vm: UTMVirtualMachine) {
+    convenience init(vm: UTMVirtualMachine, onClose: ((Notification) -> Void)?) {
         self.init(window: nil)
         self.vm = vm
+        self.onClose = onClose
         vm.delegate = self
     }
     
@@ -154,6 +156,10 @@ extension VMDisplayWindowController: NSWindowDelegate {
     func window(_ window: NSWindow, willUseFullScreenPresentationOptions proposedOptions: NSApplication.PresentationOptions = []) -> NSApplication.PresentationOptions {
         return [.autoHideToolbar, .autoHideMenuBar, .fullScreen]
     }
+    
+    func windowWillClose(_ notification: Notification) {
+        onClose?(notification)
+    }
 }
 
 // MARK: - Toolbar
@@ -170,7 +176,7 @@ extension VMDisplayWindowController: UTMVirtualMachineDelegate {
     func virtualMachine(_ vm: UTMVirtualMachine, transitionTo state: UTMVMState) {
         switch state {
         case .vmError:
-            let message = vmMessage ?? NSLocalizedString("An internal error has occured. UTM will terminate.", comment: "VMDisplayWindowController")
+            let message = vmMessage ?? NSLocalizedString("An internal error has occured.", comment: "VMDisplayWindowController")
             showErrorAlert(message)
         case .vmStopped, .vmPaused, .vmSuspended:
             enterSuspended(isBusy: false)

+ 15 - 7
Platform/macOS/UTMDataExtension.swift

@@ -19,14 +19,22 @@ import Foundation
 @available(macOS 11, *)
 extension UTMData {
     func run(vm: UTMVirtualMachine) {
-        if vm.configuration.displayConsoleOnly {
-            // TODO: console mode
-        } else {
-            self.windowController = VMDisplayMetalWindowController(vm: vm)
+        var window: VMDisplayWindowController? = vmWindows[vm]
+        if window == nil {
+            let close = { (notification: Notification) -> Void in
+                self.vmWindows.removeValue(forKey: vm)
+                window = nil
+            }
+            if vm.configuration.displayConsoleOnly {
+                // TODO: console mode
+            } else {
+                window = VMDisplayMetalWindowController(vm: vm, onClose: close)
+            }
         }
-        if let windowController = self.windowController {
-            windowController.showWindow(nil)
-            windowController.window!.makeMain()
+        if let unwrappedWindow = window {
+            vmWindows[vm] = unwrappedWindow
+            unwrappedWindow.showWindow(nil)
+            unwrappedWindow.window!.makeMain()
         } else {
             logger.critical("Failed to create window controller.")
         }