UTMKeyboardShortcuts.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 Foundation
  17. final class UTMKeyboardShortcuts {
  18. static let shared = UTMKeyboardShortcuts()
  19. @Setting("KeyboardShortcuts") private var savedKeyboardShortcuts: Data? = nil
  20. private init() {}
  21. func loadKeyboardShortcuts() -> [[QEMUKeyCode]] {
  22. let decoder = PropertyListDecoder()
  23. if let data = savedKeyboardShortcuts {
  24. if let decoded = try? decoder.decode([[QEMUKeyCode]].self, from: data) {
  25. return decoded
  26. }
  27. }
  28. // default entry
  29. return [[.keyCtrl, .keyAlt, .keyDelete]]
  30. }
  31. func saveKeyboardShortcuts(_ keyboardShortcuts: [[QEMUKeyCode]]) {
  32. let encoder = PropertyListEncoder()
  33. encoder.outputFormat = .binary
  34. if let data = try? encoder.encode(keyboardShortcuts) {
  35. savedKeyboardShortcuts = data
  36. }
  37. }
  38. }
  39. extension QEMUKeyCode: @retroactive Codable, @retroactive Identifiable, @retroactive CaseIterable {
  40. public var id: Self {
  41. self
  42. }
  43. public var title: String {
  44. QEMUMonitor.keyMap[self] ?? ""
  45. }
  46. public static var allCases: [QEMUKeyCode] {
  47. Array(QEMUMonitor.keyMap.keys).sorted { $0.rawValue < $1.rawValue }
  48. }
  49. }
  50. extension Array where Element == QEMUKeyCode {
  51. var title: String {
  52. self.map({ $0.title }).joined(separator: "+")
  53. }
  54. }