@aurigax-ai/ghostty-web 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/GHOSTTY_COMMIT +1 -0
- package/LICENSE +22 -0
- package/README.md +90 -2
- package/ghostty-vt.wasm +0 -0
- package/lib/abi.ts +227 -0
- package/lib/addons/fit.ts +242 -0
- package/lib/buffer.ts +402 -0
- package/lib/event-emitter.ts +27 -0
- package/lib/ghostty.ts +1135 -0
- package/lib/index.ts +110 -0
- package/lib/input-handler.ts +1104 -0
- package/lib/interfaces.ts +218 -0
- package/lib/link-detector.ts +215 -0
- package/lib/marker.ts +55 -0
- package/lib/providers/osc8-link-provider.ts +144 -0
- package/lib/providers/url-regex-provider.ts +150 -0
- package/lib/renderer.ts +958 -0
- package/lib/selection-manager.ts +1052 -0
- package/lib/terminal.ts +2026 -0
- package/lib/types.ts +648 -0
- package/package.json +70 -4
package/lib/renderer.ts
ADDED
|
@@ -0,0 +1,958 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas Renderer for Terminal Display
|
|
3
|
+
*
|
|
4
|
+
* High-performance canvas-based renderer that draws the terminal using
|
|
5
|
+
* Ghostty's WASM terminal emulator. Features:
|
|
6
|
+
* - Font metrics measurement with DPI scaling
|
|
7
|
+
* - Full color support (256-color palette + RGB)
|
|
8
|
+
* - All text styles (bold, italic, underline, strikethrough, etc.)
|
|
9
|
+
* - Multiple cursor styles (block, underline, bar)
|
|
10
|
+
* - Dirty line optimization for 60 FPS
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { ITheme } from './interfaces';
|
|
14
|
+
import type { SelectionManager } from './selection-manager';
|
|
15
|
+
import type { GhosttyCell } from './types';
|
|
16
|
+
import { CellFlags } from './types';
|
|
17
|
+
|
|
18
|
+
// Interface for objects that can be rendered
|
|
19
|
+
export interface IRenderable {
|
|
20
|
+
getLine(y: number): GhosttyCell[] | null;
|
|
21
|
+
getCursor(): { x: number; y: number; visible: boolean };
|
|
22
|
+
getDimensions(): { cols: number; rows: number };
|
|
23
|
+
isRowDirty(y: number): boolean;
|
|
24
|
+
/** Returns true if a full redraw is needed (e.g., screen change) */
|
|
25
|
+
needsFullRedraw?(): boolean;
|
|
26
|
+
clearDirty(): void;
|
|
27
|
+
/**
|
|
28
|
+
* Get the full grapheme string for a cell at (row, col).
|
|
29
|
+
* For cells with grapheme_len > 0, this returns all codepoints combined.
|
|
30
|
+
* For simple cells, returns the single character.
|
|
31
|
+
*/
|
|
32
|
+
getGraphemeString?(row: number, col: number): string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
export interface IScrollbackProvider {
|
|
36
|
+
getScrollbackLine(offset: number): GhosttyCell[] | null;
|
|
37
|
+
getScrollbackLength(): number;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// ============================================================================
|
|
41
|
+
// Type Definitions
|
|
42
|
+
// ============================================================================
|
|
43
|
+
|
|
44
|
+
export interface RendererOptions {
|
|
45
|
+
fontSize?: number; // Default: 15
|
|
46
|
+
fontFamily?: string; // Default: 'monospace'
|
|
47
|
+
cursorStyle?: 'block' | 'underline' | 'bar'; // Default: 'block'
|
|
48
|
+
cursorBlink?: boolean; // Default: false
|
|
49
|
+
theme?: ITheme;
|
|
50
|
+
devicePixelRatio?: number; // Default: window.devicePixelRatio
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export interface FontMetrics {
|
|
54
|
+
width: number; // Character cell width in CSS pixels
|
|
55
|
+
height: number; // Character cell height in CSS pixels
|
|
56
|
+
baseline: number; // Distance from top to text baseline
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// ============================================================================
|
|
60
|
+
// Default Theme
|
|
61
|
+
// ============================================================================
|
|
62
|
+
|
|
63
|
+
export const DEFAULT_THEME: Required<ITheme> = {
|
|
64
|
+
foreground: '#d4d4d4',
|
|
65
|
+
background: '#1e1e1e',
|
|
66
|
+
cursor: '#ffffff',
|
|
67
|
+
cursorAccent: '#1e1e1e',
|
|
68
|
+
// Selection colors: solid colors that replace cell bg/fg when selected
|
|
69
|
+
// Using Ghostty's approach: selection bg = default fg, selection fg = default bg
|
|
70
|
+
selectionBackground: '#d4d4d4',
|
|
71
|
+
selectionForeground: '#1e1e1e',
|
|
72
|
+
black: '#000000',
|
|
73
|
+
red: '#cd3131',
|
|
74
|
+
green: '#0dbc79',
|
|
75
|
+
yellow: '#e5e510',
|
|
76
|
+
blue: '#2472c8',
|
|
77
|
+
magenta: '#bc3fbc',
|
|
78
|
+
cyan: '#11a8cd',
|
|
79
|
+
white: '#e5e5e5',
|
|
80
|
+
brightBlack: '#666666',
|
|
81
|
+
brightRed: '#f14c4c',
|
|
82
|
+
brightGreen: '#23d18b',
|
|
83
|
+
brightYellow: '#f5f543',
|
|
84
|
+
brightBlue: '#3b8eea',
|
|
85
|
+
brightMagenta: '#d670d6',
|
|
86
|
+
brightCyan: '#29b8db',
|
|
87
|
+
brightWhite: '#ffffff',
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
// ============================================================================
|
|
91
|
+
// CanvasRenderer Class
|
|
92
|
+
// ============================================================================
|
|
93
|
+
|
|
94
|
+
export class CanvasRenderer {
|
|
95
|
+
private canvas: HTMLCanvasElement;
|
|
96
|
+
private ctx: CanvasRenderingContext2D;
|
|
97
|
+
private fontSize: number;
|
|
98
|
+
private fontFamily: string;
|
|
99
|
+
private cursorStyle: 'block' | 'underline' | 'bar';
|
|
100
|
+
private cursorBlink: boolean;
|
|
101
|
+
private theme: Required<ITheme>;
|
|
102
|
+
private devicePixelRatio: number;
|
|
103
|
+
private metrics: FontMetrics;
|
|
104
|
+
|
|
105
|
+
// Cursor blinking state
|
|
106
|
+
private cursorVisible: boolean = true;
|
|
107
|
+
private cursorBlinkInterval?: number;
|
|
108
|
+
|
|
109
|
+
/** Called when the renderer needs another frame (cursor blink), so an idle render loop wakes. */
|
|
110
|
+
public onNeedsFrame?: () => void;
|
|
111
|
+
private lastCursorPosition: { x: number; y: number } = { x: 0, y: 0 };
|
|
112
|
+
|
|
113
|
+
// Viewport tracking (for scrolling)
|
|
114
|
+
private lastViewportY: number = 0;
|
|
115
|
+
|
|
116
|
+
// Current buffer being rendered (for grapheme lookups)
|
|
117
|
+
private currentBuffer: IRenderable | null = null;
|
|
118
|
+
|
|
119
|
+
// Selection manager (for rendering selection)
|
|
120
|
+
private selectionManager?: SelectionManager;
|
|
121
|
+
// Cached selection coordinates for current render pass (viewport-relative)
|
|
122
|
+
private currentSelectionCoords: {
|
|
123
|
+
startCol: number;
|
|
124
|
+
startRow: number;
|
|
125
|
+
endCol: number;
|
|
126
|
+
endRow: number;
|
|
127
|
+
} | null = null;
|
|
128
|
+
|
|
129
|
+
// Link rendering state
|
|
130
|
+
private hoveredHyperlinkId: number = 0;
|
|
131
|
+
private previousHoveredHyperlinkId: number = 0;
|
|
132
|
+
|
|
133
|
+
// Regex link hover tracking (for links without hyperlink_id)
|
|
134
|
+
private hoveredLinkRange: { startX: number; startY: number; endX: number; endY: number } | null =
|
|
135
|
+
null;
|
|
136
|
+
private previousHoveredLinkRange: {
|
|
137
|
+
startX: number;
|
|
138
|
+
startY: number;
|
|
139
|
+
endX: number;
|
|
140
|
+
endY: number;
|
|
141
|
+
} | null = null;
|
|
142
|
+
|
|
143
|
+
constructor(canvas: HTMLCanvasElement, options: RendererOptions = {}) {
|
|
144
|
+
this.canvas = canvas;
|
|
145
|
+
const ctx = canvas.getContext('2d', { alpha: true });
|
|
146
|
+
if (!ctx) {
|
|
147
|
+
throw new Error('Failed to get 2D rendering context');
|
|
148
|
+
}
|
|
149
|
+
this.ctx = ctx;
|
|
150
|
+
|
|
151
|
+
// Apply options
|
|
152
|
+
this.fontSize = options.fontSize ?? 15;
|
|
153
|
+
this.fontFamily = options.fontFamily ?? 'monospace';
|
|
154
|
+
this.cursorStyle = options.cursorStyle ?? 'block';
|
|
155
|
+
this.cursorBlink = options.cursorBlink ?? false;
|
|
156
|
+
this.theme = { ...DEFAULT_THEME, ...options.theme };
|
|
157
|
+
this.devicePixelRatio = options.devicePixelRatio ?? window.devicePixelRatio ?? 1;
|
|
158
|
+
|
|
159
|
+
// Measure font metrics
|
|
160
|
+
this.metrics = this.measureFont();
|
|
161
|
+
|
|
162
|
+
// Setup cursor blinking if enabled
|
|
163
|
+
if (this.cursorBlink) {
|
|
164
|
+
this.startCursorBlink();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
// ==========================================================================
|
|
169
|
+
// Font Metrics Measurement
|
|
170
|
+
// ==========================================================================
|
|
171
|
+
|
|
172
|
+
private measureFont(): FontMetrics {
|
|
173
|
+
// Use an offscreen canvas for measurement
|
|
174
|
+
const canvas = document.createElement('canvas');
|
|
175
|
+
const ctx = canvas.getContext('2d')!;
|
|
176
|
+
|
|
177
|
+
// Set font (use actual pixel size for accurate measurement)
|
|
178
|
+
ctx.font = `${this.fontSize}px ${this.fontFamily}`;
|
|
179
|
+
|
|
180
|
+
// Measure width using 'M' (typically widest character)
|
|
181
|
+
const widthMetrics = ctx.measureText('M');
|
|
182
|
+
const width = Math.ceil(widthMetrics.width);
|
|
183
|
+
|
|
184
|
+
// Measure height using ascent + descent with padding for glyph overflow
|
|
185
|
+
const ascent = widthMetrics.actualBoundingBoxAscent || this.fontSize * 0.8;
|
|
186
|
+
const descent = widthMetrics.actualBoundingBoxDescent || this.fontSize * 0.2;
|
|
187
|
+
|
|
188
|
+
// Add 2px padding to height to account for glyphs that overflow (like 'f', 'd', 'g', 'p')
|
|
189
|
+
// and anti-aliasing pixels
|
|
190
|
+
const height = Math.ceil(ascent + descent) + 2;
|
|
191
|
+
const baseline = Math.ceil(ascent) + 1; // Offset baseline by half the padding
|
|
192
|
+
|
|
193
|
+
return { width, height, baseline };
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Remeasure font metrics (call after font loads or changes)
|
|
198
|
+
*/
|
|
199
|
+
public remeasureFont(): void {
|
|
200
|
+
this.metrics = this.measureFont();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// ==========================================================================
|
|
204
|
+
// Color Conversion
|
|
205
|
+
// ==========================================================================
|
|
206
|
+
|
|
207
|
+
private rgbToCSS(r: number, g: number, b: number): string {
|
|
208
|
+
return `rgb(${r}, ${g}, ${b})`;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
// ==========================================================================
|
|
212
|
+
// Canvas Sizing
|
|
213
|
+
// ==========================================================================
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* Resize canvas to fit terminal dimensions
|
|
217
|
+
*/
|
|
218
|
+
public resize(cols: number, rows: number): void {
|
|
219
|
+
const cssWidth = cols * this.metrics.width;
|
|
220
|
+
const cssHeight = rows * this.metrics.height;
|
|
221
|
+
|
|
222
|
+
// Set CSS size (what user sees)
|
|
223
|
+
this.canvas.style.width = `${cssWidth}px`;
|
|
224
|
+
this.canvas.style.height = `${cssHeight}px`;
|
|
225
|
+
|
|
226
|
+
// Set actual canvas size (scaled for DPI)
|
|
227
|
+
this.canvas.width = cssWidth * this.devicePixelRatio;
|
|
228
|
+
this.canvas.height = cssHeight * this.devicePixelRatio;
|
|
229
|
+
|
|
230
|
+
// Scale context to match DPI (setting canvas.width/height resets the context)
|
|
231
|
+
this.ctx.scale(this.devicePixelRatio, this.devicePixelRatio);
|
|
232
|
+
|
|
233
|
+
// Set text rendering properties for crisp text
|
|
234
|
+
this.ctx.textBaseline = 'alphabetic';
|
|
235
|
+
this.ctx.textAlign = 'left';
|
|
236
|
+
|
|
237
|
+
// Fill background after resize
|
|
238
|
+
this.ctx.fillStyle = this.theme.background;
|
|
239
|
+
this.ctx.fillRect(0, 0, cssWidth, cssHeight);
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// ==========================================================================
|
|
243
|
+
// Main Rendering
|
|
244
|
+
// ==========================================================================
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Render the terminal buffer to canvas
|
|
248
|
+
*/
|
|
249
|
+
public render(
|
|
250
|
+
buffer: IRenderable,
|
|
251
|
+
forceAll: boolean = false,
|
|
252
|
+
viewportY: number = 0,
|
|
253
|
+
scrollbackProvider?: IScrollbackProvider,
|
|
254
|
+
scrollbarOpacity: number = 1
|
|
255
|
+
): void {
|
|
256
|
+
// Store buffer reference for grapheme lookups in renderCell
|
|
257
|
+
this.currentBuffer = buffer;
|
|
258
|
+
|
|
259
|
+
// getCursor() calls update() internally to ensure fresh state.
|
|
260
|
+
// Multiple update() calls are safe - dirty state persists until clearDirty().
|
|
261
|
+
const cursor = buffer.getCursor();
|
|
262
|
+
const dims = buffer.getDimensions();
|
|
263
|
+
const scrollbackLength = scrollbackProvider ? scrollbackProvider.getScrollbackLength() : 0;
|
|
264
|
+
|
|
265
|
+
// Check if buffer needs full redraw (e.g., screen change between normal/alternate)
|
|
266
|
+
if (buffer.needsFullRedraw?.()) {
|
|
267
|
+
forceAll = true;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
// Resize canvas if dimensions changed
|
|
271
|
+
const needsResize =
|
|
272
|
+
this.canvas.width !== dims.cols * this.metrics.width * this.devicePixelRatio ||
|
|
273
|
+
this.canvas.height !== dims.rows * this.metrics.height * this.devicePixelRatio;
|
|
274
|
+
|
|
275
|
+
if (needsResize) {
|
|
276
|
+
this.resize(dims.cols, dims.rows);
|
|
277
|
+
forceAll = true; // Force full render after resize
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
// Force re-render when viewport changes (scrolling)
|
|
281
|
+
if (viewportY !== this.lastViewportY) {
|
|
282
|
+
forceAll = true;
|
|
283
|
+
this.lastViewportY = viewportY;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
// Check if cursor position changed or if blinking (need to redraw cursor line)
|
|
287
|
+
const cursorMoved =
|
|
288
|
+
cursor.x !== this.lastCursorPosition.x || cursor.y !== this.lastCursorPosition.y;
|
|
289
|
+
if (cursorMoved || this.cursorBlink) {
|
|
290
|
+
// Mark cursor lines as needing redraw
|
|
291
|
+
if (!forceAll && !buffer.isRowDirty(cursor.y)) {
|
|
292
|
+
// Need to redraw cursor line
|
|
293
|
+
const line = buffer.getLine(cursor.y);
|
|
294
|
+
if (line) {
|
|
295
|
+
this.renderLine(line, cursor.y, dims.cols);
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
if (cursorMoved && this.lastCursorPosition.y !== cursor.y) {
|
|
299
|
+
// Also redraw old cursor line if cursor moved to different line
|
|
300
|
+
if (!forceAll && !buffer.isRowDirty(this.lastCursorPosition.y)) {
|
|
301
|
+
const line = buffer.getLine(this.lastCursorPosition.y);
|
|
302
|
+
if (line) {
|
|
303
|
+
this.renderLine(line, this.lastCursorPosition.y, dims.cols);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
// Check if we need to redraw selection-related lines
|
|
310
|
+
const hasSelection = this.selectionManager && this.selectionManager.hasSelection();
|
|
311
|
+
const selectionRows = new Set<number>();
|
|
312
|
+
|
|
313
|
+
// Cache selection coordinates for use during cell rendering
|
|
314
|
+
// This is used by isInSelection() to determine if a cell needs selection colors
|
|
315
|
+
this.currentSelectionCoords = hasSelection ? this.selectionManager!.getSelectionCoords() : null;
|
|
316
|
+
|
|
317
|
+
// Mark current selection rows for redraw (includes programmatic selections)
|
|
318
|
+
if (this.currentSelectionCoords) {
|
|
319
|
+
const coords = this.currentSelectionCoords;
|
|
320
|
+
for (let row = coords.startRow; row <= coords.endRow; row++) {
|
|
321
|
+
selectionRows.add(row);
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
// Always mark dirty selection rows for redraw (to clear old overlay)
|
|
326
|
+
if (this.selectionManager) {
|
|
327
|
+
const dirtyRows = this.selectionManager.getDirtySelectionRows();
|
|
328
|
+
if (dirtyRows.size > 0) {
|
|
329
|
+
for (const row of dirtyRows) {
|
|
330
|
+
selectionRows.add(row);
|
|
331
|
+
}
|
|
332
|
+
// Clear the dirty rows tracking after marking for redraw
|
|
333
|
+
this.selectionManager.clearDirtySelectionRows();
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
// Track rows with hyperlinks that need redraw when hover changes
|
|
338
|
+
const hyperlinkRows = new Set<number>();
|
|
339
|
+
const hyperlinkChanged = this.hoveredHyperlinkId !== this.previousHoveredHyperlinkId;
|
|
340
|
+
const linkRangeChanged =
|
|
341
|
+
JSON.stringify(this.hoveredLinkRange) !== JSON.stringify(this.previousHoveredLinkRange);
|
|
342
|
+
|
|
343
|
+
if (hyperlinkChanged) {
|
|
344
|
+
// Find rows containing the old or new hovered hyperlink
|
|
345
|
+
// Must check the correct buffer based on viewportY (scrollback vs screen)
|
|
346
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
347
|
+
let line: GhosttyCell[] | null = null;
|
|
348
|
+
|
|
349
|
+
// Same logic as rendering: fetch from scrollback or screen
|
|
350
|
+
if (viewportY > 0) {
|
|
351
|
+
if (y < viewportY && scrollbackProvider) {
|
|
352
|
+
// This row is from scrollback
|
|
353
|
+
// Floor viewportY for array access (handles fractional values during smooth scroll)
|
|
354
|
+
const scrollbackOffset = scrollbackLength - Math.floor(viewportY) + y;
|
|
355
|
+
line = scrollbackProvider.getScrollbackLine(scrollbackOffset);
|
|
356
|
+
} else {
|
|
357
|
+
// This row is from visible screen
|
|
358
|
+
const screenRow = y - Math.floor(viewportY);
|
|
359
|
+
line = buffer.getLine(screenRow);
|
|
360
|
+
}
|
|
361
|
+
} else {
|
|
362
|
+
// At bottom - fetch from visible screen
|
|
363
|
+
line = buffer.getLine(y);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
if (line) {
|
|
367
|
+
for (const cell of line) {
|
|
368
|
+
if (
|
|
369
|
+
cell.hyperlink_id === this.hoveredHyperlinkId ||
|
|
370
|
+
cell.hyperlink_id === this.previousHoveredHyperlinkId
|
|
371
|
+
) {
|
|
372
|
+
hyperlinkRows.add(y);
|
|
373
|
+
break; // Found hyperlink in this row
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
// Update previous state
|
|
379
|
+
this.previousHoveredHyperlinkId = this.hoveredHyperlinkId;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
// Track rows affected by link range changes (for regex URLs)
|
|
383
|
+
if (linkRangeChanged) {
|
|
384
|
+
// Add rows from old range
|
|
385
|
+
if (this.previousHoveredLinkRange) {
|
|
386
|
+
for (
|
|
387
|
+
let y = this.previousHoveredLinkRange.startY;
|
|
388
|
+
y <= this.previousHoveredLinkRange.endY;
|
|
389
|
+
y++
|
|
390
|
+
) {
|
|
391
|
+
hyperlinkRows.add(y);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
// Add rows from new range
|
|
395
|
+
if (this.hoveredLinkRange) {
|
|
396
|
+
for (let y = this.hoveredLinkRange.startY; y <= this.hoveredLinkRange.endY; y++) {
|
|
397
|
+
hyperlinkRows.add(y);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
this.previousHoveredLinkRange = this.hoveredLinkRange;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Determine which rows need rendering.
|
|
404
|
+
// We also include adjacent rows (above and below) for each dirty row to handle
|
|
405
|
+
// glyph overflow - tall glyphs like Devanagari vowel signs can extend into
|
|
406
|
+
// adjacent rows' visual space.
|
|
407
|
+
const rowsToRender = new Set<number>();
|
|
408
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
409
|
+
// When scrolled, always force render all lines since we're showing scrollback
|
|
410
|
+
const needsRender =
|
|
411
|
+
viewportY > 0
|
|
412
|
+
? true
|
|
413
|
+
: forceAll || buffer.isRowDirty(y) || selectionRows.has(y) || hyperlinkRows.has(y);
|
|
414
|
+
|
|
415
|
+
if (needsRender) {
|
|
416
|
+
rowsToRender.add(y);
|
|
417
|
+
// Include adjacent rows to handle glyph overflow
|
|
418
|
+
if (y > 0) rowsToRender.add(y - 1);
|
|
419
|
+
if (y < dims.rows - 1) rowsToRender.add(y + 1);
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
// Render each line
|
|
424
|
+
for (let y = 0; y < dims.rows; y++) {
|
|
425
|
+
if (!rowsToRender.has(y)) {
|
|
426
|
+
continue;
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
// Fetch line from scrollback or visible screen
|
|
430
|
+
let line: GhosttyCell[] | null = null;
|
|
431
|
+
if (viewportY > 0) {
|
|
432
|
+
// Scrolled up - need to fetch from scrollback + visible screen
|
|
433
|
+
// When scrolled up N lines, we want to show:
|
|
434
|
+
// - Scrollback lines (from the end) + visible screen lines
|
|
435
|
+
|
|
436
|
+
// Check if this row should come from scrollback or visible screen
|
|
437
|
+
if (y < viewportY && scrollbackProvider) {
|
|
438
|
+
// This row is from scrollback (upper part of viewport)
|
|
439
|
+
// Get from end of scrollback buffer
|
|
440
|
+
// Floor viewportY for array access (handles fractional values during smooth scroll)
|
|
441
|
+
const scrollbackOffset = scrollbackLength - Math.floor(viewportY) + y;
|
|
442
|
+
line = scrollbackProvider.getScrollbackLine(scrollbackOffset);
|
|
443
|
+
} else {
|
|
444
|
+
// This row is from visible screen (lower part of viewport)
|
|
445
|
+
const screenRow = viewportY > 0 ? y - Math.floor(viewportY) : y;
|
|
446
|
+
line = buffer.getLine(screenRow);
|
|
447
|
+
}
|
|
448
|
+
} else {
|
|
449
|
+
// At bottom - fetch from visible screen
|
|
450
|
+
line = buffer.getLine(y);
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
if (line) {
|
|
454
|
+
this.renderLine(line, y, dims.cols);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
// Selection highlighting is now integrated into renderCellBackground/renderCellText
|
|
459
|
+
// No separate overlay pass needed - this fixes z-order issues with complex glyphs
|
|
460
|
+
|
|
461
|
+
// Link underlines are drawn during cell rendering (see renderCell)
|
|
462
|
+
|
|
463
|
+
// Render cursor (only if we're at the bottom, not scrolled)
|
|
464
|
+
if (viewportY === 0 && cursor.visible && this.cursorVisible) {
|
|
465
|
+
this.renderCursor(cursor.x, cursor.y);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// Render scrollbar if scrolled or scrollback exists (with opacity for fade effect)
|
|
469
|
+
if (scrollbackProvider && scrollbarOpacity > 0) {
|
|
470
|
+
this.renderScrollbar(viewportY, scrollbackLength, dims.rows, scrollbarOpacity);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Update last cursor position
|
|
474
|
+
this.lastCursorPosition = { x: cursor.x, y: cursor.y };
|
|
475
|
+
|
|
476
|
+
// ALWAYS clear dirty flags after rendering, regardless of forceAll.
|
|
477
|
+
// This is critical - if we don't clear after a full redraw, the dirty
|
|
478
|
+
// state persists and the next frame might not detect new changes properly.
|
|
479
|
+
buffer.clearDirty();
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/**
|
|
483
|
+
* Render a single line using two-pass approach:
|
|
484
|
+
* 1. First pass: Draw all cell backgrounds
|
|
485
|
+
* 2. Second pass: Draw all cell text and decorations
|
|
486
|
+
*
|
|
487
|
+
* This two-pass approach is necessary for proper rendering of complex scripts
|
|
488
|
+
* like Devanagari where diacritics (like vowel sign ि) can extend LEFT of the
|
|
489
|
+
* base character into the previous cell's visual area. If we draw backgrounds
|
|
490
|
+
* and text in a single pass (cell by cell), the background of cell N would
|
|
491
|
+
* cover any left-extending portions of graphemes from cell N-1.
|
|
492
|
+
*/
|
|
493
|
+
private renderLine(line: GhosttyCell[], y: number, cols: number): void {
|
|
494
|
+
const lineY = y * this.metrics.height;
|
|
495
|
+
const lineWidth = cols * this.metrics.width;
|
|
496
|
+
|
|
497
|
+
// Clear line background then fill with theme color.
|
|
498
|
+
// We clear just the cell area - glyph overflow is handled by also
|
|
499
|
+
// redrawing adjacent rows (see render() method).
|
|
500
|
+
// clearRect is needed because fillRect composites rather than replaces,
|
|
501
|
+
// so transparent/translucent backgrounds wouldn't clear previous content.
|
|
502
|
+
this.ctx.clearRect(0, lineY, lineWidth, this.metrics.height);
|
|
503
|
+
this.ctx.fillStyle = this.theme.background;
|
|
504
|
+
this.ctx.fillRect(0, lineY, lineWidth, this.metrics.height);
|
|
505
|
+
|
|
506
|
+
// PASS 1: Draw all cell backgrounds first
|
|
507
|
+
// This ensures all backgrounds are painted before any text, allowing text
|
|
508
|
+
// to "bleed" across cell boundaries without being covered by adjacent backgrounds
|
|
509
|
+
for (let x = 0; x < line.length; x++) {
|
|
510
|
+
const cell = line[x];
|
|
511
|
+
if (cell.width === 0) continue; // Skip spacer cells for wide characters
|
|
512
|
+
this.renderCellBackground(cell, x, y);
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
// PASS 2: Draw all cell text and decorations
|
|
516
|
+
// Now text can safely extend beyond cell boundaries (for complex scripts)
|
|
517
|
+
for (let x = 0; x < line.length; x++) {
|
|
518
|
+
const cell = line[x];
|
|
519
|
+
if (cell.width === 0) continue; // Skip spacer cells for wide characters
|
|
520
|
+
this.renderCellText(cell, x, y);
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
|
|
524
|
+
/**
|
|
525
|
+
* Render a cell's background only (Pass 1 of two-pass rendering)
|
|
526
|
+
* Selection highlighting is integrated here to avoid z-order issues with
|
|
527
|
+
* complex glyphs (like Devanagari) that extend outside their cell bounds.
|
|
528
|
+
*/
|
|
529
|
+
private renderCellBackground(cell: GhosttyCell, x: number, y: number): void {
|
|
530
|
+
const cellX = x * this.metrics.width;
|
|
531
|
+
const cellY = y * this.metrics.height;
|
|
532
|
+
const cellWidth = this.metrics.width * cell.width;
|
|
533
|
+
|
|
534
|
+
// Check if this cell is selected
|
|
535
|
+
const isSelected = this.isInSelection(x, y);
|
|
536
|
+
|
|
537
|
+
if (isSelected) {
|
|
538
|
+
// Draw selection background (solid color, not overlay)
|
|
539
|
+
this.ctx.fillStyle = this.theme.selectionBackground;
|
|
540
|
+
this.ctx.fillRect(cellX, cellY, cellWidth, this.metrics.height);
|
|
541
|
+
return; // Selection background replaces cell background
|
|
542
|
+
}
|
|
543
|
+
|
|
544
|
+
// Extract background color and handle inverse
|
|
545
|
+
let bg_r = cell.bg_r,
|
|
546
|
+
bg_g = cell.bg_g,
|
|
547
|
+
bg_b = cell.bg_b;
|
|
548
|
+
|
|
549
|
+
if (cell.flags & CellFlags.INVERSE) {
|
|
550
|
+
// When inverted, background becomes foreground
|
|
551
|
+
bg_r = cell.fg_r;
|
|
552
|
+
bg_g = cell.fg_g;
|
|
553
|
+
bg_b = cell.fg_b;
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// Only draw cell background if it's different from the default (black)
|
|
557
|
+
// This lets the theme background (drawn earlier) show through for default cells
|
|
558
|
+
const isDefaultBg = bg_r === 0 && bg_g === 0 && bg_b === 0;
|
|
559
|
+
if (!isDefaultBg) {
|
|
560
|
+
this.ctx.fillStyle = this.rgbToCSS(bg_r, bg_g, bg_b);
|
|
561
|
+
this.ctx.fillRect(cellX, cellY, cellWidth, this.metrics.height);
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
/**
|
|
566
|
+
* Render a cell's text and decorations (Pass 2 of two-pass rendering)
|
|
567
|
+
* Selection foreground color is applied here to match the selection background.
|
|
568
|
+
*/
|
|
569
|
+
private renderCellText(cell: GhosttyCell, x: number, y: number, colorOverride?: string): void {
|
|
570
|
+
const cellX = x * this.metrics.width;
|
|
571
|
+
const cellY = y * this.metrics.height;
|
|
572
|
+
const cellWidth = this.metrics.width * cell.width;
|
|
573
|
+
|
|
574
|
+
// Skip rendering if invisible
|
|
575
|
+
if (cell.flags & CellFlags.INVISIBLE) {
|
|
576
|
+
return;
|
|
577
|
+
}
|
|
578
|
+
|
|
579
|
+
// Check if this cell is selected
|
|
580
|
+
const isSelected = this.isInSelection(x, y);
|
|
581
|
+
|
|
582
|
+
// Set text style
|
|
583
|
+
let fontStyle = '';
|
|
584
|
+
if (cell.flags & CellFlags.ITALIC) fontStyle += 'italic ';
|
|
585
|
+
if (cell.flags & CellFlags.BOLD) fontStyle += 'bold ';
|
|
586
|
+
this.ctx.font = `${fontStyle}${this.fontSize}px ${this.fontFamily}`;
|
|
587
|
+
|
|
588
|
+
// Set text color - use override, selection foreground, or normal color
|
|
589
|
+
if (colorOverride) {
|
|
590
|
+
this.ctx.fillStyle = colorOverride;
|
|
591
|
+
} else if (isSelected) {
|
|
592
|
+
this.ctx.fillStyle = this.theme.selectionForeground;
|
|
593
|
+
} else {
|
|
594
|
+
// Extract colors and handle inverse
|
|
595
|
+
let fg_r = cell.fg_r,
|
|
596
|
+
fg_g = cell.fg_g,
|
|
597
|
+
fg_b = cell.fg_b;
|
|
598
|
+
|
|
599
|
+
if (cell.flags & CellFlags.INVERSE) {
|
|
600
|
+
// When inverted, foreground becomes background
|
|
601
|
+
fg_r = cell.bg_r;
|
|
602
|
+
fg_g = cell.bg_g;
|
|
603
|
+
fg_b = cell.bg_b;
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
this.ctx.fillStyle = this.rgbToCSS(fg_r, fg_g, fg_b);
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// Apply faint effect
|
|
610
|
+
if (cell.flags & CellFlags.FAINT) {
|
|
611
|
+
this.ctx.globalAlpha = 0.5;
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// Draw text
|
|
615
|
+
const textX = cellX;
|
|
616
|
+
const textY = cellY + this.metrics.baseline;
|
|
617
|
+
|
|
618
|
+
// Get the character to render - use grapheme lookup for complex scripts
|
|
619
|
+
let char: string;
|
|
620
|
+
if (cell.grapheme_len > 0 && this.currentBuffer?.getGraphemeString) {
|
|
621
|
+
// Cell has additional codepoints - get full grapheme cluster
|
|
622
|
+
char = this.currentBuffer.getGraphemeString(y, x);
|
|
623
|
+
} else {
|
|
624
|
+
// Simple cell - single codepoint
|
|
625
|
+
char = String.fromCodePoint(cell.codepoint || 32); // Default to space if null
|
|
626
|
+
}
|
|
627
|
+
this.ctx.fillText(char, textX, textY);
|
|
628
|
+
|
|
629
|
+
// Reset alpha
|
|
630
|
+
if (cell.flags & CellFlags.FAINT) {
|
|
631
|
+
this.ctx.globalAlpha = 1.0;
|
|
632
|
+
}
|
|
633
|
+
|
|
634
|
+
// Draw underline
|
|
635
|
+
if (cell.flags & CellFlags.UNDERLINE) {
|
|
636
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
637
|
+
this.ctx.strokeStyle = this.ctx.fillStyle;
|
|
638
|
+
this.ctx.lineWidth = 1;
|
|
639
|
+
this.ctx.beginPath();
|
|
640
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
641
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
642
|
+
this.ctx.stroke();
|
|
643
|
+
}
|
|
644
|
+
|
|
645
|
+
// Draw strikethrough
|
|
646
|
+
if (cell.flags & CellFlags.STRIKETHROUGH) {
|
|
647
|
+
const strikeY = cellY + this.metrics.height / 2;
|
|
648
|
+
this.ctx.strokeStyle = this.ctx.fillStyle;
|
|
649
|
+
this.ctx.lineWidth = 1;
|
|
650
|
+
this.ctx.beginPath();
|
|
651
|
+
this.ctx.moveTo(cellX, strikeY);
|
|
652
|
+
this.ctx.lineTo(cellX + cellWidth, strikeY);
|
|
653
|
+
this.ctx.stroke();
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// Draw hyperlink underline (for OSC8 hyperlinks)
|
|
657
|
+
if (cell.hyperlink_id > 0) {
|
|
658
|
+
const isHovered = cell.hyperlink_id === this.hoveredHyperlinkId;
|
|
659
|
+
|
|
660
|
+
// Only show underline when hovered (cleaner look)
|
|
661
|
+
if (isHovered) {
|
|
662
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
663
|
+
this.ctx.strokeStyle = '#4A90E2'; // Blue underline on hover
|
|
664
|
+
this.ctx.lineWidth = 1;
|
|
665
|
+
this.ctx.beginPath();
|
|
666
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
667
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
668
|
+
this.ctx.stroke();
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
|
|
672
|
+
// Draw regex link underline (for plain text URLs)
|
|
673
|
+
if (this.hoveredLinkRange) {
|
|
674
|
+
const range = this.hoveredLinkRange;
|
|
675
|
+
// Check if this cell is within the hovered link range
|
|
676
|
+
const isInRange =
|
|
677
|
+
(y === range.startY && x >= range.startX && (y < range.endY || x <= range.endX)) ||
|
|
678
|
+
(y > range.startY && y < range.endY) ||
|
|
679
|
+
(y === range.endY && x <= range.endX && (y > range.startY || x >= range.startX));
|
|
680
|
+
|
|
681
|
+
if (isInRange) {
|
|
682
|
+
const underlineY = cellY + this.metrics.baseline + 2;
|
|
683
|
+
this.ctx.strokeStyle = '#4A90E2'; // Blue underline on hover
|
|
684
|
+
this.ctx.lineWidth = 1;
|
|
685
|
+
this.ctx.beginPath();
|
|
686
|
+
this.ctx.moveTo(cellX, underlineY);
|
|
687
|
+
this.ctx.lineTo(cellX + cellWidth, underlineY);
|
|
688
|
+
this.ctx.stroke();
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
/**
|
|
694
|
+
* Render cursor
|
|
695
|
+
*/
|
|
696
|
+
private renderCursor(x: number, y: number): void {
|
|
697
|
+
const cursorX = x * this.metrics.width;
|
|
698
|
+
const cursorY = y * this.metrics.height;
|
|
699
|
+
|
|
700
|
+
this.ctx.fillStyle = this.theme.cursor;
|
|
701
|
+
|
|
702
|
+
switch (this.cursorStyle) {
|
|
703
|
+
case 'block':
|
|
704
|
+
// Full cell block
|
|
705
|
+
this.ctx.fillRect(cursorX, cursorY, this.metrics.width, this.metrics.height);
|
|
706
|
+
// Re-draw character under cursor with cursorAccent color
|
|
707
|
+
{
|
|
708
|
+
const line = this.currentBuffer?.getLine(y);
|
|
709
|
+
if (line?.[x]) {
|
|
710
|
+
this.ctx.save();
|
|
711
|
+
this.ctx.beginPath();
|
|
712
|
+
this.ctx.rect(cursorX, cursorY, this.metrics.width, this.metrics.height);
|
|
713
|
+
this.ctx.clip();
|
|
714
|
+
this.renderCellText(line[x], x, y, this.theme.cursorAccent);
|
|
715
|
+
this.ctx.restore();
|
|
716
|
+
}
|
|
717
|
+
}
|
|
718
|
+
break;
|
|
719
|
+
|
|
720
|
+
case 'underline':
|
|
721
|
+
// Underline at bottom of cell
|
|
722
|
+
const underlineHeight = Math.max(2, Math.floor(this.metrics.height * 0.15));
|
|
723
|
+
this.ctx.fillRect(
|
|
724
|
+
cursorX,
|
|
725
|
+
cursorY + this.metrics.height - underlineHeight,
|
|
726
|
+
this.metrics.width,
|
|
727
|
+
underlineHeight
|
|
728
|
+
);
|
|
729
|
+
break;
|
|
730
|
+
|
|
731
|
+
case 'bar':
|
|
732
|
+
// Vertical bar at left of cell
|
|
733
|
+
const barWidth = Math.max(2, Math.floor(this.metrics.width * 0.15));
|
|
734
|
+
this.ctx.fillRect(cursorX, cursorY, barWidth, this.metrics.height);
|
|
735
|
+
break;
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
|
|
739
|
+
// ==========================================================================
|
|
740
|
+
// Cursor Blinking
|
|
741
|
+
// ==========================================================================
|
|
742
|
+
|
|
743
|
+
private startCursorBlink(): void {
|
|
744
|
+
// xterm.js uses ~530ms blink interval
|
|
745
|
+
this.cursorBlinkInterval = window.setInterval(() => {
|
|
746
|
+
this.cursorVisible = !this.cursorVisible;
|
|
747
|
+
this.onNeedsFrame?.();
|
|
748
|
+
}, 530);
|
|
749
|
+
}
|
|
750
|
+
|
|
751
|
+
private stopCursorBlink(): void {
|
|
752
|
+
if (this.cursorBlinkInterval !== undefined) {
|
|
753
|
+
clearInterval(this.cursorBlinkInterval);
|
|
754
|
+
this.cursorBlinkInterval = undefined;
|
|
755
|
+
}
|
|
756
|
+
this.cursorVisible = true;
|
|
757
|
+
}
|
|
758
|
+
|
|
759
|
+
// ==========================================================================
|
|
760
|
+
// Public API
|
|
761
|
+
// ==========================================================================
|
|
762
|
+
|
|
763
|
+
/**
|
|
764
|
+
* Update theme colors
|
|
765
|
+
*/
|
|
766
|
+
public setTheme(theme: ITheme): void {
|
|
767
|
+
this.theme = { ...DEFAULT_THEME, ...theme };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
/**
|
|
771
|
+
* Update font size
|
|
772
|
+
*/
|
|
773
|
+
public setFontSize(size: number): void {
|
|
774
|
+
this.fontSize = size;
|
|
775
|
+
this.metrics = this.measureFont();
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
/**
|
|
779
|
+
* Update font family
|
|
780
|
+
*/
|
|
781
|
+
public setFontFamily(family: string): void {
|
|
782
|
+
this.fontFamily = family;
|
|
783
|
+
this.metrics = this.measureFont();
|
|
784
|
+
}
|
|
785
|
+
|
|
786
|
+
/**
|
|
787
|
+
* Update cursor style
|
|
788
|
+
*/
|
|
789
|
+
public setCursorStyle(style: 'block' | 'underline' | 'bar'): void {
|
|
790
|
+
this.cursorStyle = style;
|
|
791
|
+
}
|
|
792
|
+
|
|
793
|
+
/**
|
|
794
|
+
* Enable/disable cursor blinking
|
|
795
|
+
*/
|
|
796
|
+
public setCursorBlink(enabled: boolean): void {
|
|
797
|
+
if (enabled && !this.cursorBlink) {
|
|
798
|
+
this.cursorBlink = true;
|
|
799
|
+
this.startCursorBlink();
|
|
800
|
+
} else if (!enabled && this.cursorBlink) {
|
|
801
|
+
this.cursorBlink = false;
|
|
802
|
+
this.stopCursorBlink();
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
/**
|
|
807
|
+
* Get current font metrics
|
|
808
|
+
*/
|
|
809
|
+
|
|
810
|
+
/**
|
|
811
|
+
* Render scrollbar (Phase 2)
|
|
812
|
+
* Shows scroll position and allows click/drag interaction
|
|
813
|
+
* @param opacity Opacity level (0-1) for fade in/out effect
|
|
814
|
+
*/
|
|
815
|
+
private renderScrollbar(
|
|
816
|
+
viewportY: number,
|
|
817
|
+
scrollbackLength: number,
|
|
818
|
+
visibleRows: number,
|
|
819
|
+
opacity: number = 1
|
|
820
|
+
): void {
|
|
821
|
+
const ctx = this.ctx;
|
|
822
|
+
const canvasHeight = this.canvas.height / this.devicePixelRatio;
|
|
823
|
+
const canvasWidth = this.canvas.width / this.devicePixelRatio;
|
|
824
|
+
|
|
825
|
+
// Scrollbar dimensions
|
|
826
|
+
const scrollbarWidth = 8;
|
|
827
|
+
const scrollbarX = canvasWidth - scrollbarWidth - 4;
|
|
828
|
+
const scrollbarPadding = 4;
|
|
829
|
+
const scrollbarTrackHeight = canvasHeight - scrollbarPadding * 2;
|
|
830
|
+
|
|
831
|
+
// Always clear the scrollbar area first (fixes ghosting when fading out)
|
|
832
|
+
ctx.clearRect(scrollbarX - 2, 0, scrollbarWidth + 6, canvasHeight);
|
|
833
|
+
ctx.fillStyle = this.theme.background;
|
|
834
|
+
ctx.fillRect(scrollbarX - 2, 0, scrollbarWidth + 6, canvasHeight);
|
|
835
|
+
|
|
836
|
+
// Don't draw scrollbar if fully transparent or no scrollback
|
|
837
|
+
if (opacity <= 0 || scrollbackLength === 0) return;
|
|
838
|
+
|
|
839
|
+
// Calculate scrollbar thumb size and position
|
|
840
|
+
const totalLines = scrollbackLength + visibleRows;
|
|
841
|
+
const thumbHeight = Math.max(20, (visibleRows / totalLines) * scrollbarTrackHeight);
|
|
842
|
+
|
|
843
|
+
// Position: 0 = at bottom, scrollbackLength = at top
|
|
844
|
+
const scrollPosition = viewportY / scrollbackLength; // 0 to 1
|
|
845
|
+
const thumbY = scrollbarPadding + (scrollbarTrackHeight - thumbHeight) * (1 - scrollPosition);
|
|
846
|
+
|
|
847
|
+
// Draw scrollbar track (subtle background) with opacity
|
|
848
|
+
ctx.fillStyle = `rgba(128, 128, 128, ${0.1 * opacity})`;
|
|
849
|
+
ctx.fillRect(scrollbarX, scrollbarPadding, scrollbarWidth, scrollbarTrackHeight);
|
|
850
|
+
|
|
851
|
+
// Draw scrollbar thumb with opacity
|
|
852
|
+
const isScrolled = viewportY > 0;
|
|
853
|
+
const baseOpacity = isScrolled ? 0.5 : 0.3;
|
|
854
|
+
ctx.fillStyle = `rgba(128, 128, 128, ${baseOpacity * opacity})`;
|
|
855
|
+
ctx.fillRect(scrollbarX, thumbY, scrollbarWidth, thumbHeight);
|
|
856
|
+
}
|
|
857
|
+
public getMetrics(): FontMetrics {
|
|
858
|
+
return { ...this.metrics };
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
/**
|
|
862
|
+
* Get canvas element (needed by SelectionManager)
|
|
863
|
+
*/
|
|
864
|
+
public getCanvas(): HTMLCanvasElement {
|
|
865
|
+
return this.canvas;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
/**
|
|
869
|
+
* Set selection manager (for rendering selection)
|
|
870
|
+
*/
|
|
871
|
+
public setSelectionManager(manager: SelectionManager): void {
|
|
872
|
+
this.selectionManager = manager;
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
/**
|
|
876
|
+
* Check if a cell at (x, y) is within the current selection.
|
|
877
|
+
* Uses cached selection coordinates for performance.
|
|
878
|
+
*/
|
|
879
|
+
private isInSelection(x: number, y: number): boolean {
|
|
880
|
+
const sel = this.currentSelectionCoords;
|
|
881
|
+
if (!sel) return false;
|
|
882
|
+
|
|
883
|
+
const { startCol, startRow, endCol, endRow } = sel;
|
|
884
|
+
|
|
885
|
+
// Single line selection
|
|
886
|
+
if (startRow === endRow) {
|
|
887
|
+
return y === startRow && x >= startCol && x <= endCol;
|
|
888
|
+
}
|
|
889
|
+
|
|
890
|
+
// Multi-line selection
|
|
891
|
+
if (y === startRow) {
|
|
892
|
+
// First line: from startCol to end of line
|
|
893
|
+
return x >= startCol;
|
|
894
|
+
} else if (y === endRow) {
|
|
895
|
+
// Last line: from start of line to endCol
|
|
896
|
+
return x <= endCol;
|
|
897
|
+
} else if (y > startRow && y < endRow) {
|
|
898
|
+
// Middle lines: entire line is selected
|
|
899
|
+
return true;
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
return false;
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
/**
|
|
906
|
+
* Set the currently hovered hyperlink ID for rendering underlines
|
|
907
|
+
*/
|
|
908
|
+
public setHoveredHyperlinkId(hyperlinkId: number): void {
|
|
909
|
+
this.hoveredHyperlinkId = hyperlinkId;
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
/**
|
|
913
|
+
* Set the currently hovered link range for rendering underlines (for regex-detected URLs)
|
|
914
|
+
* Pass null to clear the hover state
|
|
915
|
+
*/
|
|
916
|
+
public setHoveredLinkRange(
|
|
917
|
+
range: {
|
|
918
|
+
startX: number;
|
|
919
|
+
startY: number;
|
|
920
|
+
endX: number;
|
|
921
|
+
endY: number;
|
|
922
|
+
} | null
|
|
923
|
+
): void {
|
|
924
|
+
this.hoveredLinkRange = range;
|
|
925
|
+
}
|
|
926
|
+
|
|
927
|
+
/**
|
|
928
|
+
* Get character cell width (for coordinate conversion)
|
|
929
|
+
*/
|
|
930
|
+
public get charWidth(): number {
|
|
931
|
+
return this.metrics.width;
|
|
932
|
+
}
|
|
933
|
+
|
|
934
|
+
/**
|
|
935
|
+
* Get character cell height (for coordinate conversion)
|
|
936
|
+
*/
|
|
937
|
+
public get charHeight(): number {
|
|
938
|
+
return this.metrics.height;
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
/**
|
|
942
|
+
* Clear entire canvas
|
|
943
|
+
*/
|
|
944
|
+
public clear(): void {
|
|
945
|
+
// clearRect first because fillRect composites rather than replaces,
|
|
946
|
+
// so transparent/translucent backgrounds wouldn't clear previous content.
|
|
947
|
+
this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
948
|
+
this.ctx.fillStyle = this.theme.background;
|
|
949
|
+
this.ctx.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
950
|
+
}
|
|
951
|
+
|
|
952
|
+
/**
|
|
953
|
+
* Cleanup resources
|
|
954
|
+
*/
|
|
955
|
+
public dispose(): void {
|
|
956
|
+
this.stopCursorBlink();
|
|
957
|
+
}
|
|
958
|
+
}
|