@@ -243,9 +243,23 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
243243 }
244244 }
245245
246+ // Expo sets a batch's props in no fixed order, so the buffer waits for the whole batch:
247+ // output fed before the same batch's sharedGrid lands would be drawn, and clipped, at the
248+ // previous grid.
246249 var initialBuffer : String = " " {
247250 didSet {
248- applyRemoteBuffer ( initialBuffer )
251+ hasPendingBuffer = true
252+ }
253+ }
254+ private var hasPendingBuffer = false
255+
256+ // The PTY grid shared by every attached client, or empty before the server reports one.
257+ // The surface draws this grid so its wrapping and cursor match the PTY; the view's own
258+ // fit still goes out through onResize.
259+ var sharedGrid : [ Int ] = [ ] {
260+ didSet {
261+ guard oldValue != sharedGrid else { return }
262+ resizeSurface ( )
249263 }
250264 }
251265
@@ -529,7 +543,7 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
529543 resizeSurface ( )
530544 if let streamedOutput {
531545 feedData ( streamedOutput . data )
532- } else {
546+ } else if ! hasPendingBuffer {
533547 feedBuffer ( initialBuffer )
534548 }
535549 }
@@ -559,6 +573,19 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
559573 }
560574 surface = nil
561575 app = nil
576+ // Ghostty adds its IOSurface layer to the viewport on iOS but never removes it on
577+ // free, leaving the last frame on screen under the next surface's layer. The
578+ // viewport has no subviews, so every sublayer belongs to a freed surface.
579+ CATransaction . begin ( )
580+ CATransaction . setDisableActions ( true )
581+ terminalViewport . layer . sublayers ? . forEach { $0 . removeFromSuperlayer ( ) }
582+ CATransaction . commit ( )
583+ }
584+
585+ func applyPendingBuffer ( ) {
586+ guard hasPendingBuffer else { return }
587+ hasPendingBuffer = false
588+ applyRemoteBuffer ( initialBuffer )
562589 }
563590
564591 private func applyRemoteBuffer ( _ buffer : String ) {
@@ -647,30 +674,32 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
647674 terminalViewport . contentScaleFactor = scale
648675 ghostty_surface_set_content_scale ( surface , Double ( scale ) , Double ( scale ) )
649676 ghostty_surface_set_size ( surface , width , height )
677+ let fitted = ghostty_surface_size ( surface )
678+ let fittedCols = max ( 1 , Int ( fitted . columns ) )
679+ let fittedRows = max ( 1 , Int ( fitted . rows ) )
680+ // Trimming whole cells keeps ghostty's padding, and the surface can only shrink
681+ // within the view, so a stale larger grid falls back to the fitted one.
682+ var surfaceWidth = width
683+ var surfaceHeight = height
684+ if sharedGrid . count == 2 , sharedGrid [ 0 ] > 0 , sharedGrid [ 1 ] > 0 {
685+ surfaceWidth -= UInt32 ( max ( fittedCols - sharedGrid [ 0 ] , 0 ) ) * fitted . cell_width_px
686+ surfaceHeight -= UInt32 ( max ( fittedRows - sharedGrid [ 1 ] , 0 ) ) * fitted . cell_height_px
687+ if surfaceWidth != width || surfaceHeight != height {
688+ ghostty_surface_set_size ( surface , surfaceWidth , surfaceHeight )
689+ }
690+ }
650691 ghostty_surface_set_occlusion ( surface , window != nil )
651- configureIOSurfaceLayers ( )
692+ configureIOSurfaceLayers (
693+ size : CGSize ( width : CGFloat ( surfaceWidth ) / scale , height : CGFloat ( surfaceHeight ) / scale ) )
652694 redrawSurface ( )
653- emitGhosttyResize ( )
695+ emitResize ( cols : fittedCols , rows : fittedRows )
654696 }
655697
656698 private func redrawSurface ( ) {
657699 guard let surface else { return }
658700 ghostty_surface_refresh ( surface )
659701 ghostty_surface_draw ( surface )
660702 markIOSurfaceLayersForDisplay ( )
661- emitGhosttyResize ( )
662- }
663-
664- private func emitGhosttyResize ( ) {
665- guard let surface else {
666- emitEstimatedResize ( )
667- return
668- }
669-
670- let size = ghostty_surface_size ( surface )
671- let cols = max ( 1 , Int ( size . columns ) )
672- let rows = max ( 1 , Int ( size . rows ) )
673- emitResize ( cols : cols , rows : rows )
674703 }
675704
676705 private func emitEstimatedResize ( ) {
@@ -721,8 +750,8 @@ public final class T3TerminalView: ExpoView, UITextFieldDelegate {
721750 ghostty_app_keyboard_changed ( app )
722751 }
723752
724- private func configureIOSurfaceLayers ( ) {
725- let targetBounds = CGRect ( origin : . zero , size : terminalViewport . bounds . size )
753+ private func configureIOSurfaceLayers ( size : CGSize ) {
754+ let targetBounds = CGRect ( origin : . zero , size : size )
726755 CATransaction . begin ( )
727756 CATransaction . setDisableActions ( true )
728757 terminalViewport . layer . sublayers ? . forEach { sublayer in
0 commit comments