zenit 开发手册The zenit manual
用 Zig 构建真正原生的 macOS 桌面界面。zenit 把窗口、GPU 渲染、文本、输入法与无障碍收进一个清晰的 UI 模型;这份手册带你从第一扇窗口走到可维护的生产应用。Build truly native macOS desktop UI in Zig. zenit folds windows, GPU rendering, text, input methods and accessibility into one clear UI model; this manual takes you from your first window to a maintainable production app.
const std = @import("std");
const ui = @import("ui");
const App = @import("zenit_app").App;
fn mountUi(cx: *ui.Cx, _: *ui.Scope) anyerror!*ui.Node {
return ui.text(cx, "Hello, zenit!", .{
.font_size = cx.tokens.font_size.xxxl,
.color = cx.tokens.color.fg_primary,
});
}
pub fn main() !void {
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
defer _ = gpa.deinit();
const app = try App.init(gpa.allocator(), .{
.window = .{ .title = "Hello" },
});
defer app.deinit();
try app.runWith(mountUi);
}推荐学习路径Learning path
按顺序完成一次,约 45 分钟,你就拥有独立开发 zenit 应用所需的完整心智模型。Work through it once, in order — about 45 minutes — and you’ll have the whole mental model for building zenit apps on your own.
你将使用的架构The architecture you use
应用只依赖稳定的公开层,平台与渲染细节由 zenit 接管。Apps depend only on the stable public layer; zenit owns platform and rendering.
zenit.attach(dep, exe) 只给你的可执行文件加上 ui 与 zenit_app 两个模块导入,并编译原生桥接、链接系统框架。内部模块不在应用的构建图里,不是「约定不要用」,而是根本导入不到。zenit.attach(dep, exe) adds exactly two module imports to your executable — ui and zenit_app — then compiles the native bridges and links the system frameworks. Internal modules aren’t in your build graph at all: not “please don’t”, but “can’t”.不只是一组 APIMore than a set of APIs
这三项能力贯穿运行时、文本与工程工具;每一项都有可运行的演示和真实窗口证据。These three run through the runtime, the text stack and the tooling — each backed by a runnable demo and real-window evidence.
三条开发原则Three principles
遵守它们,代码能在 zenit 快速演进的阶段保持清晰。Follow them and your code stays clear while zenit is still moving fast.
应用代码使用 ui.*、ui.<group>.* 与 zenit_app.App,不要深入内部目录。Use ui.*, ui.<group>.* and zenit_app.App — never reach into internal directories.
持久状态放进 Signal 或绑定状态;派生值用 Memo,副作用用 Effect。Persistent state lives in Signals or bound state; derive with Memo, side-effect with Effect.
颜色、字号、间距与圆角来自主题;确需脱离刻度时,用 ui.arb 明确表达。Color, type, spacing and radii come from the theme; when you must go off-scale, say so with ui.arb.