Skip to content

Commit 2921e93

Browse files
Line highlight improvements when not using theme background (#181)
### Description Not using theme highlight color when not using theme background. Instead, we are using system colors to highlight the current line. ### Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots When using the theme background... <img width="628" alt="image" src="https://user-images.githubusercontent.com/806104/234382400-afa7ce2f-4e81-47e1-a022-5a4cf24d8db1.png"> When not using the theme background... Before <img width="626" alt="image" src="https://user-images.githubusercontent.com/806104/234380570-5b3aac7e-4baa-4aa3-8d09-b2d4e4113b7c.png"> After <img width="634" alt="image" src="https://user-images.githubusercontent.com/806104/234380524-eb4167f4-750c-4f3b-b4d4-5467738d3cdc.png">
1 parent 2ed4b2a commit 2921e93

File tree

2 files changed

+45
-19
lines changed

2 files changed

+45
-19
lines changed

Sources/CodeEditTextView/Controller/STTextViewController+Highlighter.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,17 @@ extension STTextViewController {
3939
highlighter?.setHighlightProvider(provider)
4040
}
4141
}
42+
43+
/// Gets all attributes for the given capture including the line height, background color, and text color.
44+
/// - Parameter capture: The capture to use for syntax highlighting.
45+
/// - Returns: All attributes to be applied.
46+
public func attributesFor(_ capture: CaptureName?) -> [NSAttributedString.Key: Any] {
47+
return [
48+
.font: font,
49+
.foregroundColor: theme.colorFor(capture),
50+
.baselineOffset: baselineOffset,
51+
.paragraphStyle: paragraphStyle,
52+
.kern: kern
53+
]
54+
}
4255
}

Sources/CodeEditTextView/Controller/STTextViewController.swift

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
3636
/// Whether the code editor should use the theme background color or be transparent
3737
public var useThemeBackground: Bool
3838

39+
public var systemAppearance: NSAppearance.Name?
40+
41+
var cancellables = Set<AnyCancellable>()
42+
3943
/// The visual width of tab characters in the text view measured in number of spaces.
4044
public var tabWidth: Int {
4145
didSet {
@@ -85,7 +89,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
8589
}
8690

8791
/// The kern to use for characters. Defaults to `0.0` and is updated when `letterSpacing` is set.
88-
private var kern: CGFloat = 0.0
92+
internal var kern: CGFloat = 0.0
8993

9094
private var fontCharWidth: CGFloat {
9195
(" " as NSString).size(withAttributes: [.font: font]).width
@@ -160,7 +164,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
160164
rulerView.drawSeparator = false
161165
rulerView.baselineOffset = baselineOffset
162166
rulerView.font = rulerFont
163-
rulerView.selectedLineHighlightColor = theme.lineHighlight
167+
rulerView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
168+
? NSColor.quaternaryLabelColor
169+
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
164170
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
165171
rulerView.allowsMarkers = false
166172

@@ -180,7 +186,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
180186
textView.insertionPointColor = theme.insertionPoint
181187
textView.insertionPointWidth = 1.0
182188
textView.selectionBackgroundColor = theme.selection
183-
textView.selectedLineHighlightColor = theme.lineHighlight
189+
textView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
190+
? NSColor.quaternaryLabelColor
191+
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
184192
textView.string = self.text.wrappedValue
185193
textView.isEditable = self.isEditable
186194
textView.highlightSelectedLine = true
@@ -240,6 +248,19 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
240248
) { [weak self] _ in
241249
self?.updateTextContainerWidthIfNeeded()
242250
}
251+
252+
systemAppearance = NSApp.effectiveAppearance.name
253+
254+
NSApp.publisher(for: \.effectiveAppearance)
255+
.receive(on: RunLoop.main)
256+
.sink { [weak self] newValue in
257+
guard let self = self else { return }
258+
259+
if self.systemAppearance != newValue.name {
260+
self.systemAppearance = newValue.name
261+
}
262+
}
263+
.store(in: &cancellables)
243264
}
244265

245266
public override func viewWillAppear() {
@@ -254,7 +275,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
254275
// MARK: UI
255276

256277
/// A default `NSParagraphStyle` with a set `lineHeight`
257-
private lazy var paragraphStyle: NSMutableParagraphStyle = generateParagraphStyle()
278+
internal lazy var paragraphStyle: NSMutableParagraphStyle = generateParagraphStyle()
258279

259280
private func generateParagraphStyle() -> NSMutableParagraphStyle {
260281
// swiftlint:disable:next force_cast
@@ -284,7 +305,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
284305
textView.backgroundColor = useThemeBackground ? theme.background : .clear
285306
textView?.insertionPointColor = theme.insertionPoint
286307
textView?.selectionBackgroundColor = theme.selection
287-
textView?.selectedLineHighlightColor = theme.lineHighlight
308+
textView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
309+
? NSColor.quaternaryLabelColor
310+
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
288311
textView?.isEditable = isEditable
289312
textView.highlightSelectedLine = isEditable
290313
textView?.typingAttributes = attributesFor(nil)
@@ -293,7 +316,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
293316

294317
rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
295318
rulerView?.separatorColor = theme.invisibles
296-
rulerView?.selectedLineHighlightColor = theme.lineHighlight
319+
rulerView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
320+
? NSColor.quaternaryLabelColor
321+
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
297322
rulerView?.baselineOffset = baselineOffset
298323
rulerView.highlightSelectedLine = isEditable
299324
rulerView?.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
@@ -312,19 +337,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
312337
updateTextContainerWidthIfNeeded()
313338
}
314339

315-
/// Gets all attributes for the given capture including the line height, background color, and text color.
316-
/// - Parameter capture: The capture to use for syntax highlighting.
317-
/// - Returns: All attributes to be applied.
318-
public func attributesFor(_ capture: CaptureName?) -> [NSAttributedString.Key: Any] {
319-
return [
320-
.font: font,
321-
.foregroundColor: theme.colorFor(capture),
322-
.baselineOffset: baselineOffset,
323-
.paragraphStyle: paragraphStyle,
324-
.kern: kern
325-
]
326-
}
327-
328340
/// Calculated line height depending on ``STTextViewController/lineHeightMultiple``
329341
internal var lineHeight: Double {
330342
font.lineHeight * lineHeightMultiple
@@ -348,5 +360,6 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
348360
deinit {
349361
textView = nil
350362
highlighter = nil
363+
cancellables.forEach { $0.cancel() }
351364
}
352365
}

0 commit comments

Comments
 (0)