diff --git a/Sources/CodeEditTextView/CodeEditTextView.swift b/Sources/CodeEditTextView/CodeEditTextView.swift index 2ff103072..c2d9bb5d3 100644 --- a/Sources/CodeEditTextView/CodeEditTextView.swift +++ b/Sources/CodeEditTextView/CodeEditTextView.swift @@ -27,6 +27,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { /// built-in `TreeSitterClient` highlighter. /// - contentInsets: Insets to use to offset the content in the enclosing scroll view. Leave as `nil` to let the /// scroll view automatically adjust content insets. + /// - isEditable: A Boolean value that controls whether the text view allows the user to edit text. public init( _ text: Binding, language: CodeLanguage, @@ -39,7 +40,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { cursorPosition: Published<(Int, Int)>.Publisher? = nil, useThemeBackground: Bool = true, highlightProvider: HighlightProviding? = nil, - contentInsets: NSEdgeInsets? = nil + contentInsets: NSEdgeInsets? = nil, + isEditable: Bool = true ) { self._text = text self.language = language @@ -53,6 +55,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { self.cursorPosition = cursorPosition self.highlightProvider = highlightProvider self.contentInsets = contentInsets + self.isEditable = isEditable } @Binding private var text: String @@ -67,6 +70,7 @@ public struct CodeEditTextView: NSViewControllerRepresentable { private var useThemeBackground: Bool private var highlightProvider: HighlightProviding? private var contentInsets: NSEdgeInsets? + private var isEditable: Bool public typealias NSViewControllerType = STTextViewController @@ -82,7 +86,8 @@ public struct CodeEditTextView: NSViewControllerRepresentable { editorOverscroll: editorOverscroll, useThemeBackground: useThemeBackground, highlightProvider: highlightProvider, - contentInsets: contentInsets + contentInsets: contentInsets, + isEditable: isEditable ) controller.lineHeightMultiple = lineHeight return controller diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 56d6ecd2c..8dc29f06e 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -52,6 +52,9 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt /// Whether lines wrap to the width of the editor public var wrapLines: Bool + /// Whether or not text view is editable by user + public var isEditable: Bool + /// Filters used when applying edits.. internal var textFilters: [TextFormation.Filter] = [] @@ -81,7 +84,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt editorOverscroll: Double, useThemeBackground: Bool, highlightProvider: HighlightProviding? = nil, - contentInsets: NSEdgeInsets? = nil + contentInsets: NSEdgeInsets? = nil, + isEditable: Bool ) { self.text = text self.language = language @@ -94,6 +98,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt self.useThemeBackground = useThemeBackground self.highlightProvider = highlightProvider self.contentInsets = contentInsets + self.isEditable = isEditable super.init(nibName: nil, bundle: nil) } @@ -123,6 +128,11 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) + + if self.isEditable == false { + rulerView.selectedLineTextColor = nil + } + scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true @@ -135,11 +145,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView.selectionBackgroundColor = theme.selection textView.selectedLineHighlightColor = theme.lineHighlight textView.string = self.text.wrappedValue + textView.isEditable = self.isEditable textView.widthTracksTextView = self.wrapLines textView.highlightSelectedLine = true textView.allowsUndo = true textView.setupMenus() textView.delegate = self + textView.highlightSelectedLine = self.isEditable scrollView.documentView = textView @@ -223,10 +235,13 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView?.insertionPointColor = theme.insertionPoint textView?.selectionBackgroundColor = theme.selection textView?.selectedLineHighlightColor = theme.lineHighlight + textView?.isEditable = isEditable + textView.highlightSelectedLine = isEditable rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear rulerView?.separatorColor = theme.invisibles rulerView?.baselineOffset = baselineOffset + rulerView.highlightSelectedLine = isEditable if let scrollView = view as? NSScrollView { scrollView.drawsBackground = useThemeBackground diff --git a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift index b170b46f1..5a71412e5 100644 --- a/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift +++ b/Tests/CodeEditTextViewTests/STTextViewControllerTests.swift @@ -35,7 +35,8 @@ final class STTextViewControllerTests: XCTestCase { tabWidth: 4, wrapLines: true, editorOverscroll: 0.5, - useThemeBackground: true + useThemeBackground: true, + isEditable: true ) }