VMConfigNetworkPortForwardView.swift 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. struct VMConfigNetworkPortForwardView: View {
  18. @Binding var config: UTMQemuConfigurationNetwork
  19. var body: some View {
  20. Section(header: Text("Port Forward")) {
  21. List {
  22. ForEach($config.portForward) { $forward in
  23. NavigationLink(
  24. destination: PortForwardEdit(forward: forward,
  25. onSave: { forward = $0 },
  26. onDelete: { config.portForward.removeAll(where: { $0 == forward }) }),
  27. label: {
  28. VStack(alignment: .leading) {
  29. let guest = "\(forward.guestAddress ?? ""):\(forward.guestPort)"
  30. let host = "\(forward.hostAddress ?? ""):\(forward.hostPort)"
  31. Text("\(guest) ➡️ \(host)")
  32. Text(forward.protocol.prettyValue).font(.subheadline)
  33. }
  34. })
  35. }.onDelete(perform: deletePortForwards)
  36. NavigationLink(
  37. destination: PortForwardEdit(onSave: {
  38. config.portForward.append($0)
  39. }),
  40. label: {
  41. Text("New")
  42. })
  43. }
  44. }
  45. }
  46. private func deletePortForwards(offsets: IndexSet) {
  47. config.portForward.remove(atOffsets: offsets)
  48. }
  49. }
  50. struct PortForwardEdit: View {
  51. @State var forward: UTMQemuConfigurationPortForward = .init()
  52. var onSave: ((UTMQemuConfigurationPortForward) -> Void)
  53. var onDelete: (() -> Void)? = nil
  54. @Environment(\.presentationMode) private var presentationMode: Binding<PresentationMode>
  55. var body: some View {
  56. Form {
  57. List {
  58. VMConfigPortForwardForm(forward: $forward).multilineTextAlignment(.trailing)
  59. }
  60. }.navigationBarItems(trailing:
  61. HStack {
  62. if let onDelete = self.onDelete {
  63. Button(action: { closePopup(after: onDelete) }, label: {
  64. Text("Delete")
  65. }).foregroundColor(.red)
  66. .padding()
  67. }
  68. Button(action: { closePopup(after: { onSave(forward) }) }, label: {
  69. Text("Save")
  70. }).disabled(forward.guestPort == 0 || forward.hostPort == 0)
  71. }
  72. )
  73. }
  74. private func closePopup(after action: () -> Void) {
  75. action()
  76. self.presentationMode.wrappedValue.dismiss()
  77. }
  78. }
  79. struct VMConfigNetworkPortForwardView_Previews: PreviewProvider {
  80. @State static private var config = UTMQemuConfigurationNetwork()
  81. static var previews: some View {
  82. Group {
  83. Form {
  84. VMConfigNetworkPortForwardView(config: $config)
  85. }.onAppear {
  86. if config.portForward.count == 0 {
  87. var newConfigPort = UTMQemuConfigurationPortForward()
  88. newConfigPort.protocol = .tcp
  89. newConfigPort.guestAddress = "1.2.3.4"
  90. newConfigPort.guestPort = 1234
  91. newConfigPort.hostAddress = "4.3.2.1"
  92. newConfigPort.hostPort = 4321
  93. config.portForward.append(newConfigPort)
  94. newConfigPort.protocol = .udp
  95. newConfigPort.guestAddress = ""
  96. newConfigPort.guestPort = 2222
  97. newConfigPort.hostAddress = ""
  98. newConfigPort.hostPort = 3333
  99. config.portForward.append(newConfigPort)
  100. }
  101. }
  102. PortForwardEdit(onSave: { _ in })
  103. }
  104. }
  105. }