Skip to content

Commit 8e11ca2

Browse files
committed
Add preference keys for fonts
1 parent f315e0b commit 8e11ca2

3 files changed

Lines changed: 77 additions & 3 deletions

File tree

Tool/Sources/Preferences/Keys.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,18 @@ public extension UserDefaultPreferenceKeys {
493493
var codeBackgroundColorDark: PreferenceKey<UserDefaultsStorageBox<StorableColor?>> {
494494
.init(defaultValue: .init(nil), key: "CodeBackgroundColorDark")
495495
}
496+
497+
var suggestionFont: PreferenceKey<UserDefaultsStorageBox<StorableFont?>> {
498+
.init(defaultValue: .init(nil), key: "SuggestionFont")
499+
}
500+
501+
var promptToCodeFont: PreferenceKey<UserDefaultsStorageBox<StorableFont?>> {
502+
.init(defaultValue: .init(nil), key: "promptToCodeFont")
503+
}
504+
505+
var chatCodeFont: PreferenceKey<UserDefaultsStorageBox<StorableFont?>> {
506+
.init(defaultValue: .init(nil), key: "chatCodeFont")
507+
}
496508
}
497509

498510
// MARK: - Bing Search
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import AppKit
2+
import Foundation
3+
4+
public struct StorableFont: Codable {
5+
public var nsFont: NSFont
6+
7+
public init(nsFont: NSFont) {
8+
self.nsFont = nsFont
9+
}
10+
11+
public enum CodingKeys: String, CodingKey {
12+
case nsFont
13+
}
14+
15+
public init(from decoder: Decoder) throws {
16+
var container = try decoder.container(keyedBy: CodingKeys.self)
17+
let fontData = try container.decode(Data.self, forKey: .nsFont)
18+
guard let nsFont = try NSKeyedUnarchiver.unarchivedObject(
19+
ofClass: NSFont.self,
20+
from: fontData
21+
) else {
22+
throw DecodingError.dataCorruptedError(
23+
forKey: .nsFont,
24+
in: container,
25+
debugDescription: "Failed to decode NSFont"
26+
)
27+
}
28+
self.nsFont = nsFont
29+
}
30+
31+
public func encode(to encoder: Encoder) throws {
32+
var container = encoder.container(keyedBy: CodingKeys.self)
33+
let fontData = try NSKeyedArchiver.archivedData(
34+
withRootObject: nsFont,
35+
requiringSecureCoding: false
36+
)
37+
try container.encode(fontData, forKey: .nsFont)
38+
}
39+
}
40+

Tool/Sources/Preferences/UserDefaults.swift

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import AIModel
2+
import AppKit
23
import Configs
34
import Foundation
45

@@ -29,6 +30,27 @@ public extension UserDefaults {
2930
for: \.promptToCodeCodeFontSize,
3031
defaultValue: shared.value(for: \.suggestionCodeFontSize)
3132
)
33+
shared.setupDefaultValue(
34+
for: \.suggestionFont,
35+
defaultValue: .init(.init(nsFont: .monospacedSystemFont(
36+
ofSize: shared.value(for: \.suggestionCodeFontSize),
37+
weight: .regular
38+
)))
39+
)
40+
shared.setupDefaultValue(
41+
for: \.promptToCodeFont,
42+
defaultValue: .init(.init(nsFont: .monospacedSystemFont(
43+
ofSize: shared.value(for: \.promptToCodeCodeFontSize),
44+
weight: .regular
45+
)))
46+
)
47+
shared.setupDefaultValue(
48+
for: \.chatCodeFont,
49+
defaultValue: .init(.init(nsFont: .monospacedSystemFont(
50+
ofSize: shared.value(for: \.chatCodeFontSize),
51+
weight: .regular
52+
)))
53+
)
3254
}
3355
}
3456

@@ -69,7 +91,7 @@ public struct UserDefaultsStorageBox<Element: Codable>: RawRepresentable {
6991
public init(_ value: Element) {
7092
self.value = value
7193
}
72-
94+
7395
public init?(rawValue: String) {
7496
guard let data = rawValue.data(using: .utf8),
7597
let result = try? JSONDecoder().decode(Element.self, from: data)
@@ -147,7 +169,7 @@ public extension UserDefaultsType {
147169
}
148170
return K.Value(rawValue: rawValue) ?? key.defaultValue
149171
}
150-
172+
151173
func value<K: UserDefaultPreferenceKey, V>(
152174
for keyPath: KeyPath<UserDefaultPreferenceKeys, K>
153175
) -> V where K.Value == UserDefaultsStorageBox<V> {
@@ -173,7 +195,7 @@ public extension UserDefaultsType {
173195
let key = UserDefaultPreferenceKeys()[keyPath: keyPath]
174196
set(value.rawValue, forKey: key.key)
175197
}
176-
198+
177199
func set<K: UserDefaultPreferenceKey, V: Codable>(
178200
_ value: V,
179201
for keyPath: KeyPath<UserDefaultPreferenceKeys, K>

0 commit comments

Comments
 (0)