Skip to content

Commit 67724ca

Browse files
committed
Moved NSFont extension to its own file
1 parent 48c2267 commit 67724ca

3 files changed

Lines changed: 62 additions & 50 deletions

File tree

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,7 @@
487487
B6C6A42E29771A8D00A3D28F /* EditorTabButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */; };
488488
B6C6A43029771F7100A3D28F /* EditorTabBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */; };
489489
B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */; };
490+
B6CFD8112C20A8EE00E63F1A /* NSFont+WithWeight.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */; };
490491
B6D7EA592971078500301FAC /* InspectorSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA582971078500301FAC /* InspectorSection.swift */; };
491492
B6D7EA5C297107DD00301FAC /* InspectorField.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA5B297107DD00301FAC /* InspectorField.swift */; };
492493
B6E41C7029DD157F0088F9F4 /* AccountsSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */; };
@@ -1058,6 +1059,7 @@
10581059
B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorTabButtonStyle.swift; sourceTree = "<group>"; };
10591060
B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabBackground.swift; sourceTree = "<group>"; };
10601061
B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontWeightPicker.swift; sourceTree = "<group>"; };
1062+
B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSFont+WithWeight.swift"; sourceTree = "<group>"; };
10611063
B6D7EA582971078500301FAC /* InspectorSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorSection.swift; sourceTree = "<group>"; };
10621064
B6D7EA5B297107DD00301FAC /* InspectorField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorField.swift; sourceTree = "<group>"; };
10631065
B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSettingsView.swift; sourceTree = "<group>"; };
@@ -2975,6 +2977,7 @@
29752977
isa = PBXGroup;
29762978
children = (
29772979
58F2EAD8292FB2B0004A9BDE /* TextEditingSettings.swift */,
2980+
B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */,
29782981
);
29792982
path = Models;
29802983
sourceTree = "<group>";
@@ -3495,6 +3498,7 @@
34953498
58798284292ED0FB0085B254 /* TerminalEmulatorView.swift in Sources */,
34963499
B6C4F2AC2B3CC4D000B2B140 /* CommitChangedFileListItemView.swift in Sources */,
34973500
6C82D6B329BFD88700495C54 /* NavigateCommands.swift in Sources */,
3501+
B6CFD8112C20A8EE00E63F1A /* NSFont+WithWeight.swift in Sources */,
34983502
B66A4E4C29C9179B004573B4 /* CodeEditApp.swift in Sources */,
34993503
661EF7B82BEE215300C3E577 /* ImageFileView.swift in Sources */,
35003504
4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */,
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//
2+
// NSFont+WithWeight.swift
3+
// CodeEdit
4+
//
5+
// Created by Austin Condiff on 6/17/24.
6+
//
7+
8+
import SwiftUI
9+
10+
extension NSFont {
11+
/// Rough mapping from behavior of .systemFont(…weight:)
12+
/// to NSFontManager's Int-based weight, as of 13.4 Ventura
13+
func withWeight(weight: NSFont.Weight) -> NSFont? {
14+
let fontManager = NSFontManager.shared
15+
var intWeight: Int
16+
17+
switch weight {
18+
case .ultraLight:
19+
intWeight=0
20+
case .light:
21+
intWeight=2
22+
case .thin:
23+
intWeight=3
24+
case .medium:
25+
intWeight=6
26+
case .semibold:
27+
intWeight=8
28+
case .bold:
29+
intWeight=9
30+
case .heavy:
31+
intWeight=10
32+
case .black:
33+
intWeight=15
34+
default:
35+
intWeight=5
36+
}
37+
38+
return fontManager.font(
39+
withFamily: self.familyName ?? "",
40+
traits: [],
41+
weight: intWeight,
42+
size: self.pointSize
43+
)
44+
}
45+
}
46+
47+
extension NSFont.Weight: Codable {
48+
public func encode(to encoder: Encoder) throws {
49+
var container = encoder.singleValueContainer()
50+
try container.encode(self.rawValue)
51+
}
52+
53+
public init(from decoder: Decoder) throws {
54+
let container = try decoder.singleValueContainer()
55+
let rawValue = try container.decode(CGFloat.self)
56+
self = NSFont.Weight(rawValue: rawValue)
57+
}
58+
}

CodeEdit/Features/Settings/Pages/TextEditingSettings/Models/TextEditingSettings.swift

Lines changed: 0 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -192,53 +192,3 @@ extension SettingsData {
192192
}
193193
}
194194
}
195-
196-
extension NSFont {
197-
/// Rough mapping from behavior of .systemFont(…weight:)
198-
/// to NSFontManager's Int-based weight, as of 13.4 Ventura
199-
func withWeight(weight: NSFont.Weight) -> NSFont? {
200-
let fontManager = NSFontManager.shared
201-
var intWeight: Int
202-
203-
switch weight {
204-
case .ultraLight:
205-
intWeight=0
206-
case .light:
207-
intWeight=2
208-
case .thin:
209-
intWeight=3
210-
case .medium:
211-
intWeight=6
212-
case .semibold:
213-
intWeight=8
214-
case .bold:
215-
intWeight=9
216-
case .heavy:
217-
intWeight=10
218-
case .black:
219-
intWeight=15
220-
default:
221-
intWeight=5
222-
}
223-
224-
return fontManager.font(
225-
withFamily: self.familyName ?? "",
226-
traits: [],
227-
weight: intWeight,
228-
size: self.pointSize
229-
)
230-
}
231-
}
232-
233-
extension NSFont.Weight: Codable {
234-
public func encode(to encoder: Encoder) throws {
235-
var container = encoder.singleValueContainer()
236-
try container.encode(self.rawValue)
237-
}
238-
239-
public init(from decoder: Decoder) throws {
240-
let container = try decoder.singleValueContainer()
241-
let rawValue = try container.decode(CGFloat.self)
242-
self = NSFont.Weight(rawValue: rawValue)
243-
}
244-
}

0 commit comments

Comments
 (0)