mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 05:19:26 +08:00
Preventing vms from being deleted/renamed while booted
This commit is contained in:
@@ -14,6 +14,8 @@ import VirtualWormhole
|
||||
|
||||
@MainActor
|
||||
public final class VMInstance: NSObject, ObservableObject {
|
||||
|
||||
private let library = VMLibraryController.shared
|
||||
|
||||
private lazy var logger = Logger(for: Self.self)
|
||||
|
||||
@@ -157,6 +159,8 @@ public final class VMInstance: NSObject, ObservableObject {
|
||||
opts.bootMacOSRecovery = options.bootInRecoveryMode
|
||||
try await vm._start(with: opts)
|
||||
}
|
||||
|
||||
VMLibraryController.shared.bootedMachineIdentifiers.insert(self.virtualMachineModel.id)
|
||||
}
|
||||
|
||||
func pause() async throws {
|
||||
@@ -175,12 +179,16 @@ public final class VMInstance: NSObject, ObservableObject {
|
||||
let vm = try ensureVM()
|
||||
|
||||
try vm.requestStop()
|
||||
|
||||
library.bootedMachineIdentifiers.remove(virtualMachineModel.id)
|
||||
}
|
||||
|
||||
func forceStop() async throws {
|
||||
let vm = try ensureVM()
|
||||
|
||||
try await vm.stop()
|
||||
|
||||
library.bootedMachineIdentifiers.remove(virtualMachineModel.id)
|
||||
}
|
||||
|
||||
private func ensureVM() throws -> VZVirtualMachine {
|
||||
@@ -216,18 +224,22 @@ extension VMInstance: VZVirtualMachineDelegate {
|
||||
public func virtualMachine(_ virtualMachine: VZVirtualMachine, didStopWithError error: Error) {
|
||||
logger.error("Stopped with error: \(String(describing: error), privacy: .public)")
|
||||
|
||||
DispatchQueue.main.async {
|
||||
self._wormhole = nil
|
||||
DispatchQueue.main.async { [self] in
|
||||
library.bootedMachineIdentifiers.remove(virtualMachineModel.id)
|
||||
|
||||
self.onVMStop(error)
|
||||
_wormhole = nil
|
||||
|
||||
onVMStop(error)
|
||||
}
|
||||
}
|
||||
|
||||
public func guestDidStop(_ virtualMachine: VZVirtualMachine) {
|
||||
DispatchQueue.main.async {
|
||||
self._wormhole = nil
|
||||
DispatchQueue.main.async { [self] in
|
||||
library.bootedMachineIdentifiers.remove(virtualMachineModel.id)
|
||||
|
||||
_wormhole = nil
|
||||
|
||||
self.onVMStop(nil)
|
||||
onVMStop(nil)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -29,6 +29,8 @@ public final class VMLibraryController: ObservableObject {
|
||||
}
|
||||
|
||||
@Published public private(set) var virtualMachines: [VBVirtualMachine] = []
|
||||
|
||||
@Published public internal(set) var bootedMachineIdentifiers = Set<VBVirtualMachine.ID>()
|
||||
|
||||
public static let shared = VMLibraryController()
|
||||
|
||||
|
||||
@@ -22,8 +22,11 @@ struct VirtualMachineButtonStyle: ButtonStyle {
|
||||
|
||||
}
|
||||
|
||||
@MainActor
|
||||
struct LibraryItemView: View {
|
||||
|
||||
@EnvironmentObject var library: VMLibraryController
|
||||
|
||||
@State var vm: VBVirtualMachine
|
||||
@State var name: String
|
||||
var isPressed = false
|
||||
@@ -36,6 +39,8 @@ struct LibraryItemView: View {
|
||||
@State private var isShowingDuplicateSheet = false
|
||||
#endif
|
||||
|
||||
private var isVMBooted: Bool { library.bootedMachineIdentifiers.contains(vm.id) }
|
||||
|
||||
var body: some View {
|
||||
VStack(spacing: 12) {
|
||||
thumbnailView
|
||||
@@ -55,6 +60,7 @@ struct LibraryItemView: View {
|
||||
}
|
||||
}
|
||||
.font(.system(size: 16, weight: .medium, design: .rounded))
|
||||
.disabled(isVMBooted)
|
||||
}
|
||||
.padding([.leading, .trailing, .top], 8)
|
||||
.padding(.bottom, 12)
|
||||
@@ -147,6 +153,7 @@ struct LibraryItemView: View {
|
||||
} label: {
|
||||
Text("Rename")
|
||||
}
|
||||
.disabled(isVMBooted)
|
||||
|
||||
Divider()
|
||||
|
||||
@@ -155,12 +162,13 @@ struct LibraryItemView: View {
|
||||
do {
|
||||
try await VMLibraryController.shared.moveToTrash(vm)
|
||||
} catch {
|
||||
await NSAlert(error: error).runModal()
|
||||
NSAlert(error: error).runModal()
|
||||
}
|
||||
}
|
||||
} label: {
|
||||
Text("Move to Trash")
|
||||
}
|
||||
.disabled(isVMBooted)
|
||||
}
|
||||
|
||||
private func duplicate() {
|
||||
@@ -169,9 +177,9 @@ struct LibraryItemView: View {
|
||||
#else
|
||||
Task {
|
||||
do {
|
||||
try await VMLibraryController.shared.duplicate(vm)
|
||||
try VMLibraryController.shared.duplicate(vm)
|
||||
} catch {
|
||||
await NSAlert(error: error).runModal()
|
||||
NSAlert(error: error).runModal()
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -86,6 +86,7 @@ public struct LibraryView: View {
|
||||
launch(vm)
|
||||
}
|
||||
.buttonStyle(VirtualMachineButtonStyle(vm: vm))
|
||||
.environmentObject(library)
|
||||
}
|
||||
}
|
||||
.padding()
|
||||
|
||||
Reference in New Issue
Block a user