VMWizardOSWindowsView.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. struct VMWizardOSWindowsView: View {
  18. @ObservedObject var wizardState: VMWizardState
  19. @State private var isFileImporterPresented: Bool = false
  20. @State private var useVhdx: Bool = false
  21. var body: some View {
  22. VMWizardContent("Windows") {
  23. Section {
  24. Toggle("Install Windows 10 or higher", isOn: $wizardState.isWindows10OrHigher)
  25. .onChange(of: wizardState.isWindows10OrHigher) { newValue in
  26. if newValue {
  27. wizardState.systemBootUefi = true
  28. wizardState.systemBootTpm = true
  29. wizardState.isGuestToolsInstallRequested = true
  30. } else {
  31. wizardState.systemBootTpm = false
  32. wizardState.isGuestToolsInstallRequested = false
  33. }
  34. }
  35. if wizardState.isWindows10OrHigher {
  36. Toggle("Import VHDX Image", isOn: $useVhdx)
  37. #if os(macOS)
  38. if useVhdx {
  39. #if arch(arm64)
  40. Link(destination: URL(string: "https://www.microsoft.com/en-us/software-download/windowsinsiderpreviewARM64")!) {
  41. Label("Download Windows 11 for ARM64 Preview VHDX", systemImage: "link")
  42. }.buttonStyle(.borderless)
  43. #endif
  44. } else if #available(macOS 12, *) { // CrystalFetch is only available on macOS 12+
  45. Button {
  46. let downloadCrystalFetch = URL(string: "https://mac.getutm.app/crystalfetch/")!
  47. if let crystalFetch = NSWorkspace.shared.urlForApplication(withBundleIdentifier: "llc.turing.CrystalFetch") {
  48. NSWorkspace.shared.openApplication(at: crystalFetch, configuration: .init()) { _, error in
  49. if error != nil {
  50. NSWorkspace.shared.open(downloadCrystalFetch)
  51. }
  52. }
  53. } else {
  54. NSWorkspace.shared.open(downloadCrystalFetch)
  55. }
  56. } label: {
  57. Label("Fetch latest Windows installer…", systemImage: "link")
  58. }.buttonStyle(.link)
  59. }
  60. #endif
  61. Link(destination: URL(string: "https://docs.getutm.app/guides/windows/")!) {
  62. Label("Windows Install Guide", systemImage: "link")
  63. }.buttonStyle(.borderless)
  64. }
  65. } header: {
  66. Text("Image File Type")
  67. }
  68. .onAppear {
  69. // SwiftUI bug: on macOS 11, onAppear() is called every time the check box is clicked
  70. if #available(iOS 15, macOS 12, *) {
  71. if wizardState.windowsBootVhdx != nil {
  72. useVhdx = true
  73. }
  74. }
  75. }
  76. Section {
  77. if useVhdx {
  78. FileBrowseField(url: $wizardState.windowsBootVhdx, isFileImporterPresented: $isFileImporterPresented, hasClearButton: false)
  79. } else {
  80. FileBrowseField(url: $wizardState.bootImageURL, isFileImporterPresented: $isFileImporterPresented, hasClearButton: false)
  81. }
  82. if wizardState.isBusy {
  83. Spinner(size: .large)
  84. }
  85. } header: {
  86. if useVhdx {
  87. Text("Boot VHDX Image")
  88. } else {
  89. Text("Boot ISO Image")
  90. }
  91. }
  92. if !wizardState.isWindows10OrHigher {
  93. DetailedSection("", description: "Some older systems do not support UEFI boot, such as Windows 7 and below.") {
  94. Toggle("UEFI Boot", isOn: $wizardState.systemBootUefi)
  95. .onChange(of: wizardState.systemBootUefi) { newValue in
  96. if !newValue {
  97. wizardState.systemBootTpm = false
  98. }
  99. }
  100. Toggle("Secure Boot with TPM 2.0", isOn: $wizardState.systemBootTpm)
  101. .disabled(!wizardState.systemBootUefi)
  102. }
  103. }
  104. // Disabled on iOS 14 due to a SwiftUI layout bug
  105. // Disabled for non-Windows 10 installs due to autounattend version
  106. if #available(iOS 15, *), wizardState.isWindows10OrHigher {
  107. DetailedSection("", description: "Download and mount the guest support package for Windows. This is required for some features including dynamic resolution and clipboard sharing.") {
  108. Toggle("Install drivers and SPICE tools", isOn: $wizardState.isGuestToolsInstallRequested)
  109. }
  110. }
  111. }
  112. .fileImporter(isPresented: $isFileImporterPresented, allowedContentTypes: [.data], onCompletion: processImage)
  113. }
  114. private func processImage(_ result: Result<URL, Error>) {
  115. wizardState.busyWorkAsync {
  116. let url = try result.get()
  117. await MainActor.run {
  118. if useVhdx {
  119. wizardState.windowsBootVhdx = url
  120. wizardState.bootImageURL = nil
  121. wizardState.isSkipBootImage = true
  122. } else {
  123. wizardState.windowsBootVhdx = nil
  124. wizardState.bootImageURL = url
  125. wizardState.isSkipBootImage = false
  126. }
  127. }
  128. }
  129. }
  130. }
  131. struct VMWizardOSWindowsView_Previews: PreviewProvider {
  132. @StateObject static var wizardState = VMWizardState()
  133. static var previews: some View {
  134. VMWizardOSWindowsView(wizardState: wizardState)
  135. }
  136. }