egui 尝试

2022/7/2

cpp 有非常棒的即时模式 GUI -- Dear imgui (opens new window),转入 Rust 以后,也看到了相近的项目 egui (opens new window),于是写个 hello world 体验一下。

# 添加基础依赖

与 Dear imgui 类似:需要选择使用的后端,我这里就用的 eframe

Cargo.toml 中添加 eframeegui 依赖

[dependencies]
egui = "0.18.1"
eframe = "0.18.0"
1
2
3

# 主程序

然后在 main.rs 中引入 egui eframe 即可

use eframe;
use egui;
1
2

创建主程序

fn main() {
  let options = eframe::NativeOptions::default();
  eframe::run_native(
    "Hello world",
    options,
    Box::new(|_cc| Box::new(MyApp::default())),
  );
}
1
2
3
4
5
6
7
8

添加我们的绘图代码

struct MyApp {
  name: String,
  age: u32,
}

impl Default for MyApp {
  fn default() -> Self {
    Self {
      name: "Arthur".to_owned(),
      age: 42,
    }
  }
}

impl eframe::App for MyApp {
  fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
    egui::CentralPanel::default().show(ctx, |ui| {
      ui.heading("Title - heading");
      ui.horizontal(|ui| {
        ui.label("Your name: ");
        ui.text_edit_singleline(&mut self.name);
      });
      ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
      if ui.button("Click each year").clicked() {
        self.age += 1;
      }
      ui.label(format!("Hello '{}', age {}", self.name, self.age));
    });
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

具体的控件和使用方法可以看在 这里 (opens new window) 查看

# 添加文件选择对话框

# 添加依赖

[dependencies]
rfd           = "0.9.1"
1
2

# 添加菜单栏

egui::menu::bar(ui, |ui| {
  ui.menu_button("File", |ui| {
    if ui.button("Open").clicked() {
      // ...
    }
  });
});
1
2
3
4
5
6
7

# 引入 open 方法

具体可参考 官方文档 (opens new window)

use rfd::FileDialog;

let _files = FileDialog::new()
  .add_filter("text", &["txt", "rs"])
  .add_filter("rust", &["rs", "toml"])
  .set_directory("/")
  .pick_file();
1
2
3
4
5
6
7

# demo

impl MyApp {
  fn show_menu(&mut self, ui: &mut egui::Ui) {
    egui::menu::bar(ui, |ui| {
      ui.menu_button("File", |ui| {
        if ui.button("Open").clicked() {
          let _files = FileDialog::new()
            .add_filter("text", &["txt", "rs"])
            .add_filter("rust", &["rs", "toml"])
            .set_directory("/")
            .pick_file();
        }
      });
    });
  }
}

impl Default for MyApp {
  fn default() -> Self {
    Self {
      name: "Arthur".to_owned(),
      age: 42,
    }
  }
}

impl eframe::App for MyApp {
  fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
    egui::CentralPanel::default().show(ctx, |ui| {
      self.show_menu(ui);
      ui.heading("Title - heading");
      ui.horizontal(|ui| {
        ui.label("Your name: ");
        ui.text_edit_singleline(&mut self.name);
      });
      ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
      if ui.button("Click each year").clicked() {
        self.age += 1;
      }
      ui.label(format!("Hello '{}', age {}", self.name, self.age));
    });
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42

!egui_tyr_demo

Last Updated: 2023-10-29T08:26:04.000Z