2
0

VMQEMUSettingsView.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 VMQEMUSettingsView: View {
  18. @ObservedObject var config: UTMQemuConfiguration
  19. @EnvironmentObject private var data: UTMData
  20. @State private var infoActive: Bool = true
  21. @State private var isResetConfig: Bool = false
  22. @State private var isNewDriveShown: Bool = false
  23. var body: some View {
  24. NavigationLink(destination: VMConfigInfoView(config: $config.information).scrollable().settingsToolbar(), isActive: $infoActive) {
  25. Label("Information", systemImage: "info.circle")
  26. }
  27. NavigationLink {
  28. VMConfigSystemView(config: $config.system, isResetConfig: $isResetConfig)
  29. .scrollable()
  30. .settingsToolbar()
  31. } label: {
  32. Label("System", systemImage: "cpu")
  33. }.onChange(of: isResetConfig) { newValue in
  34. if newValue {
  35. config.reset(forArchitecture: config.system.architecture, target: config.system.target)
  36. isResetConfig = false
  37. }
  38. }
  39. NavigationLink {
  40. VMConfigQEMUView(config: $config.qemu, system: $config.system, fetchFixedArguments: {
  41. config.generatedArguments
  42. })
  43. .scrollable()
  44. .settingsToolbar()
  45. } label: {
  46. Label("QEMU", systemImage: "shippingbox")
  47. }
  48. if #available(macOS 12, *) {
  49. NavigationLink {
  50. VMConfigQEMUArgumentsView(config: $config.qemu, architecture: config.system.architecture, fixedArguments: config.generatedArguments)
  51. .settingsToolbar()
  52. } label: {
  53. Label("Arguments", systemImage: "character.textbox")
  54. .padding(.leading)
  55. }
  56. }
  57. NavigationLink {
  58. VMConfigInputView(config: $config.input, hasUsbSupport: config.system.architecture.hasUsbSupport)
  59. .scrollable()
  60. .settingsToolbar()
  61. } label: {
  62. Label("Input", systemImage: "keyboard")
  63. }
  64. NavigationLink {
  65. VMConfigSharingView(config: $config.sharing)
  66. .scrollable()
  67. .settingsToolbar()
  68. } label: {
  69. Label("Sharing", systemImage: "person.crop.circle")
  70. }
  71. Section(header: Text("Devices")) {
  72. ForEach($config.displays) { $display in
  73. NavigationLink {
  74. VMConfigDisplayView(config: $display, system: $config.system)
  75. .scrollable()
  76. .settingsToolbar {
  77. ToolbarItem(placement: .destructiveAction) {
  78. Button("Remove") {
  79. config.displays.removeAll(where: { $0.id == display.id })
  80. refresh()
  81. }
  82. }
  83. }
  84. } label: {
  85. Label("Display", systemImage: "rectangle.on.rectangle")
  86. }.contextMenu {
  87. DestructiveButton("Remove") {
  88. config.displays.removeAll(where: { $0.id == display.id })
  89. refresh()
  90. }
  91. }
  92. }
  93. ForEach($config.serials) { $serial in
  94. NavigationLink {
  95. VMConfigSerialView(config: $serial, system: $config.system)
  96. .scrollable()
  97. .settingsToolbar {
  98. ToolbarItem(placement: .destructiveAction) {
  99. Button("Remove") {
  100. config.serials.removeAll(where: { $0.id == serial.id })
  101. refresh()
  102. }
  103. }
  104. }
  105. } label: {
  106. Label("Serial", systemImage: "rectangle.connected.to.line.below")
  107. }.contextMenu {
  108. DestructiveButton("Remove") {
  109. config.serials.removeAll(where: { $0.id == serial.id })
  110. refresh()
  111. }
  112. }
  113. }
  114. ForEach($config.networks) { $network in
  115. NavigationLink {
  116. VMConfigNetworkView(config: $network, system: $config.system)
  117. .scrollable()
  118. .settingsToolbar {
  119. ToolbarItem(placement: .destructiveAction) {
  120. Button("Remove") {
  121. config.networks.removeAll(where: { $0.id == network.id })
  122. refresh()
  123. }
  124. }
  125. }
  126. } label: {
  127. Label("Network", systemImage: "network")
  128. }.contextMenu {
  129. DestructiveButton("Remove") {
  130. config.networks.removeAll(where: { $0.id == network.id })
  131. refresh()
  132. }
  133. }
  134. if #available(macOS 12, *), network.mode == .emulated {
  135. NavigationLink {
  136. VMConfigNetworkPortForwardView(config: $network)
  137. .settingsToolbar()
  138. } label: {
  139. Label("Port Forward", systemImage: "point.topleft.down.curvedto.point.bottomright.up")
  140. .padding(.leading)
  141. }
  142. }
  143. }
  144. ForEach($config.sound) { $sound in
  145. NavigationLink {
  146. VMConfigSoundView(config: $sound, system: $config.system)
  147. .scrollable()
  148. .settingsToolbar {
  149. ToolbarItem(placement: .destructiveAction) {
  150. Button("Remove") {
  151. config.sound.removeAll(where: { $0.id == sound.id })
  152. refresh()
  153. }
  154. }
  155. }
  156. } label: {
  157. Label("Sound", systemImage: "speaker.wave.2")
  158. }.contextMenu {
  159. DestructiveButton("Remove") {
  160. config.sound.removeAll(where: { $0.id == sound.id })
  161. refresh()
  162. }
  163. }
  164. }
  165. VMSettingsAddDeviceMenuView(config: config)
  166. }
  167. Section(header: Text("Drives")) {
  168. VMDrivesSettingsView(drives: $config.drives, template: UTMQemuConfigurationDrive(forArchitecture: config.system.architecture, target: config.system.target))
  169. }
  170. }
  171. private func refresh() {
  172. // SwiftUI bug: if a TextField is focused while a device is removed, the app will crash
  173. infoActive = true
  174. }
  175. }
  176. struct VMQEMUSettingsView_Previews: PreviewProvider {
  177. @State static private var config = UTMQemuConfiguration()
  178. static var previews: some View {
  179. List {
  180. VMQEMUSettingsView(config: config)
  181. }
  182. .frame(maxWidth: 400)
  183. }
  184. }