MeowLynxSea/yororen-ui
关于
基于 gpui 构建的可复用 UI 组件和控件。
网站与应用开发可以做什么
- 58 components - Buttons, inputs, badges, tooltips, modals, popovers, selects, lists, virtualised lists, trees, tables, and more
- Three-layer architecture - Headless primitives + JSON themes + swappable visual renderers (default + winui)
- JSON themes - Swap palettes at runtime with one install() call
- Animation system - AnimatedVisibility on every stateful composite, plus presets and easing functions
实测星标增长
- 24小时
- 0
- 7天
- -1
- 15天
- +1
- 1个月
- 正在收集历史
各周期内的星标净变化,周期相互重叠,并非每日新增。缺失的测量保持空白。星标代表关注度,不代表软件质量。
README_zh_CN.md
原文Yororen UI
中文版 · English
Yororen UI 是一个 headless 优先的 Rust UI 库,基于 gpui(通过 gpui-ce)构建。按钮不是某种视觉样式,而是一个可聚焦、可点击、可带文本标签和图标的组件;外观由渲染器决定。
主题 JSON ─▶ 渲染器(XxxRenderer)──▶ headless(XxxProps)──▶ gpui-ce
- Headless(
yororen-ui-core)—— 数据、状态、a11y、i18n、RTL、动画、资源。不做任何视觉决策。 - 渲染器(
yororen-ui-default-renderer·yororen-ui-winui-renderer)—— 负责将 props 转换为样式化的 div。58 个 trait 槽位分别由Token*或WinUI*实现;更换渲染器,整套应用的视觉就会一并切换。 - 主题 —— JSON 文件。渲染器按路径(如
action.primary.bg)读取;缺失的路径回退到渲染器的默认值。
聚合 crate yororen-ui 重新导出 core + 默认渲染器 + 三个内置 locale,大多数应用只需要这一个依赖。开启 winui 或 xml feature 即可启用备选渲染器或 XML DSL。
特性
| 能力 | 说明 |
|---|---|
| 58 个组件 | 按钮、输入框、徽章、工具提示、模态框、浮层、选择器、列表、虚拟化列表、树、表格等 |
| 三层架构 | headless 原语 + JSON 主题 + 可替换的可视化渲染器(默认 + winui) |
| JSON 主题 | 一次 install() 调用即可在运行时切换主题配色 |
| 动画系统 | 每个带状态的复合组件都内置 AnimatedVisibility,并附带预设动画与缓动函数 |
| 国际化 | 内置 en、zh-CN、ar 三种 locale;ar 会根据 cx.i18n().text_direction() 自动切换为 RTL 布局 |
| 无障碍 | 焦点管理、键盘导航、点击外部关闭、滚动锁定计数、焦点陷阱 |
| 嵌入式资源 | 通过 rust-embed 内置 20+ 个 SVG 图标 |
| 通知系统 | NotificationCenter 全局对象,支持自动关闭计时、常驻标志与持久化 |
| 可选的 XML DSL | 通过 xml! / xml_file! 宏声明式构建界面 |
快速开始
use gpui::{App, AppContext, Bounds, WindowBounds, WindowOptions, px, size};
use yororen_ui::assets::UiAsset;
use yororen_ui::locale_en;
use yororen_ui::renderer;
fn main() {
let app = gpui_ce_platform::application().with_assets(UiAsset);
app.run(|cx: &mut App| {
cx.spawn(async |cx| cx.update(open_window)).detach();
});
}
fn open_window(cx: &mut App) {
// 1) 渲染器 + 主题 —— 根据系统外观选择 system-light 或 system-dark,
// 安装全局 Theme,并注册 58 个默认渲染器实现。
renderer::install(cx, cx.window_appearance());
// 2) 初始化文本输入的键位映射(幂等)。
yororen_ui::headless::text_input::init(cx);
// 3) Locale。
locale_en::install(cx);
// 4) 主窗口。
let options = WindowOptions {
window_bounds: Some(WindowBounds::Windowed(
Bounds::centered(None, size(px(800.), px(600.)), cx),
)),
..Default::default()
};
cx.open_window(options, |_, cx| cx.new(|_| my_app::MyApp))
.expect("failed to open main window");
cx.activate(true);
}
使用 gpui-ce 0.3.0 时,请按示例通过 App::spawn 调度窗口初始化:macOS 原生启动回调持有 open_window 也需要的平台锁,前台任务会在该回调返回后执行。
在 Render::render 中:
use yororen_ui::headless::button::button;
use yororen_ui::headless::label::label;
fn render(&mut self, _w: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
div().size_full().flex().items_center().justify_center().gap_2()
.child(label("count", "0", cx).render(cx))
.child(button("inc", cx).caption("+").render(cx))
}
每个 headless 工厂都同时提供 .apply(div) 与 .render(cx) 两种调用方式:前者仅注入无障碍属性,后者则通过已注册的渲染器渲染完整视觉。文本输入的 .render(cx, window) 为双参数调用,并会在 window 上注册 IME 处理器。
切换渲染器或主题(可选)
WinUI 渲染器
use yororen_ui::winui_renderer;
winui_renderer::install_with(cx, winui_renderer::themes::winui_light());
自定义 JSON 主题
use yororen_ui_default_renderer::{Theme, install_with};
const MY_THEME: &str = include_str!("../themes/my-brand.json");
install_with(cx, Theme::from_json(MY_THEME).expect("valid JSON"));
运行时主题切换
在 Render 实现中调用 yororen_ui::theme::install(cx, new_theme)——可在每帧渲染时调用,也可在用户切换主题时调用。调用是幂等且廉价的。
impl Render for MyApp {
fn render(&mut self, _w: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
yororen_ui::theme::install(cx, self.current_theme());
// … 渲染剩余部分 …
}
}
示例应用
|
|
|
|
|
|
|
|
包含内容
| Crate | 角色 |
|---|---|
yororen-ui-core |
Headless 原语、主题 JSON 访问、i18n、a11y、RTL、动画、资源、通知中心 |
yororen-ui-default-renderer |
58 个 TokenXxxRenderer 默认实现 + 内置 system-light.json / system-dark.json 主题 + renderer::install 引导函数 |
yororen-ui-winui-renderer(可选,feature winui) |
WinUI 风格配色、紧凑控件、强调色焦点环、细腻边框 |
yororen-ui-xml + yororen-ui-xml-macro(可选,feature xml,默认开启) |
XML DSL:xml!、xml_file!、register_xml_component!、bind |
yororen-ui-locale-{en, zh-CN, ar} |
内置 JSON 翻译目录 |
yororen-ui(聚合 crate) |
重新导出上述所有内容,大多数应用只需要这一个 |
三层架构
主题 JSON ─▶ 渲染器(XxxRenderer)──▶ headless(XxxProps)──▶ gpui-ce
- Headless —— 数据 + 控制 + a11y。无视觉。
- 渲染器 —— 每个组件一个 trait,读取主题并生成样式化 div。
- 主题 —— 一个
serde_json::Value,可在运行时切换。
58 个组件标记(yororen-ui-core::renderer::markers)是全局 RendererRegistry 的键。默认渲染器和 WinUI 渲染器各实现全部 58 个 trait 槽位。
自定义渲染器只需要实现 58 个 XxxRenderer trait——完全不需要触碰 headless 层。
安装
从 crates.io 使用(推荐)
[dependencies]
yororen_ui = "0.4"
xml feature 默认开启,xml! / xml_file! 开箱即用。如需关闭:
[dependencies]
yororen_ui = { version = "0.4", default-features = false }
按需启用 feature:
[dependencies]
yororen_ui = { version = "0.4", default-features = false, features = ["xml", "winui"] }
从 GitHub 使用(最新开发版)
[dependencies]
yororen_ui = { git = "https://github.com/MeowLynxSea/yororen-ui.git", tag = "v0.4.0" }
从本地路径使用(开发时)
[dependencies]
yororen_ui = { path = "../yororen-ui/crates/yororen-ui" }
gpui 依赖
gpui 通过 crates.io 上的 gpui-ce crate 提供。请确保您的应用程序使用兼容的版本:
[dependencies]
gpui = { package = "gpui-ce", version = "0.3.0" }
gpui-ce-platform = { package = "gpui_ce_platform", version = "0.3.0", features = ["font-kit", "wayland", "x11"] }
在本仓库中,gpui-ce 已在 Cargo.toml 中指定。
环境要求
- Rust: 1.95 或更高版本(edition 2024)
- GPUI: 2026 年 10 月发布的
gpui-ce 0.3.0版本线;应用入口使用gpui_ce_platform 0.3.0。历史0.3.2/0.3.3已被 yank,且与新版本线不兼容。 - 迁移: Yororen UI 0.4 全部使用这一 GPUI 版本线;从 0.3 升级时需同步升级全部 Yororen crate,无需配置 git patch。
- 发布状态: 此分支准备 0.4.0;上述 0.4 crates.io 和 tag 示例需待该版本正式发布后使用。
- 平台: macOS、Linux、Windows(与
gpui-ce支持的范围一致)
许可证
采用 Apache License, Version 2.0 授权。本项目基于 gpui(Zed Industries)构建,同样采用 Apache-2.0。归属详情请参见 LICENSE 和 NOTICE。
贡献
欢迎提交 Issue 和 PR。修改视觉效果时:
- 请附上截图或简短录屏
- 代码请保持
rustfmt格式
Wiki
参见 Yororen UI Wiki 获取详细指南、配方和组件参考。
Star History
由 Yoro.ren 维护
探索相关项目
加载中…







