VMWizardView.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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 11, *)
  18. struct VMWizardView: View {
  19. @StateObject var wizardState = VMWizardState()
  20. @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
  21. @EnvironmentObject private var data: UTMData
  22. /// SwiftUI BUG: on macOS 12, when VoiceOver is enabled and isBusy changes
  23. /// the disable state of a button being clicked, the app crashes
  24. private var isNeverDisabledWorkaround: Bool {
  25. #if os(macOS)
  26. if #available(macOS 12, *) {
  27. if #unavailable(macOS 13) {
  28. return false
  29. }
  30. }
  31. return true
  32. #else
  33. return true
  34. #endif
  35. }
  36. var body: some View {
  37. Group {
  38. switch wizardState.currentPage {
  39. case .start:
  40. VMWizardStartView(wizardState: wizardState)
  41. .transition(wizardState.slide)
  42. case .operatingSystem:
  43. VMWizardOSView(wizardState: wizardState)
  44. .transition(wizardState.slide)
  45. case .otherBoot:
  46. VMWizardOSOtherView(wizardState: wizardState)
  47. .transition(wizardState.slide)
  48. case .macOSBoot:
  49. if #available(macOS 12, *) {
  50. VMWizardOSMacView(wizardState: wizardState)
  51. .transition(wizardState.slide)
  52. }
  53. case .linuxBoot:
  54. VMWizardOSLinuxView(wizardState: wizardState)
  55. .transition(wizardState.slide)
  56. case .windowsBoot:
  57. VMWizardOSWindowsView(wizardState: wizardState)
  58. .transition(wizardState.slide)
  59. case .classicMacOSBoot:
  60. VMWizardOSClassicMacView(wizardState: wizardState)
  61. .transition(wizardState.slide)
  62. case .hardware:
  63. VMWizardHardwareView(wizardState: wizardState)
  64. .transition(wizardState.slide)
  65. case .drives:
  66. VMWizardDrivesView(wizardState: wizardState)
  67. .transition(wizardState.slide)
  68. case .sharing:
  69. VMWizardSharingView(wizardState: wizardState)
  70. .transition(wizardState.slide)
  71. case .summary:
  72. VMWizardSummaryView(wizardState: wizardState)
  73. .transition(wizardState.slide)
  74. }
  75. }
  76. .padding(.top)
  77. .frame(width: 450, height: 450)
  78. .toolbar {
  79. ToolbarItem(placement: .automatic) {
  80. if wizardState.currentPage != .start {
  81. Button("Cancel") {
  82. presentationMode.wrappedValue.dismiss()
  83. }
  84. }
  85. }
  86. ToolbarItem(placement: .cancellationAction) {
  87. if wizardState.currentPage != .start {
  88. Button("Go Back") {
  89. wizardState.back()
  90. }
  91. } else {
  92. Button("Cancel") {
  93. presentationMode.wrappedValue.dismiss()
  94. }
  95. }
  96. }
  97. ToolbarItem(placement: .confirmationAction) {
  98. if wizardState.hasNextButton {
  99. Button("Continue") {
  100. wizardState.next()
  101. }
  102. } else if wizardState.currentPage == .summary {
  103. Button("Save") {
  104. presentationMode.wrappedValue.dismiss()
  105. data.busyWorkAsync {
  106. let config = try await wizardState.generateConfig()
  107. #if arch(arm64)
  108. if #available(macOS 12, *), await wizardState.isPendingIPSWDownload, let appleConfig = config as? UTMAppleConfiguration {
  109. await data.downloadIPSW(using: appleConfig)
  110. return
  111. }
  112. #endif
  113. if let qemuConfig = config as? UTMQemuConfiguration {
  114. let vm = try await data.create(config: qemuConfig)
  115. await MainActor.run {
  116. if wizardState.isGuestToolsInstallRequested {
  117. NotificationCenter.default.post(name: NSNotification.InstallGuestTools, object: vm.wrapped!)
  118. }
  119. }
  120. } else if let appleConfig = config as? UTMAppleConfiguration {
  121. _ = try await data.create(config: appleConfig)
  122. }
  123. if await wizardState.isOpenSettingsAfterCreation {
  124. await data.showSettingsForCurrentVM()
  125. }
  126. }
  127. }
  128. }
  129. }
  130. }.alert(item: $wizardState.alertMessage) { msg in
  131. Alert(title: Text(msg.message))
  132. }
  133. .disabled(wizardState.isBusy && isNeverDisabledWorkaround)
  134. }
  135. }
  136. @available(macOS 11, *)
  137. struct VMWizardView_Previews: PreviewProvider {
  138. static var previews: some View {
  139. VMWizardView()
  140. }
  141. }