Files
t3code/apps/web/src/terminal/ghostty/surface.ts
T
2026-07-31 13:15:25 +02:00

1394 lines
51 KiB
TypeScript

import { isMacPlatform } from "../../lib/utils";
import { collectWrappedTerminalLinkLine, extractTerminalLinks } from "../../terminal-links";
import {
GhosttyTerminalCore,
type GhosttyScrollbar,
type GhosttySnapshot,
type GhosttyTheme,
} from "./core";
import {
measureGhosttyCell,
renderGhosttySnapshot,
terminalGridSize,
type GhosttyCellMetrics,
} from "./renderer";
import symbolsFontUrl from "./fonts/SymbolsNerdFontMono-Regular.woff2?url";
export const DEFAULT_TERMINAL_FONT_SIZE = 12;
const MIN_TERMINAL_FONT_SIZE = 6;
const MAX_TERMINAL_FONT_SIZE = 32;
// The glyph fallbacks only supply symbols the text faces are missing (powerline
// separators, devicons, and other private-use prompt symbols), so shells
// configured for a locally installed Nerd Font keep their prompt glyphs no
// matter which text face is active.
const TERMINAL_GLYPH_FALLBACKS =
'"Symbols Nerd Font Mono", "Symbols Nerd Font", "JetBrainsMono Nerd Font", ' +
'"JetBrainsMono NF", "FiraCode Nerd Font", "Hack Nerd Font", "MesloLGS NF", ' +
'"CaskaydiaCove Nerd Font", "PowerlineSymbols", monospace';
// SF Mono where the platform has it (macOS), otherwise the bundled JetBrains
// Mono webfont, so the default rendering is identical everywhere else.
export const DEFAULT_TERMINAL_FONT_FAMILY =
'"SF Mono", "SFMono-Regular", "JetBrains Mono", ' + TERMINAL_GLYPH_FALLBACKS;
const CONTENT_PADDING = 4;
const MIN_SCROLLBAR_THUMB_HEIGHT = 18;
/** Requested terminal font; omitted fields fall back to the defaults. */
export interface GhosttyTerminalFont {
readonly family?: string;
readonly size?: number;
}
let symbolsFontLoad: Promise<void> | null = null;
/**
* Register the bundled symbols-only Nerd Font once per page. It loads lazily
* with the first terminal, and because it carries no regular text glyphs it
* composes with any text face without changing metrics — prompt symbols and
* devicons render even on machines without a locally installed Nerd Font.
*/
function ensureTerminalSymbolsFont(): Promise<void> {
if (symbolsFontLoad !== null) return symbolsFontLoad;
symbolsFontLoad = (async () => {
try {
const face = new FontFace("Symbols Nerd Font Mono", `url(${symbolsFontUrl})`);
document.fonts.add(await face.load());
} catch {
// Locally installed fallback faces still apply.
}
})();
return symbolsFontLoad;
}
export function terminalFontFamily(family?: string): string {
const custom = family?.trim();
if (!custom) return DEFAULT_TERMINAL_FONT_FAMILY;
// A custom face keeps the glyph fallbacks so prompt symbols stay covered.
return `${custom}, ${TERMINAL_GLYPH_FALLBACKS}`;
}
export function terminalFontSize(size?: number): number {
if (size === undefined || !Number.isFinite(size)) return DEFAULT_TERMINAL_FONT_SIZE;
return Math.max(MIN_TERMINAL_FONT_SIZE, Math.min(MAX_TERMINAL_FONT_SIZE, Math.round(size)));
}
/**
* Vertical origin of the grid inside the mount. While content is shorter than
* the viewport the grid sits at the top like a fresh terminal. Once scrollback
* exists the prompt lives on the bottom row, so the grid anchors to the bottom
* edge instead: the sub-row remainder moves above row 0 and resizing within a
* row boundary keeps the prompt pinned instead of snapping up and down.
*/
export function terminalContentOriginY(
mountHeight: number,
padding: number,
rows: number,
cellHeight: number,
anchorBottom: boolean,
): number {
if (!anchorBottom) return padding;
const slack = mountHeight - padding * 2 - rows * cellHeight;
return padding + Math.max(0, slack);
}
export interface TerminalScrollbarGeometry {
readonly thumbHeight: number;
readonly thumbTop: number;
readonly maxOffset: number;
}
export function terminalScrollbarGeometry(
state: GhosttyScrollbar,
trackHeight: number,
): TerminalScrollbarGeometry | null {
const total = Math.max(0, state.total);
const len = Math.max(0, Math.min(state.len, total));
const maxOffset = Math.max(0, total - len);
if (trackHeight <= 0 || len <= 0 || maxOffset === 0) return null;
const thumbHeight = Math.min(
trackHeight,
Math.max(MIN_SCROLLBAR_THUMB_HEIGHT, (trackHeight * len) / total),
);
const travel = Math.max(0, trackHeight - thumbHeight);
const offset = Math.max(0, Math.min(state.offset, maxOffset));
return {
thumbHeight,
thumbTop: travel * (offset / maxOffset),
maxOffset,
};
}
export function terminalScrollbarOffsetAtPointer(
state: GhosttyScrollbar,
trackHeight: number,
pointerY: number,
pointerOffset: number,
): number {
const geometry = terminalScrollbarGeometry(state, trackHeight);
if (geometry === null) return 0;
const travel = Math.max(0, trackHeight - geometry.thumbHeight);
if (travel === 0) return 0;
const thumbTop = Math.max(0, Math.min(pointerY - pointerOffset, travel));
return Math.round((thumbTop / travel) * geometry.maxOffset);
}
function terminalRowText(row: GhosttySnapshot["rowData"][number], trimRight: boolean): string {
const text = row.cells.map((cell) => cell.text || " ").join("");
return trimRight ? text.trimEnd() : text;
}
function terminalColumnOffset(row: GhosttySnapshot["rowData"][number], column: number): number {
let offset = 0;
for (let cellIndex = 0; cellIndex < column; cellIndex += 1) {
offset += row.cells[cellIndex]?.text.length || 1;
}
return offset;
}
export function terminalLinkAtPosition(
rows: GhosttySnapshot["rowData"],
rowIndex: number,
column: number,
): string | null {
const wrappedLine = collectWrappedTerminalLinkLine(rowIndex + 1, (index) => {
const row = rows[index];
if (!row) return null;
return {
isWrapped: row.isWrapContinuation,
translateToString: (trimRight = false) => terminalRowText(row, trimRight),
};
});
if (!wrappedLine) return null;
// Only viewport rows are available: a wrapped line whose head scrolled above
// the viewport would resolve a truncated match into a wrong link.
const firstSegment = wrappedLine.segments[0];
if (firstSegment && rows[firstSegment.bufferLineNumber - 1]?.isWrapContinuation) {
return null;
}
const segment = wrappedLine.segments.find((value) => value.bufferLineNumber === rowIndex + 1);
const row = rows[rowIndex];
if (!segment || !row) return null;
const lastSegment = wrappedLine.segments.at(-1);
const lastRow = lastSegment ? rows[lastSegment.bufferLineNumber - 1] : undefined;
// Ghostty's soft-wrap flag is authoritative: when the last collected row
// still wraps onward, its continuation is outside the viewport.
const continuesBelowViewport = lastRow !== undefined && lastRow.wrapsToNext;
const offset = segment.startIndex + terminalColumnOffset(row, column);
for (const match of extractTerminalLinks(wrappedLine.text)) {
if (offset >= match.start && offset < match.end) {
// A truncated tail must not activate as a complete link.
if (match.end === wrappedLine.text.length && continuesBelowViewport) return null;
return match.text;
}
}
return null;
}
export function terminalLinkAtColumn(row: GhosttySnapshot["rowData"][number], column: number) {
return terminalLinkAtPosition([row], 0, column);
}
export function isTerminalCopyShortcut(
event: Pick<KeyboardEvent, "ctrlKey" | "key" | "metaKey" | "shiftKey">,
platform = navigator.platform,
) {
if (event.key.toLowerCase() !== "c") return false;
return isMacPlatform(platform) ? event.metaKey : event.ctrlKey && event.shiftKey;
}
export function isTerminalPasteShortcut(
event: Pick<KeyboardEvent, "ctrlKey" | "key" | "metaKey" | "shiftKey">,
platform = navigator.platform,
) {
if (event.key.toLowerCase() !== "v") return false;
return isMacPlatform(platform) ? event.metaKey : event.ctrlKey && event.shiftKey;
}
export function isTerminalCompositionCommitInput(event: Pick<InputEvent, "inputType">): boolean {
return (
event.inputType === "" ||
event.inputType === "insertCompositionText" ||
event.inputType === "insertFromComposition"
);
}
export function isTerminalAltGraphText(
event: Pick<KeyboardEvent, "getModifierState" | "key">,
): boolean {
return event.getModifierState("AltGraph") && [...event.key].length === 1;
}
export function shouldReportTerminalMouse(
tracking: boolean,
event: Pick<MouseEvent, "ctrlKey" | "metaKey" | "shiftKey">,
): boolean {
return tracking && !event.shiftKey && !event.ctrlKey && !event.metaKey;
}
export function terminalWheelDeltaRows(
event: Pick<WheelEvent, "deltaY" | "deltaMode">,
cellHeight: number,
viewportRows: number,
remainder: number,
): { readonly rows: number; readonly remainder: number } {
// deltaMode: 0 pixels, 1 lines, 2 pages.
const pixels =
event.deltaMode === 1
? event.deltaY * cellHeight
: event.deltaMode === 2
? event.deltaY * viewportRows * cellHeight
: event.deltaY;
const total = remainder + pixels / cellHeight;
const rows = Math.trunc(total);
return { rows, remainder: total - rows };
}
export function terminalWheelArrowData(rows: number, applicationCursorKeys: boolean): string {
if (rows === 0) return "";
const sequence =
rows < 0
? applicationCursorKeys
? "\u001bOA"
: "\u001b[A"
: applicationCursorKeys
? "\u001bOB"
: "\u001b[B";
return sequence.repeat(Math.abs(rows));
}
export function isTerminalLinkPointerGesture(
event: Pick<MouseEvent, "ctrlKey" | "metaKey">,
platform = navigator.platform,
): boolean {
return isMacPlatform(platform)
? event.metaKey && !event.ctrlKey
: event.ctrlKey && !event.metaKey;
}
export function ghosttyMouseButton(button: number): number | null {
switch (button) {
case 0:
return 1;
case 1:
return 3;
case 2:
return 2;
case 3:
return 4;
case 4:
return 5;
default:
return null;
}
}
export interface TerminalSelectionClickSequence {
readonly count: number;
readonly time: number;
readonly x: number;
readonly y: number;
}
export function advanceTerminalSelectionClickSequence(
previous: TerminalSelectionClickSequence | null,
event: Pick<PointerEvent, "clientX" | "clientY" | "timeStamp">,
): TerminalSelectionClickSequence {
const repeats =
previous !== null &&
event.timeStamp - previous.time <= 500 &&
Math.hypot(event.clientX - previous.x, event.clientY - previous.y) <= 4;
return {
count: repeats ? (previous.count >= 3 ? 1 : previous.count + 1) : 1,
time: event.timeStamp,
x: event.clientX,
y: event.clientY,
};
}
export interface GhosttySelectionPosition {
readonly start: { readonly x: number; readonly y: number };
readonly end: { readonly x: number; readonly y: number };
}
export interface GhosttyTerminalSurfaceOptions {
readonly theme: GhosttyTheme;
readonly font?: GhosttyTerminalFont;
readonly onData: (data: string) => void;
readonly onResize: (cols: number, rows: number) => void;
readonly onSelectionChange: () => void;
readonly onCopy: (text: string) => void;
readonly beforeKey: (event: KeyboardEvent) => boolean;
readonly onLinkActivate: (text: string, event: MouseEvent) => void;
}
export class GhosttyTerminalSurface {
readonly canvas: HTMLCanvasElement;
readonly input: HTMLTextAreaElement;
readonly scrollbar: HTMLDivElement;
cols = 1;
rows = 1;
private readonly mount: HTMLElement;
private readonly context: CanvasRenderingContext2D;
private readonly core: GhosttyTerminalCore;
private readonly options: GhosttyTerminalSurfaceOptions;
private metrics: GhosttyCellMetrics;
private fontFamily: string;
private fontSize: number;
private fontEpoch = 0;
private readonly resizeObserver: ResizeObserver;
private readonly scrollbarThumb: HTMLDivElement;
private snapshot: GhosttySnapshot | null = null;
private frame = 0;
private cursorTimer: number | null = null;
private compositionInputToSuppress: string | null = null;
private compositionSuppressionTimer: number | null = null;
private cursorOn = true;
private renderedCursorY: number | null = null;
private forceFullRender = true;
private scrollbarDirty = true;
private scrollbarState: GhosttyScrollbar | null = null;
private scrollbarPointerId: number | null = null;
private scrollbarPointerOffset = 0;
private disposed = false;
private resizeNotifyTimer: number | null = null;
private originY = CONTENT_PADDING;
private mountHeight = 0;
private selectionEnd: { x: number; y: number } | null = null;
private selectionAnchorScreen: { x: number; y: number } | null = null;
private selectionEndScreen: { x: number; y: number } | null = null;
private selectionMode: "cell" | "word" | "line" = "cell";
// Word/line selection base in screen coordinates so streaming output cannot
// shift the origin of a drag selection.
private selectionBase: {
start: { x: number; y: number };
end: { x: number; y: number };
} | null = null;
private selectionScrollTimer: number | null = null;
private selectionScrollDelta = 0;
private selectionPointer: { x: number; y: number } | null = null;
private mouseReportingPointerId: number | null = null;
private mouseReportingButton: number | null = null;
private linkActivationPointerId: number | null = null;
private selectionClickSequence: TerminalSelectionClickSequence | null = null;
private selectionMoved = false;
private composing = false;
private focused = false;
private resizeNotified = false;
private canvasConfigured = false;
private theme: GhosttyTheme;
private readonly suppressedKeyCodes = new Set<string>();
private pasteShortcutToken = 0;
private wheelRemainder = 0;
private dprMedia: MediaQueryList | null = null;
private inputLeft = -1;
private inputTop = -1;
private constructor(
mount: HTMLElement,
canvas: HTMLCanvasElement,
input: HTMLTextAreaElement,
scrollbar: HTMLDivElement,
scrollbarThumb: HTMLDivElement,
context: CanvasRenderingContext2D,
core: GhosttyTerminalCore,
metrics: GhosttyCellMetrics,
options: GhosttyTerminalSurfaceOptions,
) {
this.mount = mount;
this.canvas = canvas;
this.input = input;
this.scrollbar = scrollbar;
this.scrollbarThumb = scrollbarThumb;
this.context = context;
this.core = core;
this.metrics = metrics;
this.options = options;
this.theme = options.theme;
this.fontFamily = terminalFontFamily(options.font?.family);
this.fontSize = terminalFontSize(options.font?.size);
this.resizeObserver = new ResizeObserver(() => this.fit());
this.installEvents();
this.watchDevicePixelRatio();
document.fonts.addEventListener("loadingdone", this.onFontsLoaded);
this.resizeObserver.observe(mount);
}
static async create(
mount: HTMLElement,
options: GhosttyTerminalSurfaceOptions,
): Promise<GhosttyTerminalSurface> {
const canvas = document.createElement("canvas");
canvas.className = "t3-ghostty-canvas";
canvas.style.cssText = "display:block;width:100%;height:100%;";
canvas.setAttribute("aria-hidden", "true");
const input = document.createElement("textarea");
input.className = "t3-ghostty-input";
input.setAttribute("aria-label", "Terminal input");
input.autocapitalize = "off";
input.autocomplete = "off";
input.spellcheck = false;
input.style.cssText =
"position:absolute;left:4px;top:4px;width:1px;height:1px;opacity:0;padding:0;border:0;resize:none;pointer-events:none;";
const scrollbar = document.createElement("div");
scrollbar.className = "t3-ghostty-scrollbar";
scrollbar.setAttribute("role", "scrollbar");
scrollbar.setAttribute("aria-label", "Terminal scrollback");
scrollbar.setAttribute("aria-orientation", "vertical");
scrollbar.tabIndex = 0;
scrollbar.hidden = true;
const scrollbarThumb = document.createElement("div");
scrollbarThumb.className = "t3-ghostty-scrollbar-thumb";
scrollbar.append(scrollbarThumb);
mount.replaceChildren(canvas, input, scrollbar);
const context = canvas.getContext("2d", { alpha: false });
if (!context) throw new Error("Canvas 2D is unavailable");
const fontFamily = terminalFontFamily(options.font?.family);
const fontSize = terminalFontSize(options.font?.size);
try {
// Cell metrics must come from the faces that will render; measuring before
// the bundled webfonts load would size the grid from a fallback font.
await ensureTerminalSymbolsFont();
await document.fonts.load(`${fontSize}px ${fontFamily}`);
} catch {
// Metrics fall back to whichever faces are already available.
}
const metrics = measureGhosttyCell(context, fontSize, fontFamily);
const grid = terminalGridSize(mount.clientWidth, mount.clientHeight, metrics, CONTENT_PADDING);
const core = await GhosttyTerminalCore.create(
grid.cols,
grid.rows,
metrics.width,
metrics.height,
options.theme,
options.onData,
);
const surface = new GhosttyTerminalSurface(
mount,
canvas,
input,
scrollbar,
scrollbarThumb,
context,
core,
metrics,
options,
);
surface.fit();
surface.requestRender();
return surface;
}
write(data: string): void {
if (this.disposed) return;
this.core.write(data);
// 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;
this.scrollbarDirty = true;
this.requestRender();
}
resetAndWrite(data: string): void {
if (this.disposed) return;
this.core.resetAndWrite(data);
this.forceFullRender = true;
this.scrollbarDirty = true;
this.requestRender();
}
setTheme(theme: GhosttyTheme): void {
if (this.disposed) return;
this.theme = theme;
this.core.setTheme(theme);
this.forceFullRender = true;
this.requestRender();
}
async setFont(font: GhosttyTerminalFont): Promise<void> {
if (this.disposed) return;
const fontFamily = terminalFontFamily(font.family);
const fontSize = terminalFontSize(font.size);
// The fields only change together with their metrics after the load, and
// the epoch lets the newest overlapping call win regardless of load order.
const epoch = ++this.fontEpoch;
try {
await document.fonts.load(`${fontSize}px ${fontFamily}`);
} catch {
// Metrics fall back to whichever faces are already available.
}
if (this.disposed || epoch !== this.fontEpoch) return;
this.fontFamily = fontFamily;
this.fontSize = fontSize;
this.applyFontMetrics();
}
private applyFontMetrics(): void {
this.metrics = measureGhosttyCell(this.context, this.fontSize, this.fontFamily);
this.core.resize(this.cols, this.rows, this.metrics.width, this.metrics.height);
// Cached IME textarea coordinates are stale in the new cell geometry.
this.inputLeft = -1;
this.inputTop = -1;
this.forceFullRender = true;
this.scrollbarDirty = true;
this.fit();
this.requestRender();
}
private readonly onFontsLoaded = () => {
if (this.disposed) return;
// A face that finished loading after the initial measurement changes glyph
// advances; re-measure and refit so the grid matches what actually renders.
const metrics = measureGhosttyCell(this.context, this.fontSize, this.fontFamily);
if (
metrics.width === this.metrics.width &&
metrics.height === this.metrics.height &&
metrics.baseline === this.metrics.baseline
) {
return;
}
this.applyFontMetrics();
};
fit(): boolean {
if (this.disposed) return false;
const width = this.mount.clientWidth;
const height = this.mount.clientHeight;
if (width <= 0 || height <= 0) return false;
const ratio = window.devicePixelRatio || 1;
const pixelWidth = Math.max(1, Math.round(width * ratio));
const pixelHeight = Math.max(1, Math.round(height * ratio));
let shouldRender = false;
// The DPR transform must be installed even when the target size happens to
// equal the canvas default 300x150 backing store, so the first fit always
// schedules a canvas configuration.
if (
this.canvas.width !== pixelWidth ||
this.canvas.height !== pixelHeight ||
!this.canvasConfigured
) {
this.canvas.width = pixelWidth;
this.canvas.height = pixelHeight;
this.context.setTransform(ratio, 0, 0, ratio, 0, 0);
this.canvasConfigured = true;
this.forceFullRender = true;
this.scrollbarDirty = true;
shouldRender = true;
}
const grid = terminalGridSize(width, height, this.metrics, CONTENT_PADDING);
this.mountHeight = height;
// onResize is the only PTY resize channel, so the first successful fit must
// notify even when the measured grid equals the 1x1 construction sentinel.
if (grid.cols !== this.cols || grid.rows !== this.rows || !this.resizeNotified) {
this.cols = grid.cols;
this.rows = grid.rows;
this.core.resize(grid.cols, grid.rows, this.metrics.width, this.metrics.height);
this.notifyResize();
this.forceFullRender = true;
this.scrollbarDirty = true;
shouldRender = true;
}
// Rendering synchronously keeps the repaint inside the same frame as the
// layout change: ResizeObserver fires before paint, so the browser never
// composites the old backing store stretched into the new element box.
if (shouldRender) this.renderFrame();
return true;
}
/**
* The local grid reflows immediately, but the PTY only hears about settled
* dimensions: notifying on every drag step makes the shell reprint its
* prompt mid-drag, which reads as jitter.
*/
private notifyResize(): void {
this.resizeNotified = true;
if (this.resizeNotifyTimer !== null) window.clearTimeout(this.resizeNotifyTimer);
this.resizeNotifyTimer = window.setTimeout(() => {
this.resizeNotifyTimer = null;
if (!this.disposed) this.options.onResize(this.cols, this.rows);
}, 150);
}
focus(): void {
this.input.focus({ preventScroll: true });
}
hasSelection(): boolean {
return this.core.selectionText().length > 0;
}
getSelection(): string {
return this.core.selectionText();
}
getSelectionPosition(): GhosttySelectionPosition | null {
if (!this.selectionAnchorScreen || !this.selectionEndScreen || !this.hasSelection())
return null;
const before =
this.selectionAnchorScreen.y < this.selectionEndScreen.y ||
(this.selectionAnchorScreen.y === this.selectionEndScreen.y &&
this.selectionAnchorScreen.x <= this.selectionEndScreen.x);
return before
? { start: this.selectionAnchorScreen, end: this.selectionEndScreen }
: { start: this.selectionEndScreen, end: this.selectionAnchorScreen };
}
getSelectionEndClientRect(): { readonly right: number; readonly bottom: number } | null {
const position = this.getSelectionPosition();
if (!position) return null;
const viewportEnd = this.core.screenPointToViewport(position.end.x, position.end.y);
if (!viewportEnd) return null;
const bounds = this.canvas.getBoundingClientRect();
return {
right: bounds.left + CONTENT_PADDING + (viewportEnd.x + 1) * this.metrics.width,
bottom: bounds.top + this.originY + (viewportEnd.y + 1) * this.metrics.height,
};
}
clearSelection(): void {
this.core.clearSelection();
this.selectionEnd = null;
this.selectionAnchorScreen = null;
this.selectionEndScreen = null;
this.selectionMode = "cell";
this.selectionBase = null;
this.setSelectionAutoscroll(0);
this.options.onSelectionChange();
// Selection highlights span rows Ghostty may not mark dirty for this change.
this.forceFullRender = true;
this.requestRender();
}
scrollToBottom(): void {
this.core.scrollToBottom();
this.forceFullRender = true;
this.scrollbarDirty = true;
this.requestRender();
}
isAtBottom(): boolean {
return this.core.isViewportActive();
}
dispose(): void {
if (this.disposed) return;
this.disposed = true;
this.resizeObserver.disconnect();
document.fonts.removeEventListener("loadingdone", this.onFontsLoaded);
this.dprMedia?.removeEventListener("change", this.onDevicePixelRatioChange);
this.dprMedia = null;
if (this.selectionScrollTimer !== null) window.clearInterval(this.selectionScrollTimer);
if (this.resizeNotifyTimer !== null) {
window.clearTimeout(this.resizeNotifyTimer);
this.resizeNotifyTimer = null;
// Flush the settled dimensions so the PTY keeps the final size even when
// the surface unmounts inside the debounce window.
this.options.onResize(this.cols, this.rows);
}
if (this.frame !== 0) window.cancelAnimationFrame(this.frame);
if (this.cursorTimer !== null) window.clearTimeout(this.cursorTimer);
if (this.compositionSuppressionTimer !== null) {
window.clearTimeout(this.compositionSuppressionTimer);
}
this.removeEvents();
this.core.dispose();
if (
this.canvas.parentElement === this.mount ||
this.input.parentElement === this.mount ||
this.scrollbar.parentElement === this.mount
) {
this.canvas.remove();
this.input.remove();
this.scrollbar.remove();
}
}
private readonly onKeyDown = (event: KeyboardEvent) => {
// Presses handled outside the terminal must also swallow their release:
// beforeKey runs side effects (keybindings, navigation sends), so it cannot
// be consulted again on keyup, and Kitty report-event-types sessions would
// otherwise receive a release for a press the shell never saw.
if (isTerminalAltGraphText(event) || !this.options.beforeKey(event)) {
this.suppressedKeyCodes.add(event.code);
return;
}
if (isTerminalCopyShortcut(event) && this.hasSelection()) {
event.preventDefault();
this.suppressedKeyCodes.add(event.code);
this.options.onCopy(this.getSelection());
return;
}
if (isTerminalPasteShortcut(event)) {
this.suppressedKeyCodes.add(event.code);
const clipboard = navigator.clipboard;
if (typeof clipboard?.readText === "function") {
// Race the async clipboard read against the browser's own paste event:
// the native event (dispatched synchronously with the default action)
// always claims the token first when it fires, and the read covers
// browsers whose paste shortcut produces no paste event. Not preventing
// the default keeps the native path alive when the read is denied.
const token = ++this.pasteShortcutToken;
void clipboard.readText().then(
(text) => {
if (this.disposed || this.pasteShortcutToken !== token) return;
this.pasteShortcutToken += 1;
if (text.length > 0) this.options.onData(this.core.encodePaste(text));
},
() => {
// Clipboard read denied; the native paste event remains the path.
},
);
}
return;
}
// keyCode 229 is Safari's only signal that this keydown opens an IME
// composition; encoding it would double the committed text.
if (event.isComposing || this.composing || event.key === "Process" || event.keyCode === 229) {
return;
}
const data = this.core.encodeKey(event);
if (data.length === 0) return;
this.suppressedKeyCodes.delete(event.code);
event.preventDefault();
event.stopPropagation();
this.options.onData(data);
};
private readonly onKeyUp = (event: KeyboardEvent) => {
if (this.suppressedKeyCodes.delete(event.code)) return;
if (event.isComposing || this.composing || event.key === "Process" || event.keyCode === 229) {
return;
}
// Ghostty's encoder only emits release codes when the terminal enabled the
// Kitty report-event-types flag, so legacy sessions send nothing here.
const data = this.core.encodeKey(event, "release");
if (data.length === 0) return;
event.preventDefault();
event.stopPropagation();
this.options.onData(data);
};
private readonly onFocus = () => {
this.focused = true;
this.cursorOn = true;
this.requestRender();
};
private readonly onBlur = () => {
this.focused = false;
// Suppressions survive blur deliberately: a shortcut that moves focus (for
// example terminal-toggle) must still swallow its own keyup if focus comes
// back before release. Stale entries are harmless — an encoding keydown
// always removes its code first.
// The steady unfocused hollow cursor must not inherit an off blink phase.
this.cursorOn = true;
this.requestRender();
};
private readonly onDevicePixelRatioChange = () => {
this.watchDevicePixelRatio();
this.fit();
};
private watchDevicePixelRatio(): void {
this.dprMedia?.removeEventListener("change", this.onDevicePixelRatioChange);
// A resolution media query only fires once for the ratio it was created at,
// so re-arm it after every change (monitor moves, browser zoom).
this.dprMedia = window.matchMedia(`(resolution: ${window.devicePixelRatio}dppx)`);
this.dprMedia.addEventListener("change", this.onDevicePixelRatioChange);
}
private readonly onPaste = (event: ClipboardEvent) => {
// Always suppress the browser's default insertion: content the textarea
// would receive (for example an html-only clipboard converted to text)
// leaks through onInput without bracketed-paste encoding.
event.preventDefault();
const data = event.clipboardData?.getData("text/plain") ?? "";
if (data.length === 0) return;
// The native paste won the race with actual text; a pending clipboard read
// must not double. An empty native paste leaves the read as the only path.
this.pasteShortcutToken += 1;
this.options.onData(this.core.encodePaste(data));
};
private readonly onCompositionStart = () => {
this.clearCompositionInputSuppression();
this.composing = true;
};
private readonly onCompositionEnd = (event: CompositionEvent) => {
this.composing = false;
const data = this.input.value || event.data;
if (data.length > 0) this.options.onData(data);
this.input.value = "";
this.compositionInputToSuppress = data;
this.compositionSuppressionTimer = window.setTimeout(() => {
this.compositionInputToSuppress = null;
this.compositionSuppressionTimer = null;
}, 100);
};
private readonly onInput = (event: Event) => {
const inputEvent = event as InputEvent;
if (this.composing || inputEvent.isComposing) return;
const data = this.input.value || inputEvent.data || "";
if (data === this.compositionInputToSuppress && isTerminalCompositionCommitInput(inputEvent)) {
this.clearCompositionInputSuppression();
this.input.value = "";
return;
}
this.clearCompositionInputSuppression();
if (data.length > 0) this.options.onData(data);
this.input.value = "";
};
private clearCompositionInputSuppression(): void {
if (this.compositionSuppressionTimer !== null) {
window.clearTimeout(this.compositionSuppressionTimer);
this.compositionSuppressionTimer = null;
}
this.compositionInputToSuppress = null;
}
private readonly onPointerDown = (event: PointerEvent) => {
this.focus();
if (shouldReportTerminalMouse(this.core.isMouseTracking(), event)) {
const button = ghosttyMouseButton(event.button);
if (button === null) return;
event.preventDefault();
event.stopPropagation();
this.mouseReportingPointerId = event.pointerId;
this.mouseReportingButton = button;
this.sendMouse("press", button, event);
this.canvas.setPointerCapture(event.pointerId);
return;
}
if (event.button !== 0) return;
if (isTerminalLinkPointerGesture(event)) {
event.preventDefault();
event.stopPropagation();
this.linkActivationPointerId = event.pointerId;
this.canvas.setPointerCapture(event.pointerId);
return;
}
const cell = this.cellAt(event.clientX, event.clientY);
this.selectionMoved = false;
this.selectionClickSequence = advanceTerminalSelectionClickSequence(
this.selectionClickSequence,
event,
);
const clickCount = this.selectionClickSequence.count;
this.selectionMode = clickCount >= 3 ? "line" : clickCount === 2 ? "word" : "cell";
const range =
this.selectionMode === "line"
? this.core.selectLine(cell.x, cell.y)
: this.selectionMode === "word"
? this.core.selectWord(cell.x, cell.y)
: null;
if (range) {
this.selectionBase = range.screen;
this.selectionEnd = range.viewport.end;
this.selectionAnchorScreen = range.screen.start;
this.selectionEndScreen = range.screen.end;
this.options.onSelectionChange();
} else {
this.selectionMode = "cell";
this.selectionBase = null;
this.selectionEnd = cell;
const screen = this.core.viewportPointToScreen(cell.x, cell.y);
this.selectionAnchorScreen = screen;
this.selectionEndScreen = screen;
if (screen) {
this.core.setSelection({ ...screen, tag: 2 }, { ...screen, tag: 2 });
} else {
this.core.setSelection(cell, cell);
}
}
this.forceFullRender = true;
this.canvas.setPointerCapture(event.pointerId);
this.requestRender();
};
private readonly onPointerMove = (event: PointerEvent) => {
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.
if (
this.mouseReportingPointerId === event.pointerId ||
shouldReportTerminalMouse(this.core.isMouseAnyEventTracking(), event)
) {
event.preventDefault();
this.canvas.style.cursor = "default";
this.sendMouse("motion", this.buttonFromButtons(event.buttons), event);
return;
}
if (!this.selectionAnchorScreen || !this.canvas.hasPointerCapture(event.pointerId)) {
this.updateHoverCursor(event);
return;
}
this.selectionPointer = { x: event.clientX, y: event.clientY };
const bounds = this.canvas.getBoundingClientRect();
this.setSelectionAutoscroll(
event.clientY < bounds.top ? -1 : event.clientY > bounds.bottom ? 1 : 0,
);
const cell = this.cellAt(event.clientX, event.clientY);
if (cell.x === this.selectionEnd?.x && cell.y === this.selectionEnd.y) return;
this.extendSelectionTo(event.clientX, event.clientY);
};
private extendSelectionTo(clientX: number, clientY: number): void {
const anchorScreen = this.selectionAnchorScreen;
if (anchorScreen === null) return;
const cell = this.cellAt(clientX, clientY);
this.selectionMoved = true;
this.selectionEnd = cell;
const range =
this.selectionMode === "line"
? this.core.selectLine(cell.x, cell.y)
: this.selectionMode === "word"
? this.core.selectWord(cell.x, cell.y)
: null;
const cellScreen = this.core.viewportPointToScreen(cell.x, cell.y);
if (cellScreen === null) return;
const base = this.selectionBase;
const beforeBase =
base !== null &&
(cellScreen.y < base.start.y ||
(cellScreen.y === base.start.y && cellScreen.x < base.start.x));
const anchor = base === null ? anchorScreen : beforeBase ? base.end : base.start;
const end = range === null ? cellScreen : beforeBase ? range.screen.start : range.screen.end;
this.selectionAnchorScreen = anchor;
this.selectionEndScreen = end;
this.core.setSelection({ ...anchor, tag: 2 }, { ...end, tag: 2 });
this.options.onSelectionChange();
this.forceFullRender = true;
this.requestRender();
}
private setSelectionAutoscroll(delta: number): void {
this.selectionScrollDelta = delta;
if (delta === 0) {
if (this.selectionScrollTimer !== null) {
window.clearInterval(this.selectionScrollTimer);
this.selectionScrollTimer = null;
}
return;
}
if (this.selectionScrollTimer !== null) return;
// Dragging past the edge scrolls the viewport and keeps extending the
// selection into the newly revealed rows, like xterm's drag scroller.
this.selectionScrollTimer = window.setInterval(() => {
if (this.disposed || this.selectionScrollDelta === 0) return;
this.scrollViewport(this.selectionScrollDelta);
const pointer = this.selectionPointer;
if (pointer) this.extendSelectionTo(pointer.x, pointer.y);
}, 80);
}
private updateHoverCursor(event: PointerEvent): void {
const overLink =
isTerminalLinkPointerGesture(event) && this.linkAt(event.clientX, event.clientY) !== null;
const cursor = overLink ? "pointer" : "";
if (this.canvas.style.cursor !== cursor) this.canvas.style.cursor = cursor;
}
private readonly onPointerUp = (event: PointerEvent) => {
this.setSelectionAutoscroll(0);
if (this.linkActivationPointerId === event.pointerId) {
event.preventDefault();
event.stopPropagation();
this.linkActivationPointerId = null;
if (this.canvas.hasPointerCapture(event.pointerId)) {
this.canvas.releasePointerCapture(event.pointerId);
}
if (event.type !== "pointercancel") {
const link = this.linkAt(event.clientX, event.clientY);
if (link) this.options.onLinkActivate(link, event);
}
return;
}
if (this.mouseReportingPointerId === event.pointerId) {
event.preventDefault();
event.stopPropagation();
this.sendMouse("release", this.mouseReportingButton, event);
this.mouseReportingPointerId = null;
this.mouseReportingButton = null;
if (this.canvas.hasPointerCapture(event.pointerId)) {
this.canvas.releasePointerCapture(event.pointerId);
}
return;
}
if (this.canvas.hasPointerCapture(event.pointerId)) {
this.canvas.releasePointerCapture(event.pointerId);
}
if (event.button !== 0) return;
if (!this.selectionMoved && this.selectionMode === "cell") {
this.clearSelection();
}
this.options.onSelectionChange();
};
private readonly onWheel = (event: WheelEvent) => {
if (event.deltaY === 0) return;
event.preventDefault();
const delta = terminalWheelDeltaRows(
event,
this.metrics.height,
this.rows,
this.wheelRemainder,
);
this.wheelRemainder = delta.remainder;
if (delta.rows === 0) return;
const magnitude = Math.abs(delta.rows);
if (shouldReportTerminalMouse(this.core.isMouseTracking(), event)) {
const button = delta.rows < 0 ? 4 : 5;
for (let index = 0; index < magnitude; index += 1) {
this.sendMouse("press", button, event);
}
return;
}
if (this.core.isAlternateScreen()) {
// The alternate screen has no scrollback: translate wheel motion into
// arrow keys so full-screen apps like vim and less scroll, matching xterm.
this.options.onData(terminalWheelArrowData(delta.rows, this.core.isApplicationCursorKeys()));
return;
}
this.scrollViewport(delta.rows);
};
private readonly onMouseDown = (event: MouseEvent) => {
if (event.button === 0) event.preventDefault();
this.focus();
};
private readonly onContextMenu = (event: MouseEvent) => {
if (shouldReportTerminalMouse(this.core.isMouseTracking(), event)) {
event.preventDefault();
}
};
private readonly onScrollbarPointerDown = (event: PointerEvent) => {
if (event.button !== 0) return;
const state = this.readScrollbarState();
if (state === null) return;
const bounds = this.scrollbar.getBoundingClientRect();
const geometry = terminalScrollbarGeometry(state, bounds.height);
if (geometry === null) return;
event.preventDefault();
event.stopPropagation();
this.scrollbarPointerId = event.pointerId;
this.scrollbarPointerOffset =
event.target === this.scrollbarThumb
? event.clientY - bounds.top - geometry.thumbTop
: geometry.thumbHeight / 2;
this.scrollbar.setPointerCapture(event.pointerId);
this.scrollbarToPointer(event.clientY, bounds);
};
private readonly onScrollbarPointerMove = (event: PointerEvent) => {
if (event.pointerId !== this.scrollbarPointerId || this.scrollbarState === null) return;
event.preventDefault();
this.scrollbarToPointer(event.clientY, this.scrollbar.getBoundingClientRect());
};
private readonly onScrollbarPointerUp = (event: PointerEvent) => {
if (event.pointerId !== this.scrollbarPointerId) return;
event.preventDefault();
this.scrollbarPointerId = null;
if (this.scrollbar.hasPointerCapture(event.pointerId)) {
this.scrollbar.releasePointerCapture(event.pointerId);
}
};
private readonly onScrollbarKeyDown = (event: KeyboardEvent) => {
const state = this.readScrollbarState();
if (state === null) return;
let delta = 0;
switch (event.key) {
case "ArrowUp":
delta = -1;
break;
case "ArrowDown":
delta = 1;
break;
case "PageUp":
delta = -Math.max(1, state.len);
break;
case "PageDown":
delta = Math.max(1, state.len);
break;
case "Home":
delta = -state.offset;
break;
case "End":
delta = state.total - state.len - state.offset;
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
this.scrollViewport(delta);
};
private installEvents(): void {
this.input.addEventListener("keydown", this.onKeyDown);
this.input.addEventListener("keyup", this.onKeyUp);
this.input.addEventListener("focus", this.onFocus);
this.input.addEventListener("blur", this.onBlur);
this.input.addEventListener("input", this.onInput);
this.input.addEventListener("paste", this.onPaste);
this.input.addEventListener("compositionstart", this.onCompositionStart);
this.input.addEventListener("compositionend", this.onCompositionEnd);
this.canvas.addEventListener("pointerdown", this.onPointerDown);
this.canvas.addEventListener("pointermove", this.onPointerMove);
this.canvas.addEventListener("pointerup", this.onPointerUp);
this.canvas.addEventListener("pointercancel", this.onPointerUp);
this.canvas.addEventListener("wheel", this.onWheel, { passive: false });
this.canvas.addEventListener("mousedown", this.onMouseDown);
this.canvas.addEventListener("contextmenu", this.onContextMenu);
this.scrollbar.addEventListener("pointerdown", this.onScrollbarPointerDown);
this.scrollbar.addEventListener("pointermove", this.onScrollbarPointerMove);
this.scrollbar.addEventListener("pointerup", this.onScrollbarPointerUp);
this.scrollbar.addEventListener("pointercancel", this.onScrollbarPointerUp);
this.scrollbar.addEventListener("keydown", this.onScrollbarKeyDown);
}
private removeEvents(): void {
this.input.removeEventListener("keydown", this.onKeyDown);
this.input.removeEventListener("keyup", this.onKeyUp);
this.input.removeEventListener("focus", this.onFocus);
this.input.removeEventListener("blur", this.onBlur);
this.input.removeEventListener("input", this.onInput);
this.input.removeEventListener("paste", this.onPaste);
this.input.removeEventListener("compositionstart", this.onCompositionStart);
this.input.removeEventListener("compositionend", this.onCompositionEnd);
this.canvas.removeEventListener("pointerdown", this.onPointerDown);
this.canvas.removeEventListener("pointermove", this.onPointerMove);
this.canvas.removeEventListener("pointerup", this.onPointerUp);
this.canvas.removeEventListener("pointercancel", this.onPointerUp);
this.canvas.removeEventListener("wheel", this.onWheel);
this.canvas.removeEventListener("mousedown", this.onMouseDown);
this.canvas.removeEventListener("contextmenu", this.onContextMenu);
this.scrollbar.removeEventListener("pointerdown", this.onScrollbarPointerDown);
this.scrollbar.removeEventListener("pointermove", this.onScrollbarPointerMove);
this.scrollbar.removeEventListener("pointerup", this.onScrollbarPointerUp);
this.scrollbar.removeEventListener("pointercancel", this.onScrollbarPointerUp);
this.scrollbar.removeEventListener("keydown", this.onScrollbarKeyDown);
}
private scrollViewport(deltaRows: number): void {
let delta = Math.trunc(deltaRows);
const state = this.readScrollbarState();
if (state !== null) {
const maxOffset = Math.max(0, state.total - state.len);
const offset = Math.max(0, Math.min(state.offset + delta, maxOffset));
delta = offset - state.offset;
this.scrollbarState = { ...state, offset };
}
if (delta === 0) return;
this.core.scroll(delta);
this.forceFullRender = true;
this.scrollbarDirty = true;
this.requestRender();
}
private scrollbarToPointer(clientY: number, bounds: DOMRect): void {
const state = this.scrollbarState;
if (state === null) return;
const offset = terminalScrollbarOffsetAtPointer(
state,
bounds.height,
clientY - bounds.top,
this.scrollbarPointerOffset,
);
this.scrollViewport(offset - state.offset);
}
private updateScrollbar(): void {
const state = this.readScrollbarState();
const geometry =
state === null
? null
: terminalScrollbarGeometry(
state,
Math.max(0, this.mount.clientHeight - CONTENT_PADDING * 2),
);
this.scrollbar.hidden = geometry === null;
if (state === null || geometry === null) return;
this.scrollbar.setAttribute("aria-valuemin", "0");
this.scrollbar.setAttribute("aria-valuemax", String(geometry.maxOffset));
this.scrollbar.setAttribute(
"aria-valuenow",
String(Math.max(0, Math.min(state.offset, geometry.maxOffset))),
);
this.scrollbarThumb.style.height = `${geometry.thumbHeight}px`;
this.scrollbarThumb.style.transform = `translateY(${geometry.thumbTop}px)`;
}
private readScrollbarState(): GhosttyScrollbar | null {
const state = this.core.scrollbarState();
this.scrollbarState = state;
return state;
}
private requestRender(): void {
if (this.disposed || this.frame !== 0) return;
this.frame = window.requestAnimationFrame(() => {
this.frame = 0;
this.renderFrame();
});
}
private renderFrame(): void {
if (this.disposed) return;
if (this.frame !== 0) {
window.cancelAnimationFrame(this.frame);
this.frame = 0;
}
this.snapshot = this.core.snapshot();
if (!this.snapshot.cursorBlinking) this.cursorOn = true;
// The origin only moves together with a forced full repaint: partial
// dirty-row redraws must never composite rows at a shifted origin over
// rows painted at the previous one. Bottom anchoring starts once
// scrollback exists, i.e. when the prompt actually lives at the bottom.
const scrollState = this.readScrollbarState();
const anchorBottom = scrollState !== null && scrollState.total > scrollState.len;
const nextOriginY = terminalContentOriginY(
this.mountHeight,
CONTENT_PADDING,
this.rows,
this.metrics.height,
anchorBottom,
);
if (nextOriginY !== this.originY) {
this.originY = nextOriginY;
this.forceFullRender = true;
}
renderGhosttySnapshot({
context: this.context,
snapshot: this.snapshot,
metrics: this.metrics,
fontSize: this.fontSize,
fontFamily: this.fontFamily,
padding: CONTENT_PADDING,
originY: this.originY,
forceFull: this.forceFullRender,
cursorOn: this.cursorOn,
previousCursorY: this.renderedCursorY,
focused: this.focused,
...(this.theme.selectionBackground !== undefined
? { selectionBackground: this.theme.selectionBackground }
: {}),
});
this.positionInput();
this.renderedCursorY =
this.cursorOn && this.snapshot.cursorVisible && this.snapshot.cursorY >= 0
? this.snapshot.cursorY
: null;
if (this.scrollbarDirty) {
this.scrollbarDirty = false;
this.updateScrollbar();
}
this.forceFullRender = false;
this.scheduleCursorBlink();
}
private scheduleCursorBlink(): void {
if (this.cursorTimer !== null) window.clearTimeout(this.cursorTimer);
this.cursorTimer = null;
// An unfocused surface shows a steady hollow cursor instead of blinking.
if (!this.focused || !this.snapshot?.cursorBlinking || !this.snapshot.cursorVisible) return;
this.cursorTimer = window.setTimeout(() => {
this.cursorTimer = null;
this.cursorOn = !this.cursorOn;
this.requestRender();
}, 500);
}
private positionInput(): void {
const snapshot = this.snapshot;
if (!snapshot || !snapshot.cursorVisible || snapshot.cursorX < 0 || snapshot.cursorY < 0) {
return;
}
// The IME candidate window anchors to the textarea, so it must follow the
// terminal cursor for composition to appear where the user is typing.
const left = CONTENT_PADDING + snapshot.cursorX * this.metrics.width;
const top = this.originY + snapshot.cursorY * this.metrics.height;
if (left === this.inputLeft && top === this.inputTop) return;
this.inputLeft = left;
this.inputTop = top;
this.input.style.left = `${left}px`;
this.input.style.top = `${top}px`;
this.input.style.height = `${this.metrics.height}px`;
}
private cellAt(clientX: number, clientY: number): { x: number; y: number } {
const bounds = this.canvas.getBoundingClientRect();
return {
x: Math.max(
0,
Math.min(
this.cols - 1,
Math.floor((clientX - bounds.left - CONTENT_PADDING) / this.metrics.width),
),
),
y: Math.max(
0,
Math.min(
this.rows - 1,
Math.floor((clientY - bounds.top - this.originY) / this.metrics.height),
),
),
};
}
private linkAt(clientX: number, clientY: number): string | null {
if (!this.snapshot) return null;
const cell = this.cellAt(clientX, clientY);
const explicitHyperlink = this.core.hyperlinkAt(cell.x, cell.y);
if (explicitHyperlink) return explicitHyperlink;
return terminalLinkAtPosition(this.snapshot.rowData, cell.y, cell.x);
}
private sendMouse(
action: "press" | "release" | "motion",
button: number | null,
event: MouseEvent,
): void {
const bounds = this.canvas.getBoundingClientRect();
const data = this.core.encodeMouse({
action,
button,
mods:
(event.shiftKey ? 1 : 0) |
(event.ctrlKey ? 1 << 1 : 0) |
(event.altKey ? 1 << 2 : 0) |
(event.metaKey ? 1 << 3 : 0),
x: Math.max(0, event.clientX - bounds.left),
y: Math.max(0, event.clientY - bounds.top),
screenWidth: bounds.width,
screenHeight: bounds.height,
cellWidth: this.metrics.width,
cellHeight: this.metrics.height,
paddingLeft: CONTENT_PADDING,
paddingRight: CONTENT_PADDING,
paddingTop: this.originY,
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);
}
private buttonFromButtons(buttons: number): number | null {
if ((buttons & 1) !== 0) return 1;
if ((buttons & 4) !== 0) return 3;
if ((buttons & 2) !== 0) return 2;
if ((buttons & 8) !== 0) return 4;
if ((buttons & 16) !== 0) return 5;
return null;
}
}