-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathSourceEditorConfiguration+Appearance.swift
More file actions
197 lines (169 loc) · 8.51 KB
/
SourceEditorConfiguration+Appearance.swift
File metadata and controls
197 lines (169 loc) · 8.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
//
// SourceEditorConfiguration+Appearance.swift
// CodeEditSourceEditor
//
// Created by Khan Winter on 6/16/25.
//
import AppKit
extension SourceEditorConfiguration {
/// Configure the appearance of the editor. Font, theme, line height, etc.
public struct Appearance: Equatable {
/// The theme for syntax highlighting.
public var theme: EditorTheme
/// Determines whether the editor uses the theme's background color, or a transparent background color.
public var useThemeBackground: Bool = true
/// The default font.
public var font: NSFont
/// The line height multiplier (e.g. `1.2`).
public var lineHeightMultiple: Double
/// The amount of space to use between letters, as a percent. Eg: `1.0` = no space, `1.5` = 1/2 a
/// character's width between characters, etc. Defaults to `1.0`.
public var letterSpacing: Double = 1.0
/// Whether lines wrap to the width of the editor.
public var wrapLines: Bool
/// If true, uses the system cursor on `>=macOS 14`.
public var useSystemCursor: Bool = true
/// The visual tab width in number of spaces.
public var tabWidth: Int
/// The type of highlight to use to highlight bracket pairs.
/// See ``BracketPairEmphasis`` for more information. Defaults to `.flash`.
public var bracketPairEmphasis: BracketPairEmphasis? = .flash
/// Create a new appearance configuration object.
/// - Parameters:
/// - theme: The theme for syntax highlighting.
/// - useThemeBackground: Determines whether the editor uses the theme's background color, or a transparent
/// background color.
/// - font: The default font.
/// - lineHeightMultiple: The line height multiplier (e.g. `1.2`).
/// - letterSpacing: The amount of space to use between letters, as a percent. Eg: `1.0` = no space, `1.5`
/// = 1/2 of a character's width between characters, etc. Defaults to `1.0`.
/// - wrapLines: Whether lines wrap to the width of the editor.
/// - useSystemCursor: If true, uses the system cursor on `>=macOS 14`.
/// - tabWidth: The visual tab width in number of spaces.
/// - bracketPairEmphasis: The type of highlight to use to highlight bracket pairs. See
/// ``BracketPairEmphasis`` for more information. Defaults to `.flash`.
public init(
theme: EditorTheme,
useThemeBackground: Bool = true,
font: NSFont,
lineHeightMultiple: Double = 1.2,
letterSpacing: Double = 1.0,
wrapLines: Bool,
useSystemCursor: Bool = true,
tabWidth: Int = 4,
bracketPairEmphasis: BracketPairEmphasis? = .flash
) {
self.theme = theme
self.useThemeBackground = useThemeBackground
self.font = font
self.lineHeightMultiple = lineHeightMultiple
self.letterSpacing = letterSpacing
self.wrapLines = wrapLines
if #available(macOS 14, *) {
self.useSystemCursor = useSystemCursor
} else {
self.useSystemCursor = false
}
self.tabWidth = tabWidth
self.bracketPairEmphasis = bracketPairEmphasis
}
@MainActor
func didSetOnController(controller: TextViewController, oldConfig: Appearance?) {
var needsHighlighterInvalidation = false
if oldConfig?.font != font {
controller.textView.font = font
controller.textView.typingAttributes = controller.attributesFor(nil)
controller.gutterView.font = font.rulerFont
// Force the layout manager to recalculate its cached estimated line height.
// The estimate is cached and not invalidated by font changes, causing vertical
// cursor movement (moveDown:) to use stale values and fail to cross line boundaries.
let renderDelegate = controller.textView.layoutManager.renderDelegate
controller.textView.layoutManager.renderDelegate = renderDelegate
needsHighlighterInvalidation = true
}
if oldConfig?.theme != theme || oldConfig?.useThemeBackground != useThemeBackground {
updateControllerNewTheme(controller: controller)
needsHighlighterInvalidation = true
}
if oldConfig?.tabWidth != tabWidth {
controller.paragraphStyle = controller.generateParagraphStyle()
controller.textView.layoutManager.setNeedsLayout()
needsHighlighterInvalidation = true
}
if oldConfig?.lineHeightMultiple != lineHeightMultiple {
controller.textView.layoutManager.lineHeightMultiplier = lineHeightMultiple
// Also invalidate the cached estimated line height (same issue as font change above).
let renderDelegate = controller.textView.layoutManager.renderDelegate
controller.textView.layoutManager.renderDelegate = renderDelegate
}
if oldConfig?.wrapLines != wrapLines {
controller.textView.layoutManager.wrapLines = wrapLines
controller.minimapView.layoutManager?.wrapLines = wrapLines
controller.scrollView.hasHorizontalScroller = !wrapLines
controller.updateTextInsets()
}
// useThemeBackground isn't needed
if oldConfig?.letterSpacing != letterSpacing {
controller.textView.letterSpacing = letterSpacing
needsHighlighterInvalidation = true
}
if oldConfig?.bracketPairEmphasis != bracketPairEmphasis {
controller.emphasizeSelectionPairs()
}
// Cant put these in one if sadly
if #available(macOS 14, *) {
if oldConfig?.useSystemCursor != useSystemCursor {
controller.textView.useSystemCursor = useSystemCursor
}
}
if needsHighlighterInvalidation {
controller.highlighter?.invalidate()
}
}
private func updateControllerNewTheme(controller: TextViewController) {
controller.textView.layoutManager.setNeedsLayout()
controller.textView.textStorage.setAttributes(
controller.attributesFor(nil),
range: NSRange(location: 0, length: controller.textView.textStorage.length)
)
controller.textView.selectionManager.selectionBackgroundColor = theme.selection
controller.textView.selectionManager.selectedLineBackgroundColor = getThemeBackground(
systemAppearance: controller.systemAppearance
)
controller.textView.selectionManager.insertionPointColor = theme.insertionPoint
controller.textView.enclosingScrollView?.backgroundColor = if useThemeBackground {
theme.background
} else {
.clear
}
controller.gutterView.textColor = theme.text.color.withAlphaComponent(0.35)
controller.gutterView.selectedLineTextColor = theme.text.color
controller.gutterView.selectedLineColor = if useThemeBackground {
theme.lineHighlight
} else if controller.systemAppearance == .darkAqua {
NSColor.quaternaryLabelColor
} else {
NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
}
controller.gutterView.backgroundColor = if useThemeBackground {
theme.background
} else {
.windowBackgroundColor
}
controller.minimapView.setTheme(theme)
controller.reformattingGuideView?.theme = theme
controller.textView.typingAttributes = controller.attributesFor(nil)
}
/// Finds the preferred use theme background.
/// - Returns: The background color to use.
private func getThemeBackground(systemAppearance: NSAppearance.Name?) -> NSColor {
if useThemeBackground {
return theme.lineHighlight
}
if systemAppearance == .darkAqua {
return NSColor.quaternaryLabelColor
}
return NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
}
}
}