summaryrefslogtreecommitdiff
path: root/src/mouse.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
committerGabriel Schneider <[email protected]>2026-10-31 06:28:48 -0300
commita479792a1ab2b593642d7f648194bfa23ce01dba (patch)
tree96ddee7d879a9084daa07db43b0b7910b072a785 /src/mouse.zig
parente179cc44fe5060c76a5b00c41e53f11209f5acd9 (diff)
parent4c761893e5b3fdae422622cfa02104ab36633045 (diff)
downloadpardes-a479792a1ab2b593642d7f648194bfa23ce01dba.tar.gz
pardes-a479792a1ab2b593642d7f648194bfa23ce01dba.zip
Merge the upstream daemon and replicated client/server refactor
Diffstat (limited to 'src/mouse.zig')
-rw-r--r--src/mouse.zig160
1 files changed, 148 insertions, 12 deletions
diff --git a/src/mouse.zig b/src/mouse.zig
index 2ff4fef3..b7307f2d 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -59,6 +59,9 @@ pub const Drag = union(enum) {
/// This gesture began over a usable native PDF raster. The payload is
/// zero-bit when PDF support is absent.
pdf: panes.Pdf.PointerDrag = .{},
+ /// Where its body was in the frontend's frame at its last hit there:
+ /// a sweep that leaves it is clamped back onto it (`bodyHitForPane`).
+ body: ?Layer.Place = null,
},
fn button(drag: Drag) ?Mouse.Button {
@@ -288,22 +291,29 @@ pub fn bodyHitForPane(p: *const Pardes, id: usize, supplied: Mouse.BodyHit, clam
supplied.metrics.tagline_w == 0 or supplied.metrics.tagline_h == 0) return null;
var mapped_hit = supplied;
if (clamp) {
+ // Onto the body the sweep is in: the place of its last hit there
+ // (the drag's `body`), the hit's own while it is over it.
+ if (supplied.serial != pane.serial) mapped_hit.place = (if (p.drag == .select) p.drag.select.body else null) orelse return null;
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;
+ if (mapped_hit.place.rows == 0) return null;
+ const v = mapped_hit.place.viewport;
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;
+ const x0 = @as(f32, @floatFromInt(v.x)) * bw;
+ const y0 = @as(f32, @floatFromInt(v.y)) * bh;
+ const x1 = x0 + @as(f32, @floatFromInt(v.w)) * bw;
+ const y1 = y0 + @as(f32, @floatFromInt(v.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);
}
+fn bodyPlace(hit: ?Mouse.BodyHit, pane: *const Pane) ?Layer.Place {
+ const h = hit orelse return null;
+ return if (h.serial == pane.serial) h.place 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;
@@ -380,7 +390,7 @@ fn programMouse(p: *Pardes, m: Mouse) bool {
else => return false,
};
const bit: u3 = if (button) |b| @as(u3, 1) << @intCast(@backingInt(b)) else 0;
- const at = p.presentation.pointer(p.screen_w, p.screen_h, @min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1));
+ const at = p.pointerCell(@min(m.col, p.screen_w -| 1), @min(m.row, p.screen_h -| 1));
var mods: panes.terminal.MouseMods = .{ .shift = m.shift, .alt = m.alt, .ctrl = m.ctrl };
var buf: [64]u8 = undefined;
if (p.mouse_grab) |*grab| {
@@ -514,6 +524,8 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void {
current.kind = previous.kind;
current.id = previous.id;
current.serial = previous.serial;
+ // ...and its place, which a clamp brings the pointer back onto.
+ current.place = previous.place;
};
}
// A drag can leave the compact text viewport. Preserve its metrics so
@@ -535,7 +547,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void {
return;
}
p.raw_hover_intent = false;
- const mapped = p.presentation.pointer(p.screen_w, p.screen_h, raw_col, raw_row) orelse {
+ const mapped = p.pointerCell(raw_col, raw_row) orelse {
look.cancelLookHover(p);
if (m.kind == .release) {
if (p.header_sweep) |sweep| if (sweep.button == m.button) {
@@ -805,6 +817,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void {
.id = id,
.button = config.select_button,
.ctrl = m.ctrl,
+ .body = bodyPlace(m.body_hit, pane),
.pdf = panes.Pdf.pointerStart(
p,
pane,
@@ -862,6 +875,7 @@ pub fn handleMouse(p: *Pardes, event: Mouse) void {
.select = .{
.id = id,
.button = m.button,
+ .body = bodyPlace(m.body_hit, pane),
// A native body gesture stays native even when its
// point later misses a letterbox or selection.
.pdf = panes.Pdf.pointerStart(
@@ -931,7 +945,9 @@ pub fn dragUpdate(p: *Pardes, mcol: u16, mrow: u16, body_hit: ?Mouse.BodyHit) vo
const r = p.rects[s.id];
const b = @backingInt(s.button);
const tag_rows: i32 = pane.tag_rows;
+ panes.File.geometry(pane);
if (pane.body_rows > 0 and pane.sel[b].r0 >= tag_rows) if (body_hit) |supplied| {
+ if (supplied.serial == pane.serial) s.body = supplied.place;
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;
@@ -989,10 +1005,17 @@ fn reportGesture(
operand: exec.PointerOperand,
chord_arg: ?[]const u8,
) bool {
- if ((p.panes[id] orelse return false).fs.readers == 0) return false;
return reportRange(p, id, cmd, text, on_tag, gestureRange(p, id, text, on_tag, operand), chord_arg);
}
+/// Whether a script reads pane `id`'s events, and so takes its looks and
+/// execs: the 9P server's, the authority's alone (a replica has its
+/// answer, `tape.authority`).
+fn readsEvents(p: *Pardes, id: usize) bool {
+ const pane = p.panes[id] orelse return false;
+ return pane.fs.readers != 0;
+}
+
/// An event reader told of a look or an exec of `text` at `range` of the
/// tag or body, by a click or a key alike: false when the pane has none.
pub fn reportRange(
@@ -1005,7 +1028,7 @@ pub fn reportRange(
chord_arg: ?[]const u8,
) bool {
const scripted = p.panes[id] orelse return false;
- if (scripted.fs.readers == 0) return false;
+ if (!pardes.tape.authority(readsEvents, .{ p, id })) 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)
@@ -1019,7 +1042,9 @@ pub fn reportRange(
if (is_look) {
if (!named and std.mem.indexOfAny(u8, text, "/.:") != null) flag |= ctlfs.events.flag_filename;
} else if (chord_arg != null) flag |= ctlfs.events.flag_chorded;
- if (!ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text)) return false;
+ // Taken whether or not this process's server has the reader: the
+ // session's said it has (a replica's has none).
+ _ = ctlfs.events.noteAction(p, id, action, range.q0, range.q1, flag, text);
// A chorded exec's argument follows as two records, its text and where
// it came from (`file:#q0,#q1`, empty when that is not known), as acme
// sends them (exec.c:182-188); the pane keeps it for the write-back.
@@ -2214,3 +2239,114 @@ test "a program tracking the mouse gets its clicks, drags and moves over its gri
try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .press }));
try std.testing.expectEqualStrings("", try send(p, &out, col, row, .{ .button = .left, .kind = .release }));
}
+
+const test_metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 };
+
+/// A session and a replica joined to it from its dump: digest for digest
+/// the same, and only the session has drawn a frame (its surface is its
+/// own, `.local`). What a step does with one input must not depend on that.
+fn sessionAndReplica(gpa: std.mem.Allocator, opts: pardes.Options) !struct { a: *Pardes, b: *Pardes } {
+ const a = try Pardes.init(gpa, opts);
+ errdefer a.deinit();
+ a.update(.{ .resize = .{ .cols = opts.cols, .rows = opts.rows, .row_metrics = test_metrics } });
+ a.sync();
+ const bytes = try pardes.dump.dumpBytes(a);
+ defer gpa.free(bytes);
+ const b = try pardes.dump.load(gpa, a.opts, bytes, null);
+ errdefer b.deinit();
+ var frame: std.heap.ArenaAllocator = .init(gpa);
+ defer frame.deinit();
+ _ = try a.render(frame.allocator());
+ try expectSame(a, b, "before the press");
+ return .{ .a = a, .b = b };
+}
+
+/// The two cores' digests equal, else the fields (and pane fields) that differ.
+fn expectSame(a: *const Pardes, b: *const Pardes, when: []const u8) !void {
+ if (pardes.walk.digest(Pardes, a) == pardes.walk.digest(Pardes, b)) return;
+ const names = comptime pardes.walk.fieldNames(Pardes);
+ var da: [names.len]u64 = undefined;
+ var db: [names.len]u64 = undefined;
+ _ = pardes.walk.fieldDigests(Pardes, a, &da);
+ _ = pardes.walk.fieldDigests(Pardes, b, &db);
+ for (names, da, db) |name, x, y| if (x != y) std.debug.print("{s}: {s} differs\n", .{ when, name });
+ const pane_names = comptime pardes.walk.fieldNames(pardes.Pane);
+ for (a.panes, b.panes, 0..) |pa, pb, id| {
+ const x = pa orelse continue;
+ const y = pb orelse continue;
+ var pda: [pane_names.len]u64 = undefined;
+ var pdb: [pane_names.len]u64 = undefined;
+ _ = pardes.walk.fieldDigests(pardes.Pane, x, &pda);
+ _ = pardes.walk.fieldDigests(pardes.Pane, y, &pdb);
+ for (pane_names, pda, pdb) |name, u, v| if (u != v) std.debug.print("{s}: pane {d}.{s} differs\n", .{ when, id, name });
+ }
+ return error.DigestsDiffer;
+}
+
+fn layerCenter(layer: *const Layer, col: u16, comptime tag: bool) [2]f32 {
+ const w: f32 = if (tag) test_metrics.tagline_w else test_metrics.body_w;
+ const h: f32 = if (tag) test_metrics.tagline_h else test_metrics.body_h;
+ return .{
+ @as(f32, @floatFromInt(layer.viewport.x)) * test_metrics.body_w + (@as(f32, @floatFromInt(col)) + 0.5) * w,
+ @as(f32, @floatFromInt(layer.viewport.y)) * test_metrics.body_h + 0.5 * h,
+ };
+}
+
+// A frontend's pointer hit is its own frame's: a step resolves it on the
+// place the hit carries (Layer.Place), never on this process's last frame
+// (`surface`, `.local`), so a session and its replica take one input alike
+// (the variant hunt's, 2026-10-30, after a window client's tag hover).
+test "a tag press is the same state whichever frame this process drew last" {
+ const gpa = std.testing.allocator;
+ const two = try sessionAndReplica(gpa, .{ .tty_only = true, .cols = 80, .rows = 24 });
+ defer two.a.deinit();
+ defer two.b.deinit();
+ const layer = for (two.a.surface.tagLayers()) |*l| {
+ if (l.rows != 0 and l.kind == .pane) break l;
+ } else return error.NoPaneTag;
+ const at = layerCenter(layer, 2, true);
+ const hit = layer.tagHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit;
+ const press: pardes.Event = .{ .mouse = .{ .button = .left, .kind = .press, .col = layer.viewport.x + 2, .row = layer.viewport.y, .tag_hit = hit } };
+ two.a.update(press);
+ two.b.update(press);
+ try expectSame(two.a, two.b, "after the press");
+}
+
+// The same through a body hit (edit.pointerTextSelection, a sweep's drag),
+// on a compact body layer (TreeContext), which a pixel shell inverts.
+test "a body press, drag and release are the same state whichever frame this process drew last" {
+ if (comptime !pardes.syntax.enabled) return error.SkipZigTest;
+ const gpa = std.testing.allocator;
+ const a = try Pardes.init(gpa, .{ .tty_only = true, .cols = 60, .rows = 16 });
+ defer a.deinit();
+ const pane = try a.setTestFile("pub const Box = struct {\n pub fn run() void {\n" ++ (std.mem.asBytes(&@as([30][16]u8, @splat(" // body\n".*)))) ++ " }\n};\n");
+ a.gpa.free(pane.file.?.path);
+ pane.file.?.path = try a.gpa.dupe(u8, "/test.zig");
+ if (!pardes.panes.File.supportsContext(pane)) return error.SkipZigTest;
+ a.update(.{ .resize = .{ .cols = 60, .rows = 16, .row_metrics = test_metrics } });
+ pane.file.?.tree_context = true;
+ pane.file.?.scroll = 8;
+ a.sync();
+ const bytes = try pardes.dump.dumpBytes(a);
+ defer gpa.free(bytes);
+ const b = try pardes.dump.load(gpa, a.opts, bytes, null);
+ defer b.deinit();
+ var frame: std.heap.ArenaAllocator = .init(gpa);
+ defer frame.deinit();
+ _ = try a.render(frame.allocator());
+ // The join agrees, the frame drawn and all.
+ try expectSame(a, b, "before the press");
+ const layer = for (&a.surface.body_layers) |*l| {
+ if (l.rows > 0 and l.serial == pane.serial) break l;
+ } else return error.NoBodyLayer;
+ const at = layerCenter(layer, 8, false);
+ const hit = layer.bodyHit(at[0], at[1], test_metrics.body_w, test_metrics.body_h, test_metrics.tagline_w, test_metrics.tagline_h) orelse return error.NoHit;
+ const col: u16 = @intFromFloat(at[0] / test_metrics.body_w);
+ const row: u16 = @intFromFloat(at[1] / test_metrics.body_h);
+ for ([_]pardes.Mouse.Kind{ .press, .drag, .release }) |kind| {
+ const ev: pardes.Event = .{ .mouse = .{ .button = .left, .kind = kind, .col = col, .row = row, .body_hit = hit } };
+ a.update(ev);
+ b.update(ev);
+ }
+ try expectSame(a, b, "after the press");
+}