Files
VirtualBuddy/VirtualUI/Source/Library/LibraryView.swift
Guilherme Rambo aabca5342d Xcode app provisioning (#823)
* Automatic provisioning of Mac apps launched via Xcode

* Improve Xcode integration sheet UI

* Security: harden local debugging socket and Xcode tool usage

* Soften rule around Xcode MCP use and xcodebuild prohibition

* Security: harden App Store Connect API key storage and access

* Fix debugged app incorrectly detected as not provisioned due to wildcard matching in provisioning profile

* Add previews for provisioning and debugging UI elements

* Disambiguate virtual machine vs. host Mac in provisioning and signing team management UIs

* Make Xcode debugging setup accessible from the Guest menu

* Completely disable debugging socket when developer features disabled, fix socket dying on accept failure

* Add versioning to scheme bindings, refer to VirtualBuddy components by a stable symlinked location, store scheme URLs so that app can potentially migrate schemes in the future

* Delay showing provisioning sheet so that an app that's already provisioned for the VM doesn't trigger it
2026-11-24 17:46:23 -03:00

230 lines
7.8 KiB
Swift
Raw 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.

//
// LibraryView.swift
// VirtualBuddy
//
// Created by Guilherme Rambo on 10/04/22.
//
import SwiftUI
import VirtualCore
public extension String {
static let vb_libraryWindowID = "library"
}
private extension UserDefaults {
/// Not using `AppStorage` because this can't update the view.
var hasSeenFirstLaunchExperience: Bool {
get { bool(forKey: #function) }
set {
set(newValue, forKey: #function)
synchronize()
}
}
}
public struct LibraryView: View {
@ObservedObject private var settingsContainer = VBSettingsContainer.current
@EnvironmentObject private var library: VMLibraryController
@EnvironmentObject private var sessionManager: VirtualMachineSessionUIManager
@Environment(\.openCocoaWindow)
private var openCocoaWindow
@Environment(\.openSettings)
private var openSettings
/// Whether the first launch experience has been presented in the current app session.
/// The defaults flag itself is not read again, but this is set whenever a non-empty library state is shown.
@State private var canShowFirstLaunchExperience = true
private var shouldShowFirstLaunchExperienceOnEmptyLibrary: Bool {
guard #available(macOS 15.0, *) else { return false }
return canShowFirstLaunchExperience && (!hasSeenFirstLaunchExperience || UserDefaults.standard.bool(forKey: "VBForceFirstLaunchExperience"))
}
/// Set on view initialization only so that when the defaults flag is updated, it doesn't cause the first launch experience to disappear.
private let hasSeenFirstLaunchExperience: Bool
public init() {
hasSeenFirstLaunchExperience = UserDefaults.standard.hasSeenFirstLaunchExperience
}
public var body: some View {
libraryContents
.frame(minWidth: 900, maxWidth: .infinity, minHeight: 600, maxHeight: .infinity)
.toolbar(content: { toolbarContents })
.task {
#if DEBUG
if UserDefaults.standard.bool(forKey: "VBOpenSettings") {
openSettings()
}
#endif
}
}
@ToolbarContentBuilder
private var toolbarContents: some ToolbarContent {
ToolbarItemGroup(placement: .primaryAction) {
Button {
openCocoaWindow {
VMInstallationWizard(library: library)
}
} label: {
Image(systemName: "plus")
}
.help("New virtual machine")
}
}
private var gridSpacing: CGFloat { 16 }
private var gridItemMinSize: CGFloat { 240 }
private var gridColumns: [GridItem] {
[.init(.adaptive(minimum: gridItemMinSize), spacing: gridSpacing)]
}
@ViewBuilder
private var libraryContents: some View {
ZStack {
BlurHashFullBleedBackground(content: .blurHash(.virtualBuddyBackground))
.ignoresSafeArea()
.frame(maxWidth: .infinity, maxHeight: .infinity)
.fullBleedBackgroundDimmed(![.loaded, .loading].contains(library.state.id))
Group {
switch library.state {
case .loaded(let machines):
grid(machines)
.task { canShowFirstLaunchExperience = false }
case .empty:
if shouldShowFirstLaunchExperienceOnEmptyLibrary {
FirstLaunchExperienceView {
sessionManager.launchInstallWizard(library: library)
}
.task { UserDefaults.standard.hasSeenFirstLaunchExperience = true }
} else {
libraryEmptyMessage
}
case .loading:
EmptyView()
case .volumeNotMounted:
libraryVolumeNotMountedMessage
case .directoryMissing:
libraryDirectoryMissingMessage
}
}
.transition(.scale(scale: 1.5).combined(with: .opacity))
}
.animation(.snappy, value: library.state.id)
}
@ViewBuilder
private func grid(_ machines: [VBVirtualMachine]) -> some View {
ScrollViewReader { proxy in
ScrollView(.vertical) {
LazyVGrid(columns: gridColumns, spacing: gridSpacing) {
ForEach(machines) { vm in
Button {
sessionManager.launch(vm, library: library, options: nil)
} label: {
LibraryItemView(vm: vm, name: vm.name)
}
.buttonStyle(.vbLibraryItem)
.environmentObject(library)
.transition(.scale(scale: 0.3).combined(with: .opacity))
.id(vm.id)
}
}
.padding()
.padding(.top)
}
/// Brings a virtual machine that was just duplicated into view.
.onChange(of: library.recentlyDuplicatedMachineIdentifier) { _, identifier in
guard let identifier else { return }
withAnimation { proxy.scrollTo(identifier, anchor: .center) }
}
}
.environment(\.virtualBuddyShowDesktopPictureThumbnails, settingsContainer.settings.showDesktopPictureThumbnails)
}
@ViewBuilder
private var libraryVolumeNotMountedMessage: some View {
BackportedContentUnavailableView(
"Library Not Mounted",
systemImage: "externaldrive.badge.questionmark",
description: Text("""
The volume containing your VirtualBuddy library is not currently mounted.
Once mounted, your virtual machines will appear here.
""")
) {
Button("Try Again") {
library.loadMachines()
}
.keyboardShortcut(.defaultAction)
Button("Open Settings") {
openSettings()
}
}
}
@ViewBuilder
private var libraryDirectoryMissingMessage: some View {
BackportedContentUnavailableView(
"Library Missing",
systemImage: "questionmark.folder",
description: Text("""
VirtualBuddy is unable to locate your library directory.
Review your settings to ensure the directory is set correctly. If it exists, there may be a permission problem.
If you’ve deleted the library directory, you can start a new empty library.
""")
) {
Button("Open Settings") {
openSettings()
}
.keyboardShortcut(.defaultAction)
Button("Create Empty Library") {
library.loadMachines(createLibrary: true)
}
}
}
@ViewBuilder
private var libraryEmptyMessage: some View {
BackportedContentUnavailableView(
"No Virtual Machines",
systemImage: "square.grid.2x2",
description: Text("""
You haven’t created any virtual machines yet. You can create a new one, or select a different library directory in settings.
""")
) {
Button("Create Virtual Machine") {
sessionManager.launchInstallWizard(library: library)
}
.keyboardShortcut(.defaultAction)
Button("Open Settings") {
openSettings()
}
}
}
}
fileprivate extension URL {
var collapsedHomePath: String {
path.replacingOccurrences(of: NSHomeDirectory(), with: "~")
}
}
#if DEBUG
#Preview {
LibraryView()
.environmentObject(VMLibraryController.preview)
.environmentObject(VirtualMachineSessionUIManager.shared)
}
#endif