egui常用组件

egui是库 将输入根据回调计算成图形
epi是库 后端的库
eframe是框架 将上面两个封装整合

居中

ui.vertical_centered(|ui| {
});

分割线

ui.separator();

复选框

ui.checkbox(show, "Perspect");

大字标头/小标签

ui.heading("Camera");
ui.small("Camera");

按钮

if ui.button("Record").clicked() {
}

保留按下状态的按钮

if ui.selectable_label(checked, "Record").clicked() {
}

水平放置

ui.horizontal(|ui| {
});

滑块

ui.add(egui::Slider::new(value, 0.0..=10.0).text("value"));

折叠

ui.collapsing("Click to see what is hidden!", |ui| {
});

frame边框

ui.group(|ui| {
});

滚动区

egui::ScrollArea::auto_sized().show(ui, |ui| {
});

ComboBox

egui::ComboBox::from_label("Take your pick")
.selected_text(format!("{}", radio))
.show_ui(ui, |ui| {
    ui.selectable_value(radio, "First".to_owned(), "First");
    ui.selectable_value(radio, "Second".to_owned(), "Second");
});

侧标栏

egui::SidePanel::left("side_panel").show(ctx, |ui| {
};

菜单栏

use native_dialog::{FileDialog, MessageDialog, MessageType};

egui::TopBottomPanel::top("top_panel").show(ctx, |ui| {
    // The top panel is often a good place for a menu bar:
    egui::menu::bar(ui, |ui| {
        egui::menu::menu(ui, "File", |ui| {
            if ui.button("Open").clicked() {
                let path = FileDialog::new()
                    .set_location("~/Desktop")
                    .add_filter("PNG Image", &["png"])
                    .add_filter("JPEG Image", &["jpg", "jpeg"])
                    .show_open_single_file()
                    .unwrap();
                println!("{:?}", path);
            }
            if ui.button("Save").clicked() {
            }
            if ui.button("Save As").clicked() {
            }
            if ui.button("Quit").clicked() {
                frame.quit();
            }
        });
        egui::menu::menu(ui, "Help", |ui| {
            if ui.button("About").clicked() {
                MessageDialog::new()
                .set_type(MessageType::Info)
                .set_title("Do you want to open the file?")
                .set_text("Path")
                .show_alert()
                .unwrap();
            }
        });
    });
});

带状态的UI Widget

//! 创建使用  egui::Memory 存储UI状态的 widget


/// 密码输入框能切换字符是否隐藏
pub fn password_ui(ui: &mut egui::Ui, text: &mut String) -> egui::Response {
    // 这个 widget 有自己的状态 — 显示或隐藏密码的字符

    // 1. 声明状态的struct
    // 此 struct 表示 widget 的状态.
    // 必须实现 Clone 和 'static
    #[derive(Clone, Copy, Default)]
    struct State(bool);

    // 2. 创建 id
    let id = ui.id().with("show_password");

    // 3. 获取此widget的状态
    // 可以在 egui::Memory  和 egui::any 了解更多的内存函数 
    // 应该使用值来获取状态,而非引用以避免内存的借用
    let mut plaintext = *ui.memory().id_data_temp.get_or_default::<State>(id);

    // 4. 处理UI, 改变状态的局部副本
    // 为了右侧放一个按钮后,文本域填充剩下的空间,从右到左布局
    let result = ui.with_layout(egui::Layout::right_to_left(), |ui| {
        // 这是使用能被用户改变的状态副本
        let response = ui
            .add(egui::SelectableLabel::new(plaintext.0, "👁"))
            .on_hover_text("Show/hide password");
        if response.clicked() {
            plaintext.0 = !plaintext.0;
        }

        let text_edit_size = ui.available_size();

        // 这里使用局部状态
        ui.add_sized(
            text_edit_size,
            egui::TextEdit::singleline(text).password(!plaintext.0),
        );
    });

    // 5. 保存改变的状态
    ui.memory().id_data_temp.insert(id, plaintext);

    result.response
}

新建一个内部窗口

egui::Window::new("Window").show(ctx, |ui| {
});

顶栏(常用作菜单栏)
底栏(常用作通知栏)

egui::TopBottomPanel::top("my_panel").show(ctx, |ui| {
});
egui::TopBottomPanel::bottom("my_panel").show(ctx, |ui| {
});

拖入文件

// Collect dropped files:
if !ctx.input().raw.dropped_files.is_empty() {
    let dropped_files:  Vec<egui::DroppedFile> = ctx.input().raw.dropped_files.clone();
}

i18n

fn setup(
    &mut self,
    ctx: &egui::CtxRef,
    _frame: &mut epi::Frame<'_>,
    storage: Option<&dyn epi::Storage>,
) {
    let mut fonts = FontDefinitions::default();

    fonts.font_data.insert("my_font".to_owned(),
    std::borrow::Cow::Borrowed(include_bytes!("../font/SourceHanSans-Normal.otf"))); // .ttf and .otf supported

    fonts.fonts_for_family.get_mut(
        &FontFamily::Proportional).unwrap().push("my_font".to_owned()
    );
    ctx.set_fonts(fonts);
}

改变光标形状

ui.output().cursor_icon = CursorIcon::Grab;

egui::Response::dragged 鼠标左键按下
egui::Response::clicked 鼠标左键抬起

请求重绘,即使是在reactive模式,也会刷新

ui.ctx().request_repaint();
Rust 是一种系统级编程语言,以其内存安全性和高性能而著称。EGui Graphs 是 Egui (Efficient GUI) 的一部分,Egui 是一个轻量级且快速的 GUI(图形用户界面)库,设计用于 Rust 中。Egui_Graphs 允许开发者在 Rust 程序中创建交互式的数据可视化图表,如直方图、折线图等。 在 Rust 中使用 EGui Graphs 的程序通常涉及以下步骤: 1. 安装和引入依赖:首先,你需要在你的 Rust 项目中添加 EGui 和相关依赖到 `Cargo.toml` 文件中。 ```toml [dependencies] egui = "0.12.0" egui_grids = "0.12.0" ``` 2. 引入并初始化:在代码中引入 `egui` 和 `egui_grids` 模块,并在渲染循环中初始化 EGui。 ```rust extern crate egui; use egui::{Context, DefaultUI}; use egui_grids::{Grid, GridElement}; ``` 3. 创建图表数据:定义你要显示的数据结构和相关的数据处理函数。 ```rust struct GraphData { // 数据项... } fn generate_data() -> GraphData { // ... } ``` 4. 创建 Graph Element:创建一个自定义的 EGui 组件来展示图表,这可能涉及绘制、配置样式和事件处理。 ```rust struct MyGraphElement { data: GraphData, } impl DefaultUI for MyGraphElement { fn on_draw(&mut self, ctx: &mut Context<'_, '_>) { // 用EGui API 绘制图表 // ctx.add_graph(&self.data, ...); } } ``` 5. 组织布局:在 EGui 的窗口或布局中添加你的图表组件。 ```rust fn main() { let mut window = egui::Window::new("My Graph", 640.0, 480.0); let mut graph_data = generate_data(); while let Some(e) = window.next_event(ctx) { // 更新数据和处理事件 // ... // 添加 MyGraphElement 到窗口内容 if window.is_open() { let graph_element = MyGraphElement { data: graph_data }; window.render_content::<MyGraphElement>(&graph_element); } } // 清理资源 // ... } ```
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值