Skip to content

Commit

Permalink
Improve viewport layout and sizing
Browse files Browse the repository at this point in the history
- Calculate the viewport bounds more accurately
- Add debug logging for the viewport bounds
- Call sizeToFit() before laying out fragments, rather than after
- Rename comment to clarify the purpose of adjusting fragment view frames
  • Loading branch information
krzyzanowskim committed Jan 1, 2025
1 parent 586943f commit 55882fb
Showing 1 changed file with 6 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,9 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
maxY = visibleRect.maxY
}

return CGRect(x: minX, y: minY, width: maxX, height: maxY - minY)
let rect = CGRect(x: minX, y: minY, width: maxX, height: maxY - minY)
logger.debug("viewportBounds \(rect.debugDescription)")
return rect
}

public func textViewportLayoutControllerWillLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
Expand All @@ -40,6 +42,8 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
contentView.subviews.removeAll {
type(of: $0) != STInsertionPointView.self
}

sizeToFit()
}

public func textViewportLayoutController(_ textViewportLayoutController: NSTextViewportLayoutController, configureRenderingSurfaceFor textLayoutFragment: NSTextLayoutFragment) {
Expand All @@ -57,7 +61,7 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
fragmentView = STTextLayoutFragmentView(layoutFragment: textLayoutFragment, frame: textLayoutFragment.layoutFragmentFrame.pixelAligned)
}

// Adjust position
// Adjust fragment view frame
if !fragmentView.frame.isAlmostEqual(to: textLayoutFragment.layoutFragmentFrame.pixelAligned) {
fragmentView.frame = textLayoutFragment.layoutFragmentFrame.pixelAligned
fragmentView.needsLayout = true
Expand All @@ -69,7 +73,6 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
}

public func textViewportLayoutControllerDidLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
sizeToFit()
updateSelectedRangeHighlight()
layoutGutter()
updateSelectedLineHighlight()
Expand Down

0 comments on commit 55882fb

Please sign in to comment.