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
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
|
//! The mouse: what a pointer is over, the three buttons' presses, drags and
//! releases (acme's chords, border and seam drags, gestures), and the
//! chrome targets a shell asks about.
const pardes = @import("pardes.zig");
const exec = @import("exec.zig");
const look = @import("look.zig");
const std = @import("std");
const layout = @import("layout.zig");
const modal = @import("modal.zig");
const ctlfs = @import("ninep/tree.zig");
const panes = @import("panes.zig");
const edit = @import("edit.zig");
const body_layer = @import("body_layer.zig");
const PanelCellDiff = @import("surface.zig").PanelCellDiff;
const builtins = @import("builtins.zig");
const config = @import("config.zig");
const pdf_enabled = panes.Pdf.enabled;
const pdf = panes.Pdf.pdf;
const platform = pardes.platform;
const Pane = panes.Pane;
const MAX_PANES = pardes.MAX_PANES;
const TagHit = @import("tag_layer.zig").Hit;
const TagKind = @import("tag_layer.zig").Kind;
const TOPBAR_H = pardes.TOPBAR_H;
const BOX_H = pardes.BOX_H;
const TAG_GAP = pardes.TAG_GAP;
const TAG_TEXT_INSET = pardes.TAG_TEXT_INSET;
const Builtin = pardes.Builtin;
const wordAtCol = pardes.wordAtCol;
const Mouse = pardes.Mouse;
const sel_slot = pardes.sel_slot;
const Pardes = pardes.Pardes;
pub const Drag = union(enum) {
none,
border_v: struct { left_col: usize, cur_x: u16, corner: ?struct { col: usize, idx: usize } = null, cur_y: u16 = 0 },
border_h: struct { col: usize, top_idx: usize, cur_y: u16 },
move: struct { id: usize, cur_x: u16, cur_y: u16 },
/// `grab`: how far into the grip it was pressed, so the column's left
/// edge lands at `cur_x - grab`. A release still on the grip moves nothing.
column_move: struct { serial: u32, cur_x: u16, grab: u16 = 0 },
/// chorded: a 1-2/1-3 cut/paste chord fired during this left drag —
/// the drag's own release is then inert
select: struct {
id: usize,
button: Mouse.Button,
chorded: bool = false,
ctrl: bool = false,
/// This gesture began over a usable native PDF raster. The payload is
/// zero-bit when PDF support is absent.
pdf: panes.Pdf.PointerDrag = .{},
},
fn button(drag: Drag) ?Mouse.Button {
return switch (drag) {
.none => null,
.border_v, .border_h, .move, .column_move => config.select_button,
.select => |selection| selection.button,
};
}
};
fn clampBorderCol(lx: u16, lw: u16, rw: u16, mcol: u16) u16 {
const lo = lx + config.MINW;
const hi = lx +| lw +| rw -| config.MINW;
return if (lo <= hi) std.math.clamp(mcol, lo, hi) else mcol;
}
fn clampBorderRow(ay: u16, ah: u16, bh: u16, mrow: u16, tag_bottom: bool) u16 {
const d: u16 = if (tag_bottom) 1 else 0;
const lo = ay + BOX_H - 1 + d;
const hi = (ay +| ah +| bh -| (BOX_H + 1)) +| d;
return if (lo <= hi) std.math.clamp(mrow, lo, hi) else mrow;
}
fn seamRowOf(p: *const Pardes, c: usize, k: usize) u16 {
const r = p.rects[p.col_panes[c][k]];
return if (p.settings.tag_bottom) r.y +| r.h else r.y + r.h -| 1;
}
fn cornerFixture(gpa: std.mem.Allocator) !*Pardes {
const p = try Pardes.init(gpa, .{ .cols = 100, .rows = 30, .shells = 3 });
p.update(.{ .resize = .{ .cols = 100, .rows = 30 } });
p.active = p.col_panes[1][0];
p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell below it, same column
p.sync();
return p;
}
fn nudgeRightSeam(p: *Pardes, delta: i32) u16 {
const from = seamRowOf(p, 1, 0);
const inside = p.col_x[1] + p.col_w[1] / 2;
const to: u16 = @intCast(@as(i32, from) + delta);
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = inside, .row = from } });
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = inside, .row = to } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = inside, .row = to } });
p.sync();
return seamRowOf(p, 1, 0);
}
pub const ChromeTarget = struct { col: u16, row: u16 };
pub fn chromeTarget(p: *const Pardes, col: u16, row: u16) ?ChromeTarget {
if (row < p.topBarHeight() + p.columnBarHeight()) return .{ .col = col, .row = row };
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const rect = p.rects[id];
const tag = p.tagTop(pane, rect);
if (row >= tag and row < tag + pane.tag_rows and col >= rect.x and col < rect.x + rect.w and col < rect.x + config.GUTTER)
return .{ .col = col, .row = row };
}
for (0..p.ncol -| 1) |column| {
const handle = p.col_x[column] + p.col_w[column] -| 1;
if (col == handle) return .{ .col = handle, .row = row };
}
for (0..p.ncol) |column| {
if (col < p.col_x[column] or col >= p.col_x[column] + p.col_w[column]) continue;
for (0..p.col_n[column] -| 1) |index| {
const rect = p.rects[p.col_panes[column][index]];
const handle = if (p.settings.tag_bottom) rect.y +| rect.h else rect.y + rect.h -| 1;
if (row == handle) return .{ .col = col, .row = handle };
}
}
for (p.panes, 0..) |slot, id| {
const pane = slot orelse continue;
const rect = p.rects[id];
const tag = p.tagTop(pane, rect);
if (row >= tag and row < tag + pane.tag_rows and col >= rect.x and col < rect.x + rect.w)
return .{ .col = col, .row = row };
}
for (0..p.ncol -| 1) |column| {
const handle = p.col_x[column] + p.col_w[column] -| 1;
if (@max(col, handle) - @min(col, handle) == 1) return .{ .col = handle, .row = row };
}
for (0..p.ncol) |column| {
if (col < p.col_x[column] or col >= p.col_x[column] + p.col_w[column]) continue;
for (0..p.col_n[column] -| 1) |index| {
const rect = p.rects[p.col_panes[column][index]];
const handle = if (p.settings.tag_bottom) rect.y +| rect.h else rect.y + rect.h -| 1;
if (@max(row, handle) - @min(row, handle) == 1) return .{ .col = col, .row = handle };
}
}
return null;
}
pub fn paneAt(p: *const Pardes, col: u16, row: u16) ?usize {
for (p.panes, 0..) |slot, id| {
if (slot == null) continue;
const r = p.rects[id];
if (col >= r.x and col < r.x + r.w and row >= r.y and row < r.y + r.h) return id;
}
return null;
}
pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clamp: bool) ?Mouse.BodyHit {
const pane = p.panes[id] orelse return null;
if (pane.file == null or (!clamp and supplied.serial != pane.serial)) return null;
if (!std.math.isFinite(supplied.pixel_x) or !std.math.isFinite(supplied.pixel_y) or
supplied.metrics.body_w == 0 or supplied.metrics.body_h == 0 or
supplied.metrics.tagline_w == 0 or supplied.metrics.tagline_h == 0) return null;
var mapped_hit = supplied;
if (clamp) {
mapped_hit.serial = pane.serial;
const layer = for (&p.surface.body_layers) |*layer| {
if (layer.rows > 0 and layer.serial == pane.serial) break layer;
} else return null;
const bw: f32 = @floatFromInt(mapped_hit.metrics.body_w);
const bh: f32 = @floatFromInt(mapped_hit.metrics.body_h);
const x0 = @as(f32, @floatFromInt(layer.viewport.x)) * bw;
const y0 = @as(f32, @floatFromInt(layer.viewport.y)) * bh;
const x1 = x0 + @as(f32, @floatFromInt(layer.viewport.w)) * bw;
const y1 = y0 + @as(f32, @floatFromInt(layer.viewport.h)) * bh;
mapped_hit.pixel_x = std.math.clamp(mapped_hit.pixel_x, x0, @max(x0, x1 - 0.001));
mapped_hit.pixel_y = std.math.clamp(mapped_hit.pixel_y, y0, @max(y0, y1 - 0.001));
}
return p.reprojectBodyHit(mapped_hit);
}
pub fn sameTagCell(a: ?TagHit, b: ?TagHit) bool {
const first = a orelse return b == null;
const second = b orelse return false;
return first.kind == second.kind and first.id == second.id and first.serial == second.serial and first.col == second.col;
}
/// The tag column a host's pointer is at. Clamped, a point off the tag is
/// brought back onto line `line` of it, for a drag that leaves it.
pub fn tagColumn(p: *const Pardes, kind: TagKind, id: usize, supplied: ?TagHit, clamp: bool, line: u16) ?u16 {
var point = supplied orelse return null;
if (point.kind != kind or point.id != id) return null;
if (clamp) for (p.surface.tagLayers()) |*layer| {
if (layer.cols == 0 or layer.kind != kind or layer.id != id or layer.serial != point.serial or layer.line != line) continue;
const bw: f32 = @floatFromInt(point.metrics.body_w);
const bh: f32 = @floatFromInt(point.metrics.body_h);
const left = @as(f32, @floatFromInt(layer.viewport.x)) * bw;
const right = @as(f32, @floatFromInt(layer.viewport.x + layer.viewport.w)) * bw;
point.pixel_x = std.math.clamp(point.pixel_x, left, @max(left, right - 0.001));
point.pixel_y = (@as(f32, @floatFromInt(layer.viewport.y)) + 0.5) * bh;
break;
};
return if (p.reprojectTagHit(point)) |mapped| mapped.col else null;
}
pub fn sameBodyCell(a: ?Mouse.BodyHit, b: ?Mouse.BodyHit) bool {
const first = a orelse return b == null;
const second = b orelse return false;
return first.serial == second.serial and first.row == second.row and first.col == second.col;
}
pub fn handleMouse(p: *Pardes, m: Mouse) void {
// A click goes somewhere else: the question it leaves unanswered is
// taken back, and the pane stays. A wheel only looks around.
if (m.kind == .press and switch (m.button) {
.left, .middle, .right => true,
else => false,
}) for (p.panes) |slot| {
const pane = slot orelse continue;
if (pane.prompt == .del_side) pane.prompt = .none;
};
if (m.button == .back or m.button == .forward) {
if (m.kind == .press) p.jumpBy(if (m.button == .back) -1 else 1);
return;
}
const raw_col = @min(m.col, p.screen_w -| 1);
const raw_row = @min(m.row, p.screen_h -| 1);
p.pointer_raw_col = raw_col;
p.pointer_raw_row = raw_row;
const previous_hit = p.pointer_body_hit;
p.pointer_body_hit = m.body_hit;
const previous_tag_hit = p.pointer_tag_hit;
p.pointer_tag_hit = if (m.tag_hit) |point| p.reprojectTagHit(point) else null;
if (m.tag_hit != null and p.pointer_tag_hit == null and m.kind == .press) return;
if (m.tag_hit == null and (m.kind == .drag or m.kind == .release) and (p.drag == .select or p.header_drag)) {
if (previous_tag_hit) |previous| {
var continued = previous;
continued.pixel_x = (@as(f32, @floatFromInt(raw_col)) + 0.5) * @as(f32, @floatFromInt(previous.metrics.body_w));
continued.pixel_y = (@as(f32, @floatFromInt(raw_row)) + 0.5) * @as(f32, @floatFromInt(previous.metrics.body_h));
p.pointer_tag_hit = continued;
}
}
if ((m.kind == .drag or m.kind == .release) and (p.drag == .select or p.header_drag)) {
if (previous_tag_hit) |previous| if (p.pointer_tag_hit) |*current| {
current.kind = previous.kind;
current.id = previous.id;
current.serial = previous.serial;
};
}
// A drag can leave the compact text viewport. Preserve its metrics so
// the endpoint still clamps to that body's last visible logical row.
if (m.body_hit == null and (m.kind == .drag or m.kind == .release) and p.drag == .select) {
if (previous_hit) |previous| {
var continued = previous;
continued.pixel_x = (@as(f32, @floatFromInt(raw_col)) + 0.5) * @as(f32, @floatFromInt(previous.metrics.body_w));
continued.pixel_y = (@as(f32, @floatFromInt(raw_row)) + 0.5) * @as(f32, @floatFromInt(previous.metrics.body_h));
p.pointer_body_hit = continued;
}
}
p.pointer_inside = true;
if (m.button == .none and m.kind == .motion) {
p.raw_hover_intent = true;
look.refreshLookHoverFromRaw(p);
return;
}
p.raw_hover_intent = false;
const mapped = p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) orelse {
look.cancelLookHover(p);
if (m.kind == .release) dragRelease(p, m.button);
return;
};
const mcol = mapped.col;
const mrow = mapped.row;
p.hover_col = mcol;
p.hover_row = mrow;
const hovered = paneAt(p, mcol, mrow);
// any press that is NOT the execute+select chord abandons a pending
// chord argument
if (m.kind == .press and
!(m.button == config.select_button and p.drag == .select and p.drag.select.button == config.exec_button))
{
if (p.chord_arg) |a| {
p.gpa.free(a);
p.chord_arg = null;
}
}
if (mrow >= p.topBarHeight() and mrow < p.topBarHeight() + p.columnBarHeight() and m.kind == .press) {
for (0..p.ncol) |column| {
if (mcol < p.col_x[column] or mcol >= p.col_x[column] + @min(config.GUTTER, p.col_w[column])) continue;
if (m.button == config.select_button) {
p.header_drag = false;
p.drag = .{ .column_move = .{ .serial = p.col_serial[column], .cur_x = mcol, .grab = mcol - p.col_x[column] } };
}
return;
}
}
if (p.header_drag and m.button == config.select_button and (m.kind == .drag or m.kind == .release)) {
const t = p.focusedHeader() orelse {
p.header_drag = false;
return;
};
const x = if (p.header_column) |c| p.col_x[c] + TAG_TEXT_INSET else 0;
const text = edit.flatSurface(p, t) catch return;
const top = if (p.header_column != null) p.topBarHeight() else 0;
const line: u16 = @intCast(@min(mrow -| top, modal.lineCount(text) -| 1, (if (p.header_column != null) p.columnBarHeight() else p.topBarHeight()) -| 1));
const bar = modal.lineSlice(text, line);
const at = @min(bar.len, panes.File.rawAtDisplay(bar, (tagColumn(p, if (p.header_column != null) .column else .workspace, p.header_column orelse 0, p.pointer_tag_hit, p.header_drag, line) orelse (mcol -| x)) + p.header_scroll));
// the anchor stays where the press put it
t.cur_row = line;
t.cur_col = @intCast(at);
t.vsel.active = t.vsel.row != t.cur_row or t.vsel.col != t.cur_col;
if (m.kind == .release) p.header_drag = false;
return;
}
if (mrow < p.topBarHeight() + p.columnBarHeight() and m.kind == .press and
(m.button == config.select_button or m.button == config.exec_button or m.button == config.look_button))
{
if (m.button == config.select_button) if (p.panes[p.active]) |pane| {
pane.focus = .body;
pane.closePrompt();
};
var column: ?usize = null;
var x: u16 = 0;
if (mrow >= p.topBarHeight()) {
for (0..p.ncol) |c| if (mcol >= p.col_x[c] and mcol < p.col_x[c] + p.col_w[c]) {
column = c;
x = p.col_x[c] + TAG_TEXT_INSET;
if (layout.findPane(p, p.active)) |active_col| {
if (active_col.col != c and p.col_n[c] > 0) p.active = p.col_panes[c][0];
}
break;
};
if (column == null or mcol < x) return;
}
const t = if (column) |c| &p.col_tags[c] else &p.global_tag;
const focused = p.header_focus and p.header_column == column;
const text = edit.flatSurface(p, t) catch return;
const line: u16 = @intCast(mrow - if (column != null) p.topBarHeight() else 0);
if (line > 0 and line >= modal.lineCount(text)) return;
const bar = modal.lineSlice(text, line);
const scroll = if (focused) p.header_scroll else 0;
const at = @min(bar.len, panes.File.rawAtDisplay(bar, (tagColumn(p, if (column != null) .column else .workspace, column orelse 0, p.pointer_tag_hit, p.header_drag, line) orelse (mcol -| x)) + scroll));
if (m.button == config.select_button) {
// A clicked header is typed straight into, as a clicked tag is.
if (!focused) p.enterHeader(column);
t.mode = .insert;
t.cur_row = line;
t.cur_col = @intCast(at);
t.vsel = .{ .active = false, .row = line, .col = @intCast(at) };
p.header_drag = true;
} else {
const span = if (focused) Pardes.headerSelection(t, text, line) else null;
const selected = if (span) |sp|
(if (at >= sp.lo and at < sp.hi) bar[sp.lo..sp.hi] else wordAtCol(bar, at))
else
wordAtCol(bar, at);
const word = p.scratch.allocator().dupe(u8, selected) catch return;
p.exitHeader();
if (word.len > 0) {
if (m.button == config.exec_button) {
const named = std.meta.stringToEnum(Builtin, word);
const held = if (named) |b| (if (builtins.registry.takesArg(b)) exec.heldSelection(p, p.active) else null) else exec.heldSelection(p, p.active);
_ = exec.execute(p, p.active, exec.withArg(p, word, held));
} else exec.runBuiltin(p, config.look_cmd, p.active, "", word);
}
}
return;
}
if (m.kind == .press) {
p.exitHeader();
}
switch (m.button) {
.none, .back, .forward => {}, // hover and navigation handled above
.wheel_up, .wheel_down => {
if (m.kind != .press) return;
// re-arms the drift guard below; vertical always scrolls, so
// the answer is never in doubt here
_ = config.wheelTick(&p.wheel_guard, true);
const id = hovered orelse return;
const pane = p.panes[id].?;
if (pane.hasPdf())
panes.Pdf.verticalWheel(p, pane, if (m.button == .wheel_up) -1 else 1)
else
pane.scrollBy(if (m.button == .wheel_up) -config.wheel_rows else config.wheel_rows);
},
.wheel_left, .wheel_right => {
if (m.kind != .press) return;
if (!config.wheelTick(&p.wheel_guard, false)) return;
const id = hovered orelse return;
const pane = p.panes[id].?;
if (pane.hasPdf()) {
panes.Pdf.horizontalWheel(p, pane, if (m.button == .wheel_right) 1 else -1);
// ponytail: no right clamp — overscroll shows blank and the
// next cursor move or left wheel pulls it back
} else if (pane.file != null and !p.settings.wrap) {
// wrapped there is nothing off to the right to reach
const line = panes.File.sourceLine(pane, pane.body.cur_row);
const visual = panes.File.rawDisplayCol(line, @intCast(@max(0, pane.hscroll)));
const next: usize = if (m.button == .wheel_right)
visual +| @as(usize, @intCast(config.wheel_cols))
else
visual -| @as(usize, @intCast(config.wheel_cols));
pane.hscroll = @intCast(panes.File.rawAtDisplay(line, next));
}
},
config.select_button => switch (m.kind) {
.press => {
if (p.drag == .select and p.drag.select.button == config.exec_button) {
if (exec.heldSelection(p, p.drag.select.id)) |tx| {
if (p.chord_arg) |old| p.gpa.free(old);
p.chord_arg = p.gpa.dupe(u8, tx) catch null;
}
return;
}
// acme 3-1: a select tap during a LOOK drag cancels the
// look; the rest of the gesture is inert
if (p.drag == .select and p.drag.select.button == config.look_button) {
if (p.panes[p.drag.select.id]) |t| {
t.sel[@intFromEnum(config.look_button)].state = .none;
if (comptime pdf_enabled)
panes.Pdf.pointerCancel(t, p.drag.select.pdf);
}
p.drag = .none;
return;
}
if (hovered) |mid| {
const mr = p.rects[mid];
const mpane = p.panes[mid].?;
const mtag = p.tagTop(mpane, mr);
if (mcol < mr.x + config.GUTTER and mrow >= mtag and mrow < mtag + mpane.tag_rows) {
p.drag = .{ .move = .{ .id = mid, .cur_x = mcol, .cur_y = mrow } };
return;
}
}
for (0..p.ncol -| 1) |c| {
if (mcol == p.col_x[c] + p.col_w[c] -| 1) {
const corner: @FieldType(@FieldType(Drag, "border_v"), "corner") = if (seamIdxAt(p, c, mrow)) |k|
.{ .col = c, .idx = k }
else if (seamIdxAt(p, c + 1, mrow)) |k|
.{ .col = c + 1, .idx = k }
else
null;
p.drag = .{ .border_v = .{ .left_col = c, .cur_x = mcol, .corner = corner, .cur_y = mrow } };
return;
}
}
for (0..p.ncol) |cc| {
if (mcol < p.col_x[cc] or mcol >= p.col_x[cc] + p.col_w[cc]) continue;
if (seamIdxAt(p, cc, mrow)) |k| {
p.drag = .{ .border_h = .{ .col = cc, .top_idx = k, .cur_y = mrow } };
return;
}
}
const id = hovered orelse return;
const r = p.rects[id];
const pane = p.panes[id] orelse return;
if (id != p.active) if (p.panes[p.active]) |previous| {
previous.focus = .body;
previous.closePrompt();
};
const tag_y = p.tagTop(pane, r);
const body_y = p.bodyTop(pane, r);
if (mcol < r.x + config.GUTTER) {
// gutter scrollbar: a left click scrolls UP to that row
p.active = id;
pane.scrollBy(-(@as(i32, mrow) - @as(i32, body_y)));
} else {
const on_tag = mrow >= tag_y and mrow < tag_y + pane.tag_rows;
if (on_tag and mcol < r.x + TAG_TEXT_INSET) return;
p.active = id;
pane.closePrompt();
const pointed = edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit) orelse return;
pane.clearPointerSelection(sel_slot);
pane.sel[sel_slot] = pointed.sel;
// A clicked tag is typed straight into, from where it was
// clicked; `:` is the door to its normal mode.
pane.focus = if (on_tag) .tag else .body;
if (on_tag) {
// a count typed before the click was meant for the body
pane.body.normal.clear();
pane.tag.mode = .insert;
pane.tag.normal.clear();
pane.tag.vsel.active = false;
pane.tag.msel.active = false;
pane.tag.nsel = 0;
pane.tag.cur_pinned = true;
const at = p.tagOffsetAt(pane, @intCast(@max(0, pointed.sel.r0)), @intCast(@max(0, pointed.sel.c0))) orelse modal.Cursor{ .row = 0, .col = 0 };
pane.tag.cur_row = @intCast(at.row);
pane.tag.cur_col = @intCast(at.col);
}
p.drag = .{ .select = .{
.id = id,
.button = config.select_button,
.ctrl = m.ctrl,
.pdf = panes.Pdf.pointerStart(
p,
pane,
config.select_button,
mcol,
mrow,
pointed.on_tag,
),
} };
}
},
.drag => dragUpdate(p, mcol, mrow, p.pointer_body_hit),
.release => {
if ((p.pointer_body_hit != null or p.pointer_tag_hit != null or p.drag == .column_move) and p.drag.button() == m.button) dragUpdate(p, mcol, mrow, p.pointer_body_hit);
dragRelease(p, config.select_button);
},
.motion => {},
},
config.exec_button, config.look_button => switch (m.kind) {
.press => {
// acme 1-2 / 1-3: the execute (Cut) or look (Paste) button
// tapped while the select button holds a selection drag
if (p.drag == .select and p.drag.select.button == config.select_button) {
chordCutPaste(p, m.button == config.exec_button);
return;
}
// acme 2-3 / 3-2: tapping the OTHER of execute/look during
// an execute or look drag cancels it (textselect23's mask)
if (p.drag == .select) {
if (p.panes[p.drag.select.id]) |t| {
t.sel[@intFromEnum(p.drag.select.button)].state = .none;
if (comptime pdf_enabled)
panes.Pdf.pointerCancel(t, p.drag.select.pdf);
}
p.drag = .none;
return;
}
const id = hovered orelse return;
const r = p.rects[id];
const pane = p.panes[id] orelse return;
// the tag row and the body's first, exactly as the select
// arm above derives them
const tag_y = p.tagTop(pane, r);
const body_y = p.bodyTop(pane, r);
const on_tag = mrow >= tag_y and mrow < tag_y + pane.tag_rows;
if (mcol < r.x + config.GUTTER and !on_tag) {
const local = @as(i32, mrow) - @as(i32, body_y);
if (m.button == config.look_button) p.active = id;
pane.scrollBy(if (m.button == config.look_button) local else -local);
} else if (edit.pointerTextSelection(p, id, mcol, mrow, m.body_hit, p.pointer_tag_hit)) |pointed| {
if (m.button == config.look_button) p.active = id;
pane.clearPointerSelection(@intFromEnum(m.button));
pane.sel[@intFromEnum(m.button)] = pointed.sel;
p.drag = .{
.select = .{
.id = id,
.button = m.button,
// A native body gesture stays native even when its
// point later misses a letterbox or selection.
.pdf = panes.Pdf.pointerStart(
p,
pane,
m.button,
mcol,
mrow,
pointed.on_tag,
),
},
};
}
},
.drag => dragUpdate(p, mcol, mrow, p.pointer_body_hit),
.release => {
if ((p.pointer_body_hit != null or p.pointer_tag_hit != null or p.drag == .column_move) and p.drag.button() == m.button) dragUpdate(p, mcol, mrow, p.pointer_body_hit);
dragRelease(p, m.button);
},
.motion => {},
},
}
}
/// Index of the pane pair in column `c` whose seam (see seamRowOf) is screen
/// row `mrow`. Out-of-range columns simply have no seam.
pub fn seamIdxAt(p: *const Pardes, c: usize, mrow: u16) ?usize {
if (c >= p.ncol) return null;
for (0..p.col_n[c] -| 1) |k| if (mrow == seamRowOf(p, c, k)) return k;
return null;
}
pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) void {
switch (p.drag) {
.border_v => |*d| {
const c = d.left_col;
d.cur_x = if (c + 1 < p.ncol)
clampBorderCol(p.col_x[c], p.col_w[c], p.col_w[c + 1], mcol)
else
mcol;
if (d.corner) |k| if (k.idx + 1 < p.col_n[k.col]) {
const a = p.rects[p.col_panes[k.col][k.idx]];
const b = p.rects[p.col_panes[k.col][k.idx + 1]];
d.cur_y = clampBorderRow(a.y, a.h, b.h, mrow, p.settings.tag_bottom);
} else {
d.cur_y = mrow;
};
},
.border_h => |*d| {
const cc = d.col;
const k = d.top_idx;
if (k + 1 < p.col_n[cc]) {
const a = p.rects[p.col_panes[cc][k]];
const b = p.rects[p.col_panes[cc][k + 1]];
d.cur_y = clampBorderRow(a.y, a.h, b.h, mrow, p.settings.tag_bottom);
} else d.cur_y = mrow;
},
.column_move => |*d| d.cur_x = mcol,
.move => |*d| {
d.cur_x = mcol;
d.cur_y = mrow;
},
.select => |*s| {
if (p.panes[s.id]) |pane| {
const r = p.rects[s.id];
const b = @intFromEnum(s.button);
const tag_rows: i32 = pane.tag_rows;
if (pane.body_rows > 0 and pane.sel[b].r0 >= tag_rows) if (body_hit) |supplied| {
const mapped_hit = bodyHitForPane(p, s.id, supplied, true) orelse return;
pane.sel[b].c1 = @as(i32, mapped_hit.col) - config.GUTTER;
pane.sel[b].r1 = @as(i32, mapped_hit.row) + tag_rows;
return;
};
// A sweep that began on the tag stays on it, on the tag line
// nearest the pointer.
const tag_y = p.tagTop(pane, r);
const tag_line: u16 = @intCast(std.math.clamp(@as(i32, mrow) - @as(i32, tag_y), 0, tag_rows - 1));
if (pane.sel[b].r0 < tag_rows) if (tagColumn(p, .pane, s.id, p.pointer_tag_hit, true, tag_line)) |value| {
pane.sel[b].c1 = @as(i32, value) + pane.tag_scroll;
pane.sel[b].r1 = tag_line;
return;
};
pane.sel[b].c1 = @as(i32, mcol) - @as(i32, r.x + config.GUTTER);
// Sel row tag_rows is the body's FIRST row, the same thing
// pointerTextSelection means by it -- so the drag has to
// measure from the body's top, notice bands included, or a
// selection made while a message is up lands rows away
// from the pointer.
const body_y = p.bodyTop(pane, r);
const body_h = r.h -| pane.tag_rows;
pane.sel[b].r1 = if (pane.sel[b].r0 < tag_rows)
tag_line
else if (body_h == 0)
0
else
@as(i32, @min(@max(mrow, body_y), body_y + body_h - 1)) - @as(i32, body_y) + tag_rows;
if (pane.sel[b].r1 < tag_rows) pane.sel[b].c1 += @as(i32, pane.tag_scroll) - TAG_GAP;
if (comptime pdf_enabled)
panes.Pdf.pointerUpdate(&s.pdf, p, pane, mcol, mrow);
}
},
.none => {},
}
}
fn reportGesture(
p: *Pardes,
id: usize,
cmd: Builtin,
text: []const u8,
on_tag: bool,
operand: exec.PointerOperand,
chorded: bool,
) bool {
const scripted = p.panes[id] orelse return false;
if (scripted.fs.readers == 0) return false;
const is_look = cmd == config.look_cmd;
const action: ctlfs.events.Action = if (is_look)
(if (on_tag) .tag_look else .body_look)
else
(if (on_tag) .tag_exec else .body_exec);
const named = std.meta.stringToEnum(Builtin, exec.commandText(text)) != null;
const range = gestureRange(p, id, text, on_tag, operand);
var flag: u32 = if (named) ctlfs.events.flag_builtin else 0;
if (range.q0 == range.q1 and text.len > 0) flag |= ctlfs.events.flag_expansion;
if (is_look) {
if (!named and std.mem.indexOfAny(u8, text, "/.:") != null) flag |= ctlfs.events.flag_filename;
} else if (chorded) flag |= ctlfs.events.flag_chorded;
return ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text);
}
fn gestureRange(p: *Pardes, id: usize, text: []const u8, on_tag: bool, operand: exec.PointerOperand) ctlfs.pane.State.Range {
const pane = p.panes[id] orelse return .{};
if (on_tag) {
const tag = p.tagText(p.scratch.allocator(), pane) catch return .{};
const sel = operand.expanded orelse operand.preview orelse return .{};
const lead = if (sel.r0 < sel.r1 or (sel.r0 == sel.r1 and sel.c0 <= sel.c1)) sel else Pane.Sel{ .r0 = sel.r1, .c0 = sel.c1 };
const row: usize = @intCast(@max(0, lead.r0));
const start = modal.lineStartOffset(tag, row);
const at = start + panes.File.rawAtDisplay(modal.lineSlice(tag, row), @intCast(@max(0, lead.c0)));
const q0: u32 = @intCast(@min(at, tag.len));
return .{ .q0 = q0, .q1 = @intCast(@min(q0 + text.len, tag.len)) };
}
const f = if (pane.file) |*file| file else return .{};
if (operand.preview) |selection| if (pane.pointerSelection(selection)) |rows| {
if (rows.len == 0) return .{};
const first = rows[0];
const last = rows[rows.len - 1];
const q0 = panes.File.lineStart(p.gpa, f, @intCast(@max(0, first.row))) +
@min(first.lo, panes.File.sourceLine(pane, first.row).len);
const q1 = panes.File.lineStart(p.gpa, f, @intCast(@max(0, last.row))) +
@min(last.hi, panes.File.sourceLine(pane, last.row).len);
// A rectangle may contain disjoint pieces. Such an operand uses
// the event's expansion text instead of inventing a source range.
if (q1 < q0 or !std.mem.eql(u8, f.content[q0..q1], text)) return .{};
return .{ .q0 = @intCast(q0), .q1 = @intCast(q1) };
};
const start: ?modal.Cursor = if (operand.file_word) |w|
.{ .row = @intCast(@max(0, w.row)), .col = @intCast(@max(0, w.lo)) }
else if (operand.expanded orelse operand.preview) |sel| lead: {
// A selection's leading corner in reading order, converted the way
// `pointerOperand` converts the click itself.
const top = @min(sel.r0, sel.r1) - @as(i32, pane.tag_rows);
if (top < 0) break :lead null;
const w = pane.wrapAt(top);
const col = edit.paneByteAtDisplay(p, &pane.body, w.line, w.at, @min(sel.c0, sel.c1) - panes.File.gutterWidth(pane));
break :lead .{
.row = @intCast(@max(0, w.line)),
.col = @intCast(@max(0, col)),
};
} else null;
const cursor = start orelse return .{};
const q0: u32 = @intCast(@min(modal.offsetAt(f.content, cursor), f.content.len));
return .{ .q0 = q0, .q1 = @intCast(@min(q0 + text.len, f.content.len)) };
}
fn dispatchPointerBuiltin(
p: *Pardes,
id: usize,
cmd: Builtin,
text: ?[]const u8,
gesture: ?struct { on_tag: bool, operand: exec.PointerOperand },
) void {
const arg = p.chord_arg;
p.chord_arg = null;
defer if (arg) |a| p.gpa.free(a);
const operand = text orelse {
if (gesture) |g| _ = reportGesture(p, id, cmd, "", g.on_tag, g.operand, arg != null);
return;
};
if (gesture) |g| if (reportGesture(p, id, cmd, operand, g.on_tag, g.operand, arg != null)) return;
exec.runBuiltin(p, cmd, id, "", exec.withArg(p, operand, arg));
}
fn mirrorTtySelection(p: *Pardes, pane: *Pane) void {
const gesture = pane.sel[sel_slot];
const rows = pane.pointerSelection(gesture) orelse return;
const text = pane.rawPointerText(gesture) orelse return;
if (rows.len == 0) return;
const endpoint = rows[if (gesture.r1 >= gesture.r0) rows.len - 1 else 0];
const body = body_layer.bodyText(p, p.scratch.allocator(), pane) catch return;
const line = modal.lineSlice(body, @intCast(@max(0, endpoint.row - panes.Terminal.gridOffset(pane))));
const byte = if (gesture.c1 >= gesture.c0) modal.prevGrapheme(line, @min(endpoint.hi, line.len)) else endpoint.lo;
pane.body.cur_row = pane.surfRow(endpoint.row);
var shown = std.mem.trimEnd(u8, line[@min(endpoint.prompt_bytes, line.len)..], " \t");
if (pane.ovl) |overlay| {
const relative = pane.body.cur_row - overlay.row;
if (relative >= 0 and relative < modal.lineCount(overlay.text))
shown = modal.lineSlice(overlay.text, @intCast(relative));
}
pane.body.cur_col = @intCast(modal.graphemeStart(shown, @min(shown.len, byte -| endpoint.prompt_bytes)));
pane.body.cur_pinned = true;
pane.body.msel.active = false;
pane.body.vsel.active = false;
pane.body.nsel = 0;
pane.body.select = false;
pane.body.append_at = null;
pane.body.sticky_col = -1;
pane.body.normal.clear();
edit.setYank(p, text);
}
fn dragRelease(p: *Pardes, button: Mouse.Button) void {
// A different button's release belongs to a chord or another host
// stream; only the button which began this gesture may end it.
if (p.drag.button() != button) return;
defer p.drag = .none;
switch (p.drag) {
.border_v => |d| {
const c = d.left_col;
const old_seam = p.col_x[c] + p.col_w[c] -| 1;
// the handle is the left pane's LAST column (inclusive), so
// the new width includes it — a no-drag click changes nothing
if (c + 1 < p.ncol and d.cur_x != old_seam)
layout.setColumnPairWidth(p, c, (d.cur_x + 1) -| p.col_x[c]);
if (d.corner) |k| {
if (d.cur_y != seamRowOf(p, k.col, k.idx))
layout.applyRowSplit(p, k.col, k.idx, d.cur_y);
}
},
.border_h => |d| if (d.cur_y != seamRowOf(p, d.col, d.top_idx))
layout.applyRowSplit(p, d.col, d.top_idx, d.cur_y),
.column_move => |d| if (layout.columnBySerial(p, d.serial)) |column| switch (layout.columnDrop(p, column, d.cur_x, d.grab)) {
.stay => {},
.reorder => |target| layout.reorderColumn(p, column, target),
.edge => |x| layout.setColumnPairWidth(p, column - 1, x - p.col_x[column - 1]),
},
.move => |d| {
layout.movePane(p, d.id, d.cur_x, d.cur_y);
if (p.panes[d.id]) |mp| p.stillborn_joiner = mp.serial;
// a file moved into the left column evicts a lone unused shell
if (p.panes[d.id]) |mt| if (mt.file != null) {
if (layout.findPane(p, d.id)) |f| if (f.col == 0)
exec.evictLonePristineTty(p, 0, d.id);
};
},
.select => |s| {
const pane = p.panes[s.id] orelse return;
if (comptime pdf_enabled) {
if (s.pdf.native) {
var release = panes.Pdf.pointerRelease(
p,
pane,
s.pdf,
s.button,
s.chorded,
);
defer release.deinit(p.pdf_gpa);
if (release.probe) |*probe| if (probe.link != null) {
look.followPdfLink(p, s.id, probe);
return;
};
// A native PDF page has no byte offsets of its own to
// report, so a scripted pane cannot intercept this one.
if (release.action) |action| dispatchPointerBuiltin(
p,
s.id,
if (action == .look) config.look_cmd else config.exec_cmd,
release.text,
null,
);
return;
}
}
if (s.chorded) {
// a cut/paste chord consumed this drag; the release is inert
pane.sel[@intFromEnum(s.button)].state = .none;
return;
}
const b = @intFromEnum(s.button);
pane.sel[b].state = .done;
if (s.button == config.select_button) {
const sl = pane.sel[sel_slot];
const dragged = sl.c0 != sl.c1 or sl.r0 != sl.r1;
if (!dragged) pane.sel[sel_slot].state = .none;
if (sl.r0 < pane.tag_rows) {
// On the tag the caret goes where the button came up. A
// click on the path, if it can be renamed, drafts a name.
const at = p.tagOffsetAt(pane, @intCast(@max(0, sl.r1)), @intCast(@max(0, sl.c1)));
if (at) |cursor| {
pane.tag.cur_row = @intCast(cursor.row);
pane.tag.cur_col = @intCast(cursor.col);
} else if (!dragged) p.draftName(pane, @intCast(@max(0, sl.c1)));
return;
}
if (dragged) edit.capturePointerSelection(p, pane, sel_slot) catch {};
if (dragged and pane.body.mode == .tty) mirrorTtySelection(p, pane);
const body_vis = sl.r1 - @as(i32, pane.tag_rows);
if (body_vis >= 0 and pane.body.mode != .tty) {
const w = pane.wrapAt(body_vis);
pane.body.cur_row = w.line;
pane.body.cur_col = edit.paneByteAtDisplay(
p,
&pane.body,
w.line,
w.at,
sl.c1 - (if (pane.file != null) @as(i32, panes.File.gutterWidth(pane)) else 0),
);
pane.body.cur_pinned = true;
if (edit.clampFileCursor(&pane.body)) pane.ensureCursorVisible();
pane.body.msel.active = false;
pane.body.normal.clear();
pane.body.vsel.active = false;
pane.body.nsel = 0; // a click says WHERE the one cursor is
pane.body.select = false;
pane.body.append_at = null;
pane.body.sticky_col = -1;
if (s.ctrl) p.lspRequest(s.id, .definition, "");
}
} else {
const clk = pane.sel[b];
// A right-click on a line NUMBER looks at that line of
// this file -- a sticky context header's number included,
// since the row map holds each header's own source line.
if (s.button == config.look_button and clk.r0 >= pane.tag_rows and clk.r0 == clk.r1 and
clk.c0 == clk.c1 and clk.c0 >= 0 and pane.body.mode != .tty)
if (pane.file) |f| if (f.output == null and clk.c0 < panes.File.gutterWidth(pane)) {
const line = pane.wrapAt(clk.r0 - pane.tag_rows).line;
if (line >= 0) {
pane.sel[b].state = .none;
var buf: [48]u8 = undefined;
const target = std.fmt.bufPrint(&buf, "@p{d}:{d}", .{ s.id, @as(usize, @intCast(line)) + 1 }) catch return;
exec.runBuiltin(p, config.look_cmd, s.id, "", target);
return;
}
};
var operand = exec.pointerOperand(p, pane, clk);
if (operand.expanded) |expanded|
pane.sel[b] = expanded
else if (operand.text == null)
pane.sel[b].state = .none;
if (s.button == config.look_button and clk.r0 >= pane.tag_rows and pane.body.mode != .tty) {
pane.body.cur_row = operand.row;
pane.body.cur_col = operand.col;
if (edit.clampFileCursor(&pane.body)) pane.ensureCursorVisible();
}
const txt = operand.text;
const cmd = if (s.button == config.look_button) config.look_cmd else config.exec_cmd;
if (clk.r0 == 0 and s.button == config.exec_button and pane.prompt == .name) {
const prefix_width = panes.File.displayWidth(p.tagPrefix(pane) catch return);
const on_name = @as(usize, @intCast(@max(0, clk.c0))) < prefix_width;
if (!p.commitTagName(pane)) return;
// Executing a staged filename commits it; executing its
// command tail uses the new identity (especially Save).
if (on_name) return;
const new_width = panes.File.displayWidth(p.tagPrefix(pane) catch return);
const shift = @as(i32, @intCast(new_width)) - @as(i32, @intCast(prefix_width));
if (operand.expanded) |*sel| {
sel.c0 += shift;
sel.c1 += shift;
}
if (operand.preview) |*sel| {
sel.c0 += shift;
sel.c1 += shift;
}
pane.sel[b].c0 += shift;
pane.sel[b].c1 += shift;
}
dispatchPointerBuiltin(p, s.id, cmd, txt, .{
.on_tag = clk.r0 < pane.tag_rows,
.operand = operand,
});
}
},
.none => {},
}
}
fn chordCutPaste(p: *Pardes, cut: bool) void {
const s = &p.drag.select;
const pane = p.panes[s.id] orelse return;
if (comptime pdf_enabled) if (pane.pdf) |pv| {
if (!s.chorded) {
s.chorded = true;
pane.sel[sel_slot].state = .none;
}
// A PDF is immutable: its Cut-side chord is Snarf (copy), while
// Paste has no document mutation to perform.
if (cut and pv.selection_text.len > 0) edit.setYank(p, pv.selection_text);
return;
};
if (@min(pane.sel[sel_slot].r0, pane.sel[sel_slot].r1) < pane.tag_rows) {
// A sweep over the tag cuts from and pastes into the tag's own text;
// the part of it over the prefix is not the tag's to cut.
const t = &pane.tag;
if (!s.chorded) {
s.chorded = true;
const sl = pane.sel[sel_slot];
pane.sel[sel_slot].state = .none;
const forward = sl.r0 < sl.r1 or (sl.r0 == sl.r1 and sl.c0 <= sl.c1);
const start = if (forward) .{ sl.r0, sl.c0 } else .{ sl.r1, sl.c1 };
const end = if (forward) .{ sl.r1, sl.c1 } else .{ sl.r0, sl.c0 };
const origin: modal.Cursor = .{ .row = 0, .col = 0 };
const from = p.tagOffsetAt(pane, @intCast(@max(0, start[0])), @intCast(@max(0, start[1]))) orelse origin;
const to = p.tagOffsetAt(pane, @intCast(@max(0, end[0])), @intCast(@max(0, end[1]))) orelse origin;
t.cur_row = @intCast(to.row);
t.cur_col = @intCast(to.col);
t.cur_pinned = true;
t.vsel = .{ .active = from.row != to.row or from.col != to.col, .row = @intCast(from.row), .col = @intCast(from.col), .explicit = false };
t.msel.active = false;
t.nsel = 0;
}
if (cut) {
if (t.vsel.active) edit.normalDelete(p, t, true);
} else if (t.vsel.active) {
edit.normalReplaceYank(p, t);
} else edit.pasteText(p, t, p.yank orelse return, true, 1);
return;
}
if (pane.body.mode == .tty) {
if (!s.chorded) {
s.chorded = true;
pane.sel[sel_slot].state = .none; // drop the sweep highlight
}
if (cut) return;
const y = p.yank orelse return;
if (y.len == 0) return;
if (panes.Terminal.reportsMouse(pane)) {
const col: u16 = @intCast(std.math.clamp(pane.sel[sel_slot].c1 + 1, 1, 9999));
const row: u16 = @intCast(std.math.clamp(pane.sel[sel_slot].r1 - @as(i32, pane.tag_rows) + 1, 1, 9999));
var mb: [32]u8 = undefined;
if (panes.Terminal.mouseFormatSgr(pane)) {
p.emitWrite(s.id, std.fmt.bufPrint(&mb, "\x1b[<0;{d};{d}M\x1b[<0;{d};{d}m", .{ col, row, col, row }) catch return);
} else {
// ponytail: legacy X10 bytes; add utf8/urxvt formats if an app ever wants them
const cb: u8 = @intCast(32 + @min(col, 222));
const rb: u8 = @intCast(32 + @min(row, 222));
p.emitWrite(s.id, &[_]u8{ 0x1b, '[', 'M', 32, cb, rb, 0x1b, '[', 'M', 35, cb, rb });
}
}
edit.typeToTty(p, s.id, pane, y);
return;
}
if (!s.chorded) {
s.chorded = true;
const sl = pane.sel[sel_slot];
pane.sel[sel_slot].state = .none;
const pfx: i32 = if (pane.file != null) panes.File.gutterWidth(pane) else 0;
// both ends of the sweep through the same last-frame map, so a
// chord over wrapped rows cuts the text that was under it
const w0 = pane.wrapAt(@max(0, sl.r0 - @as(i32, pane.tag_rows)));
const w1 = pane.wrapAt(@max(0, sl.r1 - @as(i32, pane.tag_rows)));
const row0 = w0.line;
const row1 = w1.line;
const col0 = edit.paneByteAtDisplay(p, &pane.body, w0.line, w0.at, sl.c0 - pfx);
const col1 = edit.paneByteAtDisplay(p, &pane.body, w1.line, w1.at, sl.c1 - pfx);
pane.body.cur_row = row1;
pane.body.cur_col = col1;
pane.body.cur_pinned = true;
pane.body.msel.active = false;
pane.body.normal.clear();
if (!pane.isTerminal()) pane.body.mode = .normal;
pane.body.vsel = .{ .active = row0 != row1 or col0 != col1, .row = row0, .col = col0, .explicit = false };
pane.body.nsel = 0;
}
if (cut) {
if (pane.body.vsel.active) edit.normalDelete(p, &pane.body, true);
} else if (pane.body.vsel.active) {
edit.normalReplaceYank(p, &pane.body);
} else if (pane.file) |*f| {
// bare cursor: splice the register in literally (no linewise
// magic) so a paste right after a cut restores the text exactly
const y = p.yank orelse return;
if (y.len == 0) return;
const at = modal.Cursor{ .row = @intCast(@max(0, pane.body.cur_row)), .col = @intCast(@max(0, pane.body.cur_col)) };
edit.pushUndo(p, &pane.body);
const new = modal.insertAt(p.gpa, f.content, at, y) catch return;
panes.File.setContent(p, f, new);
pane.body.vsel = .{ .active = modal.nextGrapheme(y, 0) < y.len, .row = @intCast(at.row), .col = @intCast(at.col), .explicit = false };
const end = modal.advanceBy(at, y);
if (end.col > 0) {
pane.body.cur_row = @intCast(end.row);
pane.body.cur_col = @intCast(modal.prevGrapheme(modal.lineSlice(f.content, end.row), end.col));
} else {
// the register ended in '\n': the cursor lands ON that newline
pane.body.cur_row = @intCast(end.row -| 1);
pane.body.cur_col = @intCast(modal.lineSlice(f.content, end.row -| 1).len);
}
pane.body.cur_pinned = true;
pane.body.sticky_col = -1;
pane.ensureCursorVisible();
} else edit.pasteText(p, &pane.body, p.yank orelse return, true, 1);
}
test "plain left click clears explicit modal selection" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .cols = 40, .rows = 10 });
defer p.deinit();
const pane = try p.setTestFile("abcdef\n");
pane.body.cur_row = 0;
pane.body.cur_col = 5;
pane.body.vsel = .{ .active = true, .row = 0, .col = 1, .explicit = true };
pane.body.msel = .{ .active = true, .r0 = 0, .r1 = 0 };
var frame = std.heap.ArenaAllocator.init(gpa);
defer frame.deinit();
_ = try p.render(frame.allocator());
const rect = p.rects[0];
const click_x = rect.x + config.GUTTER + config.PREFIX_W + 3;
const body_y = rect.y + BOX_H;
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = click_x, .row = body_y } });
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = click_x, .row = body_y } });
try std.testing.expectEqual(@as(i32, 3), pane.body.cur_col);
try std.testing.expect(!pane.body.vsel.active);
try std.testing.expect(!pane.body.msel.active);
try std.testing.expectEqual(.none, pane.sel[@intFromEnum(config.select_button)].state);
}
test "selection drag and queued release do not enter panel pending state" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 50, .rows = 10, .tty_only = true });
defer p.deinit();
const pane = try p.setTestFile("alpha beta\n");
p.acknowledgePanelPresentation(&.{});
const rect = p.rects[0];
const x = rect.x + config.GUTTER + config.PREFIX_W + 1;
const y = rect.y + BOX_H;
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .press, .col = x, .row = y } });
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .drag, .col = x + 2, .row = y } });
try std.testing.expect(!p.presentation.pending);
p.update(.{ .mouse = .{ .button = config.select_button, .kind = .release, .col = x + 2, .row = y } });
try std.testing.expect(p.drag == .none);
try std.testing.expectEqual(.done, pane.sel[sel_slot].state);
}
test "a corner drag's two axes clamp independently" {
// a 100-wide window split 50/50, and a 30-row column split 15/14 under the
// topbar: the shape windowops.snap drives
const lx: u16 = 0;
const lw: u16 = 50;
const rw: u16 = 50;
const ay: u16 = TOPBAR_H;
const ah: u16 = 15;
const bh: u16 = 14;
const row_lo: u16 = ay + BOX_H - 1;
const row_hi: u16 = ay + ah + bh - (BOX_H + 1);
// each axis alone, at both walls
try std.testing.expectEqual(@as(u16, config.MINW), clampBorderCol(lx, lw, rw, 0));
try std.testing.expectEqual(@as(u16, 100 - config.MINW), clampBorderCol(lx, lw, rw, 999));
try std.testing.expectEqual(row_lo, clampBorderRow(ay, ah, bh, 0, false));
try std.testing.expectEqual(row_hi, clampBorderRow(ay, ah, bh, 999, false));
// THE corner property: drag off the right edge while staying mid-height —
// x sits at its wall, y still tracks the mouse row exactly
try std.testing.expectEqual(@as(u16, 100 - config.MINW), clampBorderCol(lx, lw, rw, 999));
try std.testing.expectEqual(@as(u16, 12), clampBorderRow(ay, ah, bh, 12, false));
// and the mirror: pinned at the bottom, x still free
try std.testing.expectEqual(row_hi, clampBorderRow(ay, ah, bh, 999, false));
try std.testing.expectEqual(@as(u16, 40), clampBorderCol(lx, lw, rw, 40));
// both pinned at once is just both walls, not a stuck gesture
try std.testing.expectEqual(@as(u16, config.MINW), clampBorderCol(lx, lw, rw, 0));
try std.testing.expectEqual(row_hi, clampBorderRow(ay, ah, bh, 999, false));
try std.testing.expectEqual(row_lo + 1, clampBorderRow(ay, ah, bh, 0, true));
try std.testing.expectEqual(row_hi + 1, clampBorderRow(ay, ah, bh, 999, true));
try std.testing.expectEqual(@as(u16, 12), clampBorderRow(ay, ah, bh, 12, true));
// degenerate pairs pass through untouched, each on its own terms: a pane
// pair with no room stays draggable even when the column pair has room
try std.testing.expectEqual(@as(u16, 7), clampBorderCol(0, 5, 5, 7));
try std.testing.expectEqual(@as(u16, 40), clampBorderCol(lx, lw, rw, 40));
try std.testing.expectEqual(@as(u16, 9), clampBorderRow(TOPBAR_H, 1, 0, 9, false));
}
test "a v-handle press at the RIGHT column's seam drags both boundaries" {
if (platform == .web) return;
const p = try cornerFixture(std.testing.allocator);
defer p.deinit();
try std.testing.expectEqual(@as(usize, 2), p.col_n[1]);
const handle = p.col_x[0] + p.col_w[0] - 1;
const right_seam = nudgeRightSeam(p, 3);
try std.testing.expect(right_seam != seamRowOf(p, 0, 0));
const w0 = p.col_weight[0];
const v_left = p.panes[p.col_panes[0][0]].?.vweight;
const v_right = p.panes[p.col_panes[1][0]].?.vweight;
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = right_seam } });
try std.testing.expect(p.drag == .border_v);
const corner = p.drag.border_v.corner orelse return error.NoCorner;
try std.testing.expectEqual(@as(usize, 1), corner.col);
try std.testing.expectEqual(@as(usize, 0), corner.idx);
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = handle + 8, .row = right_seam - 4 } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle + 8, .row = right_seam - 4 } });
p.sync();
// both halves committed: the column pair widened, and the RIGHT column's
// pane pair reweighted — while the left column's panes stayed put
try std.testing.expect(p.col_weight[0] > w0);
try std.testing.expect(p.panes[p.col_panes[1][0]].?.vweight != v_right);
try std.testing.expectEqual(v_left, p.panes[p.col_panes[0][0]].?.vweight);
}
test "a tie row still moves the LEFT column's pane pair only" {
if (platform == .web) return;
const p = try cornerFixture(std.testing.allocator);
defer p.deinit();
// put column 1's seam exactly on column 0's, the row the eye reads as one
// line across the whole window
const left_seam = seamRowOf(p, 0, 0);
_ = nudgeRightSeam(p, @as(i32, left_seam) - @as(i32, seamRowOf(p, 1, 0)));
try std.testing.expectEqual(left_seam, seamRowOf(p, 1, 0));
const handle = p.col_x[0] + p.col_w[0] - 1;
const v_left = p.panes[p.col_panes[0][0]].?.vweight;
const v_right = p.panes[p.col_panes[1][0]].?.vweight;
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = left_seam } });
const corner = p.drag.border_v.corner orelse return error.NoCorner;
try std.testing.expectEqual(@as(usize, 0), corner.col);
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = handle, .row = left_seam - 4 } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle, .row = left_seam - 4 } });
p.sync();
try std.testing.expect(p.panes[p.col_panes[0][0]].?.vweight != v_left);
try std.testing.expectEqual(v_right, p.panes[p.col_panes[1][0]].?.vweight);
}
test "a v-handle press at nobody's seam is still a plain edge drag" {
if (platform == .web) return;
const p = try cornerFixture(std.testing.allocator);
defer p.deinit();
const right_seam = nudgeRightSeam(p, 3);
const left_seam = seamRowOf(p, 0, 0);
const handle = p.col_x[0] + p.col_w[0] - 1;
var row: u16 = TOPBAR_H + 1;
while (row == left_seam or row == right_seam) row += 1;
const w0 = p.col_weight[0];
const v_left = p.panes[p.col_panes[0][0]].?.vweight;
const v_right = p.panes[p.col_panes[1][0]].?.vweight;
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = row } });
try std.testing.expect(p.drag == .border_v);
try std.testing.expect(p.drag.border_v.corner == null);
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = handle + 8, .row = row + 5 } });
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle + 8, .row = row + 5 } });
p.sync();
// exactly ONE boundary moved
try std.testing.expect(p.col_weight[0] > w0);
try std.testing.expectEqual(v_left, p.panes[p.col_panes[0][0]].?.vweight);
try std.testing.expectEqual(v_right, p.panes[p.col_panes[1][0]].?.vweight);
}
test "a RIGHT-column corner's two axes clamp independently" {
if (platform == .web) return;
const p = try cornerFixture(std.testing.allocator);
defer p.deinit();
const right_seam = nudgeRightSeam(p, 3);
const handle = p.col_x[0] + p.col_w[0] - 1;
p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = handle, .row = right_seam } });
try std.testing.expectEqual(@as(usize, 1), (p.drag.border_v.corner orelse return error.NoCorner).col);
// off the right edge at mid-height: x parks at its MINW wall, y is the mouse
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = 999, .row = right_seam } });
try std.testing.expectEqual(@as(u16, 100 - config.MINW), p.drag.border_v.cur_x);
try std.testing.expectEqual(right_seam, p.drag.border_v.cur_y);
// and the mirror: below the bottom at mid-width. y parks, x tracks again
p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = handle, .row = 999 } });
try std.testing.expectEqual(handle, p.drag.border_v.cur_x);
const a = p.rects[p.col_panes[1][0]];
const b = p.rects[p.col_panes[1][1]];
try std.testing.expectEqual(clampBorderRow(a.y, a.h, b.h, 999, p.settings.tag_bottom), p.drag.border_v.cur_y);
p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = handle, .row = 999 } });
}
test "pointer coordinates follow presented panel geometry" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20, .tty_only = true });
defer p.deinit();
const pane = p.panes[0].?;
const moving: layout.Track = .{
.serial = pane.serial,
.pane = 0,
.phase = .moving,
.effect = .slide,
.from = .{ .x = 20, .y = 5, .w = 40, .h = 10 },
.to = .{ .x = 0, .y = 1, .w = 80, .h = 18 },
};
p.acknowledgePanelPresentation(&.{moving});
// One shared physical-to-logical map feeds all pointer gestures. The
// selected sample is 26.25% across and 35% down the presented rectangle.
try std.testing.expectEqual(layout.Presentation.CellPosition{ .col = 21, .row = 7 }, p.presentation.pointer(p.screen_w, p.screen_h, 30, 8).?);
// Canonical cells covered only by the not-yet-arrived target are inert.
try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, 2, 2) == null);
// Unrelated screen space remains in the ordinary grid coordinate system.
try std.testing.expectEqual(layout.Presentation.CellPosition{ .col = 100, .row = 10 }, p.presentation.pointer(p.screen_w, p.screen_h, 100, 10).?);
p.acknowledgePanelPresentation(&.{layout.Track{
.serial = pane.serial,
.pane = 0,
.phase = .opening,
.effect = .zoom,
.from = .{ .x = 40, .y = 10, .w = 0, .h = 0 },
.to = .{ .x = 0, .y = 1, .w = 80, .h = 18 },
}});
try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, 40, 10) == null);
// Tracks paint in pane-slot order inside a phase. The later slot is the
// top quad and therefore owns an overlap, even though both are moving.
const overlap = [_]layout.Track{ .{
.serial = pane.serial,
.pane = 0,
.phase = .moving,
.effect = .slide,
.from = .{ .x = 20, .y = 2, .w = 20, .h = 4 },
.to = .{ .x = 0, .y = 2, .w = 20, .h = 4 },
}, .{
.serial = pane.serial + 1,
.pane = 1,
.phase = .moving,
.effect = .slide,
.from = .{ .x = 20, .y = 2, .w = 20, .h = 4 },
.to = .{ .x = 60, .y = 2, .w = 20, .h = 4 },
} };
p.presentation.shown_tracks = @splat(null);
p.presentation.shown_tracks[0] = overlap[0];
p.presentation.shown_tracks[1] = overlap[1];
try std.testing.expectEqual(layout.Presentation.CellPosition{ .col = 65, .row = 3 }, p.presentation.pointer(p.screen_w, p.screen_h, 25, 3).?);
}
test "queued pointer input is inert until a changed layout is presented" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 100, .rows = 16, .tty_only = true });
defer p.deinit();
// Establish that a real host has presented the boot layout.
p.acknowledgePanelPresentation(&.{});
p.settings.panel_transition = .slide;
_ = try p.newShell(1, "");
try std.testing.expect(layout.splitColumn(p, 0, 1, false));
p.sync();
try std.testing.expect(p.presentation.pending);
try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, 10, 4) == null);
var tracks: [MAX_PANES]layout.Track = undefined;
var len: usize = 0;
for (p.presentation.tracks) |maybe| if (maybe) |track| {
tracks[len] = track;
len += 1;
};
p.acknowledgePanelPresentation(tracks[0..len]);
try std.testing.expect(!p.presentation.pending);
// Whether this particular opening sample exposes the chosen cell is the
// transition's concern; it is no longer rejected merely as speculative.
try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, 99, 4) != null);
}
test "pointer rejects panel content cells which have not materialized" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 12, .tty_only = true });
defer p.deinit();
const pane = p.panes[0].?;
const target: layout.Box = .{ .x = 4, .y = 2, .w = 32, .h = 4 };
p.presentation.diffs = try p.gpa.alloc(PanelCellDiff, @as(usize, p.screen_w) * p.screen_h);
@memset(p.presentation.diffs, .unchanged);
for (2..6) |row| {
for (4..36) |col| {
// Distances on both sides of frame five prove that finished ASCII
// bytes are clickable while bytes still walking are not.
p.presentation.diffs[row * p.screen_w + col] = .{ .ascii = .{
.from = ' ',
.to = @intCast(' ' + (col - 4) % 12 + 1),
} };
}
}
p.presentation.diff_state = .ready;
for ([_]layout.Transition{ .dissolve, .ascii }) |effect| {
const frame: u16 = if (effect == .dissolve) 3 else 5;
const track: layout.Track = .{
.serial = pane.serial,
.pane = 0,
.phase = .opening,
.effect = effect,
.frame = frame,
.from = target,
.to = target,
};
p.acknowledgePanelPresentation(&.{track});
var saw_visible = false;
var saw_hidden = false;
for (0..4) |relative_row| for (0..32) |relative_col| {
const rcol: u16 = @intCast(relative_col);
const rrow: u16 = @intCast(relative_row);
const col: u16 = @intCast(4 + relative_col);
const row: u16 = @intCast(2 + relative_row);
const visible = switch (effect) {
.dissolve => layout.dissolveRevealed(
pane.serial,
rcol,
rrow,
track.amount(),
),
.ascii => switch (p.presentation.cellDiff(p.screen_w, p.screen_h, col, row)) {
.ascii => |diff| diff.complete(track.frame),
.unchanged, .visual => true,
},
else => unreachable,
};
const mapped = p.presentation.pointer(p.screen_w, p.screen_h, col, row);
try std.testing.expectEqual(visible, mapped != null);
if (mapped) |point|
try std.testing.expectEqual(layout.Presentation.CellPosition{ .col = col, .row = row }, point);
saw_visible = saw_visible or visible;
saw_hidden = saw_hidden or !visible;
};
try std.testing.expect(saw_visible);
try std.testing.expect(saw_hidden);
}
}
test "unchanged content cells remain clickable through data effects" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 40, .rows = 10, .tty_only = true });
defer p.deinit();
const pane = p.panes[0].?;
p.presentation.diffs = try p.gpa.alloc(PanelCellDiff, @as(usize, p.screen_w) * p.screen_h);
@memset(p.presentation.diffs, .unchanged);
p.presentation.diff_state = .ready;
const box: layout.Box = .{ .x = 2, .y = 2, .w = 20, .h = 4 };
for ([_]layout.Transition{ .dissolve, .ascii }) |effect| {
p.acknowledgePanelPresentation(&.{.{
.serial = pane.serial,
.pane = 0,
.phase = .opening,
.effect = effect,
.from = box,
.to = box,
}});
try std.testing.expectEqual(layout.Presentation.CellPosition{ .col = 8, .row = 3 }, p.presentation.pointer(p.screen_w, p.screen_h, 8, 3).?);
}
}
test "held drag follows acknowledged panels and balances an invisible release" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20, .tty_only = true });
defer p.deinit();
const pane = p.panes[0].?;
const target = layout.panelBox(p.rects[0]);
var track: layout.Track = .{
.serial = pane.serial,
.pane = 0,
.phase = .moving,
.effect = .slide,
.from = .{ .x = 50, .y = 5, .w = 20, .h = 8 },
.to = target,
};
const raw_col: u16 = 55;
const raw_row: u16 = 7;
p.acknowledgePanelPresentation(&.{track});
p.update(.{ .mouse = .{
.button = config.select_button,
.kind = .press,
.col = raw_col,
.row = raw_row,
} });
try std.testing.expect(p.drag == .select);
const first_col = pane.sel[sel_slot].c1;
const first_row = pane.sel[sel_slot].r1;
// Advancing producer state alone cannot move input. Only the sample the
// host says it actually drew may remap the stationary held endpoint.
track.frame = track.effect.frames() - 1;
p.presentation.tracks[0] = track;
try std.testing.expectEqual(first_col, pane.sel[sel_slot].c1);
try std.testing.expectEqual(first_row, pane.sel[sel_slot].r1);
p.acknowledgePanelPresentation(&.{track});
try std.testing.expect(first_col != pane.sel[sel_slot].c1 or
first_row != pane.sel[sel_slot].r1);
const last_col = pane.sel[sel_slot].c1;
const last_row = pane.sel[sel_slot].r1;
track.phase = .opening;
track.effect = .zoom;
track.frame = 0;
track.from = .{
.x = target.x + target.w * 0.5,
.y = target.y + target.h * 0.5,
};
p.acknowledgePanelPresentation(&.{track});
try std.testing.expect(p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) == null);
try std.testing.expectEqual(last_col, pane.sel[sel_slot].c1);
try std.testing.expectEqual(last_row, pane.sel[sel_slot].r1);
// An unrelated release cannot consume the left drag. Its own release is
// nevertheless balanced at the retained endpoint while the cell is dark.
p.update(.{ .mouse = .{
.button = config.exec_button,
.kind = .release,
.col = raw_col,
.row = raw_row,
} });
try std.testing.expect(p.drag == .select);
p.update(.{ .mouse = .{
.button = config.select_button,
.kind = .release,
.col = raw_col,
.row = raw_row,
} });
try std.testing.expect(p.drag == .none);
try std.testing.expectEqual(.done, pane.sel[sel_slot].state);
}
test "mouse thumb buttons navigate once and clear selection gestures" {
const p = try Pardes.init(std.testing.allocator, .{ .cols = 60, .rows = 12 });
defer p.deinit();
const pane = try p.setTestFile("alpha beta\ngamma delta\nepsilon zeta\n");
pane.body.setRange(pane.file.?.content, 0, .{ .anchor = 1, .head = 5 }, true);
p.njumps = 3;
p.jcur = 2;
for (0..3) |i| p.jumps[i] = .{ .pane = 0, .serial = pane.serial, .line = @intCast(i + 1), .col = 1 };
p.drag = .{ .select = .{ .id = 0, .button = config.select_button } };
p.update(.{ .mouse = .{ .button = .back, .kind = .press, .col = 0, .row = 0 } });
try std.testing.expectEqual(@as(usize, 1), p.jcur);
for ([_]Mouse.Kind{ .drag, .release, .motion }) |kind|
p.update(.{ .mouse = .{ .button = .back, .kind = kind, .col = 0, .row = 0 } });
try std.testing.expectEqual(@as(usize, 1), p.jcur);
p.update(.{ .mouse = .{ .button = .forward, .kind = .press, .col = 0, .row = 0 } });
p.update(.{ .mouse = .{ .button = .forward, .kind = .release, .col = 0, .row = 0 } });
try std.testing.expectEqual(@as(usize, 2), p.jcur);
try std.testing.expect(!pane.body.vsel.active);
try std.testing.expectEqual(.none, p.drag);
}
|