From f230f0c30a7fa65b5e33e44c3461be07a79fed00 Mon Sep 17 00:00:00 2001 From: Tim Culverhouse Date: Wed, 12 Jun 2024 19:29:19 -0500 Subject: [PATCH] widgets(terminal): send a redraw event --- examples/vt.zig | 6 ++++++ src/widgets/terminal/Terminal.zig | 11 ++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/examples/vt.zig b/examples/vt.zig index c3c6c36..9e33313 100644 --- a/examples/vt.zig +++ b/examples/vt.zig @@ -56,17 +56,21 @@ pub fn main() !void { defer vt.deinit(); try vt.spawn(); + var redraw: bool = false; while (true) { std.time.sleep(8 * std.time.ns_per_ms); // try vt events first while (vt.tryEvent()) |event| { + redraw = true; switch (event) { .bell => {}, .title_change => {}, .exited => return, + .redraw => {}, } } while (loop.tryEvent()) |event| { + redraw = true; switch (event) { .key_press => |key| { if (key.matches('c', .{ .ctrl = true })) return; @@ -77,6 +81,8 @@ pub fn main() !void { }, } } + if (!redraw) continue; + redraw = false; const win = vx.window(); win.hideCursor(); diff --git a/src/widgets/terminal/Terminal.zig b/src/widgets/terminal/Terminal.zig index 7647a66..8e09c06 100644 --- a/src/widgets/terminal/Terminal.zig +++ b/src/widgets/terminal/Terminal.zig @@ -18,6 +18,7 @@ const key = @import("key.zig"); pub const Event = union(enum) { exited, + redraw, bell, title_change, }; @@ -66,6 +67,8 @@ back_screen_alt: Screen, // only applies to primary screen scroll_offset: usize = 0, back_mutex: std.Thread.Mutex = .{}, +// dirty is protected by back_mutex. Only access this field when you hold that mutex +dirty: bool = false, unicode: *const vaxis.Unicode, should_quit: bool = false, @@ -192,8 +195,10 @@ pub fn draw(self: *Terminal, win: vaxis.Window) !void { defer self.back_mutex.unlock(); // We keep this as a separate condition so we don't deadlock by obtaining the lock but not // having sync - if (!self.mode.sync) + if (!self.mode.sync) { try self.back_screen.copyTo(&self.front_screen); + self.dirty = false; + } } var row: usize = 0; @@ -261,6 +266,10 @@ fn run(self: *Terminal) !void { self.back_mutex.lock(); defer self.back_mutex.unlock(); + defer if (!self.dirty and self.event_queue.tryPush(.redraw)) { + self.dirty = true; + }; + switch (event) { .print => |str| { var iter = grapheme.Iterator.init(str, &self.unicode.grapheme_data);