Inicio rápido
Crea una app nativa de macOS que vive fuera del repo de zenit y se compila y ejecuta por sí sola.
Requisitos previos
Esta versión apunta a macOS, con Apple Silicon verificado de forma continua. Asegúrate de tener Zig 0.15.2 y las Xcode Command Line Tools; Bun solo hace falta para las pruebas end-to-end.
zig version
xcode-select -pCrear un proyecto
Parte de templates/minimal-app en el repo. Es un proyecto downstream completo:
minimal-app/
├── README.md
├── build.zig # zenit.attach() + run / app steps
├── build.zig.zon # declares the zenit dependency
├── e2e/
│ └── record-demo.ts # drives and records the app through the Harness
└── src/
└── main.zig # counter-button app- 1Copia la plantilla
Copia templates/minimal-app a un directorio nuevo fuera del repo de zenit.
- 2Ajusta la ruta de la dependencia
La plantilla usa por defecto
.path = "../../", que solo se resuelve dentro del repo. Tras copiarla, apúntala a tu checkout de zenit de forma relativa a build.zig.zon, o fija una revisión publicada conzig fetch --save=zenit <url>. - 3Genera una huella
No conserves el .fingerprint de la plantilla: dos proyectos que lo compartan chocan en la caché de paquetes de Zig. Borra la línea, ejecuta zig build una vez y pega el valor que indica el error. De paso, cambia .name = .myapp.
git clone https://github.com/version-next/zenit.git
cp -R zenit/templates/minimal-app ./myapp
cd myapp # then set .zenit = .{ .path = "../zenit" } in build.zig.zon
zig buildConectar zenit
build.zig.zon declara la dependencia; build.zig obtiene la API de build propia de zenit mediante @import("zenit").
.{
.name = .myapp,
.version = "0.1.0",
.fingerprint = 0x8798022a7f8e220e, // replace: delete this line, run zig build once
.minimum_zig_version = "0.15.2",
.dependencies = .{
// Relative to this file. Absolute paths are rejected by Zig 0.15.2.
.zenit = .{ .path = "../zenit" },
},
.paths = .{
"README.md",
"build.zig",
"build.zig.zon",
"e2e",
"src",
},
}Este es el build.zig de la plantilla tal cual. Es más que una llamada a attach: reenvía los flags de test, instala el cliente del Harness y define el paso run y el empaquetado .app:
const std = @import("std");
const zenit = @import("zenit");
pub fn build(b: *std.Build) void {
const target = b.standardTargetOptions(.{});
const optimize = b.standardOptimizeOption(.{});
const test_mode = b.option(bool, "test-mode", "Enable the Zenit automation harness") orelse false;
const e2e_port = b.option(u16, "e2e-port", "Zenit Harness RPC directory suffix") orelse 19816;
const zenit_dep = b.dependency("zenit", .{
.target = target,
.optimize = optimize,
// Dependency build options are isolated in Zig. Forward these
// explicitly so `zig build -Dtest-mode=true` reaches Zenit.
.@"test-mode" = test_mode,
.@"e2e-port" = e2e_port,
});
const exe = b.addExecutable(.{
.name = "myapp",
.root_module = b.createModule(.{
.root_source_file = b.path("src/main.zig"),
.target = target,
.optimize = optimize,
}),
});
zenit.attach(zenit_dep, exe);
zenit.installHarnessClient(b, zenit_dep);
b.installArtifact(exe);
const run_cmd = b.addRunArtifact(exe);
run_cmd.step.dependOn(b.getInstallStep());
const run_step = b.step("run", "Run the app");
run_step.dependOn(&run_cmd.step);
// Optional: `zig build app` packages a double-clickable .app bundle.
if (target.result.os.tag == .macos) {
const bundled = zenit.bundleApp(b, .{
.exe = exe,
.display_name = "My App",
.bundle_id = "com.example.myapp",
.version = "0.1.0",
.signing = .ad_hoc,
});
const app_step = b.step("app", "Build the .app bundle");
app_step.dependOn(bundled.final_step);
}
}zenit.attach(zenit_dep, exe)Añade los imports de módulo ui y zenit_app, compila los cinco puentes ObjC de macOS y enlaza Cocoa, Metal, CoreText y los demás frameworks.@"test-mode" / .@"e2e-port"En Zig las opciones de dependencia están aisladas; reenvíalas explícitamente para que zig build -Dtest-mode=true llegue a zenitzenit.installHarnessClient(b, zenit_dep)Instala el cliente tipado del Harness en zig-out/share/zenit/harness/client.ts para e2e/record-demo.tszenit.bundleApp(b, .{ ... })Genera un zig-out/<display_name>.app que se abre con doble clic, aquí con firma ad-hocMonta tu primer árbol de UI
App.runWith crea el Scope raíz, llama una vez a tu función de montaje y toma el control del bucle de eventos. La función de montaje debe tener la firma fn (cx: *ui.Cx, scope: *ui.Scope) anyerror!*ui.Node; tu app solo devuelve el nodo raíz.
const std = @import("std");
const ui = @import("ui");
const App = @import("zenit_app").App;
const Counter = struct {
n: u32 = 0,
label: ?*ui.Node = null,
buf: [32]u8 = undefined,
pub fn increment(self: *Counter) void {
self.n += 1;
const node = self.label orelse return;
const content = std.fmt.bufPrint(&self.buf, "Clicked {d} times", .{self.n}) catch return;
if (node.getText()) |old| {
var t = old;
t.content = content;
node.setText(t);
}
node.markRenderDirty();
}
};
fn mountUi(cx: *ui.Cx, scope: *ui.Scope) anyerror!*ui.Node {
const allocator = cx.allocator;
const counter = try cx.bindState(Counter, .{});
const click = cx.on(Counter, counter, Counter.increment);
const root = try ui.box(cx, .{
.width = .fill(),
.height = .fill(),
.direction = .column,
.gap = 16,
.padding = ui.Padding.all(40),
.background = cx.tokens.color.bg_primary,
.align_items = .center,
.justify = .center,
}, .{});
try root.appendChild(allocator, try ui.text(cx, "Hello, zenit!", .{
.font_size = 24,
.font_weight = 600,
.color = cx.tokens.color.fg_primary,
}));
const button = try ui.widgets.Button(.{
.label = "Click me",
.variant = .primary,
.on_click = click,
}).mount(scope, cx);
button.meta.ownership.meta.test_id = "counter.increment";
try root.appendChild(allocator, button);
const label = try ui.text(cx, "Clicked 0 times", .{
.font_size = 14,
.color = cx.tokens.color.fg_secondary,
});
try root.appendChild(allocator, label);
counter.label = label;
return root;
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const app = try App.init(gpa.allocator(), .{
.window = .{ .width = 640, .height = 480, .title = "Hello, zenit" },
});
defer app.deinit();
try app.runWith(mountUi);
}cx.bindState entrega Counter al Cx, y cx.on convierte uno de sus métodos en el callback del botón; test_id permite que los scripts E2E encuentren el botón por su significado.
Compilar y ejecutar
zig build run # build and run the unbundled executable
zig build app # package zig-out/My App.app (macOS)La plantilla también incluye un script de grabación: compila con zig build -Dtest-mode=true, arranca la app y ejecuta e2e/record-demo.ts para obtener un clip similar; consulta E2E harness.