Files
VirtualBuddy/VirtualUI/Source/Library/IdentifierConflictPrompts.swift
Guilherme Rambo 5b4006330c Improve support for virtual machines stored outside the library (#830)
* Improve support for virtual machines stored outside the library

* Resolve VM identifier conflicts when opening from outside the library

* Improve UI for virtual machine UUID conflict resolution

* Moved sheet presentation code into generic helper

* Only mention Xcode and vbctl in conflict prompts when the corresponding features are enabled and in use for the affected VMs

* Add preference for disabling showing external virtual machines in the library
2026-11-30 14:36:32 -03:00

110 lines
4.7 KiB
Swift
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import SwiftUI
import VirtualCore
import MacDebugHost
/// Everything that's presented about virtual machines that can't be told apart because they have the same identifier,
/// which happens when a bundle is copied outside of VirtualBuddy.
@MainActor
enum IdentifierConflictPrompts {
/// Explains the conflict a virtual machine in the library has and offers to fix it by giving one of the virtual machines a new identifier.
static func resolveConflict(for vm: VBVirtualMachine, in library: VMLibraryController) async {
let others = library.identifierConflicts(for: vm)
/// The conflict may have been fixed from the other virtual machine since the user was shown the warning.
guard !others.isEmpty else { return }
let machines = ([vm] + others).map { library.conflictMachine(for: $0) }
let chosen: VBVirtualMachine? = await SheetPresenter.present(from: NSApp?.keyWindow) { finish in
ResolveIdentifierConflictSheet(machines: machines, affectedFeatures: affectedFeatures(for: vm), initialSelection: vm.id, onFinish: finish)
}
guard let chosen else { return }
do {
try library.assignNewIdentifier(to: chosen)
} catch {
NSAlert(error: error).runModal()
}
}
/// What the user has set up that finds virtual machines by the identifier of this one.
private static func affectedFeatures(for vm: VBVirtualMachine) -> IdentifierConflictAffectedFeatures {
var features = IdentifierConflictAffectedFeatures()
/// Virtual machines in a conflict have the same identifier, so a scheme created for any of them is found here.
/// Not being able to tell is treated as there being none, since Xcode is then unlikely to matter to the user.
let developerFeaturesEnabled = (try? VBSettings.current.requireDeveloperFeatures()) != nil
if developerFeaturesEnabled, (try? VirtualBuddyDebugging.bindings.bindings(forDestination: vm.uuid))?.isEmpty == false {
features.insert(.xcodeSchemes)
}
if CommandLineAccess.isEnabled {
features.insert(.scripts)
}
return features
}
/// Asks whether a virtual machine that's being opened from outside the library can be given a new identifier,
/// which it needs in order to be told apart from the ones already in the library.
static func confirmAddingCopy(_ vm: VBVirtualMachine, conflictingWith existing: [VBVirtualMachine], in library: VMLibraryController) async -> Bool {
let confirmed: Bool? = await SheetPresenter.present(from: NSApp?.keyWindow) { finish in
AddCopyIdentifierConflictSheet(
copy: library.conflictMachine(for: vm),
existing: existing.map { library.conflictMachine(for: $0) },
onFinish: finish
)
}
return confirmed == true
}
}
/// What's said in a single line of text about virtual machines that have the same identifier.
struct IdentifierConflictText {
private let subject: String
private let others: String
@MainActor
init(vm: VBVirtualMachine, others: [VBVirtualMachine], library: VMLibraryController) {
/// Copies often keep the name of the original, in which case only where they're stored tells them apart.
func label(for machine: VBVirtualMachine, among all: [VBVirtualMachine]) -> String {
guard all.contains(where: { $0.name == machine.name && $0.id != machine.id }) else { return "“\(machine.name)”" }
return library.isExternal(machine)
? "“\(machine.name)” in \(machine.containingFolderPath)"
: "“\(machine.name)” in your library"
}
let all = [vm] + others
self.subject = label(for: vm, among: all)
self.others = others.map { label(for: $0, among: all) }.formatted(.list(type: .and))
}
/// Shown where the warning is, before the user asks for the explanation.
var warningSummary: String {
"VirtualBuddy can’t tell this virtual machine apart from \(others). Click to fix."
}
/// For features that were asked to find one virtual machine and found several.
var ambiguityError: String {
"VirtualBuddy can’t tell \(subject) and \(others) apart. To fix this, click the warning on one of them in the VirtualBuddy library, then try again."
}
}
private extension VMLibraryController {
func conflictMachine(for vm: VBVirtualMachine) -> IdentifierConflictMachine {
IdentifierConflictMachine(vm: vm, externalFolder: isExternal(vm) ? vm.containingFolderPath : nil)
}
}
private extension VBVirtualMachine {
var containingFolderPath: String {
(bundleURL.deletingLastPathComponent().path as NSString).abbreviatingWithTildeInPath
}
}