mod(Render): fullscreen option as comptime configuration through App(..)

This commit is contained in:
2024-11-06 17:06:45 +01:00
parent 9b165e8f81
commit 4ded0210ee
3 changed files with 57 additions and 21 deletions

View File

@@ -11,10 +11,34 @@ const Queue = @import("queue.zig").Queue;
const log = std.log.scoped(.app);
// Create the App Type with the associated user events `E` which describes
// an tagged union for all the user events that can be send through the
// applications event loop.
pub fn App(comptime E: type) type {
/// Create the App Type with the associated user events _E_ which describes
/// an tagged union for all the user events that can be send through the
/// applications event loop.
///
/// _R_ is the type function for the `Renderer` to use. The parameter boolean
/// will be set to the _fullscreen_ value at compile time. The corresponding
/// `Renderer` type is accessable through the generated type of this function.
///
/// _fullscreen_ will be used to configure the `App` and the `Renderer` to
/// respect the corresponding configuration whether to render a fullscreen tui
/// or an inline tui.
///
/// # Example
///
/// Create an `App` which renders using the `PlainRenderer` in fullscreen with
/// an empty user Event:
///
/// ```zig
/// const App = @import("app.zig").App(
/// union(enum) {},
/// @import("render.zig").PlainRenderer,
/// true,
/// );
/// // later on use
/// var app: App = .{};
/// var renderer: App.Renderer = .{};
/// ```
pub fn App(comptime E: type, comptime R: fn (comptime bool) type, comptime fullscreen: bool) type {
if (!isTaggedUnion(E)) {
@compileError("Provided user event `E` for `App(comptime E: type)` is not of type `union(enum)`.");
}
@@ -22,6 +46,7 @@ pub fn App(comptime E: type) type {
pub const Event = mergeTaggedUnions(SystemEvent, E);
pub const Layout = @import("layout.zig").Layout(Event);
pub const Widget = @import("widget.zig").Widget(Event);
pub const Renderer = R(fullscreen);
queue: Queue(Event, 256) = .{},
thread: ?std.Thread = null,
@@ -59,7 +84,10 @@ pub fn App(comptime E: type) type {
var termios: std.posix.termios = undefined;
try terminal.enableRawMode(&termios);
this.termios = termios;
try terminal.saveScreen();
if (fullscreen) {
try terminal.saveScreen();
try terminal.enterAltScreen();
}
}
pub fn interrupt(this: *@This()) !void {
@@ -74,10 +102,15 @@ pub fn App(comptime E: type) type {
try this.interrupt();
if (this.termios) |*termios| {
try terminal.disableRawMode(termios);
try terminal.restoreScreen();
if (fullscreen) {
try terminal.existAltScreen();
try terminal.restoreScreen();
}
}
}
/// Quit the application loop.
/// This will stop the internal input thread and post a **.quit** `Event`.
pub fn quit(this: *@This()) void {
this.quit_event.set();
this.postEvent(.quit);
@@ -115,9 +148,9 @@ pub fn App(comptime E: type) type {
fn run(this: *@This()) !void {
// send initial terminal size
// changes are handled by the winch signal handler (see `init` and `registerWinch`)
const size = terminal.getTerminalSize();
this.postEvent(.{ .resize = size });
// changes are handled by the winch signal handler
// see `App.start` and `App.registerWinch` for details
this.postEvent(.{ .resize = terminal.getTerminalSize() });
// thread to read user inputs
var buf: [256]u8 = undefined;