UTMUnavailableVMView.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. //
  2. // Copyright © 2022 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. struct UTMUnavailableVMView: View {
  18. @ObservedObject var vm: VMData
  19. @EnvironmentObject private var data: UTMData
  20. var body: some View {
  21. UTMPlaceholderVMView(title: vm.detailsTitleLabel,
  22. subtitle: vm.detailsSubtitleLabel,
  23. progress: nil,
  24. imageOverlaySystemName: "questionmark.circle.fill",
  25. popover: {
  26. #if WITH_REMOTE
  27. UnsupportedVMDetailsView(vm: vm)
  28. #else
  29. WrappedVMDetailsView(path: vm.pathUrl.path, onRemove: remove)
  30. #endif
  31. },
  32. onRemove: remove)
  33. }
  34. private func remove() {
  35. data.listRemove(vm: vm)
  36. }
  37. }
  38. fileprivate struct WrappedVMDetailsView: View {
  39. let path: String
  40. let onRemove: () -> Void
  41. /// Check if the path in this wrapped VM is accessible
  42. var isAccessible: Bool {
  43. FileManager.default.fileExists(atPath: path)
  44. }
  45. var body: some View {
  46. VStack(alignment: .center) {
  47. Text(isAccessible ? "This virtual machine must be re-added to UTM by opening it with Finder. You can find it at the path: \(path)"
  48. : "This virtual machine cannot be found at: \(path)")
  49. .lineLimit(nil)
  50. .padding()
  51. if #available(iOS 15, macOS 12.0, *) {
  52. Button(role: .cancel, action: onRemove) {
  53. Label("Remove", systemImage: "xmark.circle")
  54. }
  55. .buttonStyle(.borderedProminent)
  56. .tint(.red)
  57. .padding([.bottom, .leading, .trailing])
  58. } else {
  59. Button(action: onRemove) {
  60. Label("Remove", systemImage: "xmark.circle")
  61. }
  62. .foregroundColor(.red)
  63. .padding([.bottom, .leading, .trailing])
  64. }
  65. }
  66. #if os(macOS)
  67. .frame(width: 230)
  68. #else
  69. .padding()
  70. #endif
  71. }
  72. }
  73. #if WITH_REMOTE
  74. fileprivate struct UnsupportedVMDetailsView: View {
  75. @ObservedObject var vm: VMData
  76. var body: some View {
  77. VStack(alignment: .center) {
  78. if let remotevm = vm as? VMRemoteData, let reason = remotevm.unavailableReason {
  79. Text(reason)
  80. .lineLimit(nil)
  81. } else {
  82. Text("This VM is unavailable.")
  83. }
  84. }
  85. #if os(macOS)
  86. .frame(width: 230)
  87. #else
  88. .padding()
  89. #endif
  90. }
  91. }
  92. #endif
  93. struct UTMUnavailableVMView_Previews: PreviewProvider {
  94. static var previews: some View {
  95. UTMUnavailableVMView(vm: VMData(from: UTMRegistryEntry.empty))
  96. }
  97. }