Skip to content

Commit

Permalink
Weird. text segment frame gets negative width on the edge of the view…
Browse files Browse the repository at this point in the history
…port bounds
  • Loading branch information
krzyzanowskim committed Oct 11, 2023
1 parent 5cda4f5 commit 5233635
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
11 changes: 10 additions & 1 deletion Sources/STTextView/STTextView+InsertionPoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ extension STTextView {
}

let textSelectionFrames = insertionPointsRanges.compactMap { textRange -> CGRect? in
guard let selectionFrame = textLayoutManager.textSegmentFrame(in: textRange, type: .selection)?.intersection(self.frame) else {

guard var textSegmentFrame = textLayoutManager.textSegmentFrame(in: textRange, type: .selection, options: .rangeNotRequired) else {
return nil
}

if textSegmentFrame.size.width < 0 {
// New issue on macOS 14 (Sonoma): textSegmentFrame.size.width can be < 0 on the edge of viewportBounds (guess)
// resulting in unexpected selection frame
textSegmentFrame.size.width = 0
}

let selectionFrame = textSegmentFrame.intersection(frame)

// because `textLayoutManager.enumerateTextLayoutFragments(from: nil, options: [.ensuresExtraLineFragment, .ensuresLayout, .estimatesSize])`
// returns unexpected value for extra line fragment height (return 14) that is not correct in the context,
// therefore for empty override height with value manually calculated from font + paragraph style
Expand Down
10 changes: 9 additions & 1 deletion Sources/STTextView/STTextView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,15 @@ open class STTextView: NSView, NSTextInput, NSTextContent {
// NOTE: enumerateTextSegments is very slow https://github.com/krzyzanowskim/STTextView/discussions/25#discussioncomment-6464398
// Clamp enumerated range to viewport range
textLayoutManager.enumerateTextSegments(in: textRange, type: .selection, options: .rangeNotRequired) {(_, textSegmentFrame, _, _) in
let highlightFrame = textSegmentFrame.intersection(bounds).pixelAligned

var textSegmentFrame = textSegmentFrame
if textSegmentFrame.size.width < 0 {
// New issue on macOS 14 (Sonoma): textSegmentFrame.size.width can be < 0 on the edge of viewportBounds (guess)
// resulting in unexpected selection frame
textSegmentFrame.size.width = 0
}

let highlightFrame = textSegmentFrame.intersection(frame).pixelAligned
guard !highlightFrame.isNull else {
return true
}
Expand Down

0 comments on commit 5233635

Please sign in to comment.