-
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.
- Loading branch information
1 parent
181e7ff
commit 5a11ada
Showing
18 changed files
with
493 additions
and
456 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
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
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
118 changes: 118 additions & 0 deletions
118
Sources/CodeEditTextView/Controller/TextViewController+LoadView.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,118 @@ | ||
// | ||
// TextViewController+LoadView.swift | ||
// | ||
// | ||
// Created by Khan Winter on 10/14/23. | ||
// | ||
|
||
import CodeEditInputView | ||
import AppKit | ||
|
||
extension TextViewController { | ||
// swiftlint:disable:next function_body_length | ||
override public func loadView() { | ||
scrollView = NSScrollView() | ||
textView = TextView( | ||
string: string.wrappedValue, | ||
font: font, | ||
textColor: theme.text, | ||
lineHeight: lineHeightMultiple, | ||
wrapLines: wrapLines, | ||
isEditable: isEditable, | ||
letterSpacing: letterSpacing, | ||
delegate: self, | ||
storageDelegate: storageDelegate | ||
) | ||
textView.postsFrameChangedNotifications = true | ||
textView.translatesAutoresizingMaskIntoConstraints = false | ||
textView.selectionManager.insertionPointColor = theme.insertionPoint | ||
|
||
scrollView.translatesAutoresizingMaskIntoConstraints = false | ||
scrollView.contentView.postsFrameChangedNotifications = true | ||
scrollView.hasVerticalScroller = true | ||
scrollView.hasHorizontalScroller = true | ||
scrollView.documentView = textView | ||
scrollView.contentView.postsBoundsChangedNotifications = true | ||
if let contentInsets { | ||
scrollView.automaticallyAdjustsContentInsets = false | ||
scrollView.contentInsets = contentInsets | ||
} | ||
|
||
gutterView = GutterView( | ||
font: font.rulerFont, | ||
textColor: .secondaryLabelColor, | ||
textView: textView, | ||
delegate: self | ||
) | ||
gutterView.frame.origin.y = -scrollView.contentInsets.top | ||
gutterView.updateWidthIfNeeded() | ||
scrollView.addFloatingSubview( | ||
gutterView, | ||
for: .horizontal | ||
) | ||
|
||
self.view = scrollView | ||
setUpHighlighter() | ||
|
||
NSLayoutConstraint.activate([ | ||
scrollView.leadingAnchor.constraint(equalTo: view.leadingAnchor), | ||
scrollView.trailingAnchor.constraint(equalTo: view.trailingAnchor), | ||
scrollView.topAnchor.constraint(equalTo: view.topAnchor), | ||
scrollView.bottomAnchor.constraint(equalTo: view.bottomAnchor) | ||
]) | ||
|
||
// Layout on scroll change | ||
NotificationCenter.default.addObserver( | ||
forName: NSView.boundsDidChangeNotification, | ||
object: scrollView.contentView, | ||
queue: .main | ||
) { [weak self] _ in | ||
self?.textView.updatedViewport(self?.scrollView.documentVisibleRect ?? .zero) | ||
self?.gutterView.needsDisplay = true | ||
} | ||
|
||
// Layout on frame change | ||
NotificationCenter.default.addObserver( | ||
forName: NSView.frameDidChangeNotification, | ||
object: scrollView.contentView, | ||
queue: .main | ||
) { [weak self] _ in | ||
self?.textView.updatedViewport(self?.scrollView.documentVisibleRect ?? .zero) | ||
self?.gutterView.needsDisplay = true | ||
if self?.bracketPairHighlight == .flash { | ||
self?.removeHighlightLayers() | ||
} | ||
} | ||
|
||
NotificationCenter.default.addObserver( | ||
forName: NSView.frameDidChangeNotification, | ||
object: textView, | ||
queue: .main | ||
) { [weak self] _ in | ||
self?.gutterView.frame.size.height = (self?.textView.frame.height ?? 0) + 10 | ||
self?.gutterView.needsDisplay = true | ||
} | ||
|
||
NotificationCenter.default.addObserver( | ||
forName: TextSelectionManager.selectionChangedNotification, | ||
object: textView.selectionManager, | ||
queue: .main | ||
) { [weak self] _ in | ||
self?.updateCursorPosition() | ||
self?.highlightSelectionPairs() | ||
} | ||
|
||
textView.updateFrameIfNeeded() | ||
|
||
NSApp.publisher(for: \.effectiveAppearance) | ||
.receive(on: RunLoop.main) | ||
.sink { [weak self] newValue in | ||
guard let self = self else { return } | ||
|
||
if self.systemAppearance != newValue.name { | ||
self.systemAppearance = newValue.name | ||
} | ||
} | ||
.store(in: &cancellables) | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
Sources/CodeEditTextView/Controller/TextViewController+TextViewDelegate.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,26 @@ | ||
// | ||
// TextViewController+TextViewDelegate.swift | ||
// | ||
// | ||
// Created by Khan Winter on 10/14/23. | ||
// | ||
|
||
import Foundation | ||
import CodeEditInputView | ||
import TextStory | ||
|
||
extension TextViewController: TextViewDelegate { | ||
public func textView(_ textView: TextView, didReplaceContentsIn range: NSRange, with: String) { | ||
gutterView.needsDisplay = true | ||
} | ||
|
||
public func textView(_ textView: TextView, shouldReplaceContentsIn range: NSRange, with string: String) -> Bool { | ||
let mutation = TextMutation( | ||
string: string, | ||
range: range, | ||
limit: textView.textStorage.length | ||
) | ||
|
||
return shouldApplyMutation(mutation, to: textView) | ||
} | ||
} |
Oops, something went wrong.