VMSettingsAddDeviceMenuView.swift 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // Copyright © 2022 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 VMSettingsAddDeviceMenuView: View {
  18. @ObservedObject var config: UTMQemuConfiguration
  19. @Binding var isCreateDriveShown: Bool
  20. @Binding var isImportDriveShown: Bool
  21. init(config: UTMQemuConfiguration, isCreateDriveShown: Binding<Bool>? = nil, isImportDriveShown: Binding<Bool>? = nil) {
  22. self.config = config
  23. if let isCreateDriveShown = isCreateDriveShown {
  24. _isCreateDriveShown = isCreateDriveShown
  25. } else {
  26. _isCreateDriveShown = .constant(false)
  27. }
  28. if let isImportDriveShown = isImportDriveShown {
  29. _isImportDriveShown = isImportDriveShown
  30. } else {
  31. _isImportDriveShown = .constant(false)
  32. }
  33. }
  34. private var isAddDisplayEnabled: Bool {
  35. if [.sparc, .sparc64, .m68k].contains(config.system.architecture) {
  36. return config.displays.count < 1
  37. } else {
  38. return !config.system.architecture.displayDeviceType.allRawValues.isEmpty
  39. }
  40. }
  41. var body: some View {
  42. Menu {
  43. Button {
  44. let newDisplay = UTMQemuConfigurationDisplay(forArchitecture: config.system.architecture, target: config.system.target)
  45. config.displays.append(newDisplay!)
  46. } label: {
  47. Label("Display", systemImage: "rectangle.on.rectangle")
  48. }.disabled(!isAddDisplayEnabled)
  49. Button {
  50. let newSerial = UTMQemuConfigurationSerial(forArchitecture: config.system.architecture, target: config.system.target)
  51. config.serials.append(newSerial!)
  52. } label: {
  53. Label("Serial", systemImage: "rectangle.connected.to.line.below")
  54. }
  55. Button {
  56. let newNetwork = UTMQemuConfigurationNetwork(forArchitecture: config.system.architecture, target: config.system.target)
  57. config.networks.append(newNetwork!)
  58. } label: {
  59. Label("Network", systemImage: "network")
  60. }.disabled(config.system.architecture.networkDeviceType.allRawValues.isEmpty)
  61. Button {
  62. let newSound = UTMQemuConfigurationSound(forArchitecture: config.system.architecture, target: config.system.target)
  63. config.sound.append(newSound!)
  64. } label: {
  65. Label("Sound", systemImage: "speaker.wave.2")
  66. }.disabled(config.system.architecture.soundDeviceType.allRawValues.isEmpty)
  67. #if os(iOS) || os(visionOS)
  68. Divider()
  69. Button {
  70. isImportDriveShown.toggle()
  71. } label: {
  72. Label("Import Drive…", systemImage: "externaldrive")
  73. }
  74. Button {
  75. isCreateDriveShown.toggle()
  76. } label: {
  77. Label("New Drive…", systemImage: "externaldrive.badge.plus")
  78. }
  79. #endif
  80. } label: {
  81. if #available(iOS 15, macOS 11, *) {
  82. Label("New…", systemImage: "plus")
  83. } else {
  84. Label("New…", systemImage: "plus")
  85. .labelStyle(.iconOnly)
  86. }
  87. }.help("Add a new device.")
  88. .menuStyle(.borderlessButton)
  89. }
  90. }
  91. struct VMSettingsAddDeviceMenuView_Previews: PreviewProvider {
  92. @StateObject static private var config = UTMQemuConfiguration()
  93. static var previews: some View {
  94. VMSettingsAddDeviceMenuView(config: config)
  95. }
  96. }