mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 02:10:27 +08:00
224 lines
6.8 KiB
Swift
224 lines
6.8 KiB
Swift
//
|
|
// LibraryItemView.swift
|
|
// VirtualUI
|
|
//
|
|
// Created by Guilherme Rambo on 21/07/22.
|
|
//
|
|
|
|
import SwiftUI
|
|
import VirtualCore
|
|
|
|
/// This button style achieves a couple of things:
|
|
/// - Gives its label a `vbLibraryButtonPressed` environment value that can be used to react to button presses
|
|
/// - Fixes an annoying behavior common to all standard SwiftUI button styles where pressing the space bar
|
|
/// with one of its subviews in focus would trigger the button's action instead of entering a space in a text field, for example
|
|
struct VBLibraryItemButtonStyle: PrimitiveButtonStyle {
|
|
|
|
@State private var isPressed = false
|
|
|
|
/// The rectangle of the button's contents in the local coordinate space.
|
|
@State private var rect: CGRect = .zero
|
|
|
|
func makeBody(configuration: Configuration) -> some View {
|
|
configuration.label
|
|
.environment(\.vbLibraryButtonPressed, isPressed)
|
|
.overlay {
|
|
GeometryReader { proxy in
|
|
Color.clear
|
|
.preference(key: VBLibraryButtonSizePreferenceKey.self, value: proxy.size)
|
|
}
|
|
}
|
|
.onPreferenceChange(VBLibraryButtonSizePreferenceKey.self) { rect = CGRect(origin: .zero, size: $0) }
|
|
.gesture(DragGesture(minimumDistance: 0, coordinateSpace: .local).onChanged { value in
|
|
/// Replicate standard button behavior where dragging outside the button cancels the click.
|
|
isPressed = rect.contains(value.location)
|
|
}.onEnded { _ in
|
|
/// If the button is not currently pressed, then don't perform the action.
|
|
guard isPressed else { return }
|
|
|
|
configuration.trigger()
|
|
|
|
isPressed = false
|
|
})
|
|
}
|
|
|
|
}
|
|
|
|
struct VBLibraryButtonSizePreferenceKey: PreferenceKey {
|
|
static var defaultValue: CGSize = .zero
|
|
|
|
static func reduce(value: inout CGSize, nextValue: () -> CGSize) {
|
|
value = nextValue()
|
|
}
|
|
}
|
|
|
|
extension PrimitiveButtonStyle where Self == VBLibraryItemButtonStyle {
|
|
static var vbLibraryItem: VBLibraryItemButtonStyle { VBLibraryItemButtonStyle() }
|
|
}
|
|
|
|
private struct VBLibraryItemButtonPressedEnvironmentKey: EnvironmentKey {
|
|
static var defaultValue = false
|
|
}
|
|
private extension EnvironmentValues {
|
|
var vbLibraryButtonPressed: VBLibraryItemButtonPressedEnvironmentKey.Value {
|
|
get { self[VBLibraryItemButtonPressedEnvironmentKey.self] }
|
|
set { self[VBLibraryItemButtonPressedEnvironmentKey.self] = newValue }
|
|
}
|
|
}
|
|
|
|
@MainActor
|
|
struct LibraryItemView: View {
|
|
|
|
@EnvironmentObject var library: VMLibraryController
|
|
|
|
@State var vm: VBVirtualMachine
|
|
@State var name: String
|
|
|
|
@Environment(\.vbLibraryButtonPressed)
|
|
private var isPressed
|
|
|
|
@State private var thumbnail = Image(nsImage: .thumbnailPlaceholder)
|
|
|
|
var nameFieldFocus = BoolSubject()
|
|
|
|
private var isVMBooted: Bool { library.bootedMachineIdentifiers.contains(vm.id) }
|
|
|
|
var body: some View {
|
|
VStack(spacing: 12) {
|
|
thumbnailView
|
|
.clipShape(RoundedRectangle(cornerRadius: 6, style: .continuous))
|
|
.shadow(color: Color.black.opacity(0.4), radius: 4)
|
|
|
|
EphemeralTextField($name, alignment: .leading, setFocus: nameFieldFocus) { name in
|
|
Text(name)
|
|
} editableContent: { name in
|
|
TextField("VM Name", text: name)
|
|
} validate: { name in
|
|
do {
|
|
try library.validateNewName(name, for: vm)
|
|
return true
|
|
} catch {
|
|
return false
|
|
}
|
|
}
|
|
.font(.system(size: 16, weight: .medium, design: .rounded))
|
|
.disabled(isVMBooted)
|
|
}
|
|
.padding([.leading, .trailing, .top], 8)
|
|
.padding(.bottom, 12)
|
|
.background(Material.thin, in: backgroundShape)
|
|
.background {
|
|
thumbnail
|
|
.resizable()
|
|
.blur(radius: 22)
|
|
.opacity(isPressed ? 0.1 : 0.4)
|
|
}
|
|
.clipShape(backgroundShape)
|
|
.shadow(color: Color.black.opacity(0.14), radius: 12)
|
|
.shadow(color: Color.black.opacity(0.56), radius: 1)
|
|
.scaleEffect(isPressed ? 0.96 : 1)
|
|
.onAppear { refreshThumbnail() }
|
|
.onReceive(vm.didInvalidateThumbnail) { refreshThumbnail() }
|
|
.contextMenu { contextMenuItems }
|
|
.onChange(of: name) { [name] newName in
|
|
guard newName != name else { return }
|
|
|
|
do {
|
|
try library.rename(vm, to: newName)
|
|
} catch {
|
|
NSAlert(error: error).runModal()
|
|
}
|
|
}
|
|
.onChange(of: vm.name) { [vm] updatedName in
|
|
guard updatedName != vm.name else { return }
|
|
self.name = updatedName
|
|
}
|
|
}
|
|
|
|
private func refreshThumbnail() {
|
|
if let nsImage = vm.thumbnailImage() {
|
|
thumbnail = Image(nsImage: nsImage)
|
|
} else {
|
|
thumbnail = Image(nsImage: .thumbnailPlaceholder)
|
|
}
|
|
}
|
|
|
|
private var backgroundShape: some InsettableShape {
|
|
RoundedRectangle(cornerRadius: 12, style: .continuous)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var thumbnailView: some View {
|
|
thumbnail
|
|
.resizable()
|
|
.aspectRatio(contentMode: .fill)
|
|
.frame(minWidth: 0, maxWidth: .infinity, minHeight: 0, maxHeight: .infinity, alignment: .bottom)
|
|
.clipped()
|
|
.aspectRatio(16/9, contentMode: .fit)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var contextMenuItems: some View {
|
|
Button {
|
|
NSWorkspace.shared.selectFile(vm.bundleURL.path, inFileViewerRootedAtPath: vm.bundleURL.deletingLastPathComponent().path)
|
|
} label: {
|
|
Label("Show in Finder", systemImage: "folder")
|
|
}
|
|
|
|
Divider()
|
|
|
|
Button {
|
|
duplicate()
|
|
} label: {
|
|
Text("Duplicate")
|
|
}
|
|
|
|
Button {
|
|
nameFieldFocus.send(true)
|
|
} label: {
|
|
Text("Rename")
|
|
}
|
|
.disabled(isVMBooted)
|
|
|
|
#if DEBUG
|
|
Button {
|
|
NSWorkspace.shared.open(vm.metadataDirectoryURL)
|
|
} label: {
|
|
Text("Open Data Folder…")
|
|
}
|
|
#endif
|
|
|
|
Divider()
|
|
|
|
Button {
|
|
library.performMoveToTrash(for: vm)
|
|
} label: {
|
|
Text("Move to Trash")
|
|
}
|
|
.disabled(isVMBooted)
|
|
}
|
|
|
|
private func duplicate() {
|
|
Task {
|
|
do {
|
|
try library.duplicate(vm)
|
|
} catch {
|
|
NSAlert(error: error).runModal()
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
extension VMLibraryController {
|
|
func performMoveToTrash(for vm: VBVirtualMachine) {
|
|
Task {
|
|
do {
|
|
try await moveToTrash(vm)
|
|
} catch {
|
|
NSAlert(error: error).runModal()
|
|
}
|
|
}
|
|
}
|
|
}
|