diff options
| -rw-r--r-- | addons/pdf/pdf.out | 36 | ||||
| -rw-r--r-- | addons/pdf/pdf.script | 5 | ||||
| -rw-r--r-- | addons/pdf/pdf.zig | 69 | ||||
| -rw-r--r-- | test/panes.zig | 13 |
4 files changed, 101 insertions, 22 deletions
diff --git a/addons/pdf/pdf.out b/addons/pdf/pdf.out index a6ea68b3..49ba2a88 100644 --- a/addons/pdf/pdf.out +++ b/addons/pdf/pdf.out @@ -165,3 +165,39 @@ tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf |nextEffect() → perform(e) |single-threaded by construction |native input +> key 3gj +tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf +|emit(e) → fixed ring, limits.effect_cap +|nextEffect() → perform(e) +|single-threaded by construction +|native input +|termios+vaxis / SDL / DOM / +|AppKit / a byte sink on a UART +|Event +> key gk +tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf +|render(arena) → *Surface +|emit(e) → fixed ring, limits.effect_cap +|nextEffect() → perform(e) +|single-threaded by construction +|native input +|termios+vaxis / SDL / DOM / +|AppKit / a byte sink on a UART +> key 999gj +tag [2/16] petscii:on palette:commodore ascii:on docs/design.pdf +|detach is served only by a detached core’s host, and the n +|is the point rather than an oversight — a local tty or SDL +|has no session to leave, so perform reports that on the pa +|message row instead of quietly quitting something (the .de +|arm of Pardes.perform). +|2 +| +> key gj +tag [3/16] petscii:on palette:commodore ascii:on docs/design.pdf +|The watch path, in full, because it is the ask with the mo +|machinery behind it. The tty and SDL hosts and the detache +|daemon implement it in src/file_watch.zig, with Linux in +|otify or a macOS kqueue behind one mark/reconcile transact +|the native macOS host watches with debounced DispatchSourc +|instead, then restats the exact path under a pane-generati +|guard. All three macOS watchers mark the same two things f diff --git a/addons/pdf/pdf.script b/addons/pdf/pdf.script index be76f3a3..60e4bfca 100644 --- a/addons/pdf/pdf.script +++ b/addons/pdf/pdf.script @@ -18,3 +18,8 @@ dump graphics off key j key 2k +# and as text, gj and gk a line, and a page at the window's edge +key 3gj +key gk +key 999gj +key gj diff --git a/addons/pdf/pdf.zig b/addons/pdf/pdf.zig index d10407e0..4704e7c7 100644 --- a/addons/pdf/pdf.zig +++ b/addons/pdf/pdf.zig @@ -7,7 +7,8 @@ //! pictures lays out a strip of the pages at its pixels, each fitted to its //! width or height (PdfFit), a gap between two, tinted to the theme or not //! (PdfTint), and asks for the rasters it shows; a view of glyphs (a -//! terminal without graphics) shows the page's text layer. Pages render off the loop on jobs, two at once: a +//! terminal without graphics) shows the page's text layer from the line +//! the place names. Pages render off the loop on jobs, two at once: a //! pane's document has two lanes (a context and a document of their own //! each, MuPDF's threading rule), found by the jobs in a table of the open //! documents under its own lock, and a cookie stops a job nobody waits @@ -18,8 +19,9 @@ //! over the page's clean rows, repainted on the loop and said as rows //! (`from`, `dirty_*`). The body is the text of the page shown; `/` lists //! hits as `file.pdf:P:H`; PdfSections lists the outline; a click on a -//! link follows it. A fling (a window's travel or more between frames) -//! renders only the rows each page shows until it rests. The record in a dump is its fit, tint +//! link follows it. A wheel's whole notches glide there over four ticks; a +//! fling (a window's travel or more between frames) renders only the rows +//! each page shows until it rests. The record in a dump is its fit, tint //! and place. const std = @import("std"); const addon = @import("addon"); @@ -43,6 +45,7 @@ const chunk = 512; // a tall page's bands start on these const overlap = 32; // rows rendered past a band's edges const hover_ns = 32 * std.time.ns_per_ms; // the pointer's rest before its word shows const fling_ns = 100 * std.time.ns_per_ms; // a fling's end: this long with no turn +const frame_ns = 16 * std.time.ns_per_ms; // a glide's next step const grain = 64; // a fling's band starts and ends on these rows const marks_n = 1024; // highlights on a page const record_version = 1; @@ -224,8 +227,9 @@ pub const Pdf = struct { hover: struct { quad: Quad = std.mem.zeroes(Quad), page: ?u32 = null, link: bool = false, wait: u2 = 0, col: u16 = 0, row: u16 = 0, view: u8 = 0, at: u64 = 0 } = .{}, /// A fling: the strip's travel since the last tick and since the last /// frame drawn, a window or more of it a frame renders only the rows - /// each page shows, the whole page once it rests. - go: struct { since_tick: f64 = 0, travel: f64 = 0, framed: bool = false, landed: bool = false, fling: bool = false } = .{}, + /// each page shows, the whole page once it rests. A glide: a wheel's + /// pixels still to go in a view, half each frame, all by its 4th tick. + go: struct { since_tick: f64 = 0, travel: f64 = 0, framed: bool = false, landed: bool = false, fling: bool = false, glide: f64 = 0, glide_n: u8 = 0, glide_view: u8 = 0 } = .{}, count: u32 = 0, // a key's count, typed before it g: bool = false, @@ -292,6 +296,14 @@ pub const Pdf = struct { pub fn tick(st: *Pdf, now: u64, flags: addon.Flags) u64 { if (st.d == null) return 0; const go = &st.go; + if (go.glide != 0) if (pixels(st, go.glide_view)) |l| { + go.glide_n += 1; + const step = if (go.glide_n >= 4) go.glide else if (flags.framed) go.glide / 2 else 0; + go.glide -= step; + scrollBy(st, l, step); + } else { + go.glide = 0; + }; // A fling is a window's travel or more between frames; a tick a // page's landing made (no turn since) neither starts nor ends one. go.framed = go.framed or flags.framed; @@ -329,6 +341,7 @@ pub const Pdf = struct { if (f.due and ask(st, .{ .op = .search, .page = st.pages, .n = f.len }, query, st.name) != 0) f.due = false; for (&st.views, 1..) |*l, id| if (l.laid and !l.v.glyphs) reconcile(st, l, @intCast(id)); var next: u64 = if (hv.wait == 2) hv.at else 0; + if (go.glide != 0) next = now + frame_ns; // flinging: a tick when it rests, for the whole pages if (go.fling and (next == 0 or now + fling_ns < next)) next = now + fling_ns; return next; @@ -360,12 +373,13 @@ pub const Pdf = struct { if (st.d == null or win.view < 1 or win.view > views_n) return; const out = into.out; out.pointer = if (st.hover.page != null and st.hover.link) c.PA_POINTER_LINK else c.PA_POINTER_DEFAULT; - if (win.glyphs) { // the page's text layer, from its top (read on a tick) + if (win.glyphs) { // the page's text layer, from the place's line (read on a tick) out.at = @floatCast((f64Of(st.page) + std.math.clamp(st.frac, 0, 1)) / f64Of(st.pages)); out.span = @floatCast(1 / f64Of(st.pages)); if (st.text.page != st.page) return; const t = st.text.bytes; var at: usize = 0; + for (0..lineOf(st)) |_| at = if (std.mem.indexOfScalarPos(u8, t, at, '\n')) |nl| nl + 1 else t.len; for (0..win.rows) |row| { var col: usize = 0; while (at < t.len and t[at] != '\n') { @@ -435,9 +449,9 @@ pub const Pdf = struct { if (ctrl) { const n = counted(st); switch (cp) { - 'd', 'u' => move(st, l, if (cp == 'd') 1 else -1, @max(1, @floor(vh / 2)), n), - 'f', 'b' => move(st, l, if (cp == 'f') 1 else -1, vh, n), - 'e', 'y' => move(st, l, if (cp == 'e') 1 else -1, cell, 1), + 'd', 'u' => move(st, l, if (cp == 'd') 1 else -1, @max(1, @floor(vh / 2)), n, false), + 'f', 'b' => move(st, l, if (cp == 'f') 1 else -1, vh, n, false), + 'e', 'y' => move(st, l, if (cp == 'e') 1 else -1, cell, 1, true), else => return false, // the editor's: windows, jumps } return true; @@ -449,7 +463,7 @@ pub const Pdf = struct { switch (cp) { 'g' => goPage(st, n -| 1), 'e' => goPage(st, st.pages - 1), - 'j', 'k' => move(st, l, if (cp == 'j') 1 else -1, cell, @max(n, 1)), + 'j', 'k' => move(st, l, if (cp == 'j') 1 else -1, cell, @max(n, 1), true), 'h', 's', 'l' => edge(st, cp == 'l'), else => {}, } @@ -469,10 +483,10 @@ pub const Pdf = struct { 'g' => st.g = true, 'G' => goPage(st, if (st.count > 0) st.count - 1 else st.pages - 1), 'f' => addon.exec("PdfSections"), - 'j', @backingInt(K.down) => move(st, l, 1, cell, counted(st)), - 'k', @backingInt(K.up) => move(st, l, -1, cell, counted(st)), - @backingInt(K.page_down) => move(st, l, 1, vh, counted(st)), - @backingInt(K.page_up) => move(st, l, -1, vh, counted(st)), + 'j', @backingInt(K.down) => move(st, l, 1, cell, counted(st), false), + 'k', @backingInt(K.up) => move(st, l, -1, cell, counted(st), false), + @backingInt(K.page_down) => move(st, l, 1, vh, counted(st), false), + @backingInt(K.page_up) => move(st, l, -1, vh, counted(st), false), 'h', @backingInt(K.left) => panBy(st, -1, across * f64Of(counted(st))), 'l', @backingInt(K.right) => panBy(st, 1, across * f64Of(counted(st))), '0', '^', @backingInt(K.home) => edge(st, false), @@ -490,11 +504,14 @@ pub const Pdf = struct { const dir: f64 = if (ev.button == .wheel_up or ev.button == .wheel_left) -1 else 1; switch (ev.button) { .wheel_up, .wheel_down => { - // a notch a row, a touchpad's fraction of one its fraction; - // as text a page a turn + // a notch a row, gliding there; a touchpad's fraction its + // fraction at once; as text a line, a page at the edge const notches: f64 = if (ev.amount > 0) ev.amount else 1; const cell: f64 = if (l.v.cell_h != 0) f64Of(l.v.cell_h) else 16; - move(st, l, dir, notches * cell, 1); + if (l.v.glyphs) move(st, l, dir, 0, @intFromFloat(@max(1, @round(notches))), true) else if (notches != @floor(notches)) scrollBy(st, l, dir * notches * cell) else { + st.go.glide += dir * notches * cell; + st.go.glide_n, st.go.glide_view = .{ 0, id }; + } return true; }, .wheel_left, .wheel_right => { @@ -994,11 +1011,23 @@ fn goPage(st: *Pdf, p: u32) void { goTo(st, @min(p, st.pages - 1), 0); } +/// The line of the page's text the place names. +fn lineOf(st: *const Pdf) usize { + return @min(@as(usize, @intFromFloat(@round(std.math.clamp(st.frac, 0, 1) * f64Of(st.text.lines)))), st.text.lines -| 1); +} + /// Down (dir 1) or up `n` steps of `px` pixels in view `l`; as text, a -/// page a step. -fn move(st: *Pdf, l: *Layout, dir: f64, px: f64, n: u32) void { +/// page a step, or with `line` a line, and a page at the window's edge. +fn move(st: *Pdf, l: *Layout, dir: f64, px: f64, n: u32, line: bool) void { if (!l.v.glyphs) return scrollBy(st, l, dir * px * f64Of(n)); - goPage(st, @intCast(std.math.clamp(@as(i64, st.page) + @as(i64, @intFromFloat(dir)) * n, 0, st.pages - 1))); + const page = @as(i64, st.page) + @as(i64, @intFromFloat(dir)) * @as(i64, if (line) 1 else n); + const at: i64 = @intCast(lineOf(st)); + const last = @as(i64, st.text.lines) - l.v.rows; + if (!line or st.text.page != st.page or (dir > 0 and at >= last) or (dir < 0 and at == 0)) { + if (page >= 0 and page < st.pages or !line) goPage(st, @intCast(std.math.clamp(page, 0, st.pages - 1))); + return; + } + goTo(st, st.page, f64Of(std.math.clamp(at + @as(i64, @intFromFloat(dir)) * n, 0, @max(last, 0))) / f64Of(st.text.lines)); } fn visible(st: *const Pdf, l: *const Layout) struct { usize, usize } { diff --git a/test/panes.zig b/test/panes.zig index 2da7a313..55ad303d 100644 --- a/test/panes.zig +++ b/test/panes.zig @@ -4166,7 +4166,7 @@ const PdfAddon = struct { try std.testing.expect(p.surface.pointer_shape != .link); } - test "a touchpad's fraction of a turn moves a PDF's strip by that fraction of a notch's" { + test "a wheel's notch glides a PDF's strip there over four ticks, and a touchpad's fraction of a turn moves it that fraction of a notch's at once" { if (!addons.has_pdf) return error.SkipZigTest; const gpa = std.testing.allocator; const p = try Pardes.init(gpa, .{ .cols = 80, .rows = 24, .tty_only = true }); @@ -4178,13 +4178,22 @@ const PdfAddon = struct { const r = p.view().rects[id]; const slot = p.panes[id].?.addon.?; var at: [3]f32 = undefined; + var first: f32 = 0; for (&at, [_]f32{ 0, 1, 0.25 }) |*a, turn| { if (turn > 0) p.update(.{ .mouse = .{ .button = .wheel_down, .kind = .press, .col = r.x + 4, .row = r.y + r.h - 2, .amount = turn } }); _ = try p.render(arena.allocator()); + if (turn == 1) first = slot.thumb_at; + // (a frame and a tick each, four) + for (0..4) |_| { + now += 16 * std.time.ns_per_ms; + p.advance(now); + p.update(.tick); + _ = try p.render(arena.allocator()); + } a.* = slot.thumb_at; } const notch = at[1] - at[0]; - try std.testing.expect(notch > 0); + try std.testing.expect(notch > 0 and first > at[0] and first < at[1]); try std.testing.expectApproxEqRel(notch / 4, at[2] - at[1], 0.05); } }; |
