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 {
@@ -101,5 +102,17 @@ extension AppPreferences {
101102 self . size = try container. decodeIfPresent ( Int . self, forKey: . size) ?? 11
102103 self . name = try container. decodeIfPresent ( String . self, forKey: . name) ?? " SFMono-Medium "
103104 }
105+
106+ /// Returns an NSFont representation of the current configuration.
107+ ///
108+ /// Returns the custom font, if enabled and able to be instantiated.
109+ /// Otherwise returns a default system font monospaced, size 12.
110+ func current( ) -> NSFont {
111+ guard customFont,
112+ let customFont = NSFont ( name: name, size: Double ( size) ) else {
113+ return NSFont . monospacedSystemFont ( ofSize: 12 , weight: . regular)
114+ }
115+ return customFont
116+ }
104117 }
105118}
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