2
0

UTMConfigurationTerminal.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 Foundation
  17. /// Console mode settings.
  18. struct UTMConfigurationTerminal: Codable, Identifiable {
  19. /// Terminal color scheme. Mutually exclusive with foreground/background colors.
  20. var theme: QEMUTerminalTheme?
  21. /// Terminal foreground color if a theme is not used.
  22. var foregroundColor: String? = "#ffffff"
  23. /// Terminal background color if a theme is not used.
  24. var backgroundColor: String? = "#000000"
  25. /// Terminal text font.
  26. var font: QEMUTerminalFont = .init(rawValue: "Menlo")
  27. /// Terminal text font size.
  28. var fontSize: Int = 12
  29. /// Command to send when the console is resized.
  30. var resizeCommand: String?
  31. /// Terminal has a blinking cursor.
  32. var hasCursorBlink: Bool = true
  33. let id = UUID()
  34. enum CodingKeys: String, CodingKey {
  35. case theme = "Theme"
  36. case foregroundColor = "ForegroundColor"
  37. case backgroundColor = "BackgroundColor"
  38. case font = "Font"
  39. case fontSize = "FontSize"
  40. case resizeCommand = "ResizeCommand"
  41. case hasCursorBlink = "CursorBlink"
  42. }
  43. init() {
  44. }
  45. init(from decoder: Decoder) throws {
  46. let values = try decoder.container(keyedBy: CodingKeys.self)
  47. theme = try values.decodeIfPresent(QEMUTerminalTheme.self, forKey: .theme)
  48. foregroundColor = try values.decodeIfPresent(String.self, forKey: .foregroundColor)
  49. backgroundColor = try values.decodeIfPresent(String.self, forKey: .backgroundColor)
  50. font = try values.decode(QEMUTerminalFont.self, forKey: .font)
  51. fontSize = try values.decode(Int.self, forKey: .fontSize)
  52. resizeCommand = try values.decodeIfPresent(String.self, forKey: .resizeCommand)
  53. hasCursorBlink = try values.decodeIfPresent(Bool.self, forKey: .hasCursorBlink) ?? true
  54. }
  55. func encode(to encoder: Encoder) throws {
  56. var container = encoder.container(keyedBy: CodingKeys.self)
  57. if let theme = theme {
  58. try container.encode(theme, forKey: .theme)
  59. } else { // only save colors if no theme
  60. try container.encodeIfPresent(foregroundColor, forKey: .foregroundColor)
  61. try container.encodeIfPresent(backgroundColor, forKey: .backgroundColor)
  62. }
  63. try container.encode(font, forKey: .font)
  64. try container.encode(fontSize, forKey: .fontSize)
  65. try container.encodeIfPresent(resizeCommand, forKey: .resizeCommand)
  66. try container.encode(hasCursorBlink, forKey: .hasCursorBlink)
  67. }
  68. }
  69. // MARK: - Conversion of old config format
  70. extension UTMConfigurationTerminal {
  71. init(migrating oldConfig: UTMLegacyQemuConfiguration) {
  72. self.init()
  73. foregroundColor = oldConfig.consoleTextColor
  74. backgroundColor = oldConfig.consoleBackgroundColor
  75. if let fontStr = oldConfig.consoleFont {
  76. font = QEMUTerminalFont(rawValue: fontStr)
  77. }
  78. if let fontSizeNum = oldConfig.consoleFontSize {
  79. fontSize = fontSizeNum.intValue
  80. }
  81. resizeCommand = oldConfig.consoleResizeCommand
  82. hasCursorBlink = oldConfig.consoleCursorBlink
  83. }
  84. #if os(macOS)
  85. init(migrating oldConfig: UTMLegacyAppleConfiguration) {
  86. self.init()
  87. foregroundColor = oldConfig.consoleTextColor
  88. backgroundColor = oldConfig.consoleBackgroundColor
  89. if let fontStr = oldConfig.consoleFont {
  90. font = QEMUTerminalFont(rawValue: fontStr)
  91. }
  92. if let fontSizeNum = oldConfig.consoleFontSize {
  93. fontSize = fontSizeNum.intValue
  94. }
  95. resizeCommand = oldConfig.consoleResizeCommand
  96. hasCursorBlink = oldConfig.consoleCursorBlink
  97. }
  98. #endif
  99. }
  100. // MARK: - For VMConfigDisplayConsoleView
  101. extension Optional where Wrapped == UTMConfigurationTerminal {
  102. var bound: Wrapped {
  103. get {
  104. return self ?? .init()
  105. }
  106. set {
  107. self = newValue
  108. }
  109. }
  110. }