Skip to content

Commit

Permalink
Feature: Allow updating the textview
Browse files Browse the repository at this point in the history
  • Loading branch information
nanashili committed Jul 14, 2024
1 parent 7dec78d commit 7436968
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -299,8 +299,8 @@ public class TextLayoutManager: NSObject {

var height: CGFloat = 0
var width: CGFloat = 0
var relativeMinY = max(layoutData.minY - position.yPos, 0)
var relativeMaxY = max(layoutData.maxY - position.yPos, relativeMinY)
let relativeMinY = max(layoutData.minY - position.yPos, 0)
let relativeMaxY = max(layoutData.maxY - position.yPos, relativeMinY)

for lineFragmentPosition in line.typesetter.lineFragments.linesStartingAt(
relativeMinY,
Expand Down
65 changes: 54 additions & 11 deletions Sources/AuroraEditorTextView/TextView/TextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,22 @@ public class TextView: NSView, NSTextContent {
/// - foregroundColor: System text color
/// - kern: 0.0
public static var defaultTypingAttributes: [NSAttributedString.Key: Any] {
[.font: NSFont.systemFont(ofSize: 12), .foregroundColor: NSColor.textColor, .kern: 0.0]
[
.font: NSFont.systemFont(ofSize: 12),
.foregroundColor: NSColor.textColor,
.kern: 0.0
]
}

// swiftlint:disable:next line_length
public static let textDidChangeNotification: Notification.Name = .init(rawValue: "com.AuroraEditor.TextView.TextDidChangeNotification")
public static let textDidChangeNotification: Notification.Name = .init(
rawValue: "com.AuroraEditor.TextView.TextDidChangeNotification"
)

// swiftlint:disable:next line_length
public static let textWillChangeNotification: Notification.Name = .init(rawValue: "com.AuroraEditor.TextView.TextWillChangeNotification")
public static let textWillChangeNotification: Notification.Name = .init(
rawValue: "com.AuroraEditor.TextView.TextWillChangeNotification"
)

// MARK: - Configuration

Expand All @@ -61,8 +69,20 @@ public class TextView: NSView, NSTextContent {
textStorage.string
}
set {
layoutManager.willReplaceCharactersInRange(range: documentRange, with: newValue)
textStorage.setAttributedString(NSAttributedString(string: newValue, attributes: typingAttributes))
layoutManager.willReplaceCharactersInRange(
range: documentRange,
with: newValue
)
textStorage.setAttributedString(
NSAttributedString(
string: newValue,
attributes: typingAttributes
)
)
delegate?.textViewDidChange(
self,
newText: newValue
)
}
}

Expand Down Expand Up @@ -223,7 +243,9 @@ public class TextView: NSView, NSTextContent {
var mouseDragTimer: Timer?

private var fontCharWidth: CGFloat {
(" " as NSString).size(withAttributes: [.font: font]).width
(" " as NSString).size(
withAttributes: [.font: font]
).width
}

internal(set) public var _undoManager: AEUndoManager?
Expand Down Expand Up @@ -283,10 +305,16 @@ public class TextView: NSView, NSTextContent {
.foregroundColor: textColor,
]

textStorage.addAttributes(typingAttributes, range: documentRange)
textStorage.addAttributes(
typingAttributes,
range: documentRange
)
textStorage.delegate = storageDelegate

layoutManager = setUpLayoutManager(lineHeightMultiplier: lineHeightMultiplier, wrapLines: wrapLines)
layoutManager = setUpLayoutManager(
lineHeightMultiplier: lineHeightMultiplier,
wrapLines: wrapLines
)
storageDelegate.addDelegate(layoutManager)
selectionManager = setUpSelectionManager()
selectionManager.useSystemCursor = useSystemCursor
Expand All @@ -302,6 +330,7 @@ public class TextView: NSView, NSTextContent {
public func setText(_ text: String) {
let newStorage = NSTextStorage(string: text)
self.setTextStorage(newStorage)
delegate?.textViewDidChange(self, newText: text)
}

/// Set a new text storage object for the view.
Expand All @@ -313,18 +342,29 @@ public class TextView: NSView, NSTextContent {
view.removeFromSuperview()
}

textStorage.addAttributes(typingAttributes, range: documentRange)
textStorage.addAttributes(
typingAttributes,
range: documentRange
)
layoutManager.textStorage = textStorage
layoutManager.reset()

selectionManager.textStorage = textStorage
selectionManager.setSelectedRanges(selectionManager.textSelections.map { $0.range })
selectionManager.setSelectedRanges(
selectionManager.textSelections.map {
$0.range
})

_undoManager?.clearStack()

textStorage.delegate = storageDelegate
needsDisplay = true
needsLayout = true

delegate?.textViewDidChange(
self,
newText: textStorage.string
)
}

required init?(coder: NSCoder) {
Expand Down Expand Up @@ -540,7 +580,10 @@ public class TextView: NSView, NSTextContent {
var lastFrame: CGRect = .zero
while let selection = selectionManager
.textSelections
.sorted(by: { $0.boundingRect.origin.y < $1.boundingRect.origin.y })
.sorted(
by: {
$0.boundingRect.origin.y < $1.boundingRect.origin.y
})
.first,
lastFrame != selection.boundingRect {
lastFrame = selection.boundingRect
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public protocol TextViewDelegate: AnyObject {
func textView(_ textView: TextView, willReplaceContentsIn range: NSRange, with string: String)
func textView(_ textView: TextView, didReplaceContentsIn range: NSRange, with string: String)
func textView(_ textView: TextView, shouldReplaceContentsIn range: NSRange, with string: String) -> Bool
func textViewDidChange(_ textView: TextView, newText: String)
}

public extension TextViewDelegate {
Expand Down

0 comments on commit 7436968

Please sign in to comment.