Skip to content

Commit

Permalink
take enclosing scrollview visible rect or self visible rect into acco…
Browse files Browse the repository at this point in the history
…unt when calculate viewport
  • Loading branch information
krzyzanowskim committed Jan 14, 2024
1 parent 2ae10ec commit 2517b41
Showing 1 changed file with 4 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,24 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
let minX: CGFloat
let maxX: CGFloat

let visibleRect = scrollView?.documentVisibleRect ?? self.visibleRect

if !overdrawRect.isEmpty, overdrawRect.intersects(visibleRect) {
// Use preparedContentRect for vertical overdraw and ensure visibleRect is included at the minimum,
// the width is always bounds width for proper line wrapping.
minX = min(overdrawRect.minX, max(visibleRect.minX, bounds.minX))
minY = min(overdrawRect.minY, max(visibleRect.minY, bounds.minY))
maxX = max(overdrawRect.maxX, visibleRect.maxX)
maxY = max(overdrawRect.maxY, visibleRect.maxY)
} else if !visibleRect.isEmpty {
} else {
// We use visible rect directly if preparedContentRect does not intersect.
// This can happen if overdraw has not caught up with scrolling yet, such as before the first layout.
minX = visibleRect.minX
minY = visibleRect.minY
maxX = visibleRect.maxX
maxY = visibleRect.maxY
} else if let enclosingScrollView {
/// if not visible, but in the scroll view,
/// layout estimated size that enclosing scroll view display
minX = enclosingScrollView.documentVisibleRect.minX
minY = enclosingScrollView.documentVisibleRect.minY
maxX = enclosingScrollView.documentVisibleRect.maxX
maxY = enclosingScrollView.documentVisibleRect.maxY
} else {
// last reasort, when view is not visible
// and this is the fist time layout
minX = bounds.minX
minY = bounds.minY
maxX = bounds.maxX
maxY = bounds.maxY
}

return CGRect(x: minX, y: minY, width: maxX, height: maxY - minY)
}

Expand Down

0 comments on commit 2517b41

Please sign in to comment.