From 2f78a6d97b71b1ac7650e603808a884fd0755d58 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 28 Nov 2025 14:26:09 -0800 Subject: [PATCH] apprt: add Layout type --- src/apprt.zig | 2 ++ src/apprt/layout.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) create mode 100644 src/apprt/layout.zig diff --git a/src/apprt.zig b/src/apprt.zig index c467f1801..9f54c4b74 100644 --- a/src/apprt.zig +++ b/src/apprt.zig @@ -18,6 +18,7 @@ pub const gtk = @import("apprt/gtk.zig"); pub const none = @import("apprt/none.zig"); pub const browser = @import("apprt/browser.zig"); pub const embedded = @import("apprt/embedded.zig"); +pub const layout = @import("apprt/layout.zig"); pub const surface = @import("apprt/surface.zig"); pub const Action = action.Action; @@ -55,5 +56,6 @@ test { _ = Runtime; _ = runtime; _ = action; + _ = layout; _ = structs; } diff --git a/src/apprt/layout.zig b/src/apprt/layout.zig new file mode 100644 index 000000000..eb2d28a49 --- /dev/null +++ b/src/apprt/layout.zig @@ -0,0 +1,56 @@ +const std = @import("std"); +const Allocator = std.mem.Allocator; +const configpkg = @import("../config.zig"); +const datastruct = @import("../datastruct/main.zig"); + +/// SplitTree represents a desired layout of splits and their associated +/// surface configurations. This is used by apprts to launch and modify +/// predefined terminal layouts. +/// +/// This only represents a desired split tree; it doesn't represent +/// tabs, windows, or any other higher-level constructs. These will +/// be represented elsewhere. To start, we only support single-tab +/// layouts. +pub const SplitTree = datastruct.SplitTree(struct { + const View = @This(); + + /// A unique identifier for this leaf node. This is guaranteed to + /// forever unique and not reused during the lifetime of the + /// layout. This allows layouts to change at runtime and the apprt + /// to diff them. + /// + /// It is up to the creator of the layout to ensure uniqueness + /// when creating or updating layouts. + id: u64, + + /// The configuration associated with this leaf node when creating + /// the associated terminal surface. + config: configpkg.Config, + + /// The reference count for this layout node. When it reaches + /// zero it will be freed. + refs: usize = 1, + + pub fn ref(self: *View, _: Allocator) Allocator.Error!*View { + self.refs += 1; + return self; + } + + pub fn unref(self: *View, alloc: Allocator) void { + self.refs -= 1; + if (self.refs == 0) { + self.config.deinit(); + alloc.destroy(self); + } + } + + pub const CApi = struct { + pub fn get_id(self: *const View) callconv(.c) u64 { + return self.id; + } + + pub fn get_config(self: *const View) callconv(.c) *const configpkg.Config { + return &self.config; + } + }; +});