diff options
Diffstat (limited to 'src/gui')
| -rw-r--r-- | src/gui/gui.zig | 85 |
1 files changed, 56 insertions, 29 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig index 8163581a..3f7dd7ca 100644 --- a/src/gui/gui.zig +++ b/src/gui/gui.zig @@ -5428,8 +5428,6 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde shifted.y_off -= glide.offset * layout.h; const kept = holdContext(g, surface, glide.pane, held, bh, layout); shifted.y_off += kept.lift; - const cursor_idx: u32 = if (surface.cursor) |cu| @as(u32, cu.y) * surface.cols + cu.x else std.math.maxInt(u32); - const cursor_bar = if (surface.cursor) |cu| cu.bar else false; var n: u32 = 0; // Only the rows the scissor shows: lagging a body or more, every body // row is past the edge, and drawing them all was twice the frame's @@ -5442,8 +5440,9 @@ fn emitGlideRows(g: *Gui, instances: [*]CellInstance, base: u32, surface: *parde const row_cursor: ?usize = if (surface.cursor) |cu| if (cu.y == row) cu.x else null else null; var col = x0; while (col < x0 + bw) : (col += 1) { - const sidx: u32 = @as(u32, row) * surface.cols + col; - emitInstance(g, instances, base + n, col, row, shifted, win_w, win_h, null, cellFontRole(surface.at(col, row)), line, col, row_cursor, false, sidx == cursor_idx and !cursor_bar, page); + // No caret among the moving rows: it is drawn from its own + // springs (the quad), never carried by the text's glide. + emitInstance(g, instances, base + n, col, row, shifted, win_w, win_h, null, cellFontRole(surface.at(col, row)), line, col, row_cursor, false, false, page); n += 1; } } @@ -5545,6 +5544,8 @@ const GlideRun = struct { const Step = struct { frame: usize, key: pardes.Key }; offsets: [600]f32 = undefined, lines: [600]i64 = undefined, + carets: [600]f32 = undefined, + cells: [600]f32 = undefined, frames: usize = 0, fn play(seq: *GlideRun, g: *Gui, core: *pardes.Pardes, gpa: std.mem.Allocator, arena: *std.heap.ArenaAllocator, script: []const Step, frames: usize) !void { @@ -5561,10 +5562,27 @@ const GlideRun = struct { if (g.glide.moving()) g.glide.advance(g.glide.now_ns, g.glide.motion); seq.offsets[frame] = g.glide.offset; seq.lines[frame] = @intCast(core.panes[core.active].?.file.?.scroll); + seq.carets[frame] = caretRow(g, s) orelse -1; + seq.cells[frame] = if (s.cursor) |cu| @floatFromInt(cu.y) else -1; } seq.frames = frames; } + /// The caret's invariants over the run: while its cell stays and it is + /// on it, it does not move a pixel; it only ever closes on its cell (a + /// twentieth of a row of play for a jump's bow); it lands within half a + /// second of its last change. + fn checkCaret(seq: *const GlideRun) !void { + var changed: usize = 0; + for (1..seq.frames) |k| { + if (seq.cells[k] < 0 or seq.cells[k - 1] < 0) continue; + if (seq.cells[k] != seq.cells[k - 1]) changed = k; + if (seq.cells[k] == seq.cells[k - 1] and seq.carets[k - 1] == seq.cells[k - 1]) try std.testing.expectEqual(seq.carets[k - 1], seq.carets[k]); + try std.testing.expect(@abs(seq.carets[k] - seq.cells[k]) <= @abs(seq.carets[k - 1] - seq.cells[k]) + 0.05); + if (k - changed == 30) try std.testing.expectEqual(seq.cells[k], seq.carets[k]); + } + } + /// Frames from `from` until the picture is still: the scroll's glide. fn settle(seq: *const GlideRun, from: usize) usize { var k = from; @@ -5618,6 +5636,7 @@ test "every scroll glides on one law: a half page, a page, the far jumps, a run }; var seq: GlideRun = .{}; try seq.play(&g, core, gpa, &arena, &script, 240); + try seq.checkCaret(); // Every one moves the picture on the frame it is drawn in, and the // picture lags by what the core moved (a far jump, by one body). var longest: usize = 0; @@ -5692,9 +5711,24 @@ test "a held key moves the view at the stream's own rate, never a bounce a line, most = @max(most, step); } try std.testing.expect(seq.lines[89] > 0 and least > 0.4 and most < 0.6); + // The caret stays on its row, the text moving under it: not a pixel's + // wobble (the glide's fractional lag never reaches it). + for (90..110) |k| try std.testing.expectEqual(seq.carets[89], seq.carets[k]); // It comes to rest once the keys stop, never past its mark. for (110..200) |k| try std.testing.expect(seq.offsets[k] <= 0); try std.testing.expectEqual(@as(f32, 0), seq.offsets[199]); + // Ctrl-d held, then PageDown held: the caret keeps its row the whole + // way, the pages moving under it. + var pages: [40]GlideRun.Step = undefined; + for (&pages, 0..) |*step, i| step.* = .{ .frame = 10 + 2 * i, .key = if (i < 20) .{ .cp = 'd', .ctrl = true } else .{ .cp = pardes.Key.page_down } }; + var held: GlideRun = .{}; + try held.play(&g, core, gpa, &arena, &pages, 100); + try held.checkCaret(); + try seq.checkCaret(); + // (The first of each may move it, one ease: from the last row to where + // a half page keeps it, and from there to a page's.) + for (30..50) |k| try std.testing.expectEqual(held.carets[30], held.carets[k]); + for (70..90) |k| try std.testing.expectEqual(held.carets[70], held.carets[k]); } fn resetScroll(g: *Gui) void { @@ -6427,12 +6461,15 @@ fn renderFrame( // while it glides or blinks. One or the other every frame (none only at // a blink's off half). const blink_alpha: f32 = if (blinkNow(g, chrome)) |b| b.alpha else 1; - const quad_mode = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or blink_alpha < 1); + // While a glide moves the text, the caret is its quad, drawn from its + // own springs, not a cell of the moving rows. + const quad_mode = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or blink_alpha < 1 or g.glide.offset != 0); g.cursor_quad_alpha = if (quad_mode) chrome.cursor_alpha * blink_alpha else 0; // The probe's view of this frame's motion (FrameStats.Frame). if (core) |p| { const probe = &@constCast(p).frames; probe.scroll = g.glide.offset; + probe.caret = caretRow(g, surface) orelse 0; } const cursors_shown = groups.tracks == 0 and !quad_mode; var overlay_count = buildOverlay(g, core, surface, sw, sh, debug_on); @@ -10713,37 +10750,32 @@ fn postSpare(surface: *const pardes.Surface, layout: CellLayout, out: *[Post.max const CursorBox = Post.Frame.Cursor; const max_cursors = 1 + pardes.MAX_PANES + pardes.MAX_TAG_LAYERS; +/// Where the frame draws the focused caret, its top in grid rows: from its +/// own springs (the quad's, while it moves or while a glide moves the text) +/// or its cell; never from the text's glide. The probe records it and the +/// glide tests hold its invariants. +fn caretRow(g: *const Gui, surface: *const pardes.Surface) ?f32 { + const chrome = &surface.chrome; + const cursor = surface.cursor orelse return null; + const quad = chrome.cursor_glide & 2 != 0 and (chrome.cursor_glide & 1 != 0 or g.glide.offset != 0); + return if (quad) chrome.cursor_quad[1] else @floatFromInt(cursor.y); +} + /// Every cursor on the frame where it is drawn, in window pixels: the grid's /// where no layer has its cell, and each layer's at that layer's pitch. A bar /// is an eighth of its cell wide, a block the whole cell. -/// Where a scroll's glide draws grid cell (col, row): its pixels down from -/// where the grid has it (the picture lags the text), and whether it is -/// still in the gliding body at all; nothing moved off a glide. -fn glideLift(g: *const Gui, col: f32, row: f32, layout: CellLayout) struct { px: f32, shown: bool } { - const glide = &g.glide; - if (glide.offset == 0) return .{ .px = 0, .shown = true }; - const r = glide.rect; - const top: f32 = @floatFromInt(glide.body_y); - const bottom = top + @as(f32, @floatFromInt(glide.body_h)); - if (col < @as(f32, @floatFromInt(r.x + config.GUTTER)) or col >= @as(f32, @floatFromInt(r.x + r.w)) or row < top or row >= bottom) return .{ .px = 0, .shown = true }; - const at = row - glide.offset; - return .{ .px = -glide.offset * layout.h, .shown = at >= top and at + 1 <= bottom + 0.5 }; -} - fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout, win_h: f32, out: *[max_cursors]CursorBox) usize { const chrome = &surface.chrome; const page = chrome.page orelse bg_default; var n: usize = 0; if (surface.cursor) |cursor| { - // A scroll's glide carries the cursor with the text it is in. - const lift = glideLift(g, @floatFromInt(cursor.x), @floatFromInt(cursor.y), layout); - if (lift.shown and cursor.x < surface.cols and cursor.y < surface.rows and bodyLayerAt(surface, cursor.x, cursor.y) == null and tagLayerIn(surface.tagLayers(), cursor.x, cursor.y) == null) { + if (cursor.x < surface.cols and cursor.y < surface.rows and bodyLayerAt(surface, cursor.x, cursor.y) == null and tagLayerIn(surface.tagLayers(), cursor.x, cursor.y) == null) { const cell = &surface.cells[@as(usize, cursor.y) * surface.cols + cursor.x]; const role = cellFontRole(cell); const width: f32 = if (role == .tagline) @floatFromInt(g.tagline_width) else layout.w; const height = std.math.clamp(if (role == .tagline) @as(f32, @floatFromInt(g.tagline_height)) else layout.h, 1.0, layout.h); const x0 = @as(f32, @floatFromInt(cursor.x)) * layout.w; - const y0 = @as(f32, @floatFromInt(cursor.y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(cursor.y, win_h, @intFromFloat(layout.h), @intFromFloat(height)))) + lift.px; + const y0 = @as(f32, @floatFromInt(cursor.y)) * layout.h + @as(f32, @floatFromInt(taglineBandOffset(cursor.y, win_h, @intFromFloat(layout.h), @intFromFloat(height)))); out[n] = .{ .x0 = x0, .y0 = y0, .x1 = x0 + if (cursor.bar) @max(dp(1), width / 8.0) else width, .y1 = y0 + height, .rgb = cursorColor(chrome, cell, role), .text = page, .bar = cursor.bar }; n += 1; } @@ -10816,13 +10848,8 @@ fn appendCursorGlide(g: *Gui, surface: *const pardes.Surface, builder: *OverlayB const layout = fixedCellLayout(g); var px: [4][2]f32 = undefined; var centre: [2]f32 = .{ 0, 0 }; - // A scroll's glide carries the quad with the text, by where its centre is. - var cell_centre: [2]f32 = .{ 0, 0 }; - for (0..4) |i| cell_centre = .{ cell_centre[0] + chrome.cursor_quad[i * 2] / 4, cell_centre[1] + chrome.cursor_quad[i * 2 + 1] / 4 }; - const lift = glideLift(g, cell_centre[0], cell_centre[1], layout); - if (!lift.shown) return; for (&px, 0..) |*point, i| { - point.* = .{ chrome.cursor_quad[i * 2] * layout.w, chrome.cursor_quad[i * 2 + 1] * layout.h + lift.px }; + point.* = .{ chrome.cursor_quad[i * 2] * layout.w, chrome.cursor_quad[i * 2 + 1] * layout.h }; centre[0] += point[0] / 4; centre[1] += point[1] / 4; } |
