Skip to content

Commit

Permalink
settings: add drive delete confirmation on macOS
Browse files Browse the repository at this point in the history
Resolves #4687
  • Loading branch information
osy committed Nov 27, 2022
1 parent fda1fe3 commit 88fad76
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
14 changes: 7 additions & 7 deletions Platform/Shared/VMConfigDriveDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ struct VMConfigDriveDetailsView: View {
}

@Binding var config: UTMQemuConfigurationDrive
let onDelete: (() -> Void)?
@Binding var requestDriveDelete: UTMQemuConfigurationDrive?

@EnvironmentObject private var data: UTMData
@State private var isImporterPresented: Bool = false
Expand Down Expand Up @@ -85,12 +85,12 @@ struct VMConfigDriveDetailsView: View {

#if os(macOS)
HStack {
if let onDelete = onDelete {
Button(action: onDelete) {
Label("Delete Drive", systemImage: "externaldrive.badge.minus")
.foregroundColor(.red)
}.help("Delete this drive.")
}
Button {
requestDriveDelete = config
} label: {
Label("Delete Drive", systemImage: "externaldrive.badge.minus")
.foregroundColor(.red)
}.help("Delete this drive.")

if let imageUrl = config.imageURL, FileManager.default.fileExists(atPath: imageUrl.path) {
Button {
Expand Down
8 changes: 5 additions & 3 deletions Platform/macOS/VMConfigAppleDriveDetailsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@ import SwiftUI

struct VMConfigAppleDriveDetailsView: View {
@Binding var config: UTMAppleConfigurationDrive
let onDelete: () -> Void
@Binding var requestDriveDelete: UTMAppleConfigurationDrive?

var body: some View {
Form {
TextField("Name", text: .constant(config.imageURL?.lastPathComponent ?? NSLocalizedString("(New Drive)", comment: "VMConfigAppleDriveDetailsView")))
.disabled(true)
Toggle("Read Only?", isOn: $config.isReadOnly)
Button(action: onDelete) {
Button {
requestDriveDelete = config
} label: {
Label("Delete Drive", systemImage: "externaldrive.badge.minus")
.foregroundColor(.red)
}.help("Delete this drive.")
Expand All @@ -35,6 +37,6 @@ struct VMConfigAppleDriveDetailsView: View {

struct VMConfigAppleDriveDetailsView_Previews: PreviewProvider {
static var previews: some View {
VMConfigAppleDriveDetailsView(config: .constant(UTMAppleConfigurationDrive(newSize: 100)), onDelete: {})
VMConfigAppleDriveDetailsView(config: .constant(UTMAppleConfigurationDrive(newSize: 100)), requestDriveDelete: .constant(nil))
}
}
18 changes: 11 additions & 7 deletions Platform/macOS/VMDrivesSettingsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ struct VMDrivesSettingsView<Drive: UTMConfigurationDrive>: View {
@EnvironmentObject private var data: UTMData
@State private var newDrivePopover: Bool = false
@State private var importDrivePresented: Bool = false
@State private var requestDriveDelete: Drive?

init(drives: Binding<[Drive]>, template: Drive) {
self._drives = drives
Expand All @@ -33,13 +34,11 @@ struct VMDrivesSettingsView<Drive: UTMConfigurationDrive>: View {
var body: some View {
ForEach($drives) { $drive in
let driveIndex = drives.firstIndex(of: drive)!
NavigationLink(destination: DriveDetailsView(config: $drive, onDelete: {
drives.removeAll(where: { $0 == drive })
}).scrollable()) {
NavigationLink(destination: DriveDetailsView(config: $drive, requestDriveDelete: $requestDriveDelete).scrollable()) {
Label(label(for: drive), systemImage: "externaldrive")
}.contextMenu {
DestructiveButton("Delete") {
drives.removeAll(where: { $0 == drive })
requestDriveDelete = drive
}
if driveIndex != 0 {
Button {
Expand Down Expand Up @@ -97,6 +96,11 @@ struct VMDrivesSettingsView<Drive: UTMConfigurationDrive>: View {
}
}.padding()
}
.alert(item: $requestDriveDelete) { drive in
Alert(title: Text("Are you sure you want to permanently delete this disk image?"), primaryButton: .cancel(), secondaryButton: .destructive(Text("Delete")) {
drives.removeAll(where: { $0 == drive })
})
}
}

private func label(for drive: Drive) -> String {
Expand Down Expand Up @@ -147,13 +151,13 @@ struct VMDrivesSettingsView<Drive: UTMConfigurationDrive>: View {

private struct DriveDetailsView<Drive: UTMConfigurationDrive>: View {
@Binding var config: Drive
let onDelete: () -> Void
@Binding var requestDriveDelete: Drive?

var body: some View {
if config is UTMQemuConfigurationDrive {
VMConfigDriveDetailsView(config: $config as Any as! Binding<UTMQemuConfigurationDrive>, onDelete: onDelete)
VMConfigDriveDetailsView(config: $config as Any as! Binding<UTMQemuConfigurationDrive>, requestDriveDelete: $requestDriveDelete as Any as! Binding<UTMQemuConfigurationDrive?>)
} else if config is UTMAppleConfigurationDrive {
VMConfigAppleDriveDetailsView(config: $config as Any as! Binding<UTMAppleConfigurationDrive>, onDelete: onDelete)
VMConfigAppleDriveDetailsView(config: $config as Any as! Binding<UTMAppleConfigurationDrive>, requestDriveDelete: $requestDriveDelete as Any as! Binding<UTMAppleConfigurationDrive?>)
} else {
fatalError("Unsupported drive type.")
}
Expand Down

0 comments on commit 88fad76

Please sign in to comment.