2
0

VMConfigAppleSystemView.swift 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. import Virtualization
  18. struct VMConfigAppleSystemView: View {
  19. private let bytesInMib = UInt64(1048576)
  20. @Binding var config: UTMAppleConfigurationSystem
  21. var minCores: Int {
  22. VZVirtualMachineConfiguration.minimumAllowedCPUCount
  23. }
  24. var maxCores: Int {
  25. VZVirtualMachineConfiguration.maximumAllowedCPUCount
  26. }
  27. var minMemory: Int {
  28. Int(VZVirtualMachineConfiguration.minimumAllowedMemorySize / bytesInMib)
  29. }
  30. var maxMemory: Int {
  31. Int(VZVirtualMachineConfiguration.maximumAllowedMemorySize / bytesInMib)
  32. }
  33. var body: some View {
  34. Form {
  35. HStack {
  36. Stepper(value: $config.cpuCount, in: minCores...maxCores) {
  37. Text("CPU Cores")
  38. }
  39. NumberTextField("", number: $config.cpuCount, prompt: "Default", onEditingChanged: { _ in
  40. guard config.cpuCount != 0 else {
  41. return
  42. }
  43. if config.cpuCount < minCores {
  44. config.cpuCount = minCores
  45. } else if config.cpuCount > maxCores {
  46. config.cpuCount = maxCores
  47. }
  48. })
  49. .frame(width: 80)
  50. .multilineTextAlignment(.trailing)
  51. }
  52. RAMSlider(systemMemory: $config.memorySize) { _ in
  53. if config.memorySize > maxMemory {
  54. config.memorySize = maxMemory
  55. }
  56. }
  57. }
  58. }
  59. }
  60. struct VMConfigAppleSystemView_Previews: PreviewProvider {
  61. @State static private var config = UTMAppleConfigurationSystem()
  62. static var previews: some View {
  63. VMConfigAppleSystemView(config: $config)
  64. }
  65. }