@@ -432,6 +432,17 @@ export function resolveTerminalMouseData(
432432 } ;
433433}
434434
435+ export function resolveTerminalMouseTrackingState (
436+ previousTracking : boolean ,
437+ tracking : boolean ,
438+ motionData : string ,
439+ ) : { readonly tracking : boolean ; readonly motionData : string } {
440+ return {
441+ tracking,
442+ motionData : previousTracking === tracking ? motionData : "" ,
443+ } ;
444+ }
445+
435446export function terminalWheelDeltaRows (
436447 event : Pick < WheelEvent , "deltaY" | "deltaMode" > ,
437448 cellHeight : number ,
@@ -608,6 +619,7 @@ export class GhosttyTerminalSurface {
608619 private primedCopySelection = "" ;
609620 private wheelRemainder = 0 ;
610621 private lastMouseMotionData = "" ;
622+ private mouseAnyEventTracking = false ;
611623 private dprMedia : MediaQueryList | null = null ;
612624 // Read live on every blink decision, and watched so that dropping the
613625 // preference restarts a blink cycle that has no timer left to notice it.
@@ -634,6 +646,7 @@ export class GhosttyTerminalSurface {
634646 this . scrollbarThumb = scrollbarThumb ;
635647 this . context = context ;
636648 this . core = core ;
649+ this . mouseAnyEventTracking = core . isMouseAnyEventTracking ( ) ;
637650 this . metrics = metrics ;
638651 this . options = options ;
639652 this . theme = options . theme ;
@@ -725,6 +738,7 @@ export class GhosttyTerminalSurface {
725738 write ( data : string ) : void {
726739 if ( this . disposed ) return ;
727740 this . core . write ( data ) ;
741+ this . synchronizeMouseTrackingState ( ) ;
728742 // Restart the blink cycle from the visible phase so the cursor never sits
729743 // invisible through a stream of output or a burst of typing echo.
730744 this . cursorOn = true ;
@@ -734,7 +748,9 @@ export class GhosttyTerminalSurface {
734748
735749 resetAndWrite ( data : string ) : void {
736750 if ( this . disposed ) return ;
751+ this . lastMouseMotionData = "" ;
737752 this . core . resetAndWrite ( data ) ;
753+ this . synchronizeMouseTrackingState ( ) ;
738754 // A replayed session starts from the visible phase like any other write:
739755 // reattaching mid-blink must not open on an invisible cursor.
740756 this . cursorOn = true ;
@@ -1287,9 +1303,10 @@ export class GhosttyTerminalSurface {
12871303 if ( this . linkActivationPointerId === event . pointerId ) return ;
12881304 // Hover motion is only reportable in any-event tracking (DEC 1003); normal and
12891305 // button-event tracking never report motion without a captured pressed button.
1306+ const anyEventTracking = this . synchronizeMouseTrackingState ( ) ;
12901307 if (
12911308 this . mouseReportingPointerId === event . pointerId ||
1292- shouldReportTerminalMouse ( this . core . isMouseAnyEventTracking ( ) , event )
1309+ shouldReportTerminalMouse ( anyEventTracking , event )
12931310 ) {
12941311 event . preventDefault ( ) ;
12951312 this . hoverPointer = { x : event . clientX , y : event . clientY } ;
@@ -1301,6 +1318,7 @@ export class GhosttyTerminalSurface {
13011318 this . sendMouse ( "motion" , this . buttonFromButtons ( event . buttons ) , event ) ;
13021319 return ;
13031320 }
1321+ this . lastMouseMotionData = "" ;
13041322 if ( ! this . selectionAnchorScreen || ! this . canvas . hasPointerCapture ( event . pointerId ) ) {
13051323 this . updateHoverCursor ( event ) ;
13061324 return ;
@@ -1865,6 +1883,20 @@ export class GhosttyTerminalSurface {
18651883 if ( resolution . send ) this . options . onData ( data ) ;
18661884 }
18671885
1886+ private synchronizeMouseTrackingState ( ) : boolean {
1887+ // Output writes can toggle DEC 1003 without moving the pointer. Keep the
1888+ // previous mode so the next same-cell motion starts a fresh tracking session.
1889+ const tracking = this . core . isMouseAnyEventTracking ( ) ;
1890+ const state = resolveTerminalMouseTrackingState (
1891+ this . mouseAnyEventTracking ,
1892+ tracking ,
1893+ this . lastMouseMotionData ,
1894+ ) ;
1895+ this . mouseAnyEventTracking = state . tracking ;
1896+ this . lastMouseMotionData = state . motionData ;
1897+ return tracking ;
1898+ }
1899+
18681900 private buttonFromButtons ( buttons : number ) : number | null {
18691901 if ( ( buttons & 1 ) !== 0 ) return 1 ;
18701902 if ( ( buttons & 4 ) !== 0 ) return 3 ;
0 commit comments