#egui #table #widgets #row #undo-redo #ui #generic

egui-data-table

为 egui 实现的通用数据表小部件

7 个版本

0.3.0 2024 年 8 月 6 日
0.2.2 2024 年 5 月 11 日
0.2.0 2024 年 4 月 7 日
0.1.3 2024 年 3 月 25 日

#149 in GUI

Download history 144/week @ 2024-05-04 195/week @ 2024-05-11 15/week @ 2024-05-18 2/week @ 2024-05-25 4/week @ 2024-06-08 1/week @ 2024-06-15 2/week @ 2024-06-29 23/week @ 2024-07-06 40/week @ 2024-07-27 131/week @ 2024-08-03 10/week @ 2024-08-10

每月 181 次下载

自定义许可证

93KB
2K SLoC

Latest version Documentation

为 egui 实现的数据表 UI

MSRV 是 1.75,需要 RPITIT

演示网页

功能

  • 每个版本都具有撤销/重做功能
  • 显示/隐藏/重新排序列
  • 行复制/删除
  • 键盘导航
  • 内部剪贴板支持
  • 系统剪贴板支持
  • 教程文档
  • 整洁的浅色模式视觉效果

使用方法

Cargo.toml 中,将 egui-data-table 添加到依赖项部分

[dependencies]
egui-data-table = "0.1"

最小示例

// Use same version of `egui` with this crate!
use egui_data_table::egui;

// Don't need to implement any trait on row data itself.
struct MyRowData(i32, String, bool);

// Every logic is defined in `Viewer`
struct MyRowViewer;

// There are several methods that MUST be implemented to make the viewer work correctly.
impl egui_data_table::RowViewer<MyRowData> for MyRowViewer {
    fn num_columns(&mut self) -> usize {
        3
    }
    
    fn show_cell_view(&mut self, ui: &mut egui::Ui, row: &MyRowData, column: usize) {
        let _ = match column {
            0 => ui.label(format!("{}", row.0)),
            1 => ui.label(&row.1),
            2 => ui.checkbox(&mut { row.2 }, ""),
            _ => unreachable!()
        };
    }
    
    fn show_cell_editor(
        &mut self,
        ui: &mut egui::Ui,
        row: &mut MyRowData,
        column: usize,
    ) -> Option<egui::Response> {
        match column {
            0 => ui.add(egui::DragValue::new(&mut row.0).speed(1.0)),
            1 => {
                egui::TextEdit::multiline(&mut row.1)
                    .desired_rows(1)
                    .code_editor()
                    .show(ui)
                    .response
            }
            2 => ui.checkbox(&mut row.2, ""),
            _ => unreachable!()
        }
        .into() // To make focusing work correctly, valid response must be returned.
    }
    
    fn set_cell_value(&mut self, src: &MyRowData, dst: &mut MyRowData, column: usize) {
        match column {
            0 => dst.0 = src.0,
            1 => dst.1 = src.1.clone(),
            2 => dst.2 = src.2,
            _ => unreachable!()
        }
    }
    
    fn new_empty_row(&mut self) -> MyRowData {
        // Instead of requiring `Default` trait for row data types, the viewer is
        // responsible of providing default creation method.
        MyRowData(0, Default::default(), false)
    }
    
    // fn clone_row(&mut self, src: &MyRowData) -> MyRowData 
    // ^^ Overriding this method is optional. In default, it'll utilize `set_cell_value` which 
    //    would be less performant during huge duplication of lines.
}

fn show(ui: &mut egui::Ui, table: &mut egui_data_table::DataTable<MyRowData>) {
    ui.add(egui_data_table::Renderer::new(
        table,
        &mut { MyRowViewer },
    ));
}

有关更多详细信息/高级用法,请参阅 demo

依赖项

~6–11MB
~119K SLoC