add(element): interface for injecting user behavior to containers

Some additional refactoring and documentation updates have also been
applied.
This commit is contained in:
2025-02-15 15:56:30 +01:00
parent 5c148e1aa5
commit 4781e9ce39
5 changed files with 128 additions and 45 deletions

View File

@@ -27,8 +27,8 @@ const log = std.log.scoped(.app);
/// union(enum) {},
/// );
/// // later on create an `App` instance and start the event loop
/// var app: App = .{};
/// try app.start(null);
/// var app: App = .init;
/// try app.start();
/// defer app.stop() catch unreachable;
/// ```
pub fn App(comptime E: type) type {
@@ -38,19 +38,29 @@ pub fn App(comptime E: type) type {
return struct {
pub const Event = mergeTaggedUnions(event.SystemEvent, E);
pub const Container = @import("container.zig").Container(Event);
pub const Element = @import("element.zig").Element(Event);
queue: Queue(Event, 256) = .{},
thread: ?std.Thread = null,
quit_event: std.Thread.ResetEvent = .{},
queue: Queue(Event, 256),
thread: ?std.Thread,
quit_event: std.Thread.ResetEvent,
termios: ?std.posix.termios = null,
attached_handler: bool = false,
prev_size: Size = .{ .cols = 0, .rows = 0 },
prev_size: Size,
pub const SignalHandler = struct {
context: *anyopaque,
callback: *const fn (context: *anyopaque) void,
};
pub const init: @This() = .{
.queue = .{},
.thread = null,
.quit_event = .{},
.termios = null,
.attached_handler = false,
.prev_size = .{},
};
pub fn start(this: *@This()) !void {
if (this.thread) |_| return;