Skip to content

Commit 1dfd0b5

Browse files
[chore] Refactor STTextViewController to reduce duplication (#185)
### Description Fixed a few UI bugs by reducing overlap between loadView and reloadUI and calling reloadUI from loadView. These include... - There were some areas where the theme background was not getting applied. - The currently selected line was not getting a background in the ruler view until changing a setting which called reloadUI. Now loadView calls reloadUI which allows us to reduce code duplication ### Related Issues - n/a ### Checklist <!--- Add things that are not yet implemented above --> - [x] I read and understood the [contributing guide](https://github.com/CodeEditApp/CodeEdit/blob/main/CONTRIBUTING.md) as well as the [code of conduct](https://github.com/CodeEditApp/CodeEdit/blob/main/CODE_OF_CONDUCT.md) - [x] The issues this PR addresses are related to each other - [x] My changes generate no new warnings - [x] My code builds and runs on my machine - [x] My changes are all related to the related issue above - [x] I documented my code ### Screenshots n/a
1 parent 521dd43 commit 1dfd0b5

File tree

2 files changed

+21
-42
lines changed

2 files changed

+21
-42
lines changed

Sources/CodeEditTextView/Controller/STTextViewController+Lifecycle.swift

Lines changed: 6 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,62 +9,39 @@ import AppKit
99
import STTextView
1010

1111
extension STTextViewController {
12-
// swiftlint:disable:next function_body_length
1312
public override func loadView() {
1413
textView = STTextView()
1514

1615
let scrollView = CEScrollView()
1716
scrollView.translatesAutoresizingMaskIntoConstraints = false
1817
scrollView.hasVerticalScroller = true
1918
scrollView.documentView = textView
20-
scrollView.drawsBackground = useThemeBackground
2119
scrollView.automaticallyAdjustsContentInsets = contentInsets == nil
22-
if let contentInsets = contentInsets {
23-
scrollView.contentInsets = contentInsets
24-
}
2520

2621
rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView)
27-
rulerView.backgroundColor = useThemeBackground ? theme.background : .clear
28-
rulerView.textColor = .secondaryLabelColor
2922
rulerView.drawSeparator = false
3023
rulerView.baselineOffset = baselineOffset
31-
rulerView.font = rulerFont
32-
rulerView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
33-
? NSColor.quaternaryLabelColor
34-
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
35-
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
3624
rulerView.allowsMarkers = false
37-
38-
if self.isEditable == false {
39-
rulerView.selectedLineTextColor = nil
40-
rulerView.selectedLineHighlightColor = theme.background
41-
}
25+
rulerView.backgroundColor = .clear
26+
rulerView.textColor = .secondaryLabelColor
4227

4328
scrollView.verticalRulerView = rulerView
4429
scrollView.rulersVisible = true
4530

4631
textView.typingAttributes = attributesFor(nil)
4732
textView.defaultParagraphStyle = self.paragraphStyle
4833
textView.font = self.font
49-
textView.textColor = theme.text
50-
textView.backgroundColor = useThemeBackground ? theme.background : .clear
51-
textView.insertionPointColor = theme.insertionPoint
5234
textView.insertionPointWidth = 1.0
53-
textView.selectionBackgroundColor = theme.selection
54-
textView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
55-
? NSColor.quaternaryLabelColor
56-
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
35+
textView.backgroundColor = .clear
36+
5737
textView.string = self.text.wrappedValue
58-
textView.isEditable = self.isEditable
59-
textView.highlightSelectedLine = true
6038
textView.allowsUndo = true
6139
textView.setupMenus()
6240
textView.delegate = self
63-
textView.highlightSelectedLine = self.isEditable
6441

6542
scrollView.documentView = textView
66-
6743
scrollView.translatesAutoresizingMaskIntoConstraints = false
44+
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear
6845

6946
self.view = scrollView
7047

@@ -80,6 +57,7 @@ extension STTextViewController {
8057
return event
8158
}
8259

60+
reloadUI()
8361
setUpHighlighter()
8462
setHighlightProvider(self.highlightProvider)
8563
setUpTextFormation()

Sources/CodeEditTextView/Controller/STTextViewController.swift

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -187,28 +187,29 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt
187187

188188
/// Reloads the UI to apply changes to ``STTextViewController/font``, ``STTextViewController/theme``, ...
189189
internal func reloadUI() {
190-
textView?.textColor = theme.text
191-
textView.backgroundColor = useThemeBackground ? theme.background : .clear
192-
textView?.insertionPointColor = theme.insertionPoint
193-
textView?.selectionBackgroundColor = theme.selection
194-
textView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
190+
textView.textColor = theme.text
191+
textView.insertionPointColor = theme.insertionPoint
192+
textView.selectionBackgroundColor = theme.selection
193+
textView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
195194
? NSColor.quaternaryLabelColor
196195
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
197-
textView?.isEditable = isEditable
196+
textView.isEditable = isEditable
198197
textView.highlightSelectedLine = isEditable
199-
textView?.typingAttributes = attributesFor(nil)
198+
textView.typingAttributes = attributesFor(nil)
200199
paragraphStyle = generateParagraphStyle()
201-
textView?.defaultParagraphStyle = paragraphStyle
200+
textView.defaultParagraphStyle = paragraphStyle
202201

203-
rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
204-
rulerView?.separatorColor = theme.invisibles
205-
rulerView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
202+
rulerView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
206203
? NSColor.quaternaryLabelColor
207204
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
208-
rulerView?.baselineOffset = baselineOffset
205+
rulerView.baselineOffset = baselineOffset
209206
rulerView.highlightSelectedLine = isEditable
210-
rulerView?.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
211-
rulerView?.font = rulerFont
207+
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
208+
rulerView.font = rulerFont
209+
if self.isEditable == false {
210+
rulerView.selectedLineTextColor = nil
211+
rulerView.selectedLineHighlightColor = .clear
212+
}
212213

213214
if let scrollView = view as? NSScrollView {
214215
scrollView.drawsBackground = useThemeBackground

0 commit comments

Comments
 (0)