Skip to content

Commit

Permalink
[chore] Refactor STTextViewController to reduce duplication (#185)
Browse files Browse the repository at this point in the history
### 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
  • Loading branch information
austincondiff authored May 16, 2023
1 parent 521dd43 commit 1dfd0b5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,39 @@ import AppKit
import STTextView

extension STTextViewController {
// swiftlint:disable:next function_body_length
public override func loadView() {
textView = STTextView()

let scrollView = CEScrollView()
scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.hasVerticalScroller = true
scrollView.documentView = textView
scrollView.drawsBackground = useThemeBackground
scrollView.automaticallyAdjustsContentInsets = contentInsets == nil
if let contentInsets = contentInsets {
scrollView.contentInsets = contentInsets
}

rulerView = STLineNumberRulerView(textView: textView, scrollView: scrollView)
rulerView.backgroundColor = useThemeBackground ? theme.background : .clear
rulerView.textColor = .secondaryLabelColor
rulerView.drawSeparator = false
rulerView.baselineOffset = baselineOffset
rulerView.font = rulerFont
rulerView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
rulerView.allowsMarkers = false

if self.isEditable == false {
rulerView.selectedLineTextColor = nil
rulerView.selectedLineHighlightColor = theme.background
}
rulerView.backgroundColor = .clear
rulerView.textColor = .secondaryLabelColor

scrollView.verticalRulerView = rulerView
scrollView.rulersVisible = true

textView.typingAttributes = attributesFor(nil)
textView.defaultParagraphStyle = self.paragraphStyle
textView.font = self.font
textView.textColor = theme.text
textView.backgroundColor = useThemeBackground ? theme.background : .clear
textView.insertionPointColor = theme.insertionPoint
textView.insertionPointWidth = 1.0
textView.selectionBackgroundColor = theme.selection
textView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
textView.backgroundColor = .clear

textView.string = self.text.wrappedValue
textView.isEditable = self.isEditable
textView.highlightSelectedLine = true
textView.allowsUndo = true
textView.setupMenus()
textView.delegate = self
textView.highlightSelectedLine = self.isEditable

scrollView.documentView = textView

scrollView.translatesAutoresizingMaskIntoConstraints = false
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear

self.view = scrollView

Expand All @@ -80,6 +57,7 @@ extension STTextViewController {
return event
}

reloadUI()
setUpHighlighter()
setHighlightProvider(self.highlightProvider)
setUpTextFormation()
Expand Down
29 changes: 15 additions & 14 deletions Sources/CodeEditTextView/Controller/STTextViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -187,28 +187,29 @@ public class STTextViewController: NSViewController, STTextViewDelegate, ThemeAt

/// Reloads the UI to apply changes to ``STTextViewController/font``, ``STTextViewController/theme``, ...
internal func reloadUI() {
textView?.textColor = theme.text
textView.backgroundColor = useThemeBackground ? theme.background : .clear
textView?.insertionPointColor = theme.insertionPoint
textView?.selectionBackgroundColor = theme.selection
textView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
textView.textColor = theme.text
textView.insertionPointColor = theme.insertionPoint
textView.selectionBackgroundColor = theme.selection
textView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
textView?.isEditable = isEditable
textView.isEditable = isEditable
textView.highlightSelectedLine = isEditable
textView?.typingAttributes = attributesFor(nil)
textView.typingAttributes = attributesFor(nil)
paragraphStyle = generateParagraphStyle()
textView?.defaultParagraphStyle = paragraphStyle
textView.defaultParagraphStyle = paragraphStyle

rulerView?.backgroundColor = useThemeBackground ? theme.background : .clear
rulerView?.separatorColor = theme.invisibles
rulerView?.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
rulerView.selectedLineHighlightColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua
? NSColor.quaternaryLabelColor
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled)
rulerView?.baselineOffset = baselineOffset
rulerView.baselineOffset = baselineOffset
rulerView.highlightSelectedLine = isEditable
rulerView?.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
rulerView?.font = rulerFont
rulerView.rulerInsets = STRulerInsets(leading: rulerFont.pointSize * 1.6, trailing: 8)
rulerView.font = rulerFont
if self.isEditable == false {
rulerView.selectedLineTextColor = nil
rulerView.selectedLineHighlightColor = .clear
}

if let scrollView = view as? NSScrollView {
scrollView.drawsBackground = useThemeBackground
Expand Down

0 comments on commit 1dfd0b5

Please sign in to comment.