Rust UI组件库re_component_ui的使用,高效构建模块化用户界面的Rust插件库
re_component_ui
Rerun系列crate的一部分。提供用于Rerun组件数据的UI编辑器,用于注册到Rerun Viewer组件UI注册表。
安装
在项目目录中运行以下Cargo命令:
cargo add re_component_ui
或者在Cargo.toml中添加以下行:
re_component_ui = "0.24.1"
完整示例代码
以下是一个使用re_component_ui构建模块化用户界面的完整示例:
use re_component_ui::ComponentUiRegistry;
use re_log_types::ComponentName;
fn main() {
// 注册组件UI
let mut registry = ComponentUiRegistry::default();
// 定义一个自定义组件
#[derive(Debug, Clone)]
struct MyComponent {
name: String,
value: f32,
}
// 注册组件UI编辑器
registry.register::<MyComponent>(Box::new(|ui, query, _store| {
ui.label("自定义组件编辑器");
// 这里可以添加具体的UI编辑逻辑
if let Some(my_component) = query.latest::<MyComponent>() {
ui.horizontal(|ui| {
ui.label("名称:");
ui.text_edit_singleline(&mut my_component.name.clone());
});
ui.horizontal(|ui| {
ui.label("值:");
ui.add(egui::Slider::new(&mut my_component.value.clone(), 0.0..=100.0));
});
}
}));
// 在应用中使用注册的组件UI
// 这里通常会在Rerun Viewer或其他UI框架中集成
println!("组件UI已注册");
}
扩展示例代码
use re_component_ui::ComponentUiRegistry;
use re_log_types::ComponentName;
use egui::Color32;
// 定义一个更复杂的组件
#[derive(Debug, Clone)]
struct AdvancedComponent {
id: u32,
label: String,
color: Color32,
enabled: bool,
values: Vec<f32>,
}
fn register_advanced_component(registry: &mut ComponentUiRegistry) {
registry.register::<AdvancedComponent>(Box::new(|ui, query, _store| {
ui.label("高级组件编辑器");
if let Some(comp) = query.latest::<AdvancedComponent>() {
ui.horizontal(|ui| {
ui.label("ID:");
ui.label(comp.id.to_string());
});
ui.horizontal(|ui| {
ui.label("标签:");
ui.text_edit_singleline(&mut comp.label.clone());
});
ui.horizontal(|ui| {
ui.label("颜色:");
ui.color_edit_button_srgba(&mut comp.color.clone());
});
ui.checkbox(&mut comp.enabled.clone(), "启用");
ui.label("值列表:");
for (i, value) in comp.values.iter_mut().enumerate() {
ui.horizontal(|ui| {
ui.label(format!("值{}:", i));
ui.add(egui::DragValue::new(value).speed(0.1));
});
}
}
}));
}
fn main() {
let mut registry = ComponentUiRegistry::default();
// 注册简单组件
registry.register::<MyComponent>(Box::new(|ui, query, _store| {
ui.label("简单组件编辑器");
if let Some(comp) = query.latest::<MyComponent>() {
ui.horizontal(|ui| {
ui.label("名称:");
ui.text_edit_singleline(&mut comp.name.clone());
});
ui.horizontal(|ui| {
ui.label("值:");
ui.add(egui::Slider::new(&mut comp.value.clone(), 0.0..=100.0));
});
}
}));
// 注册高级组件
register_advanced_component(&mut registry);
println!("所有组件UI已注册");
}
关键特性
- 提供组件UI编辑器注册功能
- 支持与Rerun Viewer集成
- 可以自定义各种组件的UI表现形式
- 支持数据绑定和交互式编辑
注意事项
- 需要与Rerun生态系统的其他crate配合使用
- 主要设计用于可视化数据编辑场景
- 依赖于egui等UI框架
许可证
MIT OR Apache-2.0
1 回复
re_component_ui - Rust UI组件库使用指南
概述
re_component_ui是一个用于高效构建模块化用户界面的Rust插件库,提供了可复用的UI组件和布局工具,特别适合需要快速开发用户界面的Rust项目。
主要特性
- 模块化组件设计
- 响应式布局支持
- 主题定制能力
- 高性能渲染
- 与其他Rust生态良好集成
安装方法
在Cargo.toml中添加依赖:
[dependencies]
re_component_ui = "0.3.0"
基本使用方法
1. 创建简单窗口
use re_component_ui::prelude::*;
fn main() {
let app = Application::new()
.with_title("My App") // 设置应用标题
.with_size(800, 600); // 设置窗口大小
app.run(|ctx| {
Window::new(ctx) // 创建新窗口
.with_title("Main Window") // 设置窗口标题
.show(|ui| { // 显示窗口内容
ui.label("Hello, re_component_ui!"); // 添加标签
});
});
}
2. 使用内置组件
use re_component_ui::prelude::*;
fn main() {
let app = Application::new(); // 创建应用实例
app.run(|ctx| {
Window::new(ctx) // 创建窗口
.show(|ui| { // 显示窗口内容
ui.vertical(|ui| { // 垂直布局
ui.button("Click Me") // 添加按钮
.on_click(|| println!("Button clicked!")); // 设置点击事件
ui.checkbox("Enable Feature", &mut true); // 添加复选框
ui.slider("Volume", 0.0..=100.0, &mut 50.0); // 添加滑块
});
});
});
}
高级功能
自定义组件
use re_component_ui::prelude::*;
// 定义自定义组件结构体
struct MyCustomComponent {
value: i32, // 组件状态
}
// 为自定义组件实现Component trait
impl Component for MyCustomComponent {
fn render(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| { // 水平布局
ui.label("Custom Value:"); // 标签
ui.button("-").on_click(|| self.value -= 1); // 减号按钮
ui.label(&self.value.to_string()); // 显示当前值
ui.button("+").on_click(|| self.value += 1); // 加号按钮
});
}
}
fn main() {
let app = Application::new(); // 创建应用
app.run(|ctx| {
Window::new(ctx) // 创建窗口
.show(|ui| { // 显示窗口内容
MyCustomComponent { value: 0 }.render(ui); // 渲染自定义组件
});
});
}
主题定制
use re_component_ui::prelude::*;
fn main() {
// 创建应用并设置暗色主题,修改主色调
let app = Application::new()
.with_theme(Theme::dark().with_color("primary", Color::rgb(0.2, 0.6, 0.8)));
app.run(|ctx| {
Window::new(ctx) // 创建窗口
.show(|ui| { // 显示窗口内容
ui.button("Themed Button"); // 使用主题样式的按钮
});
});
}
布局系统
use re_component_ui::prelude::*;
fn main() {
let app = Application::new(); // 创建应用
app.run(|ctx| {
Window::new(ctx) // 创建窗口
.show(|ui| { // 显示窗口内容
ui.vertical(|ui| { // 垂直布局
ui.horizontal(|ui| { // 水平布局
ui.label("Left"); // 左侧标签
ui.label("Right"); // 右侧标签
});
ui.grid(2, |ui| { // 2列网格布局
ui.label("Cell 1"); // 第1单元格
ui.label("Cell 2"); // 第2单元格
ui.label("Cell 3"); // 第3单元格
ui.label("Cell 4"); // 第4单元格
});
});
});
});
}
完整示例demo
下面是一个结合了多种功能的完整示例:
use re_component_ui::prelude::*;
// 自定义计数器组件
struct Counter {
value: i32,
}
impl Component for Counter {
fn render(&mut self, ui: &mut Ui) {
ui.horizontal(|ui| {
ui.label("Counter:");
ui.button("-").on_click(|| self.value -= 1);
ui.label(&self.value.to_string());
ui.button("+").on_click(|| self.value += 1);
});
}
}
// 主应用
fn main() {
// 初始化应用并设置主题
let app = Application::new()
.with_title("Demo App")
.with_size(1024, 768)
.with_theme(Theme::dark().with_color("primary", Color::rgb(0.1, 0.5, 0.9)));
// 运行应用
app.run(|ctx| {
Window::new(ctx)
.with_title("Main Window")
.show(|ui| {
ui.vertical(|ui| {
// 标题
ui.heading("re_component_ui Demo");
// 自定义组件
Counter { value: 0 }.render(ui);
// 分隔线
ui.separator();
// 表单区域
ui.group(|ui| {
ui.label("Settings:");
let mut checkbox = true;
ui.checkbox("Enable Feature", &mut checkbox);
let mut slider = 50.0;
ui.slider("Volume", 0.0..=100.0, &mut slider);
});
// 网格布局
ui.grid(2, |ui| {
ui.label("Name:");
ui.text_input("", &mut String::new());
ui.label("Email:");
ui.text_input("", &mut String::new());
});
// 操作按钮
ui.horizontal(|ui| {
ui.button("Save");
ui.button("Cancel");
});
});
});
});
}
最佳实践
- 将复杂UI分解为小组件
- 使用模块系统组织代码
- 利用布局系统保持UI一致性
- 为常用组件创建自定义封装
- 合理使用主题系统保持UI风格统一
性能优化建议
- 使用
lazy
组件避免不必要的重绘 - 对大数据集使用虚拟滚动
- 合理使用
memoize
缓存计算结果 - 避免在渲染函数中进行繁重计算
re_component_ui通过其模块化设计和高效实现,使得在Rust中构建用户界面变得更加简单和高效。