Skip to content

Commit 80960b9

Browse files
committed
Fix Infinite Rect Bug, Remove Warnings
1 parent 6b26bae commit 80960b9

File tree

5 files changed

+15
-13
lines changed

5 files changed

+15
-13
lines changed

Example/CodeEditTextViewExample/CodeEditTextViewExample/Documents/CodeEditTextViewExampleDocument.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ struct CodeEditTextViewExampleDocument: FileDocument {
2525
guard let data = configuration.file.regularFileContents else {
2626
throw CocoaError(.fileReadCorruptFile)
2727
}
28-
text = String(bytes: data, encoding: .utf8)
28+
text = String(bytes: data, encoding: .utf8) ?? ""
2929
}
3030

3131
func fileWrapper(configuration: WriteConfiguration) throws -> FileWrapper {

Sources/CodeEditTextView/TextLine/LineFragment.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ public final class LineFragment: Identifiable, Equatable {
4545
/// - Parameter offset: The offset, relative to the start of the *line*.
4646
/// - Returns: The x position of the character in the drawn line, from the left.
4747
public func xPos(for offset: Int) -> CGFloat {
48-
let lineRange = CTLineGetStringRange(ctLine)
4948
return CTLineGetOffsetForStringIndex(ctLine, offset, nil)
5049
}
5150

@@ -72,7 +71,7 @@ public final class LineFragment: Identifiable, Equatable {
7271
context.textMatrix = .init(scaleX: 1, y: -1)
7372
context.textPosition = CGPoint(
7473
x: 0,
75-
y: height - descent + (heightDifference/2)
74+
y: yPos + height - descent + (heightDifference/2)
7675
).pixelAligned
7776

7877
CTLineDraw(ctLine, context)

Sources/CodeEditTextView/TextView/DraggingTextRenderer.swift

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,11 @@ class DraggingTextRenderer: NSView {
2525

2626
assert(!ranges.isEmpty, "Empty ranges not allowed")
2727

28-
guard let lastRange = ranges.last else { return nil }
29-
3028
var minY: CGFloat = .infinity
3129
var maxY: CGFloat = 0.0
3230

3331
for range in ranges {
3432
for line in layoutManager.lineStorage.linesInRange(range) {
35-
guard layoutManager.visibleLineIds.contains(line.data.id), // Only grab visible text
36-
let width = line.data.maxWidth else {
37-
break
38-
}
3933
minY = min(minY, line.yPos)
4034
maxY = max(maxY, line.yPos + line.height)
4135
}

Sources/CodeEditTextView/TextView/TextView+Drag.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,12 @@
88
import Foundation
99
import AppKit
1010

11-
fileprivate let pasteboardObjects = [NSString.self, NSURL.self]
11+
private let pasteboardObjects = [NSString.self, NSURL.self]
1212

1313
extension TextView: NSDraggingSource {
1414
// MARK: - Drag Gesture
1515

16+
/// Custom press gesture recognizer that fails if it does not click into a selected range.
1617
private class DragSelectionGesture: NSPressGestureRecognizer {
1718
override func mouseDown(with event: NSEvent) {
1819
guard isEnabled, let view = self.view as? TextView, event.type == .leftMouseDown else {
@@ -38,7 +39,12 @@ extension TextView: NSDraggingSource {
3839
addGestureRecognizer(dragGesture)
3940
}
4041

41-
@objc private func dragGestureHandler(_ sender: Any) {
42+
@objc private func dragGestureHandler(_ sender: DragSelectionGesture) {
43+
guard sender.state == .began else { return }
44+
defer {
45+
sender.state = .ended
46+
}
47+
4248
guard let visibleTextRange,
4349
let draggingView = DraggingTextRenderer(
4450
ranges: selectionManager.textSelections
@@ -65,7 +71,7 @@ extension TextView: NSDraggingSource {
6571
.textSelections
6672
.sorted(by: { $0.range.location < $1.range.location })
6773
.map { textStorage.attributedSubstring(from: $0.range) }
68-
var attributedString = NSMutableAttributedString()
74+
let attributedString = NSMutableAttributedString()
6975
for (idx, string) in attributedStrings.enumerated() {
7076
attributedString.append(string)
7177
if idx < attributedStrings.count - 1 {
@@ -184,6 +190,7 @@ extension TextView: NSDraggingSource {
184190
return false
185191
}
186192

193+
undoManager?.beginUndoGrouping()
187194
if let source = sender.draggingSource as? TextView, source === self {
188195
// Offset the insertion location so that we can remove the text first before pasting it into the editor.
189196
var updatedInsertionOffset = insertionOffset
@@ -200,6 +207,8 @@ extension TextView: NSDraggingSource {
200207
}
201208

202209
replaceCharacters(in: [NSRange(location: insertionOffset, length: 0)], with: insertionString)
210+
undoManager?.endUndoGrouping()
211+
203212
selectionManager.setSelectedRange(
204213
NSRange(location: insertionOffset, length: NSString(string: insertionString).length)
205214
)

Sources/CodeEditTextView/TextView/TextView.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ public class TextView: NSView, NSTextContent {
244244
/// - Warning: Do not update the text storage object directly. Doing so will very likely break the text view's
245245
/// layout system. Use methods like ``TextView/replaceCharacters(in:with:)-58mt7`` or
246246
/// ``TextView/insertText(_:)`` to modify content.
247-
private(set) public var textStorage: NSTextStorage!
247+
package(set) public var textStorage: NSTextStorage!
248248

249249
/// The layout manager for the text view.
250250
private(set) public var layoutManager: TextLayoutManager!

0 commit comments

Comments
 (0)