diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index 0030d4fa6..1b56b0d6b 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -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`) @@ -31,11 +32,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable { lineHeight: Binding, wrapLines: Binding, editorOverscroll: Binding = .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 @@ -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 @@ -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 @@ -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 diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index d9479e02b..d08862bb9 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -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 @@ -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 @@ -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) } @@ -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 @@ -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 @@ -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() diff --git a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift index 7c527c011..b866d270c 100644 --- a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift +++ b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift @@ -34,7 +34,8 @@ final class STTextViewControllerTests: XCTestCase { theme: theme, tabWidth: 4, wrapLines: true, - editorOverscroll: 0.5 + editorOverscroll: 0.5, + useThemeBackground: true ) }