@@ -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