快速入门
从零开始搭建 rgpui 开发环境并运行第一个应用。
环境要求
- Rust:1.88 或更新版本(edition 2024)
- 操作系统:Windows 10+、macOS 12+(代码保留、暂无设备验证)、Linux(X11/Wayland,需装依赖)
- GPU:支持 Vulkan/Metal/WebGPU 的显卡(软件渲染可用 llvmpipe/lavapipe)
创建项目
cargo new my_rgpui_app
cd my_rgpui_app
在 Cargo.toml 中添加依赖。必须同时依赖 rgpui-platform 并用它启动应用:
单独依赖 rgpui 在 Linux 上会因 feature 未开启而静默退化成无头后端(编译成功、
没有窗口、零报错)。
[dependencies]
rgpui = { path = "../path/to/rgpui" }
rgpui-platform = { path = "../path/to/rgpui/crates/rgpui-platform" }
# 或者使用 git 依赖
# rgpui = { git = "https://github.com/launcher-rs/rgpui.git" }
# rgpui-platform = { git = "https://github.com/launcher-rs/rgpui.git" }
Linux 上还需要系统库(用于 webview 示例,影响 cargo check --workspace):
sudo apt install libwebkit2gtk-4.1-dev libgtk-3-dev libglib2.0-dev
细节见 AGENTS.md 的「Linux 端构建前置」。
第一个应用
创建 src/main.rs:
use rgpui::{ App, Bounds, Context, SharedString, TitlebarOptions, Window, WindowBounds, WindowOptions, div, prelude::*, px, size, }; use rgpui_platform::application; fn main() { application().run(|cx: &mut App| { // 先注册全局能力,再打开窗口(详见下文「注册模块」) rgpui::theme::init(cx); rgpui::input_ui::init(cx); rgpui::menu::init(cx); rgpui::list::init(cx); rgpui::table::init(cx); rgpui::components::init(cx); let bounds = Bounds::centered(None, size(px(500.0), px(400.0)), cx); cx.open_window( WindowOptions { window_bounds: Some(WindowBounds::Windowed(bounds)), titlebar: Some(TitlebarOptions { title: Some("Hello rgpui".into()), ..Default::default() }), ..Default::default() }, |_, cx| cx.new(|_| HelloView { text: "Hello, rgpui!".into(), }), ); }); } struct HelloView { text: SharedString, } impl Render for HelloView { fn render(&mut self, _window: &mut Window, _cx: &mut Context<Self>) -> impl IntoElement { div() .flex() .items_center() .justify_center() .size_full() .text_xl() .child(self.text.clone()) } }
运行应用
cargo run
你应该会看到一个包含 "Hello, rgpui!" 文本的窗口。
代码解析
application().run()
这是应用入口。rgpui_platform::application() 会按当前平台选择后端(Windows / macOS /
Linux 的 Wayland 或 X11 / Web),并把闭包里的 cx: &mut App 交给应用。传 rgpui 自带
的 Application 而绕过 rgpui-platform 会漏掉平台后端的选择。
注册模块(init)
很多组件不是 new 出来就能用:全局动作、KeyContext、焦点上下文和主题都要先注册。
这些调用必须放在 open_window 之前,通常只在 main 里调一次:
| 调用 | 不调用的典型症状 | 覆盖组件 |
|---|---|---|
rgpui::theme::init(cx) | 颜色全用默认值,滚动条外观不同步 | 所有读 cx.theme() 的组件 |
rgpui::input_ui::init(cx) | 输入框不响应按键 | Input / TextArea / NumberInput / Editor |
rgpui::menu::init(cx) | 弹出菜单、右键菜单点击无反应 | PopupMenu / ContextMenu / DropdownMenu / Popover / AppMenuBar |
rgpui::list::init(cx) | 列表快捷键(Esc、Enter、上下键)无效 | List / VirtualList / DataTable |
rgpui::table::init(cx) | 表格全局行为缺失 | DataTable |
rgpui::components::init(cx) | 内联编辑、OTP、图片查看器、Tree 不工作 | InlineEdit / OTPInput / ImageViewer / Tree |
注意 charts / qr-code / diff / effects 等是 Cargo feature,开启即可用,不需要
运行时注册。完整对照见交互文档的「技能索引 → 初始化与注册」页,以及
skills/rgpui/references/ 下各主题文档。
cx.open_window(options, build_view)
open_window 接收 [WindowOptions] 和一个构建根视图的闭包。视图通过 cx.new(...) 创建成
Entity<T>,由框架持有;旧版的 Window::default() / window.replace_root_view(...) 写法
已不存在。
Render trait
每个视图实现 Render,render 在视图需要重绘时被调用,返回实现 IntoElement 的元素树
(通常用 div() 起手)。Context<Self> 提供 cx.listener(...)、cx.notify() 等视图级能力。