File tree Expand file tree Collapse file tree 2 files changed +15
-7
lines changed
AppPreferences/Model/Text Editing Expand file tree Collapse file tree 2 files changed +15
-7
lines changed Original file line number Diff line number Diff line change 55// Created by Nanashi Li on 2022/04/08.
66//
77
8+ import AppKit
89import Foundation
910
1011extension 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}
Original file line number Diff line number Diff 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
You can’t perform that action at this time.
0 commit comments