summaryrefslogtreecommitdiff
path: root/src/gui/gui.zig
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-07 20:29:44 -0300
committerGabriel Schneider <[email protected]>2026-11-07 20:39:56 -0300
commit8a8a68f2c0fe62a2a088fad9050aa75701b4b173 (patch)
tree03e35551a11598a4e2f69cf97ff13a90b6fde012 /src/gui/gui.zig
parent983580b6dc85274908b73daf6da82e8455bdba10 (diff)
downloadpardes-8a8a68f2c0fe62a2a088fad9050aa75701b4b173.tar.gz
pardes-8a8a68f2c0fe62a2a088fad9050aa75701b4b173.zip
gui: the caret is drawn with its line while a wheel's fraction holds the text off its cells: a bar (and the caret's quad while it eases) was drawn on its cell, up to a row off its text, which a high-resolution wheel leaves resting at a fraction nearly all the time (caretRide now carries scroll_lag as it carries the glide); a gui test pins the bar on its line at a resting half row
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src/gui/gui.zig')
-rw-r--r--src/gui/gui.zig71
1 files changed, 65 insertions, 6 deletions
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index 0696e312..3538481a 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -4649,6 +4649,36 @@ test "a wheel turned down then up never draws the picture moving against the tur
}
}
+test "a bar cursor is drawn on its line while a wheel's fraction holds the text off its row, as a block is" {
+ const gpa = std.testing.allocator;
+ const core = try wheelRunFile(gpa);
+ defer core.deinit();
+ core.settings.cursor_blink = false;
+ core.update(.{ .key = .{ .cp = 'i', .text = "i" } });
+ var wr = try WheelRun.init(gpa, core);
+ defer wr.deinit(gpa);
+ defer wr.g.decor.deinit(gpa);
+ // Half a notch down: the core a line on, the text drawn half a row
+ // below its cells, and resting there.
+ try wr.frame(gpa, &.{-0.5});
+ for (0..4) |_| try wr.frame(gpa, &.{});
+ try std.testing.expectEqual(@as(f32, -0.5), wr.g.scroll_lag);
+ _ = wr.arena.reset(.retain_capacity);
+ const s = try core.render(wr.arena.allocator());
+ const cu = s.cursor.?;
+ try std.testing.expect(cu.bar);
+ const layout = fixedCellLayout(&wr.g);
+ const win_w: f32 = @floatFromInt(@as(u32, s.cols) * wr.g.cell_w);
+ const win_h: f32 = @floatFromInt(@as(u32, s.rows) * wr.g.cell_h);
+ wr.g.decor.clearRetainingCapacity();
+ try cursorDecor(&wr.g, gpa, s, layout, win_w, win_h);
+ try std.testing.expectEqual(@as(usize, 1), wr.g.decor.items.len);
+ // Its top where its line's top is drawn (emitScrollRows: the row less
+ // the lag), not on its cell.
+ const top = (1 - wr.g.decor.items[0].y0) * win_h / 2;
+ try std.testing.expectApproxEqAbs((@as(f32, @floatFromInt(cu.y)) - wr.g.scroll_lag) * layout.h, top, 0.01);
+}
+
test "a wheel's caret rides with its line, the text's own glide, never a spring of its own" {
const gpa = std.testing.allocator;
const core = try pardes.Pardes.init(gpa, .{ .cols = 80, .rows = 24 });
@@ -8285,11 +8315,19 @@ test "a caret eases a cell at a time, keeps up with typing, and glides a jump, l
/// (its springs landed on the cell, Caret.see).
fn caretRide(g: *const Gui, surface: *const pardes.Surface) f32 {
const glide = &g.glide;
- if (!glide.caret_rides or glide.follow.offset == 0) return 0;
const cu = surface.cursor orelse return 0;
- const x0 = glide.rect.x + config.GUTTER;
- if (cu.x < x0 or cu.x >= x0 + glide.bw() or cu.y < glide.body_y or cu.y >= glide.body_y + glide.body_h) return 0;
- return glide.follow.offset;
+ if (glide.follow.offset != 0) {
+ if (!glide.caret_rides) return 0;
+ const x0 = glide.rect.x + config.GUTTER;
+ if (cu.x < x0 or cu.x >= x0 + glide.bw() or cu.y < glide.body_y or cu.y >= glide.body_y + glide.body_h) return 0;
+ return glide.follow.offset;
+ }
+ // A fine wheel's or a touchpad's fraction (scroll_lag) holds the whole
+ // body off its cells, the caret's row with the rest, at rest too.
+ if (g.scroll_lag == 0 or g.scroll_pane == null) return 0;
+ const r = g.scroll_rect;
+ if (cu.x < r.x + config.GUTTER or cu.x >= r.x + r.w or cu.y < g.scroll_body_y or cu.y >= g.scroll_body_y + g.scroll_body_h) return 0;
+ return g.scroll_lag;
}
/// Every cursor on the frame where it is drawn, in window pixels: the grid's
@@ -8343,8 +8381,29 @@ fn cursorBoxes(g: *const Gui, surface: *const pardes.Surface, layout: CellLayout
/// The bar cursors as decor; a block cursor is its cell's own colours.
fn cursorDecor(g: *Gui, gpa: std.mem.Allocator, surface: *const pardes.Surface, layout: CellLayout, win_w: f32, win_h: f32) !void {
var boxes: [max_cursors]CursorBox = undefined;
- for (boxes[0..cursorBoxes(g, surface, layout, win_h, &boxes)]) |box|
- if (box.bar) try addDecor(g, gpa, box.x0, box.y0, box.x1, box.y1, box.rgb, 1, win_w, win_h);
+ // The focused one is drawn with its line where a wheel's fraction holds
+ // the text off its cells (a block is a cell of the shifted rows).
+ // The focused box is the one nearest its cell (a body layer's is lifted
+ // off the grid), as seeCaret finds it.
+ const ride = caretRide(g, surface) * layout.h;
+ const all = boxes[0..cursorBoxes(g, surface, layout, win_h, &boxes)];
+ var focused: usize = all.len;
+ if (surface.cursor) |cu| {
+ const cx = (@as(f32, @floatFromInt(cu.x)) + 0.5) * layout.w;
+ const cy = (@as(f32, @floatFromInt(cu.y)) + 0.5) * layout.h;
+ var nearest = std.math.inf(f32);
+ for (all, 0..) |b, i| {
+ const d = ((b.x0 + b.x1) / 2 - cx) * ((b.x0 + b.x1) / 2 - cx) + ((b.y0 + b.y1) / 2 - cy) * ((b.y0 + b.y1) / 2 - cy);
+ if (d < nearest) {
+ nearest = d;
+ focused = i;
+ }
+ }
+ }
+ for (all, 0..) |box, i| if (box.bar) {
+ const shift = if (i == focused) ride else 0;
+ try addDecor(g, gpa, box.x0, box.y0 - shift, box.x1, box.y1 - shift, box.rgb, 1, win_w, win_h);
+ };
}
/// The triangle overlay: the cursor's glide, over everything.