2
0

VMSettingsView.swift 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // Copyright © 2020 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 VMSettingsView<Config: UTMConfiguration>: View {
  19. let vm: VMData
  20. @ObservedObject var config: Config
  21. @EnvironmentObject private var data: UTMData
  22. @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
  23. var body: some View {
  24. NavigationView {
  25. List {
  26. if config is UTMQemuConfiguration {
  27. VMQEMUSettingsView(config: config as! UTMQemuConfiguration)
  28. } else if config is UTMAppleConfiguration {
  29. VMAppleSettingsView(config: config as! UTMAppleConfiguration)
  30. }
  31. }.listStyle(.sidebar)
  32. Text("")
  33. .settingsToolbar()
  34. }
  35. .frame(minWidth: 800, minHeight: 400, alignment: .leading)
  36. .legacySettingsToolbar {
  37. ToolbarItemGroup(placement: .cancellationAction) {
  38. Button(action: cancel) {
  39. Text("Cancel")
  40. }
  41. }
  42. ToolbarItemGroup(placement: .confirmationAction) {
  43. Button(action: save) {
  44. Text("Save")
  45. }
  46. }
  47. }
  48. .environmentObject(vm)
  49. .disabled(data.busy)
  50. .overlay(BusyOverlay())
  51. }
  52. func save() {
  53. data.busyWorkAsync {
  54. try await data.save(vm: vm)
  55. await MainActor.run {
  56. presentationMode.wrappedValue.dismiss()
  57. }
  58. }
  59. }
  60. func cancel() {
  61. presentationMode.wrappedValue.dismiss()
  62. data.busyWorkAsync {
  63. try await data.discardChanges(for: vm)
  64. }
  65. }
  66. }
  67. @available(macOS 11, *)
  68. struct ScrollableViewModifier: ViewModifier {
  69. @State private var scrollViewContentSize: CGSize = .zero
  70. func body(content: Content) -> some View {
  71. ScrollView {
  72. content
  73. .frame(maxWidth: .infinity)
  74. .padding()
  75. .background(
  76. GeometryReader { geo -> Color in
  77. DispatchQueue.main.async {
  78. scrollViewContentSize = geo.size
  79. }
  80. return Color.clear
  81. }
  82. )
  83. }
  84. .frame(idealWidth: scrollViewContentSize.width)
  85. }
  86. }
  87. fileprivate struct EmptyToolbarContent: ToolbarContent {
  88. var body: some ToolbarContent {
  89. ToolbarItem {
  90. EmptyView()
  91. }
  92. }
  93. }
  94. @available(macOS 12, *)
  95. struct SettingsToolbarViewModifier<AdditionalContent>: ViewModifier where AdditionalContent: ToolbarContent {
  96. @EnvironmentObject private var vm: VMData
  97. @EnvironmentObject private var data: UTMData
  98. @Environment(\.dismiss) private var dismiss
  99. let additionalContent: AdditionalContent?
  100. fileprivate init() where AdditionalContent == EmptyToolbarContent {
  101. self.additionalContent = nil
  102. }
  103. init(additionalContent: () -> AdditionalContent) {
  104. self.additionalContent = additionalContent()
  105. }
  106. func body(content: Content) -> some View {
  107. let view = content.toolbar {
  108. ToolbarItemGroup(placement: .cancellationAction) {
  109. Button(action: cancel) {
  110. Text("Cancel")
  111. }
  112. }
  113. ToolbarItemGroup(placement: .confirmationAction) {
  114. Form {
  115. Button(action: save) {
  116. Text("Save")
  117. }
  118. .buttonStyle(.borderedProminent)
  119. }
  120. }
  121. }
  122. if let additionalContent = additionalContent {
  123. view.toolbar {
  124. additionalContent
  125. }
  126. } else {
  127. view
  128. }
  129. }
  130. private func save() {
  131. data.busyWorkAsync {
  132. try await data.save(vm: vm)
  133. await MainActor.run {
  134. dismiss()
  135. }
  136. }
  137. }
  138. private func cancel() {
  139. dismiss()
  140. data.busyWorkAsync {
  141. try await data.discardChanges(for: vm)
  142. }
  143. }
  144. }
  145. @available(macOS 11, *)
  146. extension View {
  147. func scrollable() -> some View {
  148. self.modifier(ScrollableViewModifier())
  149. }
  150. @ViewBuilder
  151. fileprivate func legacySettingsToolbar<Content>(@ToolbarContentBuilder content: () -> Content) -> some View where Content: ToolbarContent {
  152. if #available(macOS 12, *) {
  153. self
  154. } else {
  155. self.toolbar(content: content)
  156. }
  157. }
  158. @ViewBuilder
  159. func settingsToolbar() -> some View {
  160. if #available(macOS 12, *) {
  161. self.modifier(SettingsToolbarViewModifier())
  162. } else {
  163. self
  164. }
  165. }
  166. @ViewBuilder
  167. func settingsToolbar<Content>(@ToolbarContentBuilder additionalContent: () -> Content) -> some View where Content: ToolbarContent {
  168. if #available(macOS 12, *) {
  169. self.modifier(SettingsToolbarViewModifier(additionalContent: additionalContent))
  170. } else {
  171. self
  172. }
  173. }
  174. }
  175. @available(macOS 11, *)
  176. struct VMSettingsView_Previews: PreviewProvider {
  177. @State static private var qemuConfig = UTMQemuConfiguration()
  178. @State static private var appleConfig = UTMAppleConfiguration()
  179. @State static private var data = UTMData()
  180. static var previews: some View {
  181. VMSettingsView(vm: VMData(from: .empty), config: qemuConfig)
  182. .environmentObject(data)
  183. .previewDisplayName("QEMU VM Settings")
  184. VMSettingsView(vm: VMData(from: .empty), config: appleConfig)
  185. .environmentObject(data)
  186. .previewDisplayName("Apple VM Settings")
  187. }
  188. }