summaryrefslogtreecommitdiff
path: root/src/mouse.zig
diff options
context:
space:
mode:
Diffstat (limited to 'src/mouse.zig')
-rw-r--r--src/mouse.zig260
1 files changed, 130 insertions, 130 deletions
diff --git a/src/mouse.zig b/src/mouse.zig
index 4c909104..d15616b5 100644
--- a/src/mouse.zig
+++ b/src/mouse.zig
@@ -162,64 +162,6 @@ fn seamRowOf(p: *const Pardes, c: usize, k: usize) u16 {
return 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.view().active = p.view().cols[1].panes[0];
- p.update(.{ .key = .{ .cp = 'n', .alt = true } }); // a shell below it, same column
- p.sync();
- return p;
-}
-
-test "a pane's last row of text is no resize handle; the rule over the next pane is" {
- const p = try cornerFixture(std.testing.allocator);
- defer p.deinit();
- const seam = seamRowOf(p, 1, 0);
- const inside = p.view().col_x[1] + p.view().col_w[1] / 2;
- p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = inside, .row = seam } });
- try std.testing.expect(p.view().drag != .border_h);
- p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = inside, .row = seam } });
- try std.testing.expect(nudgeRightSeam(p, 2) == seam + 2);
-}
-
-test "a pane's grip dragged up or down its own column moves its top, down to the floor of the pane above" {
- const p = try cornerFixture(std.testing.allocator);
- defer p.deinit();
- const upper = p.view().cols[1].panes[0];
- const lower = p.view().cols[1].panes[1];
- const grip_x = p.view().rects[lower].x;
- // Up, past the pane above's first row: a text pane keeps its tag, a
- // terminal its tag and two rows (layout.floorRows).
- const top = p.view().rects[upper].y;
- const above = p.panes[upper].?;
- const floor: u16 = if (above.isTerminal()) above.tag_rows + layout.min_body_rows else 1;
- p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = grip_x, .row = p.view().rects[lower].y } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = grip_x, .row = top } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = grip_x, .row = top } });
- p.sync();
- try std.testing.expectEqual(floor, p.view().rects[upper].h);
- try std.testing.expectEqual(lower, p.view().cols[1].panes[1]);
- // Down, inside itself: its top follows, the pane above taking the rows.
- const to = p.view().rects[lower].y + 6;
- p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = grip_x, .row = p.view().rects[lower].y } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = grip_x, .row = to } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = grip_x, .row = to } });
- p.sync();
- try std.testing.expectEqual(to, p.view().rects[lower].y);
-}
-
-fn nudgeRightSeam(p: *Pardes, delta: i32) u16 {
- const from = seamRowOf(p, 1, 0);
- const inside = p.view().col_x[1] + p.view().col_w[1] / 2;
- const to: u16 = @intCast(@as(i32, from) + delta);
- // On the rule a pixel frontend draws over the lower pane's first row.
- p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = inside, .row = from + 1, .rule_above = true } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = inside, .row = to + 1 } });
- p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = inside, .row = to + 1 } });
- p.sync();
- return seamRowOf(p, 1, 0);
-}
-
pub const ChromeTarget = struct { col: u16, row: u16 };
/// The chrome a press at `col`, `row` takes: the tag rows; then,
@@ -252,26 +194,6 @@ pub fn chromeTarget(p: *const Pardes, col: u16, row: u16) ?ChromeTarget {
return null;
}
-test "a press near chrome takes it: the tag rows, a column's handle or a seam on its cell or a cell away, a pane's tag; nothing in a body" {
- const p = try cornerFixture(std.testing.allocator);
- defer p.deinit();
- // Column 0's two panes share its rows evenly; column 1's lower one is
- // tall, its middle a body row.
- const handle = p.view().col_x[0] + p.view().col_w[0] - 1;
- const seam = seamRowOf(p, 0, 0);
- const mid = p.view().col_x[0] + p.view().col_w[0] / 2;
- const top = p.view().rects[p.view().cols[0].panes[0]].y;
- const low = p.view().rects[p.view().cols[1].panes[1]];
- const body = low.y + low.h / 2;
- try std.testing.expectEqual(ChromeTarget{ .col = 5, .row = 0 }, chromeTarget(p, 5, 0).?);
- try std.testing.expectEqual(ChromeTarget{ .col = handle, .row = body }, chromeTarget(p, handle, body).?);
- try std.testing.expectEqual(ChromeTarget{ .col = handle, .row = body }, chromeTarget(p, handle + 1, body).?);
- try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = seam }, chromeTarget(p, mid, seam).?);
- try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = seam }, chromeTarget(p, mid, seam - 1).?);
- try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = top }, chromeTarget(p, mid, top).?);
- try std.testing.expect(chromeTarget(p, mid, seam - 3) == null);
-}
-
pub fn paneAt(p: *const Pardes, col: u16, row: u16) ?usize {
for (p.panes, 0..) |slot, id| {
if (slot == null) continue;
@@ -1467,6 +1389,136 @@ fn chordCutPaste(p: *Pardes, cut: bool) edit.Error!void {
} else try edit.pasteText(p, &pane.body, p.registers.text(p.gpa, pardes.Registers.default) orelse return, true, 1);
}
+const test_metrics: pardes.RowMetrics = .{ .body_w = 10, .body_h = 20, .tagline_w = 6, .tagline_h = 12 };
+
+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.view().active = p.view().cols[1].panes[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.view().col_x[1] + p.view().col_w[1] / 2;
+ const to: u16 = @intCast(@as(i32, from) + delta);
+ // On the rule a pixel frontend draws over the lower pane's first row.
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = inside, .row = from + 1, .rule_above = true } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = inside, .row = to + 1 } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = inside, .row = to + 1 } });
+ p.sync();
+ return seamRowOf(p, 1, 0);
+}
+
+/// 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,
+ };
+}
+
+test "a pane's last row of text is no resize handle; the rule over the next pane is" {
+ const p = try cornerFixture(std.testing.allocator);
+ defer p.deinit();
+ const seam = seamRowOf(p, 1, 0);
+ const inside = p.view().col_x[1] + p.view().col_w[1] / 2;
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = inside, .row = seam } });
+ try std.testing.expect(p.view().drag != .border_h);
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = inside, .row = seam } });
+ try std.testing.expect(nudgeRightSeam(p, 2) == seam + 2);
+}
+
+test "a pane's grip dragged up or down its own column moves its top, down to the floor of the pane above" {
+ const p = try cornerFixture(std.testing.allocator);
+ defer p.deinit();
+ const upper = p.view().cols[1].panes[0];
+ const lower = p.view().cols[1].panes[1];
+ const grip_x = p.view().rects[lower].x;
+ // Up, past the pane above's first row: a text pane keeps its tag, a
+ // terminal its tag and two rows (layout.floorRows).
+ const top = p.view().rects[upper].y;
+ const above = p.panes[upper].?;
+ const floor: u16 = if (above.isTerminal()) above.tag_rows + layout.min_body_rows else 1;
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = grip_x, .row = p.view().rects[lower].y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = grip_x, .row = top } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = grip_x, .row = top } });
+ p.sync();
+ try std.testing.expectEqual(floor, p.view().rects[upper].h);
+ try std.testing.expectEqual(lower, p.view().cols[1].panes[1]);
+ // Down, inside itself: its top follows, the pane above taking the rows.
+ const to = p.view().rects[lower].y + 6;
+ p.update(.{ .mouse = .{ .button = .left, .kind = .press, .col = grip_x, .row = p.view().rects[lower].y } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .drag, .col = grip_x, .row = to } });
+ p.update(.{ .mouse = .{ .button = .left, .kind = .release, .col = grip_x, .row = to } });
+ p.sync();
+ try std.testing.expectEqual(to, p.view().rects[lower].y);
+}
+
+test "a press near chrome takes it: the tag rows, a column's handle or a seam on its cell or a cell away, a pane's tag; nothing in a body" {
+ const p = try cornerFixture(std.testing.allocator);
+ defer p.deinit();
+ // Column 0's two panes share its rows evenly; column 1's lower one is
+ // tall, its middle a body row.
+ const handle = p.view().col_x[0] + p.view().col_w[0] - 1;
+ const seam = seamRowOf(p, 0, 0);
+ const mid = p.view().col_x[0] + p.view().col_w[0] / 2;
+ const top = p.view().rects[p.view().cols[0].panes[0]].y;
+ const low = p.view().rects[p.view().cols[1].panes[1]];
+ const body = low.y + low.h / 2;
+ try std.testing.expectEqual(ChromeTarget{ .col = 5, .row = 0 }, chromeTarget(p, 5, 0).?);
+ try std.testing.expectEqual(ChromeTarget{ .col = handle, .row = body }, chromeTarget(p, handle, body).?);
+ try std.testing.expectEqual(ChromeTarget{ .col = handle, .row = body }, chromeTarget(p, handle + 1, body).?);
+ try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = seam }, chromeTarget(p, mid, seam).?);
+ try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = seam }, chromeTarget(p, mid, seam - 1).?);
+ try std.testing.expectEqual(ChromeTarget{ .col = mid, .row = top }, chromeTarget(p, mid, top).?);
+ try std.testing.expect(chromeTarget(p, mid, seam - 3) == null);
+}
+
test "plain left click clears explicit modal selection" {
const gpa = std.testing.allocator;
const p = try Pardes.init(gpa, .{ .cols = 40, .rows = 10 });
@@ -1915,58 +1967,6 @@ 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 = .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