VMConfigAppleNetworkingView.swift 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 VMConfigAppleNetworkingView: View {
  19. @Binding var config: UTMAppleConfigurationNetwork
  20. @EnvironmentObject private var data: UTMData
  21. @State private var newMacAddress: String?
  22. var body: some View {
  23. Form {
  24. VMConfigConstantPicker("Network Mode", selection: $config.mode)
  25. HStack {
  26. TextField("MAC Address", text: $newMacAddress.bound, onCommit: {
  27. commitMacAddress()
  28. })
  29. .onAppear {
  30. newMacAddress = config.macAddress
  31. }
  32. Button("Random") {
  33. let random = VZMACAddress.randomLocallyAdministered().string
  34. newMacAddress = random
  35. commitMacAddress()
  36. }
  37. }
  38. if config.mode == .bridged {
  39. Section(header: Text("Bridged Settings")) {
  40. Picker("Interface", selection: $config.bridgeInterface) {
  41. Text("Automatic")
  42. .tag(nil as String?)
  43. ForEach(VZBridgedNetworkInterface.networkInterfaces, id: \.identifier) { interface in
  44. Text(interface.localizedDisplayName.map { "\($0) (\(interface.identifier))" } ?? interface.identifier)
  45. .tag(interface.identifier as String?)
  46. }
  47. }
  48. }
  49. }
  50. }
  51. }
  52. private func commitMacAddress() {
  53. guard let macAddress = newMacAddress else {
  54. return
  55. }
  56. if let _ = VZMACAddress(string: macAddress) {
  57. config.macAddress = macAddress
  58. } else {
  59. data.busyWork {
  60. throw NSLocalizedString("Invalid MAC address.", comment: "VMConfigAppleNetworkingView")
  61. }
  62. }
  63. }
  64. }
  65. struct VMConfigAppleNetworkingView_Previews: PreviewProvider {
  66. @State static private var config = UTMAppleConfigurationNetwork()
  67. static var previews: some View {
  68. VMConfigAppleNetworkingView(config: $config)
  69. }
  70. }