-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Description Fixes a strong reference cycle in `TextViewController` causing it to not release when removed from the view heirarchy. ### Related Issues * CodeEditApp/CodeEdit#1794 ### 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 Stable memory usage in the example app (mem jumps are opening & closing a large file a few times): ![Screenshot 2024-07-03 at 8 22 20 PM](https://github.com/CodeEditApp/CodeEditSourceEditor/assets/35942988/7532e0f5-f72e-44b5-ac8c-5f194736d3d4)
- Loading branch information
1 parent
160ae95
commit 528e3f1
Showing
6 changed files
with
110 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
Sources/CodeEditSourceEditor/Controller/TextViewController+StyleViews.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// | ||
// TextViewController+StyleViews.swift | ||
// CodeEditSourceEditor | ||
// | ||
// Created by Khan Winter on 7/3/24. | ||
// | ||
|
||
import AppKit | ||
|
||
extension TextViewController { | ||
package func generateParagraphStyle() -> NSMutableParagraphStyle { | ||
// swiftlint:disable:next force_cast | ||
let paragraph = NSParagraphStyle.default.mutableCopy() as! NSMutableParagraphStyle | ||
paragraph.tabStops.removeAll() | ||
paragraph.defaultTabInterval = CGFloat(tabWidth) * fontCharWidth | ||
return paragraph | ||
} | ||
|
||
/// Style the text view. | ||
package func styleTextView() { | ||
textView.selectionManager.selectionBackgroundColor = theme.selection | ||
textView.selectionManager.selectedLineBackgroundColor = getThemeBackground() | ||
textView.selectionManager.highlightSelectedLine = isEditable | ||
textView.selectionManager.insertionPointColor = theme.insertionPoint | ||
paragraphStyle = generateParagraphStyle() | ||
textView.typingAttributes = attributesFor(nil) | ||
} | ||
|
||
/// Finds the preferred use theme background. | ||
/// - Returns: The background color to use. | ||
private func getThemeBackground() -> NSColor { | ||
if useThemeBackground { | ||
return theme.lineHighlight | ||
} | ||
|
||
if systemAppearance == .darkAqua { | ||
return NSColor.quaternaryLabelColor | ||
} | ||
|
||
return NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled) | ||
} | ||
|
||
/// Style the gutter view. | ||
package func styleGutterView() { | ||
gutterView.frame.origin.y = -scrollView.contentInsets.top | ||
gutterView.selectedLineColor = useThemeBackground ? theme.lineHighlight : systemAppearance == .darkAqua | ||
? NSColor.quaternaryLabelColor | ||
: NSColor.selectedTextBackgroundColor.withSystemEffect(.disabled) | ||
gutterView.highlightSelectedLines = isEditable | ||
gutterView.font = font.rulerFont | ||
gutterView.backgroundColor = useThemeBackground ? theme.background : .textBackgroundColor | ||
if self.isEditable == false { | ||
gutterView.selectedLineTextColor = nil | ||
gutterView.selectedLineColor = .clear | ||
} | ||
} | ||
|
||
/// Style the scroll view. | ||
package func styleScrollView() { | ||
guard let scrollView = view as? NSScrollView else { return } | ||
scrollView.drawsBackground = useThemeBackground | ||
scrollView.backgroundColor = useThemeBackground ? theme.background : .clear | ||
if let contentInsets { | ||
scrollView.automaticallyAdjustsContentInsets = false | ||
scrollView.contentInsets = contentInsets | ||
} else { | ||
scrollView.automaticallyAdjustsContentInsets = true | ||
} | ||
scrollView.contentInsets.bottom = (contentInsets?.bottom ?? 0) + bottomContentInsets | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters