From 4c134eaa5c7f84eba7091c9ccef86567f1434d49 Mon Sep 17 00:00:00 2001 From: Gabriel Schneider Date: Sat, 7 Nov 2026 19:45:54 -0300 Subject: 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 Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge --- test/behaviour/addons.zig | 33 +++++++++++++++++++++++++++++++++ test/behaviour/addons_pdf.zig | 24 ++++++++++++++++++++++++ test/session.zig | 25 +++++++++++++++++++++++++ 3 files changed, 82 insertions(+) (limited to 'test') 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; -- cgit v1.3