Skip to content

Commit c611a62

Browse files
Optionally set text view to read only (#161)
1 parent b0d23bf commit c611a62

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

Sources/CodeEditTextView/CodeEditTextView.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
2727
/// built-in `TreeSitterClient` highlighter.
2828
/// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the
2929
/// scroll view automatically adjust content insets.
30+
/// - isEditable: A Boolean value that controls whether the text view allows the user to edit text.
3031
public init(
3132
_ text: Binding<String>,
3233
language: CodeLanguage,
@@ -39,7 +40,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
3940
cursorPosition: Published<(Int, Int)>.Publisher? = nil,
4041
useThemeBackground: Bool = true,
4142
highlightProvider: HighlightProviding? = nil,
42-
contentInsets: NSEdgeInsets? = nil
43+
contentInsets: NSEdgeInsets? = nil,
44+
isEditable: Bool = true
4345
) {
4446
self._text = text
4547
self.language = language
@@ -53,6 +55,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
5355
self.cursorPosition = cursorPosition
5456
self.highlightProvider = highlightProvider
5557
self.contentInsets = contentInsets
58+
self.isEditable = isEditable
5659
}
5760

5861
@Binding private var text: String
@@ -67,6 +70,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
6770
private var useThemeBackground: Bool
6871
private var highlightProvider: HighlightProviding?
6972
private var contentInsets: NSEdgeInsets?
73+
private var isEditable: Bool
7074

7175
public typealias NSViewControllerType = STTextViewController
7276

@@ -82,7 +86,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable {
8286
editorOverscroll: editorOverscroll,
8387
useThemeBackground: useThemeBackground,
8488
highlightProvider: highlightProvider,
85-
contentInsets: contentInsets
89+
contentInsets: contentInsets,
90+
isEditable: isEditable
8691
)
8792
controller.lineHeightMultiple = lineHeight
8893
return controller

Sources/CodeEditTextView/STTextViewController.swift

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
5252
/// Whether lines wrap to the width of the editor
5353
public var wrapLines: Bool
5454

55+
/// Whether or not text view is editable by user
56+
public var isEditable: Bool
57+
5558
/// Filters used when applying edits..
5659
internal var textFilters: [TextFormation.Filter] = []
5760

@@ -81,7 +84,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
8184
editorOverscroll: Double,
8285
useThemeBackground: Bool,
8386
highlightProvider: HighlightProviding? = nil,
84-
contentInsets: NSEdgeInsets? = nil
87+
contentInsets: NSEdgeInsets? = nil,
88+
isEditable: Bool
8589
) {
8690
self.text = text
8791
self.language = language
@@ -94,6 +98,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
9498
self.useThemeBackground = useThemeBackground
9599
self.highlightProvider = highlightProvider
96100
self.contentInsets = contentInsets
101+
self.isEditable = isEditable
97102
super.init(nibName: nil, bundle: nil)
98103
}
99104

@@ -123,6 +128,11 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
123128
rulerView.drawSeparator = false
124129
rulerView.baselineOffset = baselineOffset
125130
rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular)
131+
132+
if self.isEditable == false {
133+
rulerView.selectedLineTextColor = nil
134+
}
135+
126136
scrollView.verticalRulerView = rulerView
127137
scrollView.rulersVisible = true
128138

@@ -135,11 +145,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
135145
textView.selectionBackgroundColor = theme.selection
136146
textView.selectedLineHighlightColor = theme.lineHighlight
137147
textView.string = self.text.wrappedValue
148+
textView.isEditable = self.isEditable
138149
textView.widthTracksTextView = self.wrapLines
139150
textView.highlightSelectedLine = true
140151
textView.allowsUndo = true
141152
textView.setupMenus()
142153
textView.delegate = self
154+
textView.highlightSelectedLine = self.isEditable
143155

144156
scrollView.documentView = textView
145157

@@ -223,10 +235,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
223235
textView?.insertionPointColor = theme.insertionPoint
224236
textView?.selectionBackgroundColor = theme.selection
225237
textView?.selectedLineHighlightColor = theme.lineHighlight
238+
textView?.isEditable = isEditable
239+
textView.highlightSelectedLine = isEditable
226240

227241
rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
228242
rulerView?.separatorColor = theme.invisibles
229243
rulerView?.baselineOffset = baselineOffset
244+
rulerView.highlightSelectedLine = isEditable
230245

231246
if let scrollView = view as? NSScrollView {
232247
scrollView.drawsBackground = useThemeBackground

Tests/CodeEditTextViewTests/STTextViewControllerTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ final class STTextViewControllerTests: XCTestCase {
3535
tabWidth: 4,
3636
wrapLines: true,
3737
editorOverscroll: 0.5,
38-
useThemeBackground: true
38+
useThemeBackground: true,
39+
isEditable: true
3940
)
4041
}
4142

0 commit comments

Comments
 (0)