Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added font weight pickers to text editing and terminal settings #1758

Merged
merged 4 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CodeEdit.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@
B6C6A42A297716A500A3D28F /* EditorTabCloseButton.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */; };
B6C6A42E29771A8D00A3D28F /* EditorTabButtonStyle.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */; };
B6C6A43029771F7100A3D28F /* EditorTabBackground.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */; };
B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */; };
B6D7EA592971078500301FAC /* InspectorSection.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA582971078500301FAC /* InspectorSection.swift */; };
B6D7EA5C297107DD00301FAC /* InspectorField.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6D7EA5B297107DD00301FAC /* InspectorField.swift */; };
B6E41C7029DD157F0088F9F4 /* AccountsSettingsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */; };
Expand Down Expand Up @@ -1056,6 +1057,7 @@
B6C6A429297716A500A3D28F /* EditorTabCloseButton.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabCloseButton.swift; sourceTree = "<group>"; };
B6C6A42D29771A8D00A3D28F /* EditorTabButtonStyle.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = EditorTabButtonStyle.swift; sourceTree = "<group>"; };
B6C6A42F29771F7100A3D28F /* EditorTabBackground.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditorTabBackground.swift; sourceTree = "<group>"; };
B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontWeightPicker.swift; sourceTree = "<group>"; };
B6D7EA582971078500301FAC /* InspectorSection.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorSection.swift; sourceTree = "<group>"; };
B6D7EA5B297107DD00301FAC /* InspectorField.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InspectorField.swift; sourceTree = "<group>"; };
B6E41C6F29DD157F0088F9F4 /* AccountsSettingsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AccountsSettingsView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -2988,6 +2990,7 @@
B640A9A029E2188F00715F20 /* View+NavigationBarBackButtonVisible.swift */,
B6EA200129DB7F81001BF195 /* View+ConstrainHeightToWindow.swift */,
B6E41C7329DD40010088F9F4 /* View+HideSidebarToggle.swift */,
B6CFD80C2C1B9A8000E63F1A /* FontWeightPicker.swift */,
);
path = Views;
sourceTree = "<group>";
Expand Down Expand Up @@ -3547,6 +3550,7 @@
5882252B292C280D00E83CDE /* StatusBarCursorPositionLabel.swift in Sources */,
5882252D292C280D00E83CDE /* UtilityAreaSplitTerminalButton.swift in Sources */,
58798238292E30B90085B254 /* FeedbackWindowController.swift in Sources */,
B6CFD80D2C1B9A8000E63F1A /* FontWeightPicker.swift in Sources */,
587B9E6C29301D8F00AC7927 /* GitLabNamespace.swift in Sources */,
30AB4EC22BF7253200ED4431 /* KeyValueTable.swift in Sources */,
6C48D8F22972DAFC00D6D205 /* Env+IsFullscreen.swift in Sources */,
Expand Down
4 changes: 1 addition & 3 deletions CodeEdit/Features/Editor/TabBar/Tabs/Tab/EditorTabView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -134,15 +134,13 @@ struct EditorTabView: View {
// Tab content (icon and text).
HStack(alignment: .center, spacing: 3) {
Image(nsImage: item.nsIcon)
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 16, height: 16)
.foregroundColor(
fileIconStyle == .color
&& activeState != .inactive && isActiveEditor
? item.iconColor
: .secondary
)
.frame(width: 16, height: 16)
Text(item.name)
.font(
isTemporary
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ extension SettingsData {
/// The name of the custom font
var name: String = "SF Mono"

/// The weight of the custom font
var weight: NSFont.Weight = .medium

/// Default initializer
init() {}

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

/// Returns an NSFont representation of the current configuration.
///
/// Returns the custom font, if enabled and able to be instantiated.
/// Otherwise returns a default system font monospaced.
var current: NSFont {
return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight)
return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct TerminalSettingsView: View {
if !settings.useTextEditorFont {
fontSelector
fontSizeSelector
fontWeightSelector
}
}
Section {
Expand Down Expand Up @@ -86,6 +87,10 @@ private extension TerminalSettingsView {
)
}

@ViewBuilder private var fontWeightSelector: some View {
FontWeightPicker(selection: $settings.font.weight)
}

tom-ludwig marked this conversation as resolved.
Show resolved Hide resolved
@ViewBuilder private var injectionOptions: some View {
VStack {
Toggle("Shell Integration", isOn: $settings.useShellIntegration)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extension SettingsData {
"Wrap lines to editor width",
"Font",
"Font Size",
"Font Weight",
"Line Height",
"Letter Spacing",
"Autocomplete braces",
Expand Down Expand Up @@ -48,8 +49,8 @@ extension SettingsData {
/// A flag indicating whether to wrap lines to editor width
var wrapLinesToEditorWidth: Bool = true

/// A multiplier for setting the line height. Defaults to `1.45`
var lineHeightMultiple: Double = 1.45
/// A multiplier for setting the line height. Defaults to `1.2`
var lineHeightMultiple: Double = 1.2

/// A multiplier for setting the letter spacing, `1` being no spacing and
/// `2` is one character of spacing between letters, defaults to `1`.
Expand Down Expand Up @@ -87,7 +88,7 @@ extension SettingsData {
self.lineHeightMultiple = try container.decodeIfPresent(
Double.self,
forKey: .lineHeightMultiple
) ?? 1.45
) ?? 1.2
self.letterSpacing = try container.decodeIfPresent(
Double.self,
forKey: .letterSpacing
Expand Down Expand Up @@ -167,6 +168,9 @@ extension SettingsData {
/// The name of the custom font
var name: String = "SF Mono"

/// The weight of the custom font
var weight: NSFont.Weight = .medium

/// Default initializer
init() {}

Expand All @@ -175,14 +179,66 @@ extension SettingsData {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.size = try container.decodeIfPresent(Double.self, forKey: .size) ?? size
self.name = try container.decodeIfPresent(String.self, forKey: .name) ?? name
self.weight = try container.decodeIfPresent(NSFont.Weight.self, forKey: .weight) ?? weight
}

/// Returns an NSFont representation of the current configuration.
///
/// Returns the custom font, if enabled and able to be instantiated.
/// Otherwise returns a default system font monospaced.
var current: NSFont {
return NSFont(name: name, size: size) ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
let customFont = NSFont(name: name, size: size)?.withWeight(weight: weight)
return customFont ?? NSFont.monospacedSystemFont(ofSize: size, weight: .medium)
}
}
}

extension NSFont {
tom-ludwig marked this conversation as resolved.
Show resolved Hide resolved
/// Rough mapping from behavior of .systemFont(…weight:)
/// to NSFontManager's Int-based weight, as of 13.4 Ventura
func withWeight(weight: NSFont.Weight) -> NSFont? {
let fontManager = NSFontManager.shared
var intWeight: Int

switch weight {
case .ultraLight:
intWeight=0
case .light:
intWeight=2
case .thin:
intWeight=3
case .medium:
intWeight=6
case .semibold:
intWeight=8
case .bold:
intWeight=9
case .heavy:
intWeight=10
case .black:
intWeight=15
default:
intWeight=5
}

return fontManager.font(
withFamily: self.familyName ?? "",
traits: [],
weight: intWeight,
size: self.pointSize
)
}
}

extension NSFont.Weight: Codable {
public func encode(to encoder: Encoder) throws {
var container = encoder.singleValueContainer()
try container.encode(self.rawValue)
}

public init(from decoder: Decoder) throws {
let container = try decoder.singleValueContainer()
let rawValue = try container.decode(CGFloat.self)
self = NSFont.Weight(rawValue: rawValue)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ struct TextEditingSettingsView: View {
Section {
fontSelector
fontSizeSelector
fontWeightSelector
lineHeight
letterSpacing
}
Expand Down Expand Up @@ -51,6 +52,10 @@ private extension TextEditingSettingsView {
)
}

@ViewBuilder private var fontWeightSelector: some View {
FontWeightPicker(selection: $textEditing.font.weight)
}

@ViewBuilder private var autocompleteBraces: some View {
Toggle(isOn: $textEditing.autocompleteBraces) {
Text("Autocomplete braces")
Expand Down
46 changes: 46 additions & 0 deletions CodeEdit/Features/Settings/Views/FontWeightPicker.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//
// FontWeightPicker.swift
// CodeEdit
//
// Created by Austin Condiff on 6/13/24.
//

import SwiftUI

struct FontWeightPicker: View {
@Binding var selection: NSFont.Weight
var label: String?

let fontWeights: [NSFont.Weight] = [
.ultraLight,
.thin,
.light,
.regular,
.medium,
.semibold,
.bold,
.heavy,
.black
]

var weightNames: [NSFont.Weight: String] = [
.ultraLight: "Ultra Light",
.thin: "Thin",
.light: "Light",
.regular: "Regular",
.medium: "Medium",
.semibold: "Semi Bold",
.bold: "Bold",
.heavy: "Heavy",
.black: "Black"
]

var body: some View {
Picker(label ?? "Font Weight", selection: $selection) {
ForEach(fontWeights, id: \.self) { weight in
Text(weightNames[weight] ?? "Unknown")
.tag(weight)
}
}
}
}
Loading