From 7d68e8c291b8dc7e5a519b849ac0efb47fe82552 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Sun, 12 Mar 2023 23:43:54 -0500 Subject: [PATCH 1/5] Added isEditable to pass through to STTextView --- Sources/CodeEditTextView/CodeEditTextView.swift | 9 +++++++-- Sources/CodeEditTextView/STTextViewController.swift | 8 +++++++- 2 files changed, 14 insertions(+), 3 deletions(-) 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..e270fca76 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) } @@ -132,6 +137,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView.backgroundColor = useThemeBackground ? theme.background : .clear textView.insertionPointColor = theme.insertionPoint textView.insertionPointWidth = 1.0 + textView.isEditable = self.isEditable textView.selectionBackgroundColor = theme.selection textView.selectedLineHighlightColor = theme.lineHighlight textView.string = self.text.wrappedValue From 1a4c0045591609a61fcd974b07523a9e35887973 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Mon, 13 Mar 2023 00:03:39 -0500 Subject: [PATCH 2/5] Fixed tests --- Tests/CodeEditTextViewTests/STTextViewControllerTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) 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 ) } From 0323fe44ea40e9d43aaa4a47ea3345510af6e336 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Mon, 13 Mar 2023 11:53:20 -0500 Subject: [PATCH 3/5] Fixed issue where it wasn't rendering text if isEditable was false --- Sources/CodeEditTextView/STTextViewController.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index e270fca76..858a8218d 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -137,10 +137,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView.backgroundColor = useThemeBackground ? theme.background : .clear textView.insertionPointColor = theme.insertionPoint textView.insertionPointWidth = 1.0 - textView.isEditable = self.isEditable 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 @@ -229,6 +229,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView?.insertionPointColor = theme.insertionPoint textView?.selectionBackgroundColor = theme.selection textView?.selectedLineHighlightColor = theme.lineHighlight + textView?.isEditable = isEditable rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear rulerView?.separatorColor = theme.invisibles From 70c9d8c68fc14a1b7d7efc4eac1469ba7f701a22 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Mon, 13 Mar 2023 23:46:12 -0500 Subject: [PATCH 4/5] If isEditable is false, disabling current line highlight --- Sources/CodeEditTextView/STTextViewController.swift | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 858a8218d..83cbe0825 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -128,6 +128,8 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) + rulerView.highlightSelectedLine = self.isEditable + scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true @@ -146,6 +148,7 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt textView.allowsUndo = true textView.setupMenus() textView.delegate = self + textView.highlightSelectedLine = self.isEditable scrollView.documentView = textView @@ -230,10 +233,12 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt 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 From 80bf8639de71ea9a1b72ba7e98dddb3670191163 Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Wed, 15 Mar 2023 12:04:50 -0500 Subject: [PATCH 5/5] If isEditable is true, disable current line number text foreground color change and ruler background change --- Sources/CodeEditTextView/STTextViewController.swift | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/CodeEditTextView/STTextViewController.swift b/Sources/CodeEditTextView/STTextViewController.swift index 83cbe0825..8dc29f06e 100644 --- a/Sources/CodeEditTextView/STTextViewController.swift +++ b/Sources/CodeEditTextView/STTextViewController.swift @@ -128,7 +128,10 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt rulerView.drawSeparator = false rulerView.baselineOffset = baselineOffset rulerView.font = NSFont.monospacedDigitSystemFont(ofSize: 9.5, weight: .regular) - rulerView.highlightSelectedLine = self.isEditable + + if self.isEditable == false { + rulerView.selectedLineTextColor = nil + } scrollView.verticalRulerView = rulerView scrollView.rulersVisible = true