@@ -106,19 +106,20 @@ impl CursorPositionSettleState {
106106 // when typing advanced its column. Otherwise the accumulated typing
107107 // deadline can settle a later repair cell and anchor redraws there.
108108 if let ( Some ( candidate) , Some ( since) ) = ( self . candidate , self . candidate_since ) {
109+ let expired = now. duration_since ( since) >= self . candidate_hold ( ) ;
109110 let restored = self . settled . zip ( current) . is_some_and ( |( settled, current) | {
110111 settled. visible
112+ && current. visible
113+ && ( candidate. visible || !expired)
111114 && ( same_cursor_position ( settled, current)
112- || ( candidate. y != settled. y
113- && current. y == settled. y
114- && now. duration_since ( since) < self . candidate_hold ( ) ) )
115+ || ( candidate. y != settled. y && current. y == settled. y && !expired) )
115116 } ) ;
116117 if restored {
117118 self . settle ( current) ;
118119 return ;
119120 }
120121 // Preserve an eligible caret before a later redraw moves it away.
121- if now . duration_since ( since ) >= self . candidate_hold ( ) {
122+ if expired {
122123 self . settle ( Some ( candidate) ) ;
123124 }
124125 }
@@ -127,9 +128,29 @@ impl CursorPositionSettleState {
127128 return ;
128129 } ;
129130 if !current. visible {
131+ // A PTY can briefly hide a stationary caret during a redraw.
132+ // Keep the last visible cell until the existing max hold expires.
133+ if self . candidate . is_some_and ( |candidate| {
134+ !candidate. visible && same_cursor_position ( candidate, current)
135+ } ) {
136+ return ;
137+ }
138+ if self . candidate . is_none ( )
139+ && self . settled . is_some_and ( |settled| {
140+ settled. visible && same_cursor_position ( settled, current)
141+ } )
142+ {
143+ self . candidate = Some ( current) ;
144+ self . pending_since = Some ( now) ;
145+ self . candidate_since = Some ( now) ;
146+ return ;
147+ }
130148 self . settle ( Some ( current) ) ;
131149 return ;
132150 }
151+ if self . candidate . is_some_and ( |candidate| !candidate. visible ) {
152+ self . settle ( self . settled ) ;
153+ }
133154 let Some ( settled) = self . settled else {
134155 self . settle ( Some ( current) ) ;
135156 return ;
@@ -184,7 +205,9 @@ impl CursorPositionSettleState {
184205 }
185206 self . settled
186207 . map ( |settled| TerminalCursorState {
187- visible : current. visible && settled. visible ,
208+ visible : settled. visible
209+ && ( current. visible
210+ || ( !candidate. visible && same_cursor_position ( candidate, current) ) ) ,
188211 shape : current. shape ,
189212 ..settled
190213 } )
@@ -204,7 +227,7 @@ impl CursorPositionSettleState {
204227 }
205228
206229 fn candidate_hold ( & self ) -> Duration {
207- if self . candidate_jump {
230+ if self . candidate_jump || self . candidate . is_some_and ( |candidate| !candidate . visible ) {
208231 CURSOR_POSITION_MAX_HOLD
209232 } else {
210233 CURSOR_POSITION_SETTLE
@@ -499,21 +522,116 @@ mod tests {
499522 }
500523
501524 #[ test]
502- fn cursor_settle_hides_immediately_and_waits_to_reveal ( ) {
525+ fn cursor_settle_ignores_short_same_position_hides ( ) {
503526 let now = Instant :: now ( ) ;
504527 let mut settle = CursorPositionSettleState :: default ( ) ;
505528 settle. observe ( Some ( cursor ( 1 , 0 , true , 0 ) ) , now) ;
506529 settle. observe ( Some ( cursor ( 1 , 0 , false , 0 ) ) , now + Duration :: from_millis ( 1 ) ) ;
507530
508531 assert_eq ! (
509532 settle. reported_cursor( Some ( cursor( 1 , 0 , false , 0 ) ) , now + Duration :: from_millis( 2 ) ) ,
510- Some ( cursor( 1 , 0 , false , 0 ) )
533+ Some ( cursor( 1 , 0 , true , 0 ) )
534+ ) ;
535+ settle. observe (
536+ Some ( cursor ( 1 , 0 , false , 0 ) ) ,
537+ now + Duration :: from_millis ( 50 ) ,
538+ ) ;
539+ assert_eq ! (
540+ settle. reported_cursor(
541+ Some ( cursor( 1 , 0 , false , 0 ) ) ,
542+ now + Duration :: from_millis( 90 )
543+ ) ,
544+ Some ( cursor( 1 , 0 , true , 0 ) )
545+ ) ;
546+ assert ! (
547+ !settle
548+ . reported_cursor(
549+ Some ( cursor( 2 , 0 , false , 0 ) ) ,
550+ now + Duration :: from_millis( 90 )
551+ )
552+ . unwrap( )
553+ . visible
554+ ) ;
555+ settle. observe ( Some ( cursor ( 1 , 0 , true , 0 ) ) , now + Duration :: from_millis ( 91 ) ) ;
556+ assert_eq ! (
557+ settle. reported_cursor( Some ( cursor( 1 , 0 , true , 0 ) ) , now + Duration :: from_millis( 92 ) ) ,
558+ Some ( cursor( 1 , 0 , true , 0 ) )
559+ ) ;
560+ assert ! ( !settle. pending( ) ) ;
561+ }
562+
563+ #[ test]
564+ fn cursor_settle_hides_after_deadline_and_waits_to_reveal ( ) {
565+ let now = Instant :: now ( ) ;
566+ let mut settle = CursorPositionSettleState :: default ( ) ;
567+ let visible = cursor ( 1 , 0 , true , 0 ) ;
568+ let hidden = cursor ( 1 , 0 , false , 0 ) ;
569+ settle. observe ( Some ( visible) , now) ;
570+ settle. observe ( Some ( hidden) , now + Duration :: from_millis ( 1 ) ) ;
571+ assert_eq ! ( settle. render_delay( ) , Some ( CURSOR_POSITION_MAX_HOLD ) ) ;
572+ assert_eq ! (
573+ settle. reported_cursor( Some ( hidden) , now + Duration :: from_millis( 100 ) ) ,
574+ Some ( visible)
575+ ) ;
576+ assert_eq ! (
577+ settle. reported_cursor( Some ( hidden) , now + Duration :: from_millis( 101 ) ) ,
578+ Some ( hidden)
579+ ) ;
580+
581+ // The pure read above exposes expiry without changing the state.
582+ settle. observe ( Some ( visible) , now + Duration :: from_millis ( 102 ) ) ;
583+ assert_eq ! (
584+ settle. reported_cursor( Some ( visible) , now + Duration :: from_millis( 103 ) ) ,
585+ Some ( hidden)
586+ ) ;
587+ assert_eq ! (
588+ settle. reported_cursor( Some ( visible) , now + Duration :: from_millis( 122 ) ) ,
589+ Some ( visible)
590+ ) ;
591+ }
592+
593+ #[ test]
594+ fn cursor_settle_hides_immediately_outside_stationary_caret ( ) {
595+ let now = Instant :: now ( ) ;
596+ let visible = cursor ( 1 , 0 , true , 0 ) ;
597+ let hidden = cursor ( 2 , 0 , false , 0 ) ;
598+ let mut settle = CursorPositionSettleState :: default ( ) ;
599+ settle. observe ( Some ( visible) , now) ;
600+ settle. observe ( Some ( hidden) , now + Duration :: from_millis ( 1 ) ) ;
601+ assert_eq ! (
602+ settle. reported_cursor( Some ( hidden) , now + Duration :: from_millis( 2 ) ) ,
603+ Some ( hidden)
604+ ) ;
605+
606+ settle. observe ( None , now + Duration :: from_millis ( 3 ) ) ;
607+ assert_eq ! (
608+ settle. reported_cursor( None , now + Duration :: from_millis( 4 ) ) ,
609+ None
511610 ) ;
512611
513- settle. observe ( Some ( cursor ( 1 , 0 , true , 0 ) ) , now + Duration :: from_millis ( 3 ) ) ;
612+ for hide_at in [ visible, cursor ( 2 , 0 , true , 0 ) ] {
613+ let mut settle = CursorPositionSettleState :: default ( ) ;
614+ settle. observe ( Some ( visible) , now) ;
615+ settle. observe ( Some ( cursor ( 2 , 0 , true , 0 ) ) , now + Duration :: from_millis ( 1 ) ) ;
616+ let hidden = TerminalCursorState {
617+ visible : false ,
618+ ..hide_at
619+ } ;
620+ settle. observe ( Some ( hidden) , now + Duration :: from_millis ( 2 ) ) ;
621+ assert_eq ! (
622+ settle. reported_cursor( Some ( hidden) , now + Duration :: from_millis( 3 ) ) ,
623+ Some ( hidden)
624+ ) ;
625+ }
626+
627+ // Once the pending move has expired, its destination is the caret to retain.
628+ let mut settle = CursorPositionSettleState :: default ( ) ;
629+ settle. observe ( Some ( visible) , now) ;
630+ settle. observe ( Some ( cursor ( 2 , 0 , true , 0 ) ) , now + Duration :: from_millis ( 1 ) ) ;
631+ settle. observe ( Some ( hidden) , now + Duration :: from_millis ( 22 ) ) ;
514632 assert_eq ! (
515- settle. reported_cursor( Some ( cursor ( 1 , 0 , true , 0 ) ) , now + Duration :: from_millis( 4 ) ) ,
516- Some ( cursor( 1 , 0 , false , 0 ) )
633+ settle. reported_cursor( Some ( hidden ) , now + Duration :: from_millis( 23 ) ) ,
634+ Some ( cursor( 2 , 0 , true , 0 ) )
517635 ) ;
518636 }
519637
0 commit comments