Created a more scalable file structure with modular elements

This commit is contained in:
Luke Else 2023-06-03 16:43:19 +01:00
parent 07c1cb9ad5
commit 37360a24d6
3 changed files with 31 additions and 21 deletions

15
src/app/mod.rs Normal file
View File

@ -0,0 +1,15 @@
mod ui;
/// Stuct to store the state of the running application
pub struct App {
name: String
}
impl Default for App {
/// Creates the default startup state for the app;
fn default() -> Self {
Self {
name: "Hello, World!".to_owned()
}
}
}

9
src/app/ui/mod.rs Normal file
View File

@ -0,0 +1,9 @@
use crate::app::App;
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading(&self.name);
});
}
}

View File

@ -2,38 +2,24 @@
use eframe::egui; use eframe::egui;
mod app;
use app::App;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {
// Log to stdout (if you run with `RUST_LOG=debug`). // Log to stdout (if you run with `RUST_LOG=debug`).
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
// Setup the options for the default window
let options = eframe::NativeOptions { let options = eframe::NativeOptions {
initial_window_size: Some(egui::vec2(150.0, 50.0)), initial_window_size: Some(egui::vec2(150.0, 50.0)),
resizable: false, resizable: false,
..Default::default() ..Default::default()
}; };
//Start rendering and run the application
eframe::run_native( eframe::run_native(
"egui-template", "egui-template",
options, options,
Box::new(|_cc| Box::new(MyApp::default())), Box::new(|_cc| Box::new(App::default())),
) )
}
struct MyApp {
name: String
}
impl Default for MyApp {
fn default() -> Self {
Self {
name: "Hello, World!".to_owned()
}
}
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.heading(&self.name);
});
}
} }