Skip to content

Commit

Permalink
Theme background (#123)
Browse files Browse the repository at this point in the history
# Description
Adds `useThemeBackground` to `CodeEditTextView` which dictates whether
the background of `CodeEditTextView` is clear or uses the given theme
background color

# Related
* CodeEditApp/CodeEdit#1004
  • Loading branch information
neil-ptr authored Jan 23, 2023
1 parent 9f79fa8 commit 58f7941
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
10 changes: 8 additions & 2 deletions Sources/CodeEditTextView/CodeEditTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
/// - text: The text content
/// - language: The language for syntax highlighting
/// - theme: The theme for syntax highlighting
/// - useThemeBackground: Whether CodeEditTextView uses theme background color or is transparent
/// - font: The default font
/// - tabWidth: The tab width
/// - lineHeight: The line height multiplier (e.g. `1.2`)
Expand All @@ -31,11 +32,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
lineHeight: Binding<Double>,
wrapLines: Binding<Bool>,
editorOverscroll: Binding<Double> = .constant(0.0),
cursorPosition: Published<(Int, Int)>.Publisher? = nil
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
useThemeBackground: Bool = true
) {
self._text = text
self.language = language
self._theme = theme
self.useThemeBackground = useThemeBackground
self._font = font
self._tabWidth = tabWidth
self._lineHeight = lineHeight
Expand All @@ -53,6 +56,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
@Binding private var wrapLines: Bool
@Binding private var editorOverscroll: Double
private var cursorPosition: Published<(Int, Int)>.Publisher?
private var useThemeBackground: Bool

public typealias NSViewControllerType = STTextViewController

Expand All @@ -65,7 +69,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
tabWidth: tabWidth,
wrapLines: wrapLines,
cursorPosition: cursorPosition,
editorOverscroll: editorOverscroll
editorOverscroll: editorOverscroll,
useThemeBackground: useThemeBackground
)
controller.lineHeightMultiple = lineHeight
return controller
Expand All @@ -75,6 +80,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
controller.font = font
controller.tabWidth = tabWidth
controller.wrapLines = wrapLines
controller.useThemeBackground = useThemeBackground
controller.lineHeightMultiple = lineHeight
controller.editorOverscroll = editorOverscroll

Expand Down
19 changes: 13 additions & 6 deletions Sources/CodeEditTextView/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
highlighter?.invalidate()
}}

/// Whether the code editor should use the theme background color or be transparent
public var useThemeBackground: Bool

/// The number of spaces to use for a `tab '\t'` character
public var tabWidth: Int

Expand Down Expand Up @@ -63,7 +66,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
tabWidth: Int,
wrapLines: Bool,
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
editorOverscroll: Double
editorOverscroll: Double,
useThemeBackground: Bool
) {
self.text = text
self.language = language
Expand All @@ -73,6 +77,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
self.wrapLines = wrapLines
self.cursorPosition = cursorPosition
self.editorOverscroll = editorOverscroll
self.useThemeBackground = useThemeBackground
super.init(nibName: nil, bundle: nil)
}

Expand All @@ -89,9 +94,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.hasVerticalScroller = true
scrollView.documentView = textView
scrollView.drawsBackground = useThemeBackground

rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView)
rulerView.backgroundColor = theme.background
rulerView.backgroundColor = useThemeBackground ? theme.background : .clear
rulerView.textColor = .systemGray
rulerView.drawSeparator = false
rulerView.baselineOffset = baselineOffset
Expand All @@ -102,7 +108,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
textView.defaultParagraphStyle = self.paragraphStyle
textView.font = self.font
textView.textColor = theme.text
textView.backgroundColor = theme.background
textView.backgroundColor = useThemeBackground ? theme.background : .clear
textView.insertionPointColor = theme.insertionPoint
textView.insertionPointWidth = 1.0
textView.selectionBackgroundColor = theme.selection
Expand Down Expand Up @@ -207,16 +213,17 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
}

textView?.textColor = theme.text
textView?.backgroundColor = theme.background
textView.backgroundColor = useThemeBackground ? theme.background : .clear
textView?.insertionPointColor = theme.insertionPoint
textView?.selectionBackgroundColor = theme.selection
textView?.selectedLineHighlightColor = theme.lineHighlight

rulerView?.backgroundColor = theme.background
rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
rulerView?.separatorColor = theme.invisibles
rulerView?.baselineOffset = baselineOffset

(view as? NSScrollView)?.backgroundColor = theme.background
(view as? NSScrollView)?.drawsBackground = useThemeBackground
(view as? NSScrollView)?.backgroundColor = useThemeBackground ? theme.background : .clear
(view as? NSScrollView)?.contentView.contentInsets.bottom = bottomContentInsets

setStandardAttributes()
Expand Down
3 changes: 2 additions & 1 deletion Tests/CodeEditTextViewTests/STTextViewControllerTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ final class STTextViewControllerTests: XCTestCase {
theme: theme,
tabWidth: 4,
wrapLines: true,
editorOverscroll: 0.5
editorOverscroll: 0.5,
useThemeBackground: true
)
}

Expand Down

0 comments on commit 58f7941

Please sign in to comment.