VMKeyboardShortcutsView.swift 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Copyright © 2025 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 VMKeyboardShortcutsView: View {
  18. @Environment(\.presentationMode) var presentationMode
  19. @State private var keyboardShortcuts: [[QEMUKeyCode]] = []
  20. @State private var isEditing: Bool = false
  21. @State private var currentlyEditingIndex: SelectedIndex?
  22. @State private var currentlyEditingShortcut: [QEMUKeyCode] = []
  23. let onDismiss: () -> Void
  24. init(onDismiss: @escaping () -> Void = {}) {
  25. self.onDismiss = onDismiss
  26. }
  27. var body: some View {
  28. VStack {
  29. HStack {
  30. Spacer()
  31. Button {
  32. onDismiss()
  33. presentationMode.wrappedValue.dismiss()
  34. } label: {
  35. Text("Done")
  36. }
  37. }
  38. List(selection: $currentlyEditingIndex) {
  39. ForEach(Array(keyboardShortcuts.enumerated()), id: \.element) { index, element in
  40. Text(element.title)
  41. .tag(SelectedIndex(id: index))
  42. .contextMenu {
  43. Button("Edit") {
  44. isEditing.toggle()
  45. }
  46. DestructiveButton("Delete") {
  47. keyboardShortcuts.remove(at: index)
  48. }
  49. }
  50. }.onDelete { indexSet in
  51. keyboardShortcuts.remove(atOffsets: indexSet)
  52. }.onMove { indexSet, offset in
  53. keyboardShortcuts.move(fromOffsets: indexSet, toOffset: offset)
  54. }
  55. }.borderedList()
  56. .frame(height: 200)
  57. HStack {
  58. Spacer()
  59. if let index = currentlyEditingIndex?.id {
  60. DestructiveButton("Delete") {
  61. keyboardShortcuts.remove(at: index)
  62. }
  63. Button {
  64. isEditing.toggle()
  65. } label: {
  66. Text("Edit")
  67. }
  68. }
  69. Button {
  70. currentlyEditingIndex = nil
  71. currentlyEditingShortcut = []
  72. isEditing.toggle()
  73. } label: {
  74. Text("New")
  75. }
  76. }
  77. }
  78. .sheet(isPresented: $isEditing, onDismiss: {
  79. if let index = currentlyEditingIndex {
  80. if !currentlyEditingShortcut.isEmpty {
  81. keyboardShortcuts[index.id] = currentlyEditingShortcut
  82. } else {
  83. keyboardShortcuts.remove(at: index.id)
  84. }
  85. } else {
  86. if !currentlyEditingShortcut.isEmpty {
  87. keyboardShortcuts.append(currentlyEditingShortcut)
  88. }
  89. }
  90. }, content: {
  91. EditKeyboardShortcutView(keyboardShortcut: $currentlyEditingShortcut).padding()
  92. })
  93. .onAppear {
  94. keyboardShortcuts = UTMKeyboardShortcuts.shared.loadKeyboardShortcuts()
  95. }
  96. .onChange(of: keyboardShortcuts) { newValue in
  97. UTMKeyboardShortcuts.shared.saveKeyboardShortcuts(newValue)
  98. }
  99. .onChange(of: currentlyEditingIndex) { newValue in
  100. if let index = newValue?.id {
  101. currentlyEditingShortcut = keyboardShortcuts[index]
  102. } else {
  103. currentlyEditingShortcut = []
  104. }
  105. }
  106. }
  107. }
  108. private struct EditKeyboardShortcutView: View {
  109. @Environment(\.presentationMode) var presentationMode
  110. @Binding var keyboardShortcut: [QEMUKeyCode]
  111. @State private var currentlyEditingIndex: SelectedIndex?
  112. @State private var currentlyEditingKey: QEMUKeyCode?
  113. var body: some View {
  114. VStack {
  115. HStack {
  116. Spacer()
  117. Button {
  118. presentationMode.wrappedValue.dismiss()
  119. } label: {
  120. Text("Done")
  121. }
  122. }
  123. List(selection: $currentlyEditingIndex) {
  124. ForEach(Array(keyboardShortcut.enumerated()), id: \.element) { index, keyCode in
  125. Text(keyCode.title)
  126. .tag(SelectedIndex(id: index))
  127. .contextMenu {
  128. DestructiveButton("Delete") {
  129. keyboardShortcut.remove(at: index)
  130. }
  131. }
  132. }.onDelete { indexSet in
  133. keyboardShortcut.remove(atOffsets: indexSet)
  134. }.onMove { indexSet, offset in
  135. keyboardShortcut.move(fromOffsets: indexSet, toOffset: offset)
  136. }
  137. }.borderedList()
  138. .frame(height: 100)
  139. Spacer()
  140. HStack {
  141. Picker("New Key", selection: $currentlyEditingKey) {
  142. Text("").tag(nil as QEMUKeyCode?)
  143. ForEach(QEMUKeyCode.allCases) { keyCode in
  144. if !keyboardShortcut.contains(keyCode) {
  145. Text(keyCode.title).tag(keyCode)
  146. }
  147. }
  148. }
  149. Spacer()
  150. if let index = currentlyEditingIndex?.id {
  151. DestructiveButton("Delete") {
  152. keyboardShortcut.remove(at: index)
  153. }
  154. Button {
  155. if let currentlyEditingKey = currentlyEditingKey {
  156. keyboardShortcut[index] = currentlyEditingKey
  157. }
  158. currentlyEditingKey = nil
  159. } label: {
  160. Text("Update")
  161. }.disabled(currentlyEditingKey == nil)
  162. }
  163. Button {
  164. if let currentlyEditingKey = currentlyEditingKey {
  165. keyboardShortcut.append(currentlyEditingKey)
  166. }
  167. currentlyEditingKey = nil
  168. currentlyEditingIndex = nil
  169. } label: {
  170. Text("Add")
  171. }.disabled(currentlyEditingKey == nil)
  172. }
  173. }
  174. }
  175. }
  176. private struct SelectedIndex: Identifiable, Hashable {
  177. var id: Int
  178. func hash(into hasher: inout Hasher) {
  179. hasher.combine(id)
  180. }
  181. }
  182. private extension View {
  183. @ViewBuilder
  184. func borderedList() -> some View {
  185. if #available(macOS 12, *) {
  186. self.listStyle(.bordered)
  187. } else {
  188. self.border(.gray)
  189. }
  190. }
  191. }
  192. #Preview {
  193. VMKeyboardShortcutsView()
  194. }