summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-07 19:45:54 -0300
committerGabriel Schneider <[email protected]>2026-11-07 20:39:56 -0300
commit4c134eaa5c7f84eba7091c9ccef86567f1434d49 (patch)
treec6bd720bc8999887573e8d9112e553e19154f78b
parentf2784acb88c98f33fed25dbf41f1a5af278c8e84 (diff)
downloadpardes-4c134eaa5c7f84eba7091c9ccef86567f1434d49.tar.gz
pardes-4c134eaa5c7f84eba7091c9ccef86567f1434d49.zip
gui: a kind's pane (an image, a PDF) is watched again and shows its file as it is now: Kind.open and a restored kind's pane emit its watch, which kinds #14 dropped with pdf_view, and a reload counts into its pictures' keys, since its kind numbers them afresh from each open and a window keeps a texture by key; the T1 session watches files as a host does (Session.filesChanged) and two behaviour cases pin it
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
-rw-r--r--src/Kind.zig14
-rw-r--r--src/dump.zig5
-rw-r--r--src/pardes.zig1
-rw-r--r--test/behaviour/addons.zig33
-rw-r--r--test/behaviour/addons_pdf.zig24
-rw-r--r--test/session.zig25
6 files changed, 100 insertions, 2 deletions
diff --git a/src/Kind.zig b/src/Kind.zig
index 27cd1a6d..bdbfeb2e 100644
--- a/src/Kind.zig
+++ b/src/Kind.zig
@@ -134,7 +134,7 @@ pub const Slot = struct {
// jobs' inputs, its file's hash, a placeholder's record, the place
// its kind says), and what a frame makes (the thumb, the pointer,
// whether one was drawn: a view's, never a step's).
- .local = .{ "ops", "st", "gpa", "arena", "opened", "jobs", "tokens", "record", "where_text", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" },
+ .local = .{ "ops", "st", "gpa", "arena", "opened", "reloads", "jobs", "tokens", "record", "where_text", "thumb_at", "thumb_span", "pointer", "painted", "over", "shadow", "used" },
};
/// Its kind's code here; null in a placeholder (`live` false: its kind
/// not in the registry, its record kept for the day it comes) or a
@@ -150,6 +150,10 @@ pub const Slot = struct {
/// The hash of the file's bytes it was opened from, 0 for none (a
/// record's): whether the file a watch arms on is still those.
opened: u64 = 0,
+ /// Times its file changed under it (fileChanged): its pictures' keys
+ /// carry it, since its kind numbers them afresh from each open, and a
+ /// frontend keeps what it uploaded by key.
+ reloads: u32 = 0,
/// Its kind's generation when it was opened (Entry.gen).
gen: u32 = 0,
path: []u8 = &.{},
@@ -248,6 +252,9 @@ pub fn open(p: *Pardes, id: usize, k: u8, path: ?[]const u8, bytes: ?[]const u8)
const pane = try p.newDocPane(id);
pane.addon = s;
_ = reopen(p, id, s, bytes);
+ // Its file watched, as a text's is: changed on disk, it opens again
+ // from it (fileChanged); retirePane lets the watch go.
+ if (s.path.len > 0 and @import("fs.zig").localPath(s.path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } });
return pane;
}
@@ -346,6 +353,7 @@ pub fn fileChanged(p: *Pardes, id: usize, _: []const u8) bool {
if (checked) s.used.clearRetainingCapacity();
if (s.record) |r| s.gpa.free(r);
s.record = record;
+ s.reloads +%= 1;
return reopen(p, id, s, bytes orelse "");
}
@@ -445,6 +453,10 @@ pub fn paint(p: *const Pardes, slot: *Slot, id: usize, arena: std.mem.Allocator,
continue;
}
frame.buf[frame.n] = pl;
+ if (slot.reloads != 0) {
+ frame.buf[frame.n].key = std.hash.Wyhash.hash(slot.reloads, std.mem.asBytes(&pl.key));
+ if (pl.from != 0) frame.buf[frame.n].from = std.hash.Wyhash.hash(slot.reloads, std.mem.asBytes(&pl.from));
+ }
frame.n += 1;
}
slot.thumb_at = std.math.clamp(out.at, 0, 1);
diff --git a/src/dump.zig b/src/dump.zig
index 8aa4aa16..9849c8e4 100644
--- a/src/dump.zig
+++ b/src/dump.zig
@@ -596,7 +596,10 @@ fn reviveSession(p: *Pardes, previous: ?*Pardes, resumed: bool) !void {
/// kind's pane opened again (a PDF a build before the pdf addon dumped, as
/// the addon's).
fn revivePane(p: *Pardes, id: usize, pane: *pardes.panes.Pane, was_on_disk: bool) void {
- if (pane.addon) |slot| return reviveSlot(p, id, slot);
+ if (pane.addon) |slot| {
+ if (slot.path.len > 0 and filesystem.localPath(slot.path) != null) p.emit(.{ .watch = .{ .pane = @intCast(id), .on = true } });
+ return reviveSlot(p, id, slot);
+ }
if (pane.terminal != null) {
// A command pane comes back finished, showing what it said: its
// command is not run again. A host that runs shells starts one
diff --git a/src/pardes.zig b/src/pardes.zig
index ecbc0633..2fa1d390 100644
--- a/src/pardes.zig
+++ b/src/pardes.zig
@@ -84,6 +84,7 @@ pub const Addon = @import("Addon.zig");
pub const dump = @import("dump.zig");
pub const lsp = @import("lsp/lsp.zig");
pub const host_io = @import("host_io.zig");
+pub const file_watch = @import("file_watch.zig");
pub const Host = host_io.Host;
pub const frameMark = tracy.frameMark;
diff --git a/test/behaviour/addons.zig b/test/behaviour/addons.zig
index 3a53766a..9b96a3d6 100644
--- a/test/behaviour/addons.zig
+++ b/test/behaviour/addons.zig
@@ -331,3 +331,36 @@ test "addons: a Look at a file whose extension an addon claims opens a pane of i
const help = try s.read(s.at("/pane/{d}/body", .{try s.focused()}));
try std.testing.expect(has(help, "SPC z f Fake a fake pane, counting (addon fake)\n"));
}
+
+test "addons: an image pane whose file changes on disk shows the new picture, under a new key: a window keeps a texture by its key" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ try load(&s, Session.image_addon);
+ defer unload(&s, "image");
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\x0d\x4d\xe7\x0d\x4d\xe7" });
+ try s.write("/look", s.at("{s}/pic.ppm\n", .{dir}));
+ const n = try s.focused();
+ const key = "\"key\":\"";
+ const before = try s.arena.allocator().dupe(u8, try s.read("/screen"));
+ try std.testing.expect(has(before, s.at("\"serial\":{d},", .{n})) and has(before, "\"src\":[0,0,2,1]"));
+ const old_key = before[std.mem.indexOf(u8, before, key).? + key.len ..][0..16];
+ // The same size, other pixels: only its key tells a window it changed.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n2 1\n255\n\xe7\x4d\x0d\xe7\x4d\x0d" });
+ try std.testing.expect(try s.filesChanged());
+ const after = try s.read("/screen");
+ try std.testing.expect(has(after, "\"src\":[0,0,2,1]"));
+ try std.testing.expect(!has(after, old_key));
+ // Another size: the picture as it is now.
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "pic.ppm", .data = "P6\n3 1\n255\n\xe7\x4d\x0d\xe7\x4d\x0d\xe7\x4d\x0d" });
+ try std.testing.expect(try s.filesChanged());
+ try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]"));
+ // A second Look of it finds its pane, the new picture in it.
+ try s.write("/look", s.at("{s}/pic.ppm\n", .{dir}));
+ try std.testing.expectEqual(n, try s.focused());
+ try std.testing.expect(has(try s.read("/screen"), "\"src\":[0,0,3,1]"));
+}
diff --git a/test/behaviour/addons_pdf.zig b/test/behaviour/addons_pdf.zig
index 48b75806..6cdeebf1 100644
--- a/test/behaviour/addons_pdf.zig
+++ b/test/behaviour/addons_pdf.zig
@@ -232,3 +232,27 @@ test "addons: a PDF's body is its page's text layer, read from any offset and ne
_ = try s.refused("/look", s.at("{s}/cut.pdf\n", .{dir}));
try std.testing.expect(has(s.said, "cut.pdf") and has(s.said, "not a PDF pardes can read"));
}
+
+test "addons: a PDF whose file changes on disk shows its new pages, under new keys: a window keeps a texture by its key" {
+ var s = try Session.initFile(std.testing.allocator, "x\n");
+ defer s.deinit();
+ s.graphics();
+ var tmp = std.testing.tmpDir(.{});
+ defer tmp.cleanup();
+ var buf: [4096]u8 = undefined;
+ const dir = buf[0..try tmp.dir.realPath(std.testing.io, &buf)];
+ const a = s.arena.allocator();
+ const links = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/links.pdf", a, .unlimited);
+ const outline = try std.Io.Dir.cwd().readFileAlloc(std.testing.io, "test/pdf-fixtures/outline.pdf", a, .unlimited);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = links });
+ const n = try s.lookFile(s.at("{s}/doc.pdf", .{dir}));
+ const first = try shown(&s, n);
+ try std.testing.expect(first.len >= 1);
+ try tmp.dir.writeFile(std.testing.io, .{ .sub_path = "doc.pdf", .data = outline });
+ try std.testing.expect(try s.filesChanged());
+ // Its pages laid out at the step after (the host's next).
+ try s.keys("0");
+ const now = try shown(&s, n);
+ try std.testing.expect(now.len >= 1);
+ for (now) |pic| for (first) |old| try std.testing.expect(!std.mem.eql(u8, pic.key, old.key));
+}
diff --git a/test/session.zig b/test/session.zig
index 8e03b424..b35d08ad 100644
--- a/test/session.zig
+++ b/test/session.zig
@@ -41,6 +41,10 @@ pub const Session = struct {
/// meanwhile (a reload).
hold_jobs: bool = false,
held: std.ArrayList(*pardes.host_io.KindJob) = .empty,
+ /// The files the core asked to watch, watched as a host watches them
+ /// (file_watch.zig, inotify): `filesChanged` wakes them.
+ watch_fd: c_int = -1,
+ watches: pardes.file_watch.Table = @splat(null),
/// A session showing one file pane holding `text`, keeping what outlives
/// it in a database in memory (src/Db.zig).
@@ -95,6 +99,10 @@ pub const Session = struct {
pub fn deinit(s: *Session) void {
s.held.deinit(pardesOf(s).gpa);
+ if (s.watch_fd >= 0) {
+ pardes.file_watch.unwatchAll(s.watch_fd, &s.watches);
+ _ = std.c.close(s.watch_fd);
+ }
pardesOf(s).deinit();
s.arena.deinit();
}
@@ -241,10 +249,27 @@ pub const Session = struct {
// held while a test holds them (`hold_jobs`).
.job => |j| if (!s.hold_jobs) pardesOf(s).perform(e) else if (pardes.host_io.KindJob.take(pardesOf(s), j.pane, j.serial, j.token)) |taken| s.held.append(pardesOf(s).gpa, taken) catch taken.drop(),
.lsp => |q| if (s.languages) |io| s.askServer(io, .{ .id = q.id, .kind = q.kind, .pane = q.pane, .offset = q.offset, .arg = q.arg.slice() }),
+ .watch => |w| {
+ if (s.watch_fd < 0) s.watch_fd = pardes.file_watch.init(true);
+ if (s.watch_fd >= 0) _ = pardes.file_watch.applyEffect(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), s.watch_fd, &s.watches, w.pane, w.on, w.mode);
+ },
else => {},
};
}
+ /// What a host does when its watch wakes: every watched file that
+ /// changed on disk read again into its pane. Waits for the wake (up to
+ /// a second); false when none came.
+ pub fn filesChanged(s: *Session) !bool {
+ if (s.watch_fd < 0) return false;
+ var pfd = [_]std.c.pollfd{.{ .fd = s.watch_fd, .events = std.c.POLL.IN, .revents = 0 }};
+ if (std.c.poll(&pfd, 1, 1000) <= 0) return false;
+ _ = pardes.file_watch.drain(s.watch_fd);
+ _ = pardes.file_watch.reloadChanged(pardesOf(s), std.Io.Threaded.global_single_threaded.io(), pardesOf(s).gpa, &s.watches);
+ s.drain();
+ return true;
+ }
+
/// The jobs held, run now and answered as the host's workers would.
pub fn releaseJobs(s: *Session) void {
s.hold_jobs = false;