# awtk-zig **Repository Path**: ufbycd/awtk-zig ## Basic Information - **Project Name**: awtk-zig - **Description**: AWTK-Zig 是一个建立在 AWTK GUI 之上用 Zig 语言实现的声明式UI框架。 - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2026-01-18 - **Last Updated**: 2026-02-17 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # AWTK-Zig ## 项目简介 AWTK-Zig 是一个建立在 AWTK GUI 之上用 Zig 语言实现的声明式UI框架。本框架使用 Zig 语言对UI进行描述以替代 AWTK 原本使用XML描述UI的方式。 ## UI声明 本项目直接使用Zig语言的结构体来对UI进行描述,以替代传统UI框架使用XML等标记语言描述UI的方式。借助Zig强大的编译时特性,此举措对比XML之类有如下优点: - 统一编程语言和UI描述,不需要额外的标记语言或DSL语言 - 去除UI描述标记繁琐的编解码过程,或从UI描述标记生成目标编程语言代码的过程 - 结构体在编译时通过`inline for`展开,控件构建效率高于UI描述标记的解码过程 - UI复合组件可直接复用 - 可在UI描述中直接编写事件响应代码 - 可将运行时的上下文结构指针传入到UI描述中 UI描述举例如下: ```C fn counterLabelUi(contex: *Contex) struct { comptime widget: [:0]const u8 = "label", x: i32 = 300, y: i32 = 10, w: i32 = 150, h: i32 = 64, text: [:0]const u8 = "Count", on_init: struct { ctx: *Contex = undefined, pub fn init(self: @This(), widget: *zawtk.Widget) void { self.ctx.label = widget; std.log.debug("{s}({d}): {s}", .{ @src().fn_name, @src().line, c.widget_get_type(widget) }); } }, } { return .{ .on_init = .{ .ctx = contex } }; } const exit_button_ui = .{ .widget = "button", .self_layout = "default(x=300,y=m,w=150,h=64)", .child = .{ .widget = "label", .self_layout = "default(x=0,y=0,w=100%,h=100%)", .text = "Close" }, .callback = struct { pub fn onClick(_: *zevent.Event) zawtk.Ret { return c.tk_quit(); } pub fn onDestroy(event: *zevent.Event) zawtk.Ret { const target = zevent.getTargetWidget(event); std.log.info("Destroy widget: {s}", .{c.widget_get_type(target)}); return c.RET_OK; } }, }; const main_ui = .{ .widget = "window", .text = "ZAWTK Window", .children = .{ counterLabelUi(contex), .{ .widget = "button", .x = 300, .y = 100, .w = 150, .h = 64, .text = "Add Count", .callback = struct { ctx: *Contex, pub fn onClick(the_ctx: *Contex, event: *zevent.Event) zawtk.Ret { const target = zevent.getTargetWidget(event); the_ctx.count += 1; std.log.info("{s} clicded, count: {d}", .{ c.widget_get_type(target), the_ctx.count }); const label = the_ctx.label orelse return c.RET_FAIL; var buf: [64]u8 = undefined; const str = std.fmt.bufPrintZ(&buf, "Count: {d}", .{the_ctx.count}) catch "Count: ?"; return c.widget_set_text_utf8(label, str); } }{ .ctx = contex }, }, exit_button_ui, }, }; ``` ## 环境要求 - Zig 0.15.x 编译器 - AWTK SDK - Python 3.x (用于资源生成工具) - Linux/Windows/macOS 操作系统 ## 准备 1. 获取 awtk 并编译 ```shell git clone -b allocator https://gitee.com/ufbycd/awtk.git cd awtk && scons -j16 && cd - ``` 2. 获取本项目源码 ```shell git clone https://gitee.com/ufbycd/awtk-zig.git ``` ## 运行 1. 生成UI资源 ```shell zig build res ``` 2. 编译并运行 ```shell zig build run ``` ## 贡献指南 欢迎贡献代码和资源: 1. Fork 本仓库 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 创建 Pull Request ## 许可证 本项目遵循 AWTK 相关的许可证协议。 ## 联系方式 - 项目地址: https://gitee.com/ufbycd/awtk-zig - AWTK 官网: https://awtk.zlg.cn ## 参考资源 - [AWTK 文档](https://awtk.zlg.cn/docs/) - [Zig 语言官网](https://ziglang.org/) - [AWTK GitHub](https://github.com/zlgopen/awtk) --- **注意**: 确保在构建和运行项目前正确配置 AWTK 环境变量,并安装所有必要的依赖项。