Skip to content

Commit 58f7941

Browse files
authored
Theme background (#123)
# 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
1 parent 9f79fa8 commit 58f7941

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
1717
/// - text: The text content
1818
/// - language: The language for syntax highlighting
1919
/// - theme: The theme for syntax highlighting
20+
/// - useThemeBackground: Whether CodeEditTextView uses theme background color or is transparent
2021
/// - font: The default font
2122
/// - tabWidth: The tab width
2223
/// - lineHeight: The line height multiplier (e.g. `1.2`)
@@ -31,11 +32,13 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
3132
lineHeight: Binding<Double>,
3233
wrapLines: Binding<Bool>,
3334
editorOverscroll: Binding<Double> = .constant(0.0),
34-
cursorPosition: Published<(Int, Int)>.Publisher? = nil
35+
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
36+
useThemeBackground: Bool = true
3537
) {
3638
self._text = text
3739
self.language = language
3840
self._theme = theme
41+
self.useThemeBackground = useThemeBackground
3942
self._font = font
4043
self._tabWidth = tabWidth
4144
self._lineHeight = lineHeight
@@ -53,6 +56,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5356
@Binding private var wrapLines: Bool
5457
@Binding private var editorOverscroll: Double
5558
private var cursorPosition: Published<(Int, Int)>.Publisher?
59+
private var useThemeBackground: Bool
5660

5761
public typealias NSViewControllerType = STTextViewController
5862

@@ -65,7 +69,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
6569
tabWidth: tabWidth,
6670
wrapLines: wrapLines,
6771
cursorPosition: cursorPosition,
68-
editorOverscroll: editorOverscroll
72+
editorOverscroll: editorOverscroll,
73+
useThemeBackground: useThemeBackground
6974
)
7075
controller.lineHeightMultiple = lineHeight
7176
return controller
@@ -75,6 +80,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
7580
controller.font = font
7681
controller.tabWidth = tabWidth
7782
controller.wrapLines = wrapLines
83+
controller.useThemeBackground = useThemeBackground
7884
controller.lineHeightMultiple = lineHeight
7985
controller.editorOverscroll = editorOverscroll
8086

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
3333
highlighter?.invalidate()
3434
}}
3535

36+
/// Whether the code editor should use the theme background color or be transparent
37+
public var useThemeBackground: Bool
38+
3639
/// The number of spaces to use for a `tab '\t'` character
3740
public var tabWidth: Int
3841

@@ -63,7 +66,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
6366
tabWidth: Int,
6467
wrapLines: Bool,
6568
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
66-
editorOverscroll: Double
69+
editorOverscroll: Double,
70+
useThemeBackground: Bool
6771
) {
6872
self.text = text
6973
self.language = language
@@ -73,6 +77,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
7377
self.wrapLines = wrapLines
7478
self.cursorPosition = cursorPosition
7579
self.editorOverscroll = editorOverscroll
80+
self.useThemeBackground = useThemeBackground
7681
super.init(nibName: nil, bundle: nil)
7782
}
7883

@@ -89,9 +94,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
8994
scrollView.translatesAutoresizingMaskIntoConstraints = false
9095
scrollView.hasVerticalScroller = true
9196
scrollView.documentView = textView
97+
scrollView.drawsBackground = useThemeBackground
9298

9399
rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView)
94-
rulerView.backgroundColor = theme.background
100+
rulerView.backgroundColor = useThemeBackground ? theme.background : .clear
95101
rulerView.textColor = .systemGray
96102
rulerView.drawSeparator = false
97103
rulerView.baselineOffset = baselineOffset
@@ -102,7 +108,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
102108
textView.defaultParagraphStyle = self.paragraphStyle
103109
textView.font = self.font
104110
textView.textColor = theme.text
105-
textView.backgroundColor = theme.background
111+
textView.backgroundColor = useThemeBackground ? theme.background : .clear
106112
textView.insertionPointColor = theme.insertionPoint
107113
textView.insertionPointWidth = 1.0
108114
textView.selectionBackgroundColor = theme.selection
@@ -207,16 +213,17 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
207213
}
208214

209215
textView?.textColor = theme.text
210-
textView?.backgroundColor = theme.background
216+
textView.backgroundColor = useThemeBackground ? theme.background : .clear
211217
textView?.insertionPointColor = theme.insertionPoint
212218
textView?.selectionBackgroundColor = theme.selection
213219
textView?.selectedLineHighlightColor = theme.lineHighlight
214220

215-
rulerView?.backgroundColor = theme.background
221+
rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
216222
rulerView?.separatorColor = theme.invisibles
217223
rulerView?.baselineOffset = baselineOffset
218224

219-
(view as? NSScrollView)?.backgroundColor = theme.background
225+
(view as? NSScrollView)?.drawsBackground = useThemeBackground
226+
(view as? NSScrollView)?.backgroundColor = useThemeBackground ? theme.background : .clear
220227
(view as? NSScrollView)?.contentView.contentInsets.bottom = bottomContentInsets
221228

222229
setStandardAttributes()

Tests/CodeEditTextViewTests/STTextViewControllerTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@ final class STTextViewControllerTests: XCTestCase {
3434
theme: theme,
3535
tabWidth: 4,
3636
wrapLines: true,
37-
editorOverscroll: 0.5
37+
editorOverscroll: 0.5,
38+
useThemeBackground: true
3839
)
3940
}
4041

0 commit comments

Comments
 (0)