diff options
| -rw-r--r-- | dev/gui/gui.zig | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/dev/gui/gui.zig b/dev/gui/gui.zig index 3193d966..6113c9e9 100644 --- a/dev/gui/gui.zig +++ b/dev/gui/gui.zig @@ -3349,6 +3349,8 @@ const WheelRun = struct { mc: MouseCell, drawn: f32 = 0, now_ns: u64 = std.time.ns_per_s, + /// Each frame given a change's lanes (testLanes), as `Changes` would. + lanes: bool = false, fn init(gpa: std.mem.Allocator, core: *pardes.Pardes) !WheelRun { var wr: WheelRun = .{ .g = testGui(10, 20, 6, 12), .core = core, .arena = .init(gpa), .mc = undefined }; @@ -3384,6 +3386,7 @@ const WheelRun = struct { if (!wr.core.needs_frame and !frontendMoving(&wr.g)) return; _ = wr.arena.reset(.retain_capacity); const s = try wr.core.render(wr.arena.allocator()); + if (wr.lanes) try testLanes(gpa, wr.core, s); wr.g.frame = s; wr.g.facts = s.facts.*; wr.g.now_ns = wr.now_ns; @@ -6650,6 +6653,121 @@ test "a wheel's caret rides with its line, the text's own glide, never a spring try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.y)), caretRow(&g, s, now).?); } +/// The active pane's body in a frame. +fn testBody(core: *pardes.Pardes, s: *const pardes.Surface) pardes.Rect { + for (s.regionList()) |region| if (region.kind == .body and region.owner == core.view().active) return region.rect; + unreachable; +} + +/// A change's lanes on the active body's rows as jj.paint lays them for a +/// window (Surface.lanes), for a test: line L's on its row, 1 + L % 3 +/// levels, coloured its line, its level and 0x37, so a strip names its line. +fn testLanes(gpa: std.mem.Allocator, core: *pardes.Pardes, s: *pardes.Surface) !void { + const body = testBody(core, s); + const top = core.panes[core.view().active].?.scroll(); + for (0..body.h) |vr| { + const line: usize = @intCast(top + @as(i32, @intCast(vr))); + for (0..1 + line % 3) |level| + try s.lanes.append(gpa, .{ .x = body.x + 1, .y = body.y + @as(u16, @intCast(vr)), .level = @intCast(level), .rgb = .{ @intCast(line), @intCast(level), 0x37 } }); + } +} + +/// The frame's lane strips (testLanes') against where their lines are +/// drawn: line L of a body whose first line is the core's `top`, its rows +/// `lag` behind (a glide's offset or a touchpad's fraction), at body row +/// L - top - lag (emitScrollRows), with its strips there, cut to the body; +/// a line drawn outside it, none. +fn expectLanesRide(g: *Gui, gpa: std.mem.Allocator, core: *pardes.Pardes, s: *const pardes.Surface, lag: f32) !void { + g.decor.clearRetainingCapacity(); + var groups: Groups = .{}; + const layout = fixedCellLayout(g); + const win_h: f32 = @floatFromInt(@as(u32, s.rows) * g.cell_h); + try buildDecor(g, gpa, s, &groups, layout, @floatFromInt(@as(u32, s.cols) * g.cell_w), win_h, false); + const body = testBody(core, s); + const top = core.panes[core.view().active].?.scroll(); + const body_y0 = @as(f32, @floatFromInt(body.y)) * layout.h; + const body_y1 = @as(f32, @floatFromInt(body.y + body.h)) * layout.h; + var strips: [256]u8 = @splat(0); + for (g.decor.items) |item| { + if (@abs(item.bb * 255 - 0x37) > 0.01 or item.bg * 255 > 2.01) continue; + const line: u8 = @intFromFloat(@round(item.br * 255)); + const at = (@as(f32, @floatFromInt(body.y)) + @as(f32, @floatFromInt(@as(i32, line) - top)) - lag) * layout.h; + try std.testing.expectApproxEqAbs(@max(at, body_y0), (1 - item.y0) / 2 * win_h, 0.01); + try std.testing.expectApproxEqAbs(@min(at + layout.h, body_y1), (1 - item.y1) / 2 * win_h, 0.01); + strips[line] += 1; + } + for (strips, 0..) |n, line| { + const at = (@as(f32, @floatFromInt(body.y)) + @as(f32, @floatFromInt(@as(i32, @intCast(line)) - top)) - lag) * layout.h; + const shown = @min(at + layout.h, body_y1) - @max(at, body_y0); + // A sliver under a hundredth of a pixel either way. + if (@abs(shown) < 0.01) continue; + try std.testing.expectEqual(@as(usize, if (shown > 0) 1 + line % 3 else 0), n); + } +} + +test "a change's lanes ride their lines: mid-glide (a wheel's notch, Ctrl-d) and under a touchpad's fraction each strip is where its line is drawn, cut to the body, and the rows that left keep theirs" { + const gpa = std.testing.allocator; + { + const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 }); + defer core.deinit(); + var text: std.ArrayList(u8) = .empty; + defer text.deinit(gpa); + for (0..200) |i| try text.print(gpa, "line {d}\n", .{i}); + _ = try core.setTestFile(text.items); + for ("100G") |ch| core.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); + var arena: std.heap.ArenaAllocator = .init(gpa); + defer arena.deinit(); + var g = testGui(10, 20, 6, 12); + defer g.glide.deinit(gpa); + defer g.decor.deinit(gpa); + const r = core.view().rects[core.view().active]; + g.scroll_pane = core.view().active; + g.scroll_col = r.x + 10; + g.scroll_row = r.y + r.h - 2; + var now: u64 = std.time.ns_per_s; + var mid: usize = 0; + // The view drawn once at rest, then a notch down (the wheel's) and + // Ctrl-d, each glided to rest, every frame checked. + for ([_]?pardes.Key{ null, null, .{ .cp = 'd', .ctrl = true } }, 0..) |key, step| { + if (key) |k| core.update(.{ .key = k }) else if (step > 0) g.scroll_notch = 1; + var frames: usize = 0; + while (true) : (frames += 1) { + stepGlide(&g, core); + now += std.time.ns_per_s / 60; + core.advance(now); + _ = arena.reset(.retain_capacity); + const s = try core.render(arena.allocator()); + try testLanes(gpa, core, s); + g.now_ns = now; + glideFrame(&g, gpa, s); + if (g.glide.follow.offset != 0) mid += 1; + try expectLanesRide(&g, gpa, core, s, g.glide.follow.offset); + if (!g.glide.moving()) break; + if (frames > 200) return error.NeverSettled; + } + } + try std.testing.expect(mid > 10); + } + // A touchpad's half notch down, settled, then up: the text half a row + // off its cells, and the row that left in the half row it opens. + const core = try wheelRunFile(gpa); + defer core.deinit(); + var wr = try WheelRun.init(gpa, core); + defer wr.deinit(gpa); + defer wr.g.decor.deinit(gpa); + wr.lanes = true; + core.needs_frame = true; + try wr.frame(gpa, &.{}); + for ([_]f32{ -0.5, 0.5 }) |turn| { + try wr.frame(gpa, &.{turn}); + try std.testing.expectEqual(turn, wr.g.scroll_lag); + try expectLanesRide(&wr.g, gpa, core, wr.g.frame.?, wr.g.scroll_lag); + for (0..20) |_| try wr.frame(gpa, &.{}); + try std.testing.expectEqual(@as(f32, 0), wr.g.scroll_lag); + try expectLanesRide(&wr.g, gpa, core, wr.g.frame.?, 0); + } +} + test "every scroll glides on one law: a half page, a page, the far jumps, a run of them retargeting" { const gpa = std.testing.allocator; const core = try pardes.Pardes.init(gpa, .{ .cols = 100, .rows = 40 }); |
