VMConfigDrivesView.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. //
  2. // Copyright © 2020 osy. All rights reserved.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. //
  16. import SwiftUI
  17. // MARK: - Drives list
  18. @available(iOS 14, *)
  19. struct VMConfigDrivesView: View {
  20. @ObservedObject var config: UTMQemuConfiguration
  21. @State private var createDriveVisible: Bool = false
  22. @State private var attemptDelete: IndexSet?
  23. @State private var importDrivePresented: Bool = false
  24. @EnvironmentObject private var data: UTMData
  25. var body: some View {
  26. Group {
  27. if config.countDrives == 0 {
  28. Text("No drives added.").font(.headline)
  29. } else {
  30. Text("Note: Boot order is as listed.")
  31. Form {
  32. List {
  33. ForEach(0..<config.countDrives, id: \.self) { index in
  34. let fileName = config.driveImagePath(for: index) ?? ""
  35. let displayName = config.driveRemovable(for: index) ? NSLocalizedString("Removable Drive", comment: "VMConfigDrivesView") : fileName
  36. let imageType = config.driveImageType(for: index)
  37. let interfaceType = config.driveInterfaceType(for: index) ?? ""
  38. NavigationLink(
  39. destination: VMConfigDriveDetailsView(config: config, index: index), label: {
  40. VStack(alignment: .leading) {
  41. Text(displayName)
  42. .lineLimit(1)
  43. HStack {
  44. Text(imageType.description).font(.caption)
  45. if imageType == .disk || imageType == .CD {
  46. Text("-")
  47. Text(interfaceType).font(.caption)
  48. }
  49. }
  50. }
  51. })
  52. }.onDelete { offsets in
  53. attemptDelete = offsets
  54. }
  55. .onMove(perform: moveDrives)
  56. }
  57. }
  58. }
  59. }
  60. .navigationBarItems(trailing:
  61. HStack {
  62. EditButton().padding(.trailing, 10)
  63. Button(action: { importDrivePresented.toggle() }, label: {
  64. Label("Import Drive", systemImage: "square.and.arrow.down").labelStyle(IconOnlyLabelStyle())
  65. }).padding(.trailing, 10)
  66. Button(action: { createDriveVisible.toggle() }, label: {
  67. Label("New Drive", systemImage: "plus").labelStyle(IconOnlyLabelStyle())
  68. })
  69. }
  70. )
  71. .fileImporter(isPresented: $importDrivePresented, allowedContentTypes: [.item], onCompletion: importDrive)
  72. .sheet(isPresented: $createDriveVisible) {
  73. CreateDrive(target: config.systemTarget, onDismiss: newDrive)
  74. }
  75. .actionSheet(item: $attemptDelete) { offsets in
  76. ActionSheet(title: Text("Confirm Delete"), message: Text("Are you sure you want to permanently delete this disk image?"), buttons: [.cancel(), .destructive(Text("Delete")) {
  77. deleteDrives(offsets: offsets)
  78. }])
  79. }
  80. }
  81. private func importDrive(result: Result<URL, Error>) {
  82. data.busyWork {
  83. switch result {
  84. case .success(let url):
  85. try data.importDrive(url, for: config)
  86. break
  87. case .failure(let err):
  88. throw err
  89. }
  90. }
  91. }
  92. private func newDrive(driveImage: VMDriveImage) {
  93. data.busyWork {
  94. try data.createDrive(driveImage, for: config)
  95. }
  96. }
  97. private func deleteDrives(offsets: IndexSet) {
  98. data.busyWork {
  99. for offset in offsets {
  100. try data.removeDrive(at: offset, for: config)
  101. }
  102. }
  103. }
  104. private func moveDrives(source: IndexSet, destination: Int) {
  105. for offset in source {
  106. let realDestination: Int
  107. if offset < destination {
  108. realDestination = destination - 1
  109. } else {
  110. realDestination = destination
  111. }
  112. config.moveDrive(offset, to: realDestination)
  113. }
  114. }
  115. }
  116. // MARK: - Create Drive
  117. @available(iOS 14, *)
  118. private struct CreateDrive: View {
  119. let target: String?
  120. let onDismiss: (VMDriveImage) -> Void
  121. @StateObject private var driveImage = VMDriveImage()
  122. @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
  123. init(target: String?, onDismiss: @escaping (VMDriveImage) -> Void) {
  124. self.target = target
  125. self.onDismiss = onDismiss
  126. }
  127. var body: some View {
  128. NavigationView {
  129. VMConfigDriveCreateView(target: target, driveImage: driveImage)
  130. .navigationBarItems(leading: Button(action: cancel, label: {
  131. Text("Cancel")
  132. }), trailing: Button(action: done, label: {
  133. Text("Done")
  134. }))
  135. }.navigationViewStyle(StackNavigationViewStyle())
  136. .onAppear {
  137. driveImage.reset(forSystemTarget: target, removable: false)
  138. }
  139. }
  140. private func cancel() {
  141. presentationMode.wrappedValue.dismiss()
  142. }
  143. private func done() {
  144. presentationMode.wrappedValue.dismiss()
  145. onDismiss(driveImage)
  146. }
  147. }
  148. // MARK: - Preview
  149. @available(iOS 14, *)
  150. struct VMConfigDrivesView_Previews: PreviewProvider {
  151. @ObservedObject static private var config = UTMQemuConfiguration()
  152. static var previews: some View {
  153. Group {
  154. VMConfigDrivesView(config: config)
  155. CreateDrive(target: nil) { _ in
  156. }
  157. }.onAppear {
  158. if config.countDrives == 0 {
  159. config.newDrive("", path: "test.img", type: .disk, interface: "ide")
  160. config.newDrive("", path: "bios.bin", type: .BIOS, interface: "none")
  161. }
  162. }
  163. }
  164. }