Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 5.1k
perf(web): dedupe terminal mouse motion reports#7845
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Uh oh!
There was an error while loading. Please reload this page.
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -418,6 +418,31 @@ export function shouldReportTerminalMouse( | ||
| return tracking && !event.shiftKey && !event.ctrlKey && !event.metaKey; | ||
| } | ||
| type TerminalMouseAction = "press" | "release" | "motion"; | ||
| export function resolveTerminalMouseData( | ||
| action: TerminalMouseAction, | ||
| data: string, | ||
| previousMotionData: string, | ||
| ): { readonly send: boolean; readonly nextMotionData: string } { | ||
| const nextMotionData = action === "motion" ? data : ""; | ||
| return { | ||
| send: data.length > 0 && (action !== "motion" || data !== previousMotionData), | ||
| nextMotionData, | ||
| }; | ||
| } | ||
| export function resolveTerminalMouseTrackingState( | ||
| previousTracking: boolean, | ||
| tracking: boolean, | ||
| motionData: string, | ||
| ): { readonly tracking: boolean; readonly motionData: string } { | ||
| return { | ||
| tracking, | ||
| motionData: previousTracking === tracking ? motionData : "", | ||
| }; | ||
| } | ||
| export function terminalWheelDeltaRows( | ||
| event: Pick<WheelEvent, "deltaY" | "deltaMode">, | ||
| cellHeight: number, | ||
| @@ -593,6 +618,8 @@ export class GhosttyTerminalSurface { | ||
| private clearSelectionAfterCopy = false; | ||
| private primedCopySelection = ""; | ||
| private wheelRemainder = 0; | ||
| private lastMouseMotionData = ""; | ||
| private mouseAnyEventTracking = false; | ||
| private dprMedia: MediaQueryList | null = null; | ||
| // Read live on every blink decision, and watched so that dropping the | ||
| // preference restarts a blink cycle that has no timer left to notice it. | ||
| @@ -619,6 +646,7 @@ export class GhosttyTerminalSurface { | ||
| this.scrollbarThumb = scrollbarThumb; | ||
| this.context = context; | ||
| this.core = core; | ||
| this.mouseAnyEventTracking = core.isMouseAnyEventTracking(); | ||
| this.metrics = metrics; | ||
| this.options = options; | ||
| this.theme = options.theme; | ||
| @@ -710,6 +738,7 @@ export class GhosttyTerminalSurface { | ||
| write(data: string): void { | ||
| if (this.disposed) return; | ||
| this.core.write(data); | ||
| this.synchronizeMouseTrackingState(); | ||
| // Restart the blink cycle from the visible phase so the cursor never sits | ||
| // invisible through a stream of output or a burst of typing echo. | ||
| this.cursorOn = true; | ||
| @@ -719,7 +748,9 @@ export class GhosttyTerminalSurface { | ||
| resetAndWrite(data: string): void { | ||
| if (this.disposed) return; | ||
| this.lastMouseMotionData = ""; | ||
| this.core.resetAndWrite(data); | ||
| this.synchronizeMouseTrackingState(); | ||
| // A replayed session starts from the visible phase like any other write: | ||
| // reattaching mid-blink must not open on an invisible cursor. | ||
| this.cursorOn = true; | ||
| @@ -1272,9 +1303,10 @@ export class GhosttyTerminalSurface { | ||
| if (this.linkActivationPointerId === event.pointerId) return; | ||
| // Hover motion is only reportable in any-event tracking (DEC 1003); normal and | ||
| // button-event tracking never report motion without a captured pressed button. | ||
| const anyEventTracking = this.synchronizeMouseTrackingState(); | ||
| if ( | ||
| this.mouseReportingPointerId === event.pointerId || | ||
| shouldReportTerminalMouse(this.core.isMouseAnyEventTracking(), event) | ||
| shouldReportTerminalMouse(anyEventTracking, event) | ||
| ) { | ||
| event.preventDefault(); | ||
| this.hoverPointer = { x: event.clientX, y: event.clientY }; | ||
| @@ -1286,6 +1318,7 @@ export class GhosttyTerminalSurface { | ||
| this.sendMouse("motion", this.buttonFromButtons(event.buttons), event); | ||
| return; | ||
| } | ||
| this.lastMouseMotionData = ""; | ||
| if (!this.selectionAnchorScreen || !this.canvas.hasPointerCapture(event.pointerId)) { | ||
| this.updateHoverCursor(event); | ||
| return; | ||
| @@ -1364,6 +1397,7 @@ export class GhosttyTerminalSurface { | ||
| } | ||
| private readonly onPointerLeave = () => { | ||
| this.lastMouseMotionData = ""; | ||
| this.clearHoveredLink(); | ||
| }; | ||
| @@ -1822,11 +1856,7 @@ export class GhosttyTerminalSurface { | ||
| return terminalLinkAtPositionWithRange(this.snapshot.rowData, cell.y, cell.x); | ||
| } | ||
| private sendMouse( | ||
| action: "press" | "release" | "motion", | ||
| button: number | null, | ||
| event: MouseEvent, | ||
| ): void { | ||
| private sendMouse(action: TerminalMouseAction, button: number | null, event: MouseEvent): void { | ||
| const bounds = this.canvas.getBoundingClientRect(); | ||
| const data = this.core.encodeMouse({ | ||
| action, | ||
| @@ -1848,7 +1878,23 @@ export class GhosttyTerminalSurface { | ||
| paddingBottom: Math.max(0, bounds.height - this.originY - this.rows * this.metrics.height), | ||
| anyButtonPressed: event.buttons !== 0, | ||
| }); | ||
| if (data.length > 0) this.options.onData(data); | ||
| const resolution = resolveTerminalMouseData(action, data, this.lastMouseMotionData); | ||
| this.lastMouseMotionData = resolution.nextMotionData; | ||
macroscopeapp[bot] marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| if (resolution.send) this.options.onData(data); | ||
| } | ||
| private synchronizeMouseTrackingState(): boolean { | ||
| // Output writes can toggle DEC 1003 without moving the pointer. Keep the | ||
| // previous mode so the next same-cell motion starts a fresh tracking session. | ||
| const tracking = this.core.isMouseAnyEventTracking(); | ||
| const state = resolveTerminalMouseTrackingState( | ||
| this.mouseAnyEventTracking, | ||
| tracking, | ||
| this.lastMouseMotionData, | ||
| ); | ||
| this.mouseAnyEventTracking = state.tracking; | ||
| this.lastMouseMotionData = state.motionData; | ||
| return tracking; | ||
| } | ||
| private buttonFromButtons(buttons: number): number | null { | ||
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.