egui/eframe/examples/hello_world.rs

47 lines
1.2 KiB
Rust
Raw Normal View History

#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")] // hide console window on Windows in release
use eframe::egui;
fn main() {
let options = eframe::NativeOptions::default();
2022-03-18 13:23:07 +00:00
eframe::run_native(
"My egui App",
options,
Box::new(|_cc| Box::new(MyApp::default())),
);
}
2021-01-02 12:18:21 +00:00
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: &eframe::Frame) {
2021-01-02 12:18:21 +00:00
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
2021-01-02 12:18:21 +00:00
ui.horizontal(|ui| {
ui.label("Your name: ");
2022-02-19 19:51:51 +00:00
ui.text_edit_singleline(&mut self.name);
2021-01-02 12:18:21 +00:00
});
2022-02-19 19:51:51 +00:00
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Click each year").clicked() {
2022-02-19 19:51:51 +00:00
self.age += 1;
2021-01-02 12:18:21 +00:00
}
2022-02-19 19:51:51 +00:00
ui.label(format!("Hello '{}', age {}", self.name, self.age));
2021-01-02 12:18:21 +00:00
});
// Resize the native window to be just the size we need it to be:
frame.set_window_size(ctx.used_size());
}
}