Skip to content

Commit 0816105

Browse files
(fix:#227) Apply Theme Selection Color (#230)
1 parent 704b76d commit 0816105

File tree

2 files changed

+41
-16
lines changed

2 files changed

+41
-16
lines changed

Sources/CodeEditSourceEditor/Controller/TextViewController+LoadView.swift

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@ extension TextViewController {
1414
scrollView = NSScrollView()
1515
textView.postsFrameChangedNotifications = true
1616
textView.translatesAutoresizingMaskIntoConstraints = false
17-
textView.selectionManager.insertionPointColor = theme.insertionPoint
1817

1918
scrollView.translatesAutoresizingMaskIntoConstraints = false
2019
scrollView.contentView.postsFrameChangedNotifications = true
2120
scrollView.hasVerticalScroller = true
2221
scrollView.hasHorizontalScroller = true
2322
scrollView.documentView = textView
2423
scrollView.contentView.postsBoundsChangedNotifications = true
25-
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
2624
if let contentInsets {
2725
scrollView.automaticallyAdjustsContentInsets = false
2826
scrollView.contentInsets = contentInsets
@@ -35,7 +33,6 @@ extension TextViewController {
3533
delegate: self
3634
)
3735
gutterView.frame.origin.y = -scrollView.contentInsets.top
38-
gutterView.backgroundColor = useThemeBackground ? theme.background : .textBackgroundColor
3936
gutterView.updateWidthIfNeeded()
4037
scrollView.addFloatingSubview(
4138
gutterView,
@@ -46,6 +43,10 @@ extension TextViewController {
4643
if let _undoManager {
4744
textView.setUndoManager(_undoManager)
4845
}
46+
47+
styleTextView()
48+
styleGutterView()
49+
styleScrollView()
4950
setUpHighlighter()
5051
setUpTextFormation()
5152

Sources/CodeEditSourceEditor/Controller/TextViewController.swift

+37-13
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public class TextViewController: NSViewController {
5959
attributesFor(nil),
6060
range: NSRange(location: 0, length: textView.textStorage.length)
6161
)
62+
textView.selectionManager.selectedLineBackgroundColor = theme.selection
6263
highlighter?.invalidate()
6364
}
6465
}
@@ -263,16 +264,39 @@ public class TextViewController: NSViewController {
263264
textView.isEditable = isEditable
264265
textView.isSelectable = isSelectable
265266

267+
styleTextView()
268+
styleGutterView()
269+
styleScrollView()
270+
271+
highlighter?.invalidate()
272+
}
273+
274+
/// Style the text view.
275+
package func styleTextView() {
266276
textView.selectionManager.selectionBackgroundColor = theme.selection
267-
textView.selectionManager.selectedLineBackgroundColor = useThemeBackground
268-
? theme.lineHighlight
269-
: systemAppearance == .darkAqua
270-
? NSColor.quaternaryLabelColor : NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
277+
textView.selectionManager.selectedLineBackgroundColor = getThemeBackground()
271278
textView.selectionManager.highlightSelectedLine = isEditable
272279
textView.selectionManager.insertionPointColor = theme.insertionPoint
273280
paragraphStyle = generateParagraphStyle()
274281
textView.typingAttributes = attributesFor(nil)
282+
}
283+
284+
/// Finds the preferred use theme background.
285+
/// - Returns: The background color to use.
286+
private func getThemeBackground() -> NSColor {
287+
if useThemeBackground {
288+
return theme.lineHighlight
289+
}
275290

291+
if systemAppearance == .darkAqua {
292+
return NSColor.quaternaryLabelColor
293+
}
294+
295+
return NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
296+
}
297+
298+
/// Style the gutter view.
299+
package func styleGutterView() {
276300
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
277301
? NSColor.quaternaryLabelColor
278302
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
@@ -283,17 +307,17 @@ public class TextViewController: NSViewController {
283307
gutterView.selectedLineTextColor = nil
284308
gutterView.selectedLineColor = .clear
285309
}
310+
}
286311

287-
if let scrollView = view as? NSScrollView {
288-
scrollView.drawsBackground = useThemeBackground
289-
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
290-
if let contentInsets = contentInsets {
291-
scrollView.contentInsets = contentInsets
292-
}
293-
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
312+
/// Style the scroll view.
313+
package func styleScrollView() {
314+
guard let scrollView = view as? NSScrollView else { return }
315+
scrollView.drawsBackground = useThemeBackground
316+
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
317+
if let contentInsets = contentInsets {
318+
scrollView.contentInsets = contentInsets
294319
}
295-
296-
highlighter?.invalidate()
320+
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
297321
}
298322

299323
deinit {

0 commit comments

Comments
 (0)