VMConfigAppleSharingView.swift 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. //
  2. // Copyright © 2021 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. @available(macOS 12, *)
  18. struct VMConfigAppleSharingView: View {
  19. @ObservedObject var config: UTMAppleConfiguration
  20. @EnvironmentObject private var data: UTMData
  21. @State private var selectedID: UUID?
  22. @State private var isImporterPresented: Bool = false
  23. @State private var isAddReadOnly: Bool = false
  24. var body: some View {
  25. Form {
  26. if config.system.boot.operatingSystem == .macOS {
  27. Text("Shared directories in macOS VMs are only available in macOS 13 and later.")
  28. }
  29. Table(config.sharedDirectories, selection: $selectedID) {
  30. TableColumn("Shared Path") { share in
  31. Text(share.directoryURL?.path ?? "")
  32. }
  33. TableColumn("Read Only?") { share in
  34. Toggle("", isOn: .constant(share.isReadOnly))
  35. .disabled(true)
  36. .help("To change this, remove the shared directory and add it again.")
  37. }
  38. }
  39. HStack {
  40. Spacer()
  41. Button("Delete") {
  42. config.sharedDirectories.removeAll { share in
  43. share.id == selectedID
  44. }
  45. }.disabled(selectedID == nil)
  46. Button("Add") {
  47. isImporterPresented.toggle()
  48. }
  49. }.fileImporter(isPresented: $isImporterPresented, allowedContentTypes: [.folder]) { result in
  50. data.busyWorkAsync {
  51. let url = try result.get()
  52. if await config.sharedDirectories.contains(where: { existing in
  53. url == existing.directoryURL
  54. }) {
  55. throw NSLocalizedString("This directory is already being shared.", comment: "VMConfigAppleSharingView")
  56. }
  57. await MainActor.run {
  58. config.sharedDirectories.append(UTMAppleConfigurationSharedDirectory(directoryURL: url, isReadOnly: isAddReadOnly))
  59. }
  60. }
  61. }
  62. HStack {
  63. Spacer()
  64. Toggle("Add read only", isOn: $isAddReadOnly)
  65. }
  66. }
  67. }
  68. }
  69. @available(macOS 12, *)
  70. struct VMConfigAppleSharingView_Previews: PreviewProvider {
  71. @State static private var config = UTMAppleConfiguration()
  72. static var previews: some View {
  73. VMConfigAppleSharingView(config: config)
  74. }
  75. }