diff options
Diffstat (limited to 'src/draw.zig')
| -rw-r--r-- | src/draw.zig | 444 |
1 files changed, 0 insertions, 444 deletions
diff --git a/src/draw.zig b/src/draw.zig index 9db5d0ff..fe4921ff 100644 --- a/src/draw.zig +++ b/src/draw.zig @@ -160,166 +160,6 @@ fn inkOn(bg: [3]u8) [3]u8 { return if (lum > 140) .{ 0x00, 0x00, 0x00 } else .{ 0xff, 0xff, 0xff }; } -/// The focused cursor's box in grid cells, at the pitch it is drawn at: a -/// tag's at the tagline's (Layer.Metrics), else a grid cell. -fn focusedCursorBox(p: *const Pardes, s: *const Surface) ?animation.Box { - const cursor = s.cursor orelse return null; - const m = p.row_metrics; - const bw: f32 = @floatFromInt(@max(1, m.body_w)); - const bh: f32 = @floatFromInt(@max(1, m.body_h)); - const tw: f32 = @floatFromInt(@max(1, m.tagline_w)); - const th: f32 = @floatFromInt(@max(1, @min(m.tagline_h, m.body_h))); - for (s.tagLayers()) |*layer| { - const caret = layer.cursor orelse continue; - if (layer.rows == 0 or layer.viewport.y + caret.y != cursor.y) continue; - if (cursor.x < layer.viewport.x or cursor.x >= layer.viewport.x + layer.viewport.w) continue; - const w = if (caret.bar) tw / bw / 8 else tw / bw; - const x = layer.caretSpan(caret.x, 1, tw / bw, w)[0]; - return .{ .x = x, .y = @as(f32, @floatFromInt(cursor.y)) + (bh - th) / 2 / bh, .w = w, .h = th / bh }; - } - return .{ .x = @floatFromInt(cursor.x), .y = @floatFromInt(cursor.y), .w = if (cursor.bar) 1.0 / 8.0 else 1, .h = 1 }; -} - -/// G3 (docs/render-pipeline.md ยง5.4): the focused cursor on its springs, -/// at the Motion flavour's pace, a jump gliding and a step easing over a -/// few frames (CursorGlide.aim), the thin insert-mode caret as the block; -/// blinking unless typing. A pixel shell's: a grid's cursor is the -/// terminal's own. -/// -/// The springs hold the cursor where the pane's final layout puts it, and -/// the quad is drawn through the pane's frame (paneFrame): while the pane -/// slides, zooms or is dragged, the cursor rides with its text. A pane that -/// moved in the layout carries the springs with it (they keep their place -/// in the pane), so with no transition the cursor lands with the text. A -/// focus change glides from where the cursor is drawn to the new pane's -/// cursor, wherever that pane is drawn now. -fn glideCursor(p: *Pardes, s: *Surface) void { - s.chrome.cursor_glide = 0; - s.chrome.cursor_alpha = 1; - s.chrome.cursor_idle_ms = std.math.maxInt(u32); - if (!builtins.frontend.lift) return; - const box = focusedCursorBox(p, s) orelse { - // No cursor drawn (a PDF or picture took focus, a program hid its - // own): a glide under way has nowhere to land, and its springs, - // never stepped again, held continuous() and the frames on for - // ever. It stops; the next cursor lands where it is. - p.cursor_glide = .{}; - p.cursor_crossing = false; - return; - }; - const motion = animation.Motion.of(p.settings.motion); - // The thin insert-mode caret glides as the block does (a jump smears - // and bows, a step eases); a scroll lands it, below. - var how: animation.CursorGlide.How = .ease; - var frame: PaneFrame = .{}; - // In a column's or the workspace's tag the cursor is not the pane's, - // and does not move with it. - var inside = false; - if (p.panes[p.active]) |pane| { - const rect = p.rects[p.active]; - const cx = box.x + box.w / 2; - const cy = box.y + box.h / 2; - inside = cx >= @as(f32, @floatFromInt(rect.x)) and cx <= @as(f32, @floatFromInt(rect.x + rect.w)) and - cy >= @as(f32, @floatFromInt(rect.y)) and cy <= @as(f32, @floatFromInt(rect.y + rect.h)); - if (inside) frame = paneFrame(s, pane.serial, rect); - const origin: [2]f32 = .{ @floatFromInt(rect.x), @floatFromInt(rect.y) }; - const view = p.cursor_view; - const line: i64 = if (pane.file) |f| @intCast(f.scroll) else if (pane.isTerminal()) panes.terminal.gridOffset(pane) else 0; - if (view.serial == pane.serial and view.inside == inside) { - // The pane moved in the layout: the springs move with it (its - // own cursor's; one in a column's tag stays). - if (inside) p.cursor_glide.shift(origin[0] - view.origin[0], origin[1] - view.origin[1]); - // Scrolled (a file's view, a terminal's output): the caret did not - // move, the text did. Its springs land on its cell (the core's - // view is the settled one), so a page that keeps its row leaves - // it still, and a scroll that carries its line has the shell draw - // it with the text's own glide (Glide.caret_rides), one picture, - // never a second spring chasing the first. - if (inside and line != view.line) how = .land; - } else if (view.serial != 0 and p.cursor_glide.placed) { - // Focus moved (to another pane, or between a pane and a - // column's or the workspace's tag): the glide starts where the - // cursor is drawn, in the old place's frame, and heads for the - // new cursor through the new one's. - const old: PaneFrame = if (!view.inside) .{} else for (p.panes, 0..) |slot, id| { - const other = slot orelse continue; - if (other.serial != view.serial) continue; - const r = p.rects[id]; - p.cursor_glide.shift(@as(f32, @floatFromInt(r.x)) - view.origin[0], @as(f32, @floatFromInt(r.y)) - view.origin[1]); - break paneFrame(s, view.serial, r); - } else .{}; - const at = p.cursor_glide.centreAt(p.now_ns); - const seen = frame.unplace(old.place(at)); - p.cursor_glide.shift(seen[0] - at[0], seen[1] - at[1]); - p.cursor_crossing = true; - p.cursor_held = p.drag == .select; - } - p.cursor_view = .{ .serial = pane.serial, .line = line, .origin = origin, .inside = inside }; - } - // A click that took focus places the cursor on its release; aimed at - // the pane's old cursor meanwhile, it would glide there first. - if (p.drag != .select) p.cursor_held = false; - if (!p.cursor_held) p.cursor_glide.aim(box, p.now_ns, motion, how); - _ = p.cursor_glide.step(p.now_ns); - const moving = p.cursor_glide.moving(); - if (!moving and !p.cursor_held) p.cursor_crossing = false; - // Drawn as the quad while it glides, while its pane moves and while it - // waits for a click's release. - s.chrome.cursor_glide = 2 | @as(u8, @intFromBool(moving or frame.moving() or p.cursor_held)); - // The shell blinks it, from how long ago the last key or click was. - if (p.settings.cursor_blink) - s.chrome.cursor_idle_ms = @intCast(@min(std.math.maxInt(u32) - 1, (p.now_ns -| p.input_ns) / std.time.ns_per_ms)); - // A little less than solid while it glides, so the glyphs it passes - // stay legible (less on the long flavours), solid again as it lands. - const remaining = p.cursor_glide.remaining(p.now_ns); - const see_through: f32 = if (motion.zeta < 1) 0.7 else 0.85; - s.chrome.cursor_alpha = if (moving) see_through + (1 - see_through) * std.math.clamp(1 - remaining / 0.5, 0, 1) else 1; - const quad = p.cursor_glide.sample(p.now_ns, motion); - // Never outside the focused pane as drawn, however far a flavour - // overshoots or a scroll carried it: it squashes against the pane's - // edges. Crossing to another pane, it may pass over any of the grid. - const bounds: animation.Box = if (p.cursor_crossing or !inside) - .{ .w = @floatFromInt(s.cols), .h = @floatFromInt(s.rows) } - else - frame.at; - for (quad, 0..) |corner, i| { - const point = frame.place(corner); - s.chrome.cursor_quad[i * 2] = std.math.clamp(point[0], bounds.x, bounds.x + bounds.w); - s.chrome.cursor_quad[i * 2 + 1] = std.math.clamp(point[1], bounds.y, bounds.y + bounds.h); - } -} - -/// Where a pane is drawn against where the layout puts it: its transition -/// track's presented box against the track's target, or the layout's box -/// both ways when it is still. Maps grid points between the two. -const PaneFrame = struct { - to: animation.Box = .{ .w = 1, .h = 1 }, - at: animation.Box = .{ .w = 1, .h = 1 }, - - fn moving(f: PaneFrame) bool { - return !std.meta.eql(f.to, f.at); - } - - fn place(f: PaneFrame, point: [2]f32) [2]f32 { - return .{ f.at.x + (point[0] - f.to.x) * scale(f.at.w, f.to.w), f.at.y + (point[1] - f.to.y) * scale(f.at.h, f.to.h) }; - } - - fn unplace(f: PaneFrame, point: [2]f32) [2]f32 { - return .{ f.to.x + (point[0] - f.at.x) * scale(f.to.w, f.at.w), f.to.y + (point[1] - f.at.y) * scale(f.to.h, f.at.h) }; - } - - fn scale(num: f32, den: f32) f32 { - return if (den > 0.001) num / den else 1; - } -}; - -fn paneFrame(s: *const Surface, serial: u32, rect: Rect) PaneFrame { - _ = s; - _ = serial; - const still = layout.panelBox(rect); - return .{ .to = still, .at = still }; -} - pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { const taped = pardes.tape.enter(p, .render); defer pardes.tape.leave(p, taped); @@ -526,7 +366,6 @@ pub fn render(p: *Pardes, arena: std.mem.Allocator) !*Surface { } } }; - glideCursor(p, s); return s; } @@ -1357,174 +1196,6 @@ test "a notice wider than its band, as a shader's compile error is, draws its en } } -test "with no motion a focus switch asks for no frames" { - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 24 }); - defer p.deinit(); - _ = try p.setTestFile("text\n"); - p.newScratchBelow(0); - // With motion the cursor glides to the other pane (glideCursor). - p.settings.motion = .off; - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - p.advance(std.time.ns_per_s); - _ = try p.render(arena.allocator()); - p.active = 0; - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - try std.testing.expect(!p.continuous()); -} - -test "the caret is its own body: a page leaves it still under the moving text, a wheel scroll lands its springs on its line's new row" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); - defer p.deinit(); - var text: std.ArrayList(u8) = .empty; - defer text.deinit(std.testing.allocator); - for (0..400) |i| try text.print(std.testing.allocator, "line {d}\n", .{i}); - const pane = try p.setTestFile(text.items); - p.settings.cursor_blink = false; - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - var now: u64 = std.time.ns_per_s; - p.advance(now); - // Mid-file, where a half page keeps the caret's row. - for ("200G") |ch| p.update(.{ .key = .{ .cp = ch, .text = &.{ch} } }); - _ = try p.render(arena.allocator()); - now += std.time.ns_per_s; - p.advance(now); - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - // Ctrl-d: the caret moves with the view and keeps its screen row: it - // does not ride the glide, and it does not move. - const row_before = (try p.render(arena.allocator())).cursor.?.y; - p.update(.{ .key = .{ .cp = 'd', .ctrl = true } }); - now += 16 * std.time.ns_per_ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - var s = try p.render(arena.allocator()); - try std.testing.expect(pane.file.?.scroll > 0); - try std.testing.expectEqual(row_before, s.cursor.?.y); - try std.testing.expectEqual(@as(u8, 0), s.chrome.cursor_glide & 1); - try std.testing.expectEqual(@as(f32, @floatFromInt(row_before)), s.chrome.cursor_quad[1]); - // The view scrolled with the caret on its line: its springs land on - // the line's new row at once, no second glide; the shell carries it - // there with the text's own (Glide.caret_rides). - pane.scrollBy(-3); - now += 16 * std.time.ns_per_ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 0), s.chrome.cursor_glide & 1); - try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.y)), s.chrome.cursor_quad[1]); -} - -test "the focused cursor glides a jump, eases a step, the thin caret too, and is exact at rest" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 30 }); - defer p.deinit(); - _ = try p.setTestFile(std.mem.asBytes(&@as([20][20]u8, @splat("some text on a line\n".*)))); - p.settings.cursor_blink = false; - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - const ms = std.time.ns_per_ms; - var now: u64 = std.time.ns_per_s; - p.advance(now); - _ = try p.render(arena.allocator()); - // A step down: eases there in a few frames. - p.update(.{ .key = .{ .cp = 'j' } }); - now += 16 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - var s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); - now += 100 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); - const before = s.cursor.?; - // To the last line: it glides, a quad partway, and asks for frames. - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'e' } }); - now += 16 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - const target = s.cursor.?; - try std.testing.expect(target.y > before.y + 5); - try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); - try std.testing.expect(s.chrome.cursor_quad[1] < @as(f32, @floatFromInt(target.y)) - 1); - try std.testing.expect(p.continuous()); - // Frame by frame to rest: gliding exactly while it moves (the shell then - // draws the quad, never the cells' cursor too), at under full alpha - // until it lands, solid as it does. - var frames: usize = 0; - while (p.continuous()) : (frames += 1) { - now += 7 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - try std.testing.expect(s.chrome.cursor_glide & 2 != 0); - try std.testing.expectEqual(p.cursor_glide.moving(), s.chrome.cursor_glide & 1 != 0); - if (s.chrome.cursor_glide & 1 != 0) try std.testing.expect(s.chrome.cursor_alpha <= 1 and s.chrome.cursor_alpha >= 0.7); - for (s.chrome.cursor_quad) |value| try std.testing.expect(std.math.isFinite(value)); - } - try std.testing.expect(frames > 3); - try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); - try std.testing.expectEqual(@as(f32, 1), s.chrome.cursor_alpha); - // At rest the quad is the cursor's own cell. - try std.testing.expectEqual(@as(f32, @floatFromInt(target.x)), s.chrome.cursor_quad[0]); - try std.testing.expectEqual(@as(f32, @floatFromInt(target.y)), s.chrome.cursor_quad[1]); - // A mode change mid-glide: the thin caret glides on to the mark. - p.update(.{ .key = .{ .cp = 'g' } }); - p.update(.{ .key = .{ .cp = 'g' } }); - now += 7 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); - p.update(.{ .key = .{ .cp = 'i' } }); - now += 7 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 3), s.chrome.cursor_glide); - try std.testing.expect(s.cursor.?.bar); - while (p.continuous()) { - now += 7 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - s = try p.render(arena.allocator()); - } - // At rest, a bar's width at the caret's cell. - try std.testing.expectEqual(@as(f32, @floatFromInt(s.cursor.?.x)), s.chrome.cursor_quad[0]); - try std.testing.expectApproxEqAbs(@as(f32, 1.0 / 8.0), s.chrome.cursor_quad[2] - s.chrome.cursor_quad[0], 1e-4); -} - -test "a terminal's cursor lands with scrolling output, never gliding against it" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 80, .rows = 12 }); - defer p.deinit(); - while (p.nextEffect()) |_| {} - const term = p.active; - try std.testing.expect(p.panes[term].?.isTerminal()); - p.settings.cursor_blink = false; - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - const ms = std.time.ns_per_ms; - var now: u64 = std.time.ns_per_s; - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = std.mem.asBytes(&@as([20][6]u8, @splat("line\r\n".*))) ++ "$ some command typed" } }); - p.advance(now); - _ = try p.render(arena.allocator()); - // Enter: three lines of output scroll up, the cursor to a new prompt. - p.update(.{ .output = .{ .pane = @intCast(term), .bytes = "\r\nout\r\nout\r\nout\r\n$ " } }); - now += 16 * ms; - p.advance(now); - _ = arena.reset(.retain_capacity); - const s = try p.render(arena.allocator()); - try std.testing.expectEqual(@as(u8, 2), s.chrome.cursor_glide); -} - test "a monkey over notices, prompts, tags and tiny panes at a gui's metrics never panics" { // The snapshot harness runs the tty, whose tagline pitch is the body's, // so it never reaches what a narrower tagline does to a notice's band: @@ -1679,118 +1350,3 @@ test "a body with context rows is painted once, and the grid's rows are what a p } } -/// Two columns, the file in the left one and a shell in the right, the -/// right one focused with its cursor settled. -fn twoColumns(p: *Pardes, arena: *std.heap.ArenaAllocator) !void { - _ = try p.setTestFile(std.mem.asBytes(&@as([6][20]u8, @splat("some text on a line\n".*)))); - _ = try p.newShell(1, ""); - try std.testing.expect(layout.splitColumn(p, 0, 1, false)); - p.sync(); - p.advance(std.time.ns_per_s); - _ = try p.render(arena.allocator()); - p.presented(); - p.advance(2 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - p.presented(); - try std.testing.expect(!p.cursor_glide.moving()); -} - -test "focus moved to another column glides from where the cursor was drawn" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); - defer p.deinit(); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - p.settings.motion = .smooth; - try twoColumns(p, &arena); - p.active = 0; - p.advance(3 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - const before = try p.render(arena.allocator()); - try std.testing.expect(!p.cursor_glide.moving()); - const was: [2]f32 = .{ before.chrome.cursor_quad[0], before.chrome.cursor_quad[1] }; - - p.active = 1; - _ = arena.reset(.retain_capacity); - const start = try p.render(arena.allocator()); - // It sets off from where it was drawn, in the left column... - try std.testing.expectApproxEqAbs(was[0], start.chrome.cursor_quad[0], 0.01); - try std.testing.expectApproxEqAbs(was[1], start.chrome.cursor_quad[1], 0.01); - try std.testing.expect(p.cursor_glide.moving() and start.chrome.cursor_glide & 1 != 0); - // ...crosses the grid, not squashed against the right one's edge... - p.advance(3 * std.time.ns_per_s + 40 * std.time.ns_per_ms); - _ = arena.reset(.retain_capacity); - const mid = try p.render(arena.allocator()); - try std.testing.expect(mid.chrome.cursor_quad[0] > was[0] and mid.chrome.cursor_quad[0] < @as(f32, @floatFromInt(p.rects[1].x)) + 1); - // ...and lands on the right one's cursor. - p.advance(5 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - const landed = try p.render(arena.allocator()); - const box = focusedCursorBox(p, landed).?; - try std.testing.expectApproxEqAbs(box.x, landed.chrome.cursor_quad[0], 0.01); - try std.testing.expect(!p.cursor_glide.moving() and !p.cursor_crossing); -} - -test "a click that takes focus glides the cursor once, to where it lands on release" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); - defer p.deinit(); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - p.settings.motion = .smooth; - try twoColumns(p, &arena); - p.active = 0; - p.advance(3 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - const before = try p.render(arena.allocator()); - const was: [2]f32 = .{ before.chrome.cursor_quad[0], before.chrome.cursor_quad[1] }; - // Pressed in the other column: focus moves, the cursor waits. - p.active = 1; - p.drag = .{ .select = .{ .id = 1, .button = config.select_button } }; - for (0..3) |_| { - p.advance(p.now_ns + 100 * std.time.ns_per_ms); - _ = arena.reset(.retain_capacity); - const held = try p.render(arena.allocator()); - try std.testing.expectApproxEqAbs(was[0], held.chrome.cursor_quad[0], 0.01); - try std.testing.expectApproxEqAbs(was[1], held.chrome.cursor_quad[1], 0.01); - } - // Released: one glide, from there to the placed cursor. - p.drag = .none; - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - try std.testing.expect(p.cursor_glide.moving()); - p.advance(p.now_ns + 5 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - const landed = try p.render(arena.allocator()); - try std.testing.expectApproxEqAbs(focusedCursorBox(p, landed).?.x, landed.chrome.cursor_quad[0], 0.01); -} - -test "focus moving to a pane with no cursor while the cursor glides lets the session go idle" { - if (!builtins.frontend.lift) return error.SkipZigTest; - const p = try Pardes.init(std.testing.allocator, .{ .cols = 120, .rows = 20 }); - defer p.deinit(); - var arena: std.heap.ArenaAllocator = .init(std.testing.allocator); - defer arena.deinit(); - p.settings.motion = .smooth; - try twoColumns(p, &arena); - p.active = 0; - p.advance(3 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - p.active = 1; - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - try std.testing.expect(p.cursor_glide.moving()); - // Mid-glide, the pane focused draws no cursor (as a PDF pane does). - p.panes[1].?.body.mode = .tty; - p.update(.{ .output = .{ .pane = 1, .bytes = "\x1b[?25l" } }); - p.advance(3 * std.time.ns_per_s + 20 * std.time.ns_per_ms); - _ = arena.reset(.retain_capacity); - const hidden = try p.render(arena.allocator()); - try std.testing.expect(hidden.cursor == null); - p.advance(5 * std.time.ns_per_s); - _ = arena.reset(.retain_capacity); - _ = try p.render(arena.allocator()); - try std.testing.expect(!p.cursor_glide.moving() and !p.continuous()); -} |
