Skip to content

Commit 4a6b234

Browse files
Added font weight pickers to text editing and terminal settings (#1758)
* Default font weight is now medium. Added font weight pickers to text editing and terminal sections within settings. Small fix when using custom icons in editior tab. * Fixed SwiftLint errors * Fixed SwiftLint errors * Moved NSFont extension to its own file
1 parent e3e51f4 commit 4a6b234

File tree

8 files changed

+139
-8
lines changed

8 files changed

+139
-8
lines changed

CodeEdit.xcodeproj/project.pbxproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -486,6 +486,8 @@
486486
B6C6A42A297716A500A3D28F /* EditorTabCloseButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */; };
487487
B6C6A42E29771A8D00A3D28F /* EditorTabButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */; };
488488
B6C6A43029771F7100A3D28F /* EditorTabBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */; };
489+
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 */; };
489491
B6D7EA592971078500301FAC /* InspectorSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA582971078500301FAC /* InspectorSection.swift */; };
490492
B6D7EA5C297107DD00301FAC /* InspectorField.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA5B297107DD00301FAC /* InspectorField.swift */; };
491493
B6E41C7029DD157F0088F9F4 /* AccountsSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */; };
@@ -1056,6 +1058,8 @@
10561058
B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabCloseButton.swift; sourceTree = "<group>"; };
10571059
B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorTabButtonStyle.swift; sourceTree = "<group>"; };
10581060
B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabBackground.swift; sourceTree = "<group>"; };
1061+
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>"; };
10591063
B6D7EA582971078500301FAC /* InspectorSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorSection.swift; sourceTree = "<group>"; };
10601064
B6D7EA5B297107DD00301FAC /* InspectorField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorField.swift; sourceTree = "<group>"; };
10611065
B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSettingsView.swift; sourceTree = "<group>"; };
@@ -2973,6 +2977,7 @@
29732977
isa = PBXGroup;
29742978
children = (
29752979
58F2EAD8292FB2B0004A9BDE /* TextEditingSettings.swift */,
2980+
B6CFD8102C20A8EE00E63F1A /* NSFont+WithWeight.swift */,
29762981
);
29772982
path = Models;
29782983
sourceTree = "<group>";
@@ -2988,6 +2993,7 @@
29882993
B640A9A029E2188F00715F20 /* View+NavigationBarBackButtonVisible.swift */,
29892994
B6EA200129DB7F81001BF195 /* View+ConstrainHeightToWindow.swift */,
29902995
B6E41C7329DD40010088F9F4 /* View+HideSidebarToggle.swift */,
2996+
B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */,
29912997
);
29922998
path = Views;
29932999
sourceTree = "<group>";
@@ -3492,6 +3498,7 @@
34923498
58798284292ED0FB0085B254 /* TerminalEmulatorView.swift in Sources */,
34933499
B6C4F2AC2B3CC4D000B2B140 /* CommitChangedFileListItemView.swift in Sources */,
34943500
6C82D6B329BFD88700495C54 /* NavigateCommands.swift in Sources */,
3501+
B6CFD8112C20A8EE00E63F1A /* NSFont+WithWeight.swift in Sources */,
34953502
B66A4E4C29C9179B004573B4 /* CodeEditApp.swift in Sources */,
34963503
661EF7B82BEE215300C3E577 /* ImageFileView.swift in Sources */,
34973504
4E7F066629602E7B00BB3C12 /* CodeEditSplitViewController.swift in Sources */,
@@ -3547,6 +3554,7 @@
35473554
5882252B292C280D00E83CDE /* StatusBarCursorPositionLabel.swift in Sources */,
35483555
5882252D292C280D00E83CDE /* UtilityAreaSplitTerminalButton.swift in Sources */,
35493556
58798238292E30B90085B254 /* FeedbackWindowController.swift in Sources */,
3557+
B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */,
35503558
587B9E6C29301D8F00AC7927 /* GitLabNamespace.swift in Sources */,
35513559
30AB4EC22BF7253200ED4431 /* KeyValueTable.swift in Sources */,
35523560
6C48D8F22972DAFC00D6D205 /* Env+IsFullscreen.swift in Sources */,

CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,13 @@ struct EditorTabView: View {
134134
// Tab content (icon and text).
135135
HStack(alignment: .center, spacing: 3) {
136136
Image(nsImage: item.nsIcon)
137-
.resizable()
138-
.aspectRatio(contentMode: .fit)
137+
.frame(width: 16, height: 16)
139138
.foregroundColor(
140139
fileIconStyle == .color
141140
&& activeState != .inactive && isActiveEditor
142141
? item.iconColor
143142
: .secondary
144143
)
145-
.frame(width: 16, height: 16)
146144
Text(item.name)
147145
.font(
148146
isTemporary

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,9 @@ extension SettingsData {
104104
/// The name of the custom font
105105
var name: String = "SF Mono"
106106

107+
/// The weight of the custom font
108+
var weight: NSFont.Weight = .medium
109+
107110
/// Default initializer
108111
init() {}
109112

@@ -112,14 +115,16 @@ extension SettingsData {
112115
let container = try decoder.container(keyedBy: CodingKeys.self)
113116
self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size
114117
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name
118+
self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight
115119
}
116120

117121
/// Returns an NSFont representation of the current configuration.
118122
///
119123
/// Returns the custom font, if enabled and able to be instantiated.
120124
/// Otherwise returns a default system font monospaced.
121125
var current: NSFont {
122-
return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
126+
let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight)
127+
return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
123128
}
124129
}
125130
}

CodeEdit/Features/Settings/Pages/TerminalSettings/TerminalSettingsView.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct TerminalSettingsView: View {
2222
if !settings.useTextEditorFont {
2323
fontSelector
2424
fontSizeSelector
25+
fontWeightSelector
2526
}
2627
}
2728
Section {
@@ -86,6 +87,10 @@ private extension TerminalSettingsView {
8687
)
8788
}
8889

90+
@ViewBuilder private var fontWeightSelector: some View {
91+
FontWeightPicker(selection: $settings.font.weight)
92+
}
93+
8994
@ViewBuilder private var injectionOptions: some View {
9095
VStack {
9196
Toggle("Shell Integration", isOn: $settings.useShellIntegration)
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: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ extension SettingsData {
2020
"Wrap lines to editor width",
2121
"Font",
2222
"Font Size",
23+
"Font Weight",
2324
"Line Height",
2425
"Letter Spacing",
2526
"Autocomplete braces",
@@ -48,8 +49,8 @@ extension SettingsData {
4849
/// A flag indicating whether to wrap lines to editor width
4950
var wrapLinesToEditorWidth: Bool = true
5051

51-
/// A multiplier for setting the line height. Defaults to `1.45`
52-
var lineHeightMultiple: Double = 1.45
52+
/// A multiplier for setting the line height. Defaults to `1.2`
53+
var lineHeightMultiple: Double = 1.2
5354

5455
/// A multiplier for setting the letter spacing, `1` being no spacing and
5556
/// `2` is one character of spacing between letters, defaults to `1`.
@@ -87,7 +88,7 @@ extension SettingsData {
8788
self.lineHeightMultiple = try container.decodeIfPresent(
8889
Double.self,
8990
forKey: .lineHeightMultiple
90-
) ?? 1.45
91+
) ?? 1.2
9192
self.letterSpacing = try container.decodeIfPresent(
9293
Double.self,
9394
forKey: .letterSpacing
@@ -167,6 +168,9 @@ extension SettingsData {
167168
/// The name of the custom font
168169
var name: String = "SF Mono"
169170

171+
/// The weight of the custom font
172+
var weight: NSFont.Weight = .medium
173+
170174
/// Default initializer
171175
init() {}
172176

@@ -175,14 +179,16 @@ extension SettingsData {
175179
let container = try decoder.container(keyedBy: CodingKeys.self)
176180
self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size
177181
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name
182+
self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight
178183
}
179184

180185
/// Returns an NSFont representation of the current configuration.
181186
///
182187
/// Returns the custom font, if enabled and able to be instantiated.
183188
/// Otherwise returns a default system font monospaced.
184189
var current: NSFont {
185-
return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
190+
let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight)
191+
return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
186192
}
187193
}
188194
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ struct TextEditingSettingsView: View {
2222
Section {
2323
fontSelector
2424
fontSizeSelector
25+
fontWeightSelector
2526
lineHeight
2627
letterSpacing
2728
}
@@ -51,6 +52,10 @@ private extension TextEditingSettingsView {
5152
)
5253
}
5354

55+
@ViewBuilder private var fontWeightSelector: some View {
56+
FontWeightPicker(selection: $textEditing.font.weight)
57+
}
58+
5459
@ViewBuilder private var autocompleteBraces: some View {
5560
Toggle(isOn: $textEditing.autocompleteBraces) {
5661
Text("Autocomplete braces")
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// FontWeightPicker.swift
3+
// CodeEdit
4+
//
5+
// Created by Austin Condiff on 6/13/24.
6+
//
7+
8+
import SwiftUI
9+
10+
struct FontWeightPicker: View {
11+
@Binding var selection: NSFont.Weight
12+
var label: String?
13+
14+
let fontWeights: [NSFont.Weight] = [
15+
.ultraLight,
16+
.thin,
17+
.light,
18+
.regular,
19+
.medium,
20+
.semibold,
21+
.bold,
22+
.heavy,
23+
.black
24+
]
25+
26+
var weightNames: [NSFont.Weight: String] = [
27+
.ultraLight: "Ultra Light",
28+
.thin: "Thin",
29+
.light: "Light",
30+
.regular: "Regular",
31+
.medium: "Medium",
32+
.semibold: "Semi Bold",
33+
.bold: "Bold",
34+
.heavy: "Heavy",
35+
.black: "Black"
36+
]
37+
38+
var body: some View {
39+
Picker(label ?? "Font Weight", selection: $selection) {
40+
ForEach(fontWeights, id: \.self) { weight in
41+
Text(weightNames[weight] ?? "Unknown")
42+
.tag(weight)
43+
}
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)