Skip to content

Commit

Permalink
(fix:#227) Apply Theme Selection Color (#230)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecoolwinter authored Feb 5, 2024
1 parent 704b76d commit 0816105
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ extension TextViewController {
scrollView = NSScrollView()
textView.postsFrameChangedNotifications = true
textView.translatesAutoresizingMaskIntoConstraints = false
textView.selectionManager.insertionPointColor = theme.insertionPoint

scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.contentView.postsFrameChangedNotifications = true
scrollView.hasVerticalScroller = true
scrollView.hasHorizontalScroller = true
scrollView.documentView = textView
scrollView.contentView.postsBoundsChangedNotifications = true
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
if let contentInsets {
scrollView.automaticallyAdjustsContentInsets = false
scrollView.contentInsets = contentInsets
Expand All @@ -35,7 +33,6 @@ extension TextViewController {
delegate: self
)
gutterView.frame.origin.y = -scrollView.contentInsets.top
gutterView.backgroundColor = useThemeBackground ? theme.background : .textBackgroundColor
gutterView.updateWidthIfNeeded()
scrollView.addFloatingSubview(
gutterView,
Expand All @@ -46,6 +43,10 @@ extension TextViewController {
if let _undoManager {
textView.setUndoManager(_undoManager)
}

styleTextView()
styleGutterView()
styleScrollView()
setUpHighlighter()
setUpTextFormation()

Expand Down
50 changes: 37 additions & 13 deletions Sources/CodeEditSourceEditor/Controller/TextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class TextViewController: NSViewController {
attributesFor(nil),
range: NSRange(location: 0, length: textView.textStorage.length)
)
textView.selectionManager.selectedLineBackgroundColor = theme.selection
highlighter?.invalidate()
}
}
Expand Down Expand Up @@ -263,16 +264,39 @@ public class TextViewController: NSViewController {
textView.isEditable = isEditable
textView.isSelectable = isSelectable

styleTextView()
styleGutterView()
styleScrollView()

highlighter?.invalidate()
}

/// Style the text view.
package func styleTextView() {
textView.selectionManager.selectionBackgroundColor = theme.selection
textView.selectionManager.selectedLineBackgroundColor = useThemeBackground
? theme.lineHighlight
: systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor : NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
textView.selectionManager.selectedLineBackgroundColor = getThemeBackground()
textView.selectionManager.highlightSelectedLine = isEditable
textView.selectionManager.insertionPointColor = theme.insertionPoint
paragraphStyle = generateParagraphStyle()
textView.typingAttributes = attributesFor(nil)
}

/// Finds the preferred use theme background.
/// - Returns: The background color to use.
private func getThemeBackground() -> NSColor {
if useThemeBackground {
return theme.lineHighlight
}

if systemAppearance == .darkAqua {
return NSColor.quaternaryLabelColor
}

return NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
}

/// Style the gutter view.
package func styleGutterView() {
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
Expand All @@ -283,17 +307,17 @@ public class TextViewController: NSViewController {
gutterView.selectedLineTextColor = nil
gutterView.selectedLineColor = .clear
}
}

if let scrollView = view as? NSScrollView {
scrollView.drawsBackground = useThemeBackground
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
if let contentInsets = contentInsets {
scrollView.contentInsets = contentInsets
}
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
/// Style the scroll view.
package func styleScrollView() {
guard let scrollView = view as? NSScrollView else { return }
scrollView.drawsBackground = useThemeBackground
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
if let contentInsets = contentInsets {
scrollView.contentInsets = contentInsets
}

highlighter?.invalidate()
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets
}

deinit {
Expand Down

0 comments on commit 0816105

Please sign in to comment.