Skip to content

Commit 62b6cee

Browse files
committed
Fix bug which made reverting to system font impossible. Create convenience factory method on EditorFont
1 parent 21f70a2 commit 62b6cee

File tree

2 files changed

+15
-7
lines changed

2 files changed

+15
-7
lines changed

CodeEdit/Features/AppPreferences/Model/Text Editing/TextEditingPreferences.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Nanashi Li on 2022/04/08.
66
//
77

8+
import AppKit
89
import Foundation
910

1011
extension AppPreferences {
@@ -93,5 +94,17 @@ extension AppPreferences {
9394
self.size = try container.decodeIfPresent(Int.self, forKey: .size) ?? 11
9495
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? "SFMono-Medium"
9596
}
97+
98+
/// Returns an NSFont representation of the current configuration.
99+
///
100+
/// Returns the custom font, if enabled and able to be instantiated.
101+
/// Otherwise returns a default system font monospaced, size 12.
102+
func current() -> NSFont {
103+
guard customFont,
104+
let customFont = NSFont(name: name, size: Double(size)) else {
105+
return NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)
106+
}
107+
return customFont
108+
}
96109
}
97110
}

CodeEdit/Features/CodeFile/CodeFileView.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ struct CodeFileView: View {
5757

5858
@State
5959
private var font: NSFont = {
60-
let size = AppPreferencesModel.shared.preferences.textEditing.font.size
61-
let name = AppPreferencesModel.shared.preferences.textEditing.font.name
62-
return NSFont(name: name, size: Double(size)) ?? NSFont.monospacedSystemFont(ofSize: 12, weight: .regular)
60+
return AppPreferencesModel.shared.preferences.textEditing.font.current()
6361
}()
6462

6563
var body: some View {
@@ -89,10 +87,7 @@ struct CodeFileView: View {
8987
}
9088
}
9189
.onChange(of: prefs.preferences.textEditing.font) { _ in
92-
font = NSFont(
93-
name: prefs.preferences.textEditing.font.name,
94-
size: Double(prefs.preferences.textEditing.font.size)
95-
) ?? .monospacedSystemFont(ofSize: 12, weight: .regular)
90+
font = AppPreferencesModel.shared.preferences.textEditing.font.current()
9691
}
9792
}
9893

0 commit comments

Comments
 (0)