import SwiftUI import VirtualCore /// A virtual machine as it's shown in the sheets about identifier conflicts. struct IdentifierConflictMachine: Identifiable { var id: VBVirtualMachine.ID { vm.id } var vm: VBVirtualMachine /// The folder the virtual machine is in, when it's stored outside the library. /// The user knows where their library is, so it's never spelled out. var externalFolder: String? /// Where the virtual machine is stored, which is what tells apart copies that have the same name. var location: String { externalFolder ?? "In your library" } /// How the virtual machine is referred to in a sentence: by its name, or by where it is when others have the same name. func reference(among machines: [IdentifierConflictMachine]) -> String { guard machines.contains(where: { $0.id != id && $0.vm.name == vm.name }) else { return vm.name } return externalFolder.map { "the one in \($0)" } ?? "the one in your library" } } /// The features that find a virtual machine by its identifier and that the user has a use for, /// so that an identifier conflict is explained in terms of what they actually have set up. struct IdentifierConflictAffectedFeatures: OptionSet { let rawValue: Int /// Developer features are on and an Xcode scheme was created for the identifier. static let xcodeSchemes = Self(rawValue: 1 << 0) /// `vbctl` is allowed to control the app, so there may be scripts that name a virtual machine by its identifier. static let scripts = Self(rawValue: 1 << 1) } /// Explains that virtual machines in the library can't be told apart and asks which one should get a new identifier. struct ResolveIdentifierConflictSheet: View { /// The virtual machines that have the same identifier. var machines: [IdentifierConflictMachine] var affectedFeatures: IdentifierConflictAffectedFeatures /// Finishes with the virtual machine that gets a new identifier, or `nil` if the user cancelled. var onFinish: (VBVirtualMachine?) -> Void @State private var selection: VBVirtualMachine.ID /// - Parameter initialSelection: The virtual machine the user asked to fix the conflict from. init( machines: [IdentifierConflictMachine], affectedFeatures: IdentifierConflictAffectedFeatures, initialSelection: VBVirtualMachine.ID, onFinish: @escaping (VBVirtualMachine?) -> Void ) { self.machines = machines self.affectedFeatures = affectedFeatures self.onFinish = onFinish self._selection = State(initialValue: initialSelection) } /// What the user has already set up goes on finding the virtual machines that keep the identifier. /// `nil` when they have nothing set up that this would matter for. private func keepsUsingOthers(whenSelecting selection: VBVirtualMachine.ID) -> String? { let existingSetup: String if affectedFeatures.contains([.xcodeSchemes, .scripts]) { existingSetup = "Xcode schemes and scripts" } else if affectedFeatures.contains(.xcodeSchemes) { existingSetup = "Xcode schemes" } else if affectedFeatures.contains(.scripts) { existingSetup = "Scripts" } else { return nil } let others = machines .filter { $0.id != selection } .map { $0.reference(among: machines) } .formatted(.list(type: .and)) return "\(existingSetup) you’ve already set up keep using \(others)." } private var explanation: String { let consequence: String if affectedFeatures.contains([.xcodeSchemes, .scripts]) { consequence = "Until each has its own, features like Xcode debugging and vbctl can’t tell which one you mean." } else if affectedFeatures.contains(.xcodeSchemes) { consequence = "Until each has its own, Xcode debugging can’t tell which one you mean." } else if affectedFeatures.contains(.scripts) { consequence = "Until each has its own, vbctl can’t tell which one you mean." } else { consequence = "Until each has its own, VirtualBuddy may mix them up." } return "VirtualBuddy tells virtual machines apart by an internal identifier, and a copy made in Finder keeps the one from its original. \(consequence) Nothing inside the virtual machine changes." } var body: some View { IdentifierConflictSheetLayout( symbol: "exclamationmark.triangle.fill", title: "Can’t Tell These Apart", subtitle: "\(machines.count == 2 ? "Both" : "These") virtual machines have the same identifier.", confirmTitle: "Give New Identifier", onCancel: { onFinish(nil) }, onConfirm: { onFinish(machines.first { $0.id == selection }?.vm) } ) { VStack(alignment: .leading, spacing: 10) { Text("Choose which one gets a new identifier:") .font(.headline) ForEach(machines) { machine in IdentifierConflictMachineCard(machine: machine, accessory: .selection(isSelected: machine.id == selection)) { selection = machine.id } } if let keepsUsingOthers = keepsUsingOthers(whenSelecting: selection) { ZStack(alignment: .topLeading) { /// The sheet keeps the size it's presented with, so room is made for the longest /// of the sentences, which may take more lines than the one for the current selection. ForEach(machines) { machine in Text(self.keepsUsingOthers(whenSelecting: machine.id) ?? "") .fixedSize(horizontal: false, vertical: true) .hidden() .accessibilityHidden(true) } Text(keepsUsingOthers) .fixedSize(horizontal: false, vertical: true) } .font(.callout) .foregroundStyle(.secondary) .frame(maxWidth: .infinity, alignment: .leading) } } Text(explanation) .font(.caption) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) } } } /// Asks whether a virtual machine opened from outside the library can get a new identifier, /// which it needs in order to be told apart from the ones in the library. struct AddCopyIdentifierConflictSheet: View { /// The virtual machine that's being opened. var copy: IdentifierConflictMachine /// The virtual machines in the library that have the same identifier. var existing: [IdentifierConflictMachine] /// Finishes with whether the user agreed. var onFinish: (Bool?) -> Void var body: some View { IdentifierConflictSheetLayout( symbol: "plus.square.on.square", title: "Add as a Separate Virtual Machine?", subtitle: "It has the same identifier as one that’s already in your library.", confirmTitle: "Add and Open", onCancel: { onFinish(false) }, onConfirm: { onFinish(true) } ) { VStack(alignment: .leading, spacing: 10) { IdentifierConflictMachineCard(machine: copy, accessory: .tag("Gets New Identifier")) ForEach(existing) { machine in IdentifierConflictMachineCard(machine: machine, accessory: .none) } } Text("VirtualBuddy tells virtual machines apart by an internal identifier, and a copy made in Finder keeps the one from its original. Giving this one a new identifier lets you use both. Nothing inside the virtual machine changes.") .font(.caption) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) } } } // MARK: - Components private struct IdentifierConflictSheetLayout: View { var symbol: String var title: String var subtitle: String var confirmTitle: String var onCancel: () -> Void var onConfirm: () -> Void @ViewBuilder var content: Content var body: some View { VStack(alignment: .leading, spacing: 20) { HStack(alignment: .center, spacing: 14) { Image(systemName: symbol) .font(.system(size: 24, weight: .semibold)) .foregroundStyle(.white) .shadow(color: .black.opacity(0.2), radius: 2) .frame(width: 52, height: 52) .background(Color.accentColor.gradient, in: RoundedRectangle(cornerRadius: 14)) VStack(alignment: .leading, spacing: 3) { Text(title) .font(.title2.weight(.semibold)) Text(subtitle) .foregroundStyle(.secondary) .fixedSize(horizontal: false, vertical: true) } } content Divider() HStack { Button("Cancel", action: onCancel) .keyboardShortcut(.cancelAction) .airGlassButtonStyle() Spacer() Button(confirmTitle, action: onConfirm) .keyboardShortcut(.defaultAction) .airGlassButtonStyle(prominent: true) } .controlSize(.large) } .padding(24) .frame(width: 460) } } private struct IdentifierConflictMachineCard: View { enum Accessory { case none /// Says what happens to the virtual machine. case tag(String) /// The virtual machine is one of the options the user picks from. case selection(isSelected: Bool) } var machine: IdentifierConflictMachine var accessory: Accessory var action: (() -> Void)? @State private var isHovering = false private var isSelected: Bool { if case .selection(true) = accessory { true } else { false } } private var shape: RoundedRectangle { RoundedRectangle(cornerRadius: 12, style: .continuous) } var body: some View { if let action { Button(action: action) { card } .buttonStyle(.plain) .accessibilityAddTraits(isSelected ? .isSelected : []) .onHover { isHovering = $0 } } else { card .accessibilityElement(children: .combine) } } private var card: some View { HStack(alignment: .center, spacing: 12) { VMArtworkView(virtualMachine: machine.vm, alwaysUseBlurHash: true, iconSize: 18) .aspectRatio(contentMode: .fill) .frame(width: 64, height: 40) .clipShape(RoundedRectangle(cornerRadius: 6, style: .continuous)) VStack(alignment: .leading, spacing: 2) { Text(machine.vm.name) .font(.headline) .lineLimit(1) Text(machine.location) .font(.callout) .foregroundStyle(.secondary) .lineLimit(2) .truncationMode(.middle) .multilineTextAlignment(.leading) .fixedSize(horizontal: false, vertical: true) } Spacer(minLength: 0) switch accessory { case .none: EmptyView() case .tag(let title): Text(title) .font(.caption2.weight(.semibold)) .padding(.horizontal, 6) .padding(.vertical, 2) .background(Color.accentColor.opacity(0.18), in: Capsule()) .foregroundStyle(Color.accentColor) .fixedSize() case .selection(let isSelected): Image(systemName: isSelected ? "checkmark.circle.fill" : "circle") .font(.system(size: 18)) .foregroundStyle(isSelected ? Color.accentColor : Color.secondary.opacity(0.6)) } } .padding(12) .frame(maxWidth: .infinity, alignment: .leading) .background(shape.fill(Color.primary.opacity(isHovering && !isSelected ? 0.09 : 0.05))) .overlay(shape.strokeBorder(isSelected ? Color.accentColor.opacity(0.7) : Color.primary.opacity(0.12), lineWidth: isSelected ? 1.5 : 1)) .contentShape(shape) } } #if DEBUG #Preview("Resolve") { ResolveIdentifierConflictSheet( machines: [ IdentifierConflictMachine(vm: .preview), IdentifierConflictMachine(vm: .previewLinux, externalFolder: "/Volumes/Rambo/StandaloneVMTest"), ], affectedFeatures: [.xcodeSchemes, .scripts], initialSelection: VBVirtualMachine.previewLinux.id ) { _ in } } #Preview("Resolve - Xcode Only") { ResolveIdentifierConflictSheet( machines: [ IdentifierConflictMachine(vm: .preview), IdentifierConflictMachine(vm: .previewBlurHash, externalFolder: "/Volumes/Rambo/StandaloneVMTest"), ], affectedFeatures: .xcodeSchemes, initialSelection: VBVirtualMachine.previewBlurHash.id ) { _ in } } #Preview("Resolve - Nothing Set Up") { ResolveIdentifierConflictSheet( machines: [ IdentifierConflictMachine(vm: .preview), IdentifierConflictMachine(vm: .previewLinux), ], affectedFeatures: [], initialSelection: VBVirtualMachine.previewLinux.id ) { _ in } } #Preview("Add Copy") { AddCopyIdentifierConflictSheet( copy: IdentifierConflictMachine(vm: .preview, externalFolder: "/Volumes/Rambo/StandaloneVMTest"), existing: [IdentifierConflictMachine(vm: .preview)] ) { _ in } } #endif