Add simple egui counter app

This commit is contained in:
Ethan 2024-12-24 12:29:47 -05:00
parent e3b859175c
commit fc895c5e44
2 changed files with 61 additions and 0 deletions

7
rust-learning/Cargo.toml Normal file
View File

@ -0,0 +1,7 @@
[package]
name = "rust-learning"
version = "0.1.0"
edition = "2021"
[dependencies]
eframe = "0.30.0"

54
rust-learning/src/main.rs Normal file
View File

@ -0,0 +1,54 @@
use eframe::egui;
fn main() -> eframe::Result {
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([320.0, 240.0]),
..Default::default()
};
eframe::run_native(
"rust-learning",
options,
Box::new(|cc| Ok(Box::new(MyApp::new(cc)))),
)
}
struct MyApp {
counter: i32,
}
impl MyApp {
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
Self { counter: 0 }
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Hello eframe!");
ui.label("This is a simple eframe app.");
ui.horizontal(|ui| {
let counter_color = match self.counter {
0 => egui::Color32::WHITE,
_ if self.counter > 0 => egui::Color32::GREEN,
_ => egui::Color32::RED,
};
ui.label("Counter:");
ui.label(egui::RichText::new(format!("{}", self.counter)).color(counter_color));
});
ui.horizontal(|ui| {
if ui.button("Increment").clicked() {
self.counter += 1;
}
if ui.button("Decrement").clicked() {
self.counter -= 1;
}
});
});
}
}