From 1dfd0b535e4d80fd8037298a30020bbfd7b5fb9c Mon Sep 17 00:00:00 2001 From: Austin Condiff Date: Tue, 16 May 2023 02:33:30 -0500 Subject: [PATCH] [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 - [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 --- .../STTextViewController+Lifecycle.swift | 34 ++++--------------- .../Controller/STTextViewController.swift | 29 ++++++++-------- 2 files changed, 21 insertions(+), 42 deletions(-) diff --git a/Sources/CodeEditTextView/Controller/STTextViewController+Lifecycle.swift b/Sources/CodeEditTextView/Controller/STTextViewController+Lifecycle.swift index 8cb55c110..6896db984 100644 --- a/Sources/CodeEditTextView/Controller/STTextViewController+Lifecycle.swift +++ b/Sources/CodeEditTextView/Controller/STTextViewController+Lifecycle.swift @@ -9,7 +9,6 @@ import AppKit import STTextView extension STTextViewController { - // swiftlint:disable:next function_body_length public override func loadView() { textView = STTextView() @@ -17,28 +16,14 @@ extension STTextViewController { 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 @@ -46,25 +31,17 @@ extension STTextViewController { 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 @@ -80,6 +57,7 @@ extension STTextViewController { return event } + reloadUI() setUpHighlighter() setHighlightProvider(self.highlightProvider) setUpTextFormation() diff --git a/Sources/CodeEditTextView/Controller/STTextViewController.swift b/Sources/CodeEditTextView/Controller/STTextViewController.swift index 3d7915cda..8c9a60ed4 100644 --- a/Sources/CodeEditTextView/Controller/STTextViewController.swift +++ b/Sources/CodeEditTextView/Controller/STTextViewController.swift @@ -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