-
Notifications
You must be signed in to change notification settings - Fork 115
Expand file tree
/
Copy pathselection-manager.ts
More file actions
970 lines (831 loc) · 31.9 KB
/
selection-manager.ts
File metadata and controls
970 lines (831 loc) · 31.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/**
* Selection Manager - Handles text selection in the terminal
*
* Features:
* - Mouse drag selection
* - Double-click word selection
* - Text extraction from terminal buffer
* - Automatic clipboard copy
* - Visual selection highlighting (integrated into CanvasRenderer cell rendering)
* - Auto-scroll during drag selection
*/
import { EventEmitter } from './event-emitter';
import type { GhosttyTerminal } from './ghostty';
import type { IEvent } from './interfaces';
import type { CanvasRenderer } from './renderer';
import type { Terminal } from './terminal';
import type { GhosttyCell } from './types';
// ============================================================================
// Type Definitions
// ============================================================================
export interface SelectionCoordinates {
startCol: number;
startRow: number;
endCol: number;
endRow: number;
}
// ============================================================================
// SelectionManager Class
// ============================================================================
export class SelectionManager {
private terminal: Terminal;
private renderer: CanvasRenderer;
private wasmTerm: GhosttyTerminal;
private textarea: HTMLTextAreaElement;
// Selection state - coordinates are in ABSOLUTE buffer space (viewportY + viewportRow)
// This ensures selection persists correctly when scrolling
private selectionStart: { col: number; absoluteRow: number } | null = null;
private selectionEnd: { col: number; absoluteRow: number } | null = null;
private isSelecting: boolean = false;
private mouseDownTarget: EventTarget | null = null; // Track where mousedown occurred
// Track rows that need redraw for clearing old selection
// Using a Set prevents the overwrite bug where mousemove would clobber
// the rows marked by clearSelection()
private dirtySelectionRows: Set<number> = new Set();
// Event emitter
private selectionChangedEmitter = new EventEmitter<void>();
// Store bound event handlers for cleanup
private boundMouseUpHandler: ((e: MouseEvent) => void) | null = null;
private boundContextMenuHandler: ((e: MouseEvent) => void) | null = null;
private boundClickHandler: ((e: MouseEvent) => void) | null = null;
private boundDocumentMouseMoveHandler: ((e: MouseEvent) => void) | null = null;
// Auto-scroll state for drag selection
private autoScrollInterval: ReturnType<typeof setInterval> | null = null;
private autoScrollDirection: number = 0; // -1 = up, 0 = none, 1 = down
private static readonly AUTO_SCROLL_EDGE_SIZE = 30; // pixels from edge to trigger scroll
/**
* Get current viewport Y position (how many lines scrolled into history)
*/
private getViewportY(): number {
const rawViewportY =
typeof (this.terminal as any).getViewportY === 'function'
? (this.terminal as any).getViewportY()
: (this.terminal as any).viewportY || 0;
return Math.max(0, Math.floor(rawViewportY));
}
/**
* Convert viewport row to absolute buffer row
* Absolute row is an index into combined buffer: scrollback (0 to len-1) + screen (len to len+rows-1)
*/
private viewportRowToAbsolute(viewportRow: number): number {
const scrollbackLength = this.wasmTerm.getScrollbackLength();
const viewportY = this.getViewportY();
return scrollbackLength + viewportRow - viewportY;
}
/**
* Convert absolute buffer row to viewport row (may be outside visible range)
*/
private absoluteRowToViewport(absoluteRow: number): number {
const scrollbackLength = this.wasmTerm.getScrollbackLength();
const viewportY = this.getViewportY();
return absoluteRow - scrollbackLength + viewportY;
}
private static readonly AUTO_SCROLL_SPEED = 3; // lines per interval
private static readonly AUTO_SCROLL_INTERVAL = 50; // ms between scroll steps
constructor(
terminal: Terminal,
renderer: CanvasRenderer,
wasmTerm: GhosttyTerminal,
textarea: HTMLTextAreaElement
) {
this.terminal = terminal;
this.renderer = renderer;
this.wasmTerm = wasmTerm;
this.textarea = textarea;
// Attach mouse event listeners
this.attachEventListeners();
}
// ==========================================================================
// Public API
// ==========================================================================
/**
* Get the selected text as a string
*/
getSelection(): string {
if (!this.selectionStart || !this.selectionEnd) return '';
// Get absolute row coordinates (not clamped to viewport)
let { col: startCol, absoluteRow: startAbsRow } = this.selectionStart;
let { col: endCol, absoluteRow: endAbsRow } = this.selectionEnd;
// Swap if selection goes backwards
if (startAbsRow > endAbsRow || (startAbsRow === endAbsRow && startCol > endCol)) {
[startCol, endCol] = [endCol, startCol];
[startAbsRow, endAbsRow] = [endAbsRow, startAbsRow];
}
const scrollbackLength = this.wasmTerm.getScrollbackLength();
let text = '';
for (let absRow = startAbsRow; absRow <= endAbsRow; absRow++) {
// Fetch line based on absolute row position
// Absolute row < scrollbackLength means it's in scrollback
// Absolute row >= scrollbackLength means it's in the screen buffer
let line: GhosttyCell[] | null = null;
if (absRow < scrollbackLength) {
// Row is in scrollback
line = this.wasmTerm.getScrollbackLine(absRow);
} else {
// Row is in screen buffer
const screenRow = absRow - scrollbackLength;
line = this.wasmTerm.getLine(screenRow);
}
if (!line) continue;
// Track the last non-empty column for trimming trailing spaces
let lastNonEmpty = -1;
// Determine column range for this row
const colStart = absRow === startAbsRow ? startCol : 0;
const colEnd = absRow === endAbsRow ? endCol : line.length - 1;
// Build the line text
let lineText = '';
for (let col = colStart; col <= colEnd; col++) {
const cell = line[col];
if (cell && cell.codepoint !== 0) {
// Use grapheme lookup for cells with multi-codepoint characters
let char: string;
if (cell.grapheme_len > 0) {
// Row is in scrollback or screen - determine which and use appropriate method
if (absRow < scrollbackLength) {
char = this.wasmTerm.getScrollbackGraphemeString(absRow, col);
} else {
const screenRow = absRow - scrollbackLength;
char = this.wasmTerm.getGraphemeString(screenRow, col);
}
} else {
char = String.fromCodePoint(cell.codepoint);
}
lineText += char;
if (char.trim()) {
lastNonEmpty = lineText.length;
}
} else if (!cell || cell.width !== 0) {
// Only add space for truly empty cells, not wide character continuation cells.
// Wide characters (like CJK) occupy 2 terminal cells:
// - First cell: has codepoint, width=2
// - Second cell: codepoint=0, width=0 (continuation marker)
// We skip continuation cells to avoid inserting spaces between characters.
lineText += ' ';
}
}
// Trim trailing spaces from each line
if (lastNonEmpty >= 0) {
lineText = lineText.substring(0, lastNonEmpty);
} else {
lineText = '';
}
text += lineText;
// Add newline between rows (but not after the last row)
if (absRow < endAbsRow) {
text += '\n';
}
}
return text;
}
/**
* Check if there's an active selection
*/
hasSelection(): boolean {
if (!this.selectionStart || !this.selectionEnd) return false;
// Check if start and end are the same (single cell, no real selection)
return !(
this.selectionStart.col === this.selectionEnd.col &&
this.selectionStart.absoluteRow === this.selectionEnd.absoluteRow
);
}
/**
* Copy the current selection to clipboard
* @returns true if there was text to copy, false otherwise
*/
copySelection(): boolean {
if (!this.hasSelection()) return false;
const text = this.getSelection();
if (text) {
this.copyToClipboard(text);
return true;
}
return false;
}
/**
* Clear the selection
*/
clearSelection(): void {
if (!this.hasSelection()) return;
// Mark current selection rows as dirty for redraw
const coords = this.normalizeSelection();
if (coords) {
for (let row = coords.startRow; row <= coords.endRow; row++) {
this.dirtySelectionRows.add(row);
}
}
this.selectionStart = null;
this.selectionEnd = null;
this.isSelecting = false;
// Force redraw of previously selected lines to clear the overlay
this.requestRender();
}
/**
* Select all text in the terminal
*/
selectAll(): void {
const dims = this.wasmTerm.getDimensions();
const viewportY = this.getViewportY();
this.selectionStart = { col: 0, absoluteRow: viewportY };
this.selectionEnd = { col: dims.cols - 1, absoluteRow: viewportY + dims.rows - 1 };
this.requestRender();
this.selectionChangedEmitter.fire();
}
/**
* Select text at specific column and row with length
* xterm.js compatible API
*/
select(column: number, row: number, length: number): void {
// Clamp to valid ranges
const dims = this.wasmTerm.getDimensions();
row = Math.max(0, Math.min(row, dims.rows - 1));
column = Math.max(0, Math.min(column, dims.cols - 1));
// Calculate end position
let endRow = row;
let endCol = column + length - 1;
// Handle wrapping if selection extends past end of line
while (endCol >= dims.cols) {
endCol -= dims.cols;
endRow++;
}
// Clamp end row
endRow = Math.min(endRow, dims.rows - 1);
// Convert viewport rows to absolute rows
const viewportY = this.getViewportY();
this.selectionStart = { col: column, absoluteRow: viewportY + row };
this.selectionEnd = { col: endCol, absoluteRow: viewportY + endRow };
this.requestRender();
this.selectionChangedEmitter.fire();
}
/**
* Select entire lines from start to end
* xterm.js compatible API
*/
selectLines(start: number, end: number): void {
const dims = this.wasmTerm.getDimensions();
// Clamp to valid row ranges
start = Math.max(0, Math.min(start, dims.rows - 1));
end = Math.max(0, Math.min(end, dims.rows - 1));
// Ensure start <= end
if (start > end) {
[start, end] = [end, start];
}
// Convert viewport rows to absolute rows
const viewportY = this.getViewportY();
this.selectionStart = { col: 0, absoluteRow: viewportY + start };
this.selectionEnd = { col: dims.cols - 1, absoluteRow: viewportY + end };
this.requestRender();
this.selectionChangedEmitter.fire();
}
/**
* Get selection position as buffer range
* xterm.js compatible API
*/
getSelectionPosition():
| { start: { x: number; y: number }; end: { x: number; y: number } }
| undefined {
const coords = this.normalizeSelection();
if (!coords) return undefined;
return {
start: { x: coords.startCol, y: coords.startRow },
end: { x: coords.endCol, y: coords.endRow },
};
}
/**
* Deselect all text
* xterm.js compatible API
*/
deselect(): void {
this.clearSelection();
this.selectionChangedEmitter.fire();
}
/**
* Focus the terminal (make it receive keyboard input)
*/
focus(): void {
const canvas = this.renderer.getCanvas();
if (canvas.parentElement) {
canvas.parentElement.focus();
}
}
/**
* Get current selection coordinates (for rendering)
*/
getSelectionCoords(): SelectionCoordinates | null {
return this.normalizeSelection();
}
/**
* Get dirty selection rows that need redraw (for clearing old highlight)
*/
getDirtySelectionRows(): Set<number> {
return this.dirtySelectionRows;
}
/**
* Clear the dirty selection rows tracking (after redraw)
*/
clearDirtySelectionRows(): void {
this.dirtySelectionRows.clear();
}
/**
* Get selection change event accessor
*/
get onSelectionChange(): IEvent<void> {
return this.selectionChangedEmitter.event;
}
/**
* Cleanup resources
*/
dispose(): void {
this.selectionChangedEmitter.dispose();
// Stop auto-scroll if active
this.stopAutoScroll();
// Clean up document event listener
if (this.boundMouseUpHandler) {
document.removeEventListener('mouseup', this.boundMouseUpHandler);
this.boundMouseUpHandler = null;
}
// Clean up document mousemove listener
if (this.boundDocumentMouseMoveHandler) {
document.removeEventListener('mousemove', this.boundDocumentMouseMoveHandler);
this.boundDocumentMouseMoveHandler = null;
}
// Clean up context menu event listener
if (this.boundContextMenuHandler) {
const canvas = this.renderer.getCanvas();
canvas.removeEventListener('contextmenu', this.boundContextMenuHandler);
this.boundContextMenuHandler = null;
}
// Clean up document click listener
if (this.boundClickHandler) {
document.removeEventListener('click', this.boundClickHandler);
this.boundClickHandler = null;
}
// Canvas event listeners will be cleaned up when canvas is removed from DOM
}
// ==========================================================================
// Private Methods
// ==========================================================================
/**
* Attach mouse event listeners to canvas
*/
private attachEventListeners(): void {
const canvas = this.renderer.getCanvas();
// Mouse down - start selection or clear existing
canvas.addEventListener('mousedown', (e: MouseEvent) => {
if (e.button === 0) {
// Left click only
// CRITICAL: Focus the terminal so it can receive keyboard input
// The canvas doesn't have tabindex, but the parent container does
if (canvas.parentElement) {
canvas.parentElement.focus();
}
const cell = this.pixelToCell(e.offsetX, e.offsetY);
// Always clear previous selection on new click
const hadSelection = this.hasSelection();
if (hadSelection) {
this.clearSelection();
}
// Start new selection (convert to absolute coordinates)
const absoluteRow = this.viewportRowToAbsolute(cell.row);
this.selectionStart = { col: cell.col, absoluteRow };
this.selectionEnd = { col: cell.col, absoluteRow };
this.isSelecting = true;
}
});
// Mouse move on canvas - update selection
canvas.addEventListener('mousemove', (e: MouseEvent) => {
if (this.isSelecting) {
// Mark current selection rows as dirty before updating
this.markCurrentSelectionDirty();
const cell = this.pixelToCell(e.offsetX, e.offsetY);
const absoluteRow = this.viewportRowToAbsolute(cell.row);
this.selectionEnd = { col: cell.col, absoluteRow };
this.requestRender();
// Check if near edges for auto-scroll
this.updateAutoScroll(e.offsetY, canvas.clientHeight);
}
});
// Mouse leave - check for auto-scroll when leaving canvas during drag
canvas.addEventListener('mouseleave', (e: MouseEvent) => {
if (this.isSelecting) {
// Determine scroll direction based on where mouse left
const rect = canvas.getBoundingClientRect();
if (e.clientY < rect.top) {
this.startAutoScroll(-1); // Scroll up
} else if (e.clientY > rect.bottom) {
this.startAutoScroll(1); // Scroll down
}
}
});
// Mouse enter - stop auto-scroll when mouse returns to canvas
canvas.addEventListener('mouseenter', () => {
if (this.isSelecting) {
this.stopAutoScroll();
}
});
// Document-level mousemove for tracking mouse position during drag outside canvas
this.boundDocumentMouseMoveHandler = (e: MouseEvent) => {
if (this.isSelecting) {
const rect = canvas.getBoundingClientRect();
// Update selection based on clamped position
const clampedX = Math.max(rect.left, Math.min(e.clientX, rect.right));
const clampedY = Math.max(rect.top, Math.min(e.clientY, rect.bottom));
// Convert to canvas-relative coordinates
const offsetX = clampedX - rect.left;
const offsetY = clampedY - rect.top;
// Only update if mouse is outside the canvas
if (
e.clientX < rect.left ||
e.clientX > rect.right ||
e.clientY < rect.top ||
e.clientY > rect.bottom
) {
// Update auto-scroll direction based on mouse position
if (e.clientY < rect.top) {
this.startAutoScroll(-1);
} else if (e.clientY > rect.bottom) {
this.startAutoScroll(1);
} else {
this.stopAutoScroll();
}
// Only update selection position if NOT auto-scrolling
// During auto-scroll, the scroll handler extends the selection
if (this.autoScrollDirection === 0) {
// Mark current selection rows as dirty before updating
this.markCurrentSelectionDirty();
const cell = this.pixelToCell(offsetX, offsetY);
const absoluteRow = this.viewportRowToAbsolute(cell.row);
this.selectionEnd = { col: cell.col, absoluteRow };
this.requestRender();
}
}
}
};
document.addEventListener('mousemove', this.boundDocumentMouseMoveHandler);
// Track mousedown on document to know if a click started inside the canvas
document.addEventListener('mousedown', (e: MouseEvent) => {
this.mouseDownTarget = e.target;
});
// CRITICAL FIX: Listen for mouseup on DOCUMENT, not just canvas
// This catches mouseup events that happen outside the canvas (common during drag)
this.boundMouseUpHandler = (e: MouseEvent) => {
if (this.isSelecting) {
this.isSelecting = false;
this.stopAutoScroll();
const text = this.getSelection();
if (text) {
this.copyToClipboard(text);
this.selectionChangedEmitter.fire();
}
}
};
document.addEventListener('mouseup', this.boundMouseUpHandler);
// Double-click - select word
canvas.addEventListener('dblclick', (e: MouseEvent) => {
const cell = this.pixelToCell(e.offsetX, e.offsetY);
const word = this.getWordAtCell(cell.col, cell.row);
if (word) {
const absoluteRow = this.viewportRowToAbsolute(cell.row);
this.selectionStart = { col: word.startCol, absoluteRow };
this.selectionEnd = { col: word.endCol, absoluteRow };
this.requestRender();
const text = this.getSelection();
if (text) {
this.copyToClipboard(text);
this.selectionChangedEmitter.fire();
}
}
});
// Right-click (context menu) - position textarea to show browser's native menu
// This allows Copy/Paste options to appear in the context menu
this.boundContextMenuHandler = (e: MouseEvent) => {
// Position textarea at mouse cursor
const canvas = this.renderer.getCanvas();
const rect = canvas.getBoundingClientRect();
this.textarea.style.position = 'fixed';
this.textarea.style.left = `${e.clientX}px`;
this.textarea.style.top = `${e.clientY}px`;
this.textarea.style.width = '1px';
this.textarea.style.height = '1px';
this.textarea.style.zIndex = '1000';
this.textarea.style.opacity = '0';
// Enable pointer events temporarily so context menu targets the textarea
this.textarea.style.pointerEvents = 'auto';
// If there's a selection, populate textarea with it and select the text
if (this.hasSelection()) {
const text = this.getSelection();
this.textarea.value = text;
this.textarea.select();
this.textarea.setSelectionRange(0, text.length);
} else {
// No selection - clear textarea but still show menu (for paste)
this.textarea.value = '';
}
// Focus the textarea so the context menu appears on it
this.textarea.focus();
// After a short delay, restore the textarea to its hidden state
// This allows the context menu to appear first
setTimeout(() => {
// Listen for when the context menu closes (user clicks away or selects an option)
const resetTextarea = () => {
this.textarea.style.pointerEvents = 'none';
this.textarea.style.zIndex = '-10';
this.textarea.style.width = '0';
this.textarea.style.height = '0';
this.textarea.style.left = '0';
this.textarea.style.top = '0';
this.textarea.value = '';
// Remove the one-time listeners
document.removeEventListener('click', resetTextarea);
document.removeEventListener('contextmenu', resetTextarea);
this.textarea.removeEventListener('blur', resetTextarea);
};
// Reset on any of these events (menu closed)
document.addEventListener('click', resetTextarea, { once: true });
document.addEventListener('contextmenu', resetTextarea, { once: true });
this.textarea.addEventListener('blur', resetTextarea, { once: true });
}, 10);
// Don't prevent default - let browser show the context menu on the textarea
};
canvas.addEventListener('contextmenu', this.boundContextMenuHandler);
// Click outside canvas - clear selection
// This allows users to deselect by clicking anywhere outside the terminal
this.boundClickHandler = (e: MouseEvent) => {
// Don't clear selection if we're actively selecting
if (this.isSelecting) {
return;
}
// A click is only valid for clearing selection if BOTH mousedown and mouseup
// happened outside the canvas. If mousedown was inside (drag selection),
// don't clear even if mouseup/click is outside.
const mouseDownWasInCanvas =
this.mouseDownTarget && canvas.contains(this.mouseDownTarget as Node);
if (mouseDownWasInCanvas) {
return;
}
// Check if the click is outside the canvas
const target = e.target as Node;
if (!canvas.contains(target)) {
// Clicked outside the canvas - clear selection
if (this.hasSelection()) {
this.clearSelection();
}
}
};
document.addEventListener('click', this.boundClickHandler);
}
/**
* Mark current selection rows as dirty for redraw
*/
private markCurrentSelectionDirty(): void {
const coords = this.normalizeSelection();
if (coords) {
for (let row = coords.startRow; row <= coords.endRow; row++) {
this.dirtySelectionRows.add(row);
}
}
}
/**
* Update auto-scroll based on mouse Y position within canvas
*/
private updateAutoScroll(offsetY: number, canvasHeight: number): void {
const edgeSize = SelectionManager.AUTO_SCROLL_EDGE_SIZE;
if (offsetY < edgeSize) {
// Near top edge - scroll up
this.startAutoScroll(-1);
} else if (offsetY > canvasHeight - edgeSize) {
// Near bottom edge - scroll down
this.startAutoScroll(1);
} else {
// In middle - stop scrolling
this.stopAutoScroll();
}
}
/**
* Start auto-scrolling in the given direction
*/
private startAutoScroll(direction: number): void {
// Don't restart if already scrolling in same direction
if (this.autoScrollInterval !== null && this.autoScrollDirection === direction) {
return;
}
// Stop any existing scroll
this.stopAutoScroll();
this.autoScrollDirection = direction;
// Start scrolling interval
this.autoScrollInterval = setInterval(() => {
if (!this.isSelecting) {
this.stopAutoScroll();
return;
}
// Scroll the terminal to reveal more content in the direction user is dragging
// autoScrollDirection: -1 = dragging up (wants to see history), 1 = dragging down (wants to see newer)
// scrollLines convention: negative = scroll up into history, positive = scroll down to newer
// So direction maps directly to scrollLines sign
const scrollAmount = SelectionManager.AUTO_SCROLL_SPEED * this.autoScrollDirection;
(this.terminal as any).scrollLines(scrollAmount);
// Extend selection in the scroll direction
// Key insight: we need to EXTEND the selection, not reset it to viewport edge
if (this.selectionEnd) {
const dims = this.wasmTerm.getDimensions();
if (this.autoScrollDirection < 0) {
// Scrolling up - extend selection upward (decrease absoluteRow)
// Set to top of viewport, but only if it extends the selection
const topAbsoluteRow = this.viewportRowToAbsolute(0);
if (topAbsoluteRow < this.selectionEnd.absoluteRow) {
this.selectionEnd = { col: 0, absoluteRow: topAbsoluteRow };
}
} else {
// Scrolling down - extend selection downward (increase absoluteRow)
// Set to bottom of viewport, but only if it extends the selection
const bottomAbsoluteRow = this.viewportRowToAbsolute(dims.rows - 1);
if (bottomAbsoluteRow > this.selectionEnd.absoluteRow) {
this.selectionEnd = { col: dims.cols - 1, absoluteRow: bottomAbsoluteRow };
}
}
}
this.requestRender();
}, SelectionManager.AUTO_SCROLL_INTERVAL);
}
/**
* Stop auto-scrolling
*/
private stopAutoScroll(): void {
if (this.autoScrollInterval !== null) {
clearInterval(this.autoScrollInterval);
this.autoScrollInterval = null;
}
this.autoScrollDirection = 0;
}
/**
* Convert pixel coordinates to terminal cell coordinates
*/
private pixelToCell(x: number, y: number): { col: number; row: number } {
const metrics = this.renderer.getMetrics();
const col = Math.floor(x / metrics.width);
const row = Math.floor(y / metrics.height);
// Clamp to terminal bounds
return {
col: Math.max(0, Math.min(col, this.terminal.cols - 1)),
row: Math.max(0, Math.min(row, this.terminal.rows - 1)),
};
}
/**
* Normalize selection coordinates (handle backward selection)
* Returns coordinates in VIEWPORT space for rendering, clamped to visible area
*/
private normalizeSelection(): SelectionCoordinates | null {
if (!this.selectionStart || !this.selectionEnd) return null;
let { col: startCol, absoluteRow: startAbsRow } = this.selectionStart;
let { col: endCol, absoluteRow: endAbsRow } = this.selectionEnd;
// Swap if selection goes backwards
if (startAbsRow > endAbsRow || (startAbsRow === endAbsRow && startCol > endCol)) {
[startCol, endCol] = [endCol, startCol];
[startAbsRow, endAbsRow] = [endAbsRow, startAbsRow];
}
// Convert to viewport coordinates
let startRow = this.absoluteRowToViewport(startAbsRow);
let endRow = this.absoluteRowToViewport(endAbsRow);
// Clamp to visible viewport range
const dims = this.wasmTerm.getDimensions();
const maxRow = dims.rows - 1;
// If entire selection is outside viewport, return null
if (endRow < 0 || startRow > maxRow) {
return null;
}
// Clamp rows to visible range, adjusting columns for partial rows
if (startRow < 0) {
startRow = 0;
startCol = 0; // Selection starts from beginning of first visible row
}
if (endRow > maxRow) {
endRow = maxRow;
endCol = dims.cols - 1; // Selection extends to end of last visible row
}
return { startCol, startRow, endCol, endRow };
}
/**
* Get word boundaries at a cell position
*/
private getWordAtCell(col: number, row: number): { startCol: number; endCol: number } | null {
const line = this.wasmTerm.getLine(row);
if (!line) return null;
// Word characters: letters, numbers, underscore, dash
const isWordChar = (cell: GhosttyCell) => {
if (!cell || cell.codepoint === 0) return false;
const char = String.fromCodePoint(cell.codepoint);
return /[\w-]/.test(char);
};
// Only return if we're actually on a word character
if (!isWordChar(line[col])) return null;
// Find start of word
let startCol = col;
while (startCol > 0 && isWordChar(line[startCol - 1])) {
startCol--;
}
// Find end of word
let endCol = col;
while (endCol < line.length - 1 && isWordChar(line[endCol + 1])) {
endCol++;
}
return { startCol, endCol };
}
/**
* Copy text to clipboard
*
* Strategy (modern APIs first):
* 1. Try ClipboardItem API (works in Safari and modern browsers)
* - Safari requires the ClipboardItem to be created synchronously within user gesture
* 2. Try navigator.clipboard.writeText (modern async API, may fail in Safari)
* 3. Fall back to execCommand (legacy, for older browsers)
*/
private copyToClipboard(text: string): void {
// First try: ClipboardItem API (modern, Safari-compatible)
// Safari allows this because we create the ClipboardItem synchronously
// within the user gesture, even though the write is async
if (navigator.clipboard && typeof ClipboardItem !== 'undefined') {
try {
const blob = new Blob([text], { type: 'text/plain' });
const clipboardItem = new ClipboardItem({
'text/plain': blob,
});
navigator.clipboard.write([clipboardItem]).catch((err) => {
console.warn('ClipboardItem write failed, trying writeText:', err);
// Try writeText as fallback
this.copyWithWriteText(text);
});
return;
} catch (err) {
// ClipboardItem not supported or failed, fall through
}
}
// Second try: basic async writeText (works in Chrome, may fail in Safari)
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).catch((err) => {
console.warn('Clipboard writeText failed, trying execCommand:', err);
// Fall back to execCommand
this.copyWithExecCommand(text);
});
return;
}
// Third try: legacy execCommand fallback
this.copyWithExecCommand(text);
}
/**
* Copy using navigator.clipboard.writeText
*/
private copyWithWriteText(text: string): void {
if (navigator.clipboard && navigator.clipboard.writeText) {
navigator.clipboard.writeText(text).catch((err) => {
console.warn('Clipboard writeText failed, trying execCommand:', err);
this.copyWithExecCommand(text);
});
} else {
this.copyWithExecCommand(text);
}
}
/**
* Copy using legacy execCommand (fallback for older browsers)
*/
private copyWithExecCommand(text: string): void {
const previouslyFocused = document.activeElement as HTMLElement;
try {
// Position textarea offscreen but in a way that allows selection
const textarea = this.textarea;
textarea.value = text;
textarea.style.position = 'fixed';
textarea.style.left = '-9999px';
textarea.style.top = '0';
textarea.style.width = '1px';
textarea.style.height = '1px';
textarea.style.opacity = '0';
// Select all text and copy
textarea.focus();
textarea.select();
textarea.setSelectionRange(0, text.length);
const success = document.execCommand('copy');
// Restore focus
if (previouslyFocused) {
previouslyFocused.focus();
}
if (!success) {
console.warn('execCommand copy failed');
}
} catch (err) {
console.warn('execCommand copy threw:', err);
// Restore focus on error
if (previouslyFocused) {
previouslyFocused.focus();
}
}
}
/**
* Request a render update (triggers selection overlay redraw)
*/
private requestRender(): void {
// The render loop will automatically pick up the new selection state
// and redraw the affected lines. This happens at 60fps.
//
// Note: When clearSelection() is called, it adds dirty rows to dirtySelectionRows
// which the renderer can use to know which lines to redraw.
}
}