跳转到内容

快速开始

几分钟内从零跑通一个原生 GPUI 桌面窗口。GPUI-RSX 会在编译期把类 JSX 标记直接展开为原生 GPUI builder 调用——没有虚拟 DOM,也没有运行时模板解析开销。

在 Cargo.toml 中引入 GPUI Kit(重命名为 gpui,确保宏展开路径一致)与 GPUI-RSX:

Cargo.toml
[dependencies]
gpui = { package = "gpui-kit", version = "=0.10.0" }
gpui-rsx = "0.13.0"

GPUI Kit 锁定了匹配的 gpui-pre 快照,并统一重导出引擎、gpui-component 控件与图标资源。建议将 Cargo.lock 提交到仓库,确保团队构建一致。

如果你正在从 Zed Git 依赖或官方 gpui = "0.2.2" 迁移,请参阅迁移指南。

将 src/main.rs 替换为下面的完整计数器示例。请务必同时导入 gpui::* 和 gpui::prelude::*,让宏展开后的 builder 方法位于当前作用域中:

src/main.rs
use gpui::component::{
ActiveTheme as _,
button::{Button, ButtonVariants as _},
};
use gpui::prelude::*;
use gpui::*;
use gpui_rsx::{rsx, rsx_listener};
#[derive(Default)]
struct CounterView {
count: usize,
}
impl Render for CounterView {
fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
rsx! {
<div
class="size-full flex flex-col gap-[16px] p-[24px]"
bg={cx.theme().background}
text_color={cx.theme().foreground}
>
<h1 class="text-xl font-bold">{format!("Count: {}", self.count)}</h1>
<p class="text-sm text-gray-500">
{"静态 RSX 会在编译期直接展开为原生 GPUI builder 调用。"}
</p>
<Button("increment")
label="加一"
primary
onClick={rsx_listener!(cx, notify, |view, _, _, _| {
view.count += 1;
})}
/>
</div>
}
}
}
fn main() {
application().with_assets(assets::Assets).run(|cx: &mut App| {
gpui::init(cx);
gpui::open_window(WindowOptions::default(), cx, |_, cx| {
cx.new(|_| CounterView::default())
})
.expect("failed to open window");
cx.activate(true);
});
}

在应用根目录执行:

终端窗口
cargo run

GPUI 依赖当前操作系统的原生图形编译环境。请参考兼容性矩阵核对 Rust、GPUI Kit 与组件库版本。

窗口跑起来之后,掌握下面四个模式就能覆盖 90% 的日常界面开发:

每个标签都会展开为 GPUI builder 链,你可以在标签内自由混写字符串字面量与 Rust 表达式:

rsx! {
<div>
"Count: "
{self.count.to_string()}
</div>
}
// 编译期展开为:
// div().child("Count: ").child(self.count.to_string())
RSX 写法 展开结果 适用场景
<div flex flex_col rounded_md /> .flex().flex_col().rounded_md() 无参布尔标志方法
<div gap={px(16.0)} bg={rgb(0x3b82f6)} /> .gap(px(16.0)).bg(rgb(0x3b82f6)) 强类型 Rust 数值与主题色
<button onClick={handler} textColor={rgb(0xffffff)} /> .on_click(handler).text_color(...) 用驼峰别名映射 GPUI 的下划线方法

静态字符串与 clsx 风格的条件数组都会在编译期完全展开,零运行时解析成本:

rsx! {
<div
class={[
"flex items-center p-8 rounded-md",
if selected { "bg-blue-500 text-white" } else { "bg-gray-100" },
disabled && "opacity-50",
]}
w={px(self.sidebar_width)}
bg={cx.theme().background}
>
{label.as_str()}
</div>
}

当 class 字符串在运行时动态拼接(class={row_class})时,宏会生成紧凑的 StyleRefinement 匹配器。动态字符串不支持 hover: / active: 等状态变体与带状态的滚动类,完整边界请阅读 Class 处理。

  • 组件构造: 用括号直传构造参数(<Button("save") ... /> → Button::new("save"))。
  • 事件更新: 使用 rsx_listener!(cx, notify, |view, _, _, _| { ... }) 自动在状态变更后触发重绘,也可以继续手写 cx.listener(...)。
  • 循环列表: 在 {for ...} 中渲染带交互状态的元素时,务必提供 key={...} 维持稳定身份。数百行以上的等高列表请直接使用 <uniform_list>。
rsx! {
<ul>
{for task in &self.tasks {
<li key={task.id} onClick={handler}>
{task.title.as_str()}
</li>
}}
</ul>
}
  • 语法参考 — 全面掌握标签、Fragment、循环、#[component] 与具名插槽。
  • Class 处理 — 深入了解静态展开、条件数组、状态变体与动态匹配边界。
  • 主题与设计令牌 — 用 rsx.toml 统一管理颜色、间距与字体令牌。
  • ID 与 Key — 理解自动 ID 生成机制及循环中 key / id 的选择。
  • CLI 与编辑器 — 配置 VS Code / Zed LSP、格式化、Lint 与 cargo rsx dev。
  • 事故响应控制台 — 学习完整的多栏原生桌面应用架构。