Skip to content

Commit 55882fb

Browse files
committed
Improve viewport layout and sizing
- 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
1 parent 586943f commit 55882fb

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Sources/STTextViewAppKit/STTextView+NSTextViewportLayoutControllerDelegate.swift

+6-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
3131
maxY = visibleRect.maxY
3232
}
3333

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

3739
public func textViewportLayoutControllerWillLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
@@ -40,6 +42,8 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
4042
contentView.subviews.removeAll {
4143
type(of: $0) != STInsertionPointView.self
4244
}
45+
46+
sizeToFit()
4347
}
4448

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

60-
// Adjust position
64+
// Adjust fragment view frame
6165
if !fragmentView.frame.isAlmostEqual(to: textLayoutFragment.layoutFragmentFrame.pixelAligned) {
6266
fragmentView.frame = textLayoutFragment.layoutFragmentFrame.pixelAligned
6367
fragmentView.needsLayout = true
@@ -69,7 +73,6 @@ extension STTextView: NSTextViewportLayoutControllerDelegate {
6973
}
7074

7175
public func textViewportLayoutControllerDidLayout(_ textViewportLayoutController: NSTextViewportLayoutController) {
72-
sizeToFit()
7376
updateSelectedRangeHighlight()
7477
layoutGutter()
7578
updateSelectedLineHighlight()

0 commit comments

Comments
 (0)