termigan 0.0.0-stage → 0.0.1
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/LICENSE +21 -0
- package/README.md +304 -2
- package/assets/logo.svg +97 -0
- package/dist/termigan.wasm +0 -0
- package/docs/api.md +295 -0
- package/docs/architecture.md +102 -0
- package/package.json +62 -5
- package/types/index.d.ts +123 -0
- package/web/canvas-renderer.js +427 -0
- package/web/input-controller.js +181 -0
- package/web/pty-socket.js +125 -0
- package/web/termigan.js +870 -0
- package/web/viewport-controller.js +184 -0
- package/web/wasm-bridge.js +264 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
export const DEFAULT_THEME = {
|
|
2
|
+
background: "#0f1117",
|
|
3
|
+
foreground: "#e6edf3",
|
|
4
|
+
cursor: "#58a6ff",
|
|
5
|
+
cursorAccent: "#0f1117",
|
|
6
|
+
selection: "rgba(88, 166, 255, 0.32)",
|
|
7
|
+
scrollbarTrack: "rgba(255, 255, 255, 0.06)",
|
|
8
|
+
scrollbarThumb: "rgba(255, 255, 255, 0.28)",
|
|
9
|
+
palette: [
|
|
10
|
+
"#1f242c", "#f85149", "#3fb950", "#d29922",
|
|
11
|
+
"#58a6ff", "#bc8cff", "#39c5cf", "#b1bac4",
|
|
12
|
+
"#6e7681", "#ff7b72", "#56d364", "#e3b341",
|
|
13
|
+
"#79c0ff", "#d2a8ff", "#56d4dd", "#f0f6fc",
|
|
14
|
+
],
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
function buildAnsi256Palette(base16) {
|
|
18
|
+
const out = new Array(256);
|
|
19
|
+
for (let i = 0; i < 16; i++) {
|
|
20
|
+
out[i] = base16[i];
|
|
21
|
+
}
|
|
22
|
+
const levels = [0, 95, 135, 175, 215, 255];
|
|
23
|
+
let idx = 16;
|
|
24
|
+
for (let r = 0; r < 6; r++) {
|
|
25
|
+
for (let g = 0; g < 6; g++) {
|
|
26
|
+
for (let b = 0; b < 6; b++) {
|
|
27
|
+
out[idx++] = `rgb(${levels[r]},${levels[g]},${levels[b]})`;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
for (let i = 0; i < 24; i++) {
|
|
32
|
+
const v = 8 + i * 10;
|
|
33
|
+
out[idx++] = `rgb(${v},${v},${v})`;
|
|
34
|
+
}
|
|
35
|
+
return out;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function measureFontMetrics(fontFamily, fontSizePx) {
|
|
39
|
+
const canvas = document.createElement("canvas");
|
|
40
|
+
const ctx = canvas.getContext("2d");
|
|
41
|
+
ctx.font = `${fontSizePx}px ${fontFamily}`;
|
|
42
|
+
const sample = ctx.measureText("MMMMMMMMMM");
|
|
43
|
+
const cellWidth = Math.max(4, sample.width / 10);
|
|
44
|
+
const cellHeight = Math.max(8, Math.ceil(fontSizePx * 1.32));
|
|
45
|
+
return { fontSize: fontSizePx, cellWidth, cellHeight };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export class CanvasRenderer {
|
|
49
|
+
constructor(canvas, options = {}) {
|
|
50
|
+
this._canvas = canvas;
|
|
51
|
+
this._ctx = canvas.getContext("2d", { alpha: false });
|
|
52
|
+
this._theme = {
|
|
53
|
+
...DEFAULT_THEME,
|
|
54
|
+
...(options.theme || {}),
|
|
55
|
+
palette: (options.theme && options.theme.palette) || DEFAULT_THEME.palette,
|
|
56
|
+
};
|
|
57
|
+
this._palette256 = buildAnsi256Palette(this._theme.palette);
|
|
58
|
+
this._fontFamily = options.fontFamily || "'JetBrains Mono', 'Fira Code', ui-monospace, SFMono-Regular, Menlo, Consolas, monospace";
|
|
59
|
+
this._fontSize = options.fontSize || 13;
|
|
60
|
+
this._cellWidth = 8;
|
|
61
|
+
this._cellHeight = 18;
|
|
62
|
+
this._cols = 80;
|
|
63
|
+
this._rows = 24;
|
|
64
|
+
this._dpr = 1;
|
|
65
|
+
this._rowCache = [];
|
|
66
|
+
this._lastCursor = null;
|
|
67
|
+
this._focused = false;
|
|
68
|
+
this._selection = null;
|
|
69
|
+
this._padX = 6;
|
|
70
|
+
this._padY = 6;
|
|
71
|
+
this._scrollbarWidth = 8;
|
|
72
|
+
this._cssWidth = 0;
|
|
73
|
+
this._cssHeight = 0;
|
|
74
|
+
this._cssScale = 1;
|
|
75
|
+
this._lastFrameMeta = null;
|
|
76
|
+
this._baseFontSize = this._fontSize;
|
|
77
|
+
this._baseMetrics = this._computeMetrics(this._fontSize);
|
|
78
|
+
this.setFontSize(this._fontSize);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
_computeMetrics(fontSizePx) {
|
|
82
|
+
const m = measureFontMetrics(this._fontFamily, fontSizePx);
|
|
83
|
+
return {
|
|
84
|
+
fontSize: m.fontSize,
|
|
85
|
+
cellWidth: m.cellWidth,
|
|
86
|
+
cellHeight: m.cellHeight,
|
|
87
|
+
padX: this._padX,
|
|
88
|
+
padY: this._padY,
|
|
89
|
+
scrollbarWidth: this._scrollbarWidth,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
get baseMetrics() {
|
|
94
|
+
return this._baseMetrics;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
get metrics() {
|
|
98
|
+
return {
|
|
99
|
+
fontSize: this._fontSize,
|
|
100
|
+
cellWidth: this._cellWidth,
|
|
101
|
+
cellHeight: this._cellHeight,
|
|
102
|
+
padX: this._padX,
|
|
103
|
+
padY: this._padY,
|
|
104
|
+
scrollbarWidth: this._scrollbarWidth,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
setFontSize(fontSizePx) {
|
|
109
|
+
const next = Math.max(4, Number(fontSizePx) || this._baseFontSize);
|
|
110
|
+
if (Math.abs(next - this._fontSize) < 0.05 && this._cellWidth > 0) {
|
|
111
|
+
return false;
|
|
112
|
+
}
|
|
113
|
+
this._fontSize = next;
|
|
114
|
+
const m = measureFontMetrics(this._fontFamily, next);
|
|
115
|
+
this._cellWidth = m.cellWidth;
|
|
116
|
+
this._cellHeight = m.cellHeight;
|
|
117
|
+
return true;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
setCssScale(scale) {
|
|
121
|
+
const next = Math.max(0.05, Math.min(1, Number(scale) || 1));
|
|
122
|
+
this._cssScale = next;
|
|
123
|
+
this._canvas.style.transform = "";
|
|
124
|
+
if (this._cssWidth > 0 && this._cssHeight > 0) {
|
|
125
|
+
this._canvas.style.width = `${Math.floor(this._cssWidth * next)}px`;
|
|
126
|
+
this._canvas.style.height = `${Math.floor(this._cssHeight * next)}px`;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
setFocused(focused) {
|
|
131
|
+
this._focused = Boolean(focused);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
setSelection(selection) {
|
|
135
|
+
this._selection = selection;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
resizeCanvas(cols, rows, dpr = window.devicePixelRatio || 1) {
|
|
139
|
+
const cssW = Math.ceil(cols * this._cellWidth + this._padX * 2 + this._scrollbarWidth);
|
|
140
|
+
const cssH = Math.ceil(rows * this._cellHeight + this._padY * 2);
|
|
141
|
+
const pxW = Math.max(1, Math.round(cssW * dpr));
|
|
142
|
+
const pxH = Math.max(1, Math.round(cssH * dpr));
|
|
143
|
+
const changed =
|
|
144
|
+
this._cols !== cols ||
|
|
145
|
+
this._rows !== rows ||
|
|
146
|
+
this._dpr !== dpr ||
|
|
147
|
+
this._canvas.width !== pxW ||
|
|
148
|
+
this._canvas.height !== pxH;
|
|
149
|
+
this._cols = cols;
|
|
150
|
+
this._rows = rows;
|
|
151
|
+
this._dpr = dpr;
|
|
152
|
+
this._cssWidth = cssW;
|
|
153
|
+
this._cssHeight = cssH;
|
|
154
|
+
if (changed) {
|
|
155
|
+
this._canvas.width = pxW;
|
|
156
|
+
this._canvas.height = pxH;
|
|
157
|
+
this._canvas.style.width = `${Math.floor(cssW * this._cssScale)}px`;
|
|
158
|
+
this._canvas.style.height = `${Math.floor(cssH * this._cssScale)}px`;
|
|
159
|
+
this._rowCache = new Array(rows);
|
|
160
|
+
this._ctx.setTransform(dpr, 0, 0, dpr, 0, 0);
|
|
161
|
+
this._ctx.fillStyle = this._theme.background;
|
|
162
|
+
this._ctx.fillRect(0, 0, cssW, cssH);
|
|
163
|
+
}
|
|
164
|
+
return { cssW, cssH, changed };
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
clientToLogical(clientX, clientY) {
|
|
168
|
+
const rect = this._canvas.getBoundingClientRect();
|
|
169
|
+
const scaleX = rect.width > 0 ? this._cssWidth / rect.width : 1;
|
|
170
|
+
const scaleY = rect.height > 0 ? this._cssHeight / rect.height : 1;
|
|
171
|
+
return {
|
|
172
|
+
x: (clientX - rect.left) * scaleX,
|
|
173
|
+
y: (clientY - rect.top) * scaleY,
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
clientToCell(clientX, clientY) {
|
|
178
|
+
const pt = this.clientToLogical(clientX, clientY);
|
|
179
|
+
return this.pixelToCell(pt.x, pt.y);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
isInScrollbar(clientX, clientY) {
|
|
183
|
+
if (!this._lastFrameMeta || this._lastFrameMeta.altScreen || this._lastFrameMeta.scrollbackLen === 0) {
|
|
184
|
+
return false;
|
|
185
|
+
}
|
|
186
|
+
const pt = this.clientToLogical(clientX, clientY);
|
|
187
|
+
const trackX = this._padX * 2 + this._cols * this._cellWidth - 2;
|
|
188
|
+
return pt.x >= trackX && pt.y >= 0 && pt.y <= this._cssHeight;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
clientYToScrollOffset(clientY) {
|
|
192
|
+
if (!this._lastFrameMeta || this._lastFrameMeta.scrollbackLen === 0) {
|
|
193
|
+
return 0;
|
|
194
|
+
}
|
|
195
|
+
const pt = this.clientToLogical(0, clientY);
|
|
196
|
+
const trackY = this._padY;
|
|
197
|
+
const trackH = Math.max(1, this._rows * this._cellHeight);
|
|
198
|
+
const ratio = Math.max(0, Math.min(1, (pt.y - trackY) / trackH));
|
|
199
|
+
return Math.round((1 - ratio) * this._lastFrameMeta.scrollbackLen);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
pixelToCell(localX, localY) {
|
|
203
|
+
const col = Math.floor((localX - this._padX) / this._cellWidth);
|
|
204
|
+
const row = Math.floor((localY - this._padY) / this._cellHeight);
|
|
205
|
+
return {
|
|
206
|
+
col: Math.max(0, Math.min(this._cols - 1, col)),
|
|
207
|
+
row: Math.max(0, Math.min(this._rows - 1, row)),
|
|
208
|
+
};
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
_resolveColor(kind, r, g, b, isFg, isBold) {
|
|
212
|
+
if (kind === 0) {
|
|
213
|
+
return isFg ? this._theme.foreground : this._theme.background;
|
|
214
|
+
}
|
|
215
|
+
if (kind === 1) {
|
|
216
|
+
const idx = isFg && isBold && r < 8 ? r + 8 : r;
|
|
217
|
+
return this._palette256[idx] || this._theme.foreground;
|
|
218
|
+
}
|
|
219
|
+
return `rgb(${r},${g},${b})`;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
_isCellSelected(absRow, col) {
|
|
223
|
+
const sel = this._selection;
|
|
224
|
+
if (!sel) {
|
|
225
|
+
return false;
|
|
226
|
+
}
|
|
227
|
+
let sRow = sel.startAbsRow;
|
|
228
|
+
let sCol = sel.startCol;
|
|
229
|
+
let eRow = sel.endAbsRow;
|
|
230
|
+
let eCol = sel.endCol;
|
|
231
|
+
if (sRow > eRow || (sRow === eRow && sCol > eCol)) {
|
|
232
|
+
[sRow, sCol, eRow, eCol] = [eRow, eCol, sRow, sCol];
|
|
233
|
+
}
|
|
234
|
+
if (absRow < sRow || absRow > eRow) {
|
|
235
|
+
return false;
|
|
236
|
+
}
|
|
237
|
+
if (sRow === eRow) {
|
|
238
|
+
return col >= sCol && col <= eCol;
|
|
239
|
+
}
|
|
240
|
+
if (absRow === sRow) {
|
|
241
|
+
return col >= sCol;
|
|
242
|
+
}
|
|
243
|
+
if (absRow === eRow) {
|
|
244
|
+
return col <= eCol;
|
|
245
|
+
}
|
|
246
|
+
return true;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
_paintRow(rowIndex, rowData, meta) {
|
|
250
|
+
const ctx = this._ctx;
|
|
251
|
+
const cw = this._cellWidth;
|
|
252
|
+
const ch = this._cellHeight;
|
|
253
|
+
const y = this._padY + rowIndex * ch;
|
|
254
|
+
const totalW = this._cols * cw;
|
|
255
|
+
|
|
256
|
+
ctx.fillStyle = this._theme.background;
|
|
257
|
+
ctx.fillRect(0, y, this._padX * 2 + totalW, ch);
|
|
258
|
+
|
|
259
|
+
if (!rowData) {
|
|
260
|
+
return;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
const absRow = meta.altScreen
|
|
264
|
+
? rowIndex
|
|
265
|
+
: meta.scrollbackLen - meta.scrollOffset + rowIndex;
|
|
266
|
+
const baseline = y + Math.floor(ch * 0.78);
|
|
267
|
+
|
|
268
|
+
for (let c = 0; c < rowData.cells.length; c++) {
|
|
269
|
+
const cell = rowData.cells[c];
|
|
270
|
+
if (cell.width === 0) {
|
|
271
|
+
continue;
|
|
272
|
+
}
|
|
273
|
+
const span = cell.width === 2 ? 2 : 1;
|
|
274
|
+
const x = this._padX + c * cw;
|
|
275
|
+
const w = cw * span;
|
|
276
|
+
const bold = (cell.attrs & 1) !== 0;
|
|
277
|
+
const dim = (cell.attrs & 2) !== 0;
|
|
278
|
+
const italic = (cell.attrs & 4) !== 0;
|
|
279
|
+
const underline = (cell.attrs & 8) !== 0;
|
|
280
|
+
const inverse = (cell.attrs & 32) !== 0;
|
|
281
|
+
const invisible = (cell.attrs & 64) !== 0;
|
|
282
|
+
const strike = (cell.attrs & 128) !== 0;
|
|
283
|
+
|
|
284
|
+
let fg = this._resolveColor(cell.fgKind, cell.fgR, cell.fgG, cell.fgB, true, bold);
|
|
285
|
+
let bg = this._resolveColor(cell.bgKind, cell.bgR, cell.bgG, cell.bgB, false, false);
|
|
286
|
+
if (inverse) {
|
|
287
|
+
const tmp = fg;
|
|
288
|
+
fg = bg;
|
|
289
|
+
bg = tmp;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
if (bg !== this._theme.background) {
|
|
293
|
+
ctx.fillStyle = bg;
|
|
294
|
+
ctx.fillRect(x, y, w + 0.5, ch);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
const cellSelected = this._isCellSelected(absRow, c) || (span === 2 && this._isCellSelected(absRow, c + 1));
|
|
298
|
+
if (cellSelected) {
|
|
299
|
+
ctx.fillStyle = this._theme.selection;
|
|
300
|
+
ctx.fillRect(x, y, Math.ceil(w), ch);
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (!invisible && cell.cp > 32) {
|
|
304
|
+
ctx.save();
|
|
305
|
+
if (dim) {
|
|
306
|
+
ctx.globalAlpha = 0.55;
|
|
307
|
+
}
|
|
308
|
+
const weight = bold ? "700" : "400";
|
|
309
|
+
const slant = italic ? "italic" : "normal";
|
|
310
|
+
ctx.font = `${slant} ${weight} ${this._fontSize}px ${this._fontFamily}`;
|
|
311
|
+
ctx.fillStyle = fg;
|
|
312
|
+
ctx.fillText(String.fromCodePoint(cell.cp), x, baseline);
|
|
313
|
+
ctx.restore();
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if (underline) {
|
|
317
|
+
ctx.fillStyle = fg;
|
|
318
|
+
ctx.fillRect(x, y + ch - 2, w, 1);
|
|
319
|
+
}
|
|
320
|
+
if (strike) {
|
|
321
|
+
ctx.fillStyle = fg;
|
|
322
|
+
ctx.fillRect(x, y + Math.floor(ch * 0.52), w, 1);
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
_paintCursor(meta) {
|
|
328
|
+
if (!meta.cursorVisible || meta.scrollOffset !== 0) {
|
|
329
|
+
return;
|
|
330
|
+
}
|
|
331
|
+
const ctx = this._ctx;
|
|
332
|
+
const cw = this._cellWidth;
|
|
333
|
+
const ch = this._cellHeight;
|
|
334
|
+
const x = this._padX + meta.cursorCol * cw;
|
|
335
|
+
const y = this._padY + meta.cursorRow * ch;
|
|
336
|
+
const style = meta.cursorStyle;
|
|
337
|
+
|
|
338
|
+
ctx.save();
|
|
339
|
+
if (!this._focused) {
|
|
340
|
+
ctx.strokeStyle = this._theme.cursor;
|
|
341
|
+
ctx.lineWidth = 1.5;
|
|
342
|
+
ctx.strokeRect(x + 0.75, y + 0.75, cw - 1.5, ch - 1.5);
|
|
343
|
+
} else if (style === 3 || style === 4) {
|
|
344
|
+
ctx.fillStyle = this._theme.cursor;
|
|
345
|
+
ctx.fillRect(x, y + ch - 2.5, cw, 2.5);
|
|
346
|
+
} else if (style === 5 || style === 6) {
|
|
347
|
+
ctx.fillStyle = this._theme.cursor;
|
|
348
|
+
ctx.fillRect(x, y + 1, 2, ch - 2);
|
|
349
|
+
} else {
|
|
350
|
+
ctx.fillStyle = this._theme.cursor;
|
|
351
|
+
ctx.fillRect(x, y, cw, ch);
|
|
352
|
+
const row = this._rowCache[meta.cursorRow];
|
|
353
|
+
const cell = row && row.cells[meta.cursorCol];
|
|
354
|
+
if (cell && cell.cp > 32) {
|
|
355
|
+
ctx.fillStyle = this._theme.cursorAccent;
|
|
356
|
+
ctx.font = `400 ${this._fontSize}px ${this._fontFamily}`;
|
|
357
|
+
ctx.fillText(String.fromCodePoint(cell.cp), x, y + Math.floor(ch * 0.78));
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
ctx.restore();
|
|
361
|
+
}
|
|
362
|
+
|
|
363
|
+
_paintScrollbar(meta) {
|
|
364
|
+
const ctx = this._ctx;
|
|
365
|
+
const trackX = this._padX * 2 + this._cols * this._cellWidth;
|
|
366
|
+
const trackY = this._padY;
|
|
367
|
+
const trackH = this._rows * this._cellHeight;
|
|
368
|
+
const trackW = this._scrollbarWidth - 2;
|
|
369
|
+
|
|
370
|
+
ctx.fillStyle = this._theme.background;
|
|
371
|
+
ctx.fillRect(trackX, 0, this._scrollbarWidth, trackH + this._padY * 2);
|
|
372
|
+
|
|
373
|
+
if (meta.altScreen || meta.scrollbackLen === 0) {
|
|
374
|
+
return;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
ctx.fillStyle = this._theme.scrollbarTrack;
|
|
378
|
+
ctx.fillRect(trackX, trackY, trackW, trackH);
|
|
379
|
+
|
|
380
|
+
const totalLines = meta.scrollbackLen + meta.rows;
|
|
381
|
+
const thumbH = Math.max(18, Math.floor((meta.rows / totalLines) * trackH));
|
|
382
|
+
const maxTravel = Math.max(0, trackH - thumbH);
|
|
383
|
+
const ratio = meta.scrollbackLen > 0 ? 1 - meta.scrollOffset / meta.scrollbackLen : 1;
|
|
384
|
+
const thumbY = trackY + Math.round(maxTravel * ratio);
|
|
385
|
+
|
|
386
|
+
ctx.fillStyle = this._theme.scrollbarThumb;
|
|
387
|
+
ctx.fillRect(trackX, thumbY, trackW, thumbH);
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
renderFrame(frame, forceRepaintAll = false) {
|
|
391
|
+
this.resizeCanvas(frame.cols, frame.rows, window.devicePixelRatio || 1);
|
|
392
|
+
const rowsToRepaint = new Set();
|
|
393
|
+
|
|
394
|
+
if (this._lastCursor) {
|
|
395
|
+
rowsToRepaint.add(this._lastCursor.row);
|
|
396
|
+
}
|
|
397
|
+
rowsToRepaint.add(frame.cursorRow);
|
|
398
|
+
|
|
399
|
+
for (const r of frame.dirtyRows) {
|
|
400
|
+
this._rowCache[r.rowIndex] = r;
|
|
401
|
+
rowsToRepaint.add(r.rowIndex);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
if (forceRepaintAll) {
|
|
405
|
+
for (let i = 0; i < frame.rows; i++) {
|
|
406
|
+
rowsToRepaint.add(i);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
this._lastFrameMeta = frame;
|
|
411
|
+
for (const idx of rowsToRepaint) {
|
|
412
|
+
if (idx >= 0 && idx < frame.rows) {
|
|
413
|
+
this._paintRow(idx, this._rowCache[idx], frame);
|
|
414
|
+
}
|
|
415
|
+
}
|
|
416
|
+
this._paintCursor(frame);
|
|
417
|
+
this._paintScrollbar(frame);
|
|
418
|
+
this._lastCursor = { row: frame.cursorRow, col: frame.cursorCol };
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
repaintCached() {
|
|
422
|
+
if (!this._lastFrameMeta) {
|
|
423
|
+
return;
|
|
424
|
+
}
|
|
425
|
+
this.renderFrame({ ...this._lastFrameMeta, dirtyRows: [] }, true);
|
|
426
|
+
}
|
|
427
|
+
}
|
|
@@ -0,0 +1,181 @@
|
|
|
1
|
+
const MODIFIER_KEYS = new Set([
|
|
2
|
+
"Shift",
|
|
3
|
+
"Control",
|
|
4
|
+
"Alt",
|
|
5
|
+
"Meta",
|
|
6
|
+
"CapsLock",
|
|
7
|
+
"NumLock",
|
|
8
|
+
"ScrollLock",
|
|
9
|
+
"AltGraph",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
const SPECIAL_VT_KEYS = new Set([
|
|
13
|
+
"Enter",
|
|
14
|
+
"Tab",
|
|
15
|
+
"Escape",
|
|
16
|
+
"Backspace",
|
|
17
|
+
" ",
|
|
18
|
+
"ArrowUp",
|
|
19
|
+
"ArrowDown",
|
|
20
|
+
"ArrowLeft",
|
|
21
|
+
"ArrowRight",
|
|
22
|
+
"Home",
|
|
23
|
+
"End",
|
|
24
|
+
"Insert",
|
|
25
|
+
"Delete",
|
|
26
|
+
"PageUp",
|
|
27
|
+
"PageDown",
|
|
28
|
+
"F1",
|
|
29
|
+
"F2",
|
|
30
|
+
"F3",
|
|
31
|
+
"F4",
|
|
32
|
+
"F5",
|
|
33
|
+
"F6",
|
|
34
|
+
"F7",
|
|
35
|
+
"F8",
|
|
36
|
+
"F9",
|
|
37
|
+
"F10",
|
|
38
|
+
"F11",
|
|
39
|
+
"F12",
|
|
40
|
+
]);
|
|
41
|
+
|
|
42
|
+
export class InputController {
|
|
43
|
+
constructor({ isMac = false } = {}) {
|
|
44
|
+
this._isMac = Boolean(isMac);
|
|
45
|
+
this._focusState = "unfocused";
|
|
46
|
+
this._interceptor = null;
|
|
47
|
+
this._suppressNextInput = false;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
get focusState() {
|
|
51
|
+
return this._focusState;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
get focused() {
|
|
55
|
+
return this._focusState !== "unfocused";
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
setKeyInterceptor(fn) {
|
|
59
|
+
this._interceptor = typeof fn === "function" ? fn : null;
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
onFocus() {
|
|
63
|
+
if (this._focusState === "unfocused") {
|
|
64
|
+
this._focusState = "focused";
|
|
65
|
+
this._suppressNextInput = false;
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
return false;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
onBlur() {
|
|
72
|
+
if (this._focusState !== "unfocused") {
|
|
73
|
+
this._focusState = "unfocused";
|
|
74
|
+
this._suppressNextInput = false;
|
|
75
|
+
return true;
|
|
76
|
+
}
|
|
77
|
+
return false;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
onCompositionStart() {
|
|
81
|
+
if (this._focusState === "focused") {
|
|
82
|
+
this._focusState = "composing";
|
|
83
|
+
this._suppressNextInput = false;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
onCompositionEnd(data) {
|
|
88
|
+
if (this._focusState === "composing") {
|
|
89
|
+
this._focusState = "focused";
|
|
90
|
+
}
|
|
91
|
+
this._suppressNextInput = false;
|
|
92
|
+
return typeof data === "string" ? data : "";
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
shouldEmitTextareaInput(eventIsComposing) {
|
|
96
|
+
if (this._suppressNextInput) {
|
|
97
|
+
this._suppressNextInput = false;
|
|
98
|
+
return false;
|
|
99
|
+
}
|
|
100
|
+
if (this._focusState !== "focused") {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
if (eventIsComposing) {
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
return true;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
routeKeyDown(event, { hasSelection = false } = {}) {
|
|
110
|
+
this._suppressNextInput = false;
|
|
111
|
+
if (this._focusState === "unfocused") {
|
|
112
|
+
return { action: "ignore" };
|
|
113
|
+
}
|
|
114
|
+
if (
|
|
115
|
+
this._focusState === "composing" ||
|
|
116
|
+
event.isComposing ||
|
|
117
|
+
event.key === "Process" ||
|
|
118
|
+
event.keyCode === 229
|
|
119
|
+
) {
|
|
120
|
+
return { action: "ignore" };
|
|
121
|
+
}
|
|
122
|
+
if (this._interceptor && this._interceptor(event) === "host") {
|
|
123
|
+
this._suppressNextInput = true;
|
|
124
|
+
return { action: "host" };
|
|
125
|
+
}
|
|
126
|
+
if (MODIFIER_KEYS.has(event.key)) {
|
|
127
|
+
return { action: "ignore" };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const isAltGraph = typeof event.getModifierState === "function" && event.getModifierState("AltGraph");
|
|
131
|
+
if (isAltGraph || (!this._isMac && event.ctrlKey && event.altKey && typeof event.key === "string" && event.key.length === 1 && event.key.charCodeAt(0) >= 128)) {
|
|
132
|
+
return { action: "textarea" };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const lower = typeof event.key === "string" ? event.key.toLowerCase() : "";
|
|
136
|
+
|
|
137
|
+
if (this._isMac) {
|
|
138
|
+
if (event.metaKey && !event.ctrlKey && !event.altKey) {
|
|
139
|
+
if (lower === "c") {
|
|
140
|
+
return { action: "copy" };
|
|
141
|
+
}
|
|
142
|
+
if (lower === "v") {
|
|
143
|
+
return { action: "native_paste" };
|
|
144
|
+
}
|
|
145
|
+
return { action: "ignore" };
|
|
146
|
+
}
|
|
147
|
+
} else {
|
|
148
|
+
if (event.ctrlKey && event.shiftKey && !event.altKey && !event.metaKey) {
|
|
149
|
+
if (lower === "c") {
|
|
150
|
+
return { action: "copy" };
|
|
151
|
+
}
|
|
152
|
+
if (lower === "v") {
|
|
153
|
+
return { action: "paste_clipboard" };
|
|
154
|
+
}
|
|
155
|
+
}
|
|
156
|
+
if (event.ctrlKey && !event.shiftKey && !event.altKey && !event.metaKey && lower === "c" && hasSelection) {
|
|
157
|
+
return { action: "copy" };
|
|
158
|
+
}
|
|
159
|
+
if (event.metaKey && !event.ctrlKey && !event.altKey) {
|
|
160
|
+
return { action: "ignore" };
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
if (event.key === "Dead") {
|
|
165
|
+
return { action: "textarea" };
|
|
166
|
+
}
|
|
167
|
+
if (event.ctrlKey || event.altKey || SPECIAL_VT_KEYS.has(event.key)) {
|
|
168
|
+
return {
|
|
169
|
+
action: "encode_key",
|
|
170
|
+
keySpec: {
|
|
171
|
+
key: event.key,
|
|
172
|
+
ctrl: Boolean(event.ctrlKey),
|
|
173
|
+
alt: Boolean(event.altKey),
|
|
174
|
+
shift: Boolean(event.shiftKey),
|
|
175
|
+
meta: Boolean(event.metaKey),
|
|
176
|
+
},
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
return { action: "textarea" };
|
|
180
|
+
}
|
|
181
|
+
}
|