Compare commits

..

2 Commits

Author SHA1 Message Date
a13a2e3105 Introduced saving template to file 2025-12-28 17:39:16 -06:00
f9cb5ded06 Improved error handling 2025-12-28 14:38:26 -06:00
3 changed files with 34 additions and 17 deletions

View File

@@ -1,25 +1,34 @@
use std::{fs::File, io::Write};
use eframe::egui::{self, Id}; use eframe::egui::{self, Id};
use rfd::FileDialog; use rfd::FileDialog;
use crate::template::{self, Template}; use crate::template::{self, Template};
pub struct MyEguiApp { pub struct App {
templates: Vec<Template>, templates: Vec<Template>,
selected_template: Option<usize>, selected_template: Option<usize>,
error_modal: bool,
error_value: Option<anyhow_serde::Error>, error_value: Option<anyhow_serde::Error>,
} }
impl MyEguiApp { impl App {
pub fn new(cc: &eframe::CreationContext<'_>) -> Self { pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
MyEguiApp { templates: Vec::new(), selected_template: None, error_modal: false, error_value: None} Self { templates: Vec::new(), selected_template: None, error_value: None}
} }
} }
impl eframe::App for MyEguiApp { impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| { egui::CentralPanel::default().show(ctx, |ui| {
ui.heading("Hello, World!"); if self.error_value.is_some() {
egui::Modal::new(Id::new("Error Box")).show(ui.ctx(), |ui| {
ui.heading("An Error Has Occurred");
ui.label(format!("{:?}", self.error_value));
if ui.button("Okay").clicked() {
self.error_value = None;
}
});
}
if ui.button("Add Template").clicked() { if ui.button("Add Template").clicked() {
let file = FileDialog::new().add_filter("text", &["txt"]).set_directory("/").pick_file(); let file = FileDialog::new().add_filter("text", &["txt"]).set_directory("/").pick_file();
@@ -29,22 +38,29 @@ impl eframe::App for MyEguiApp {
Ok(templ) => self.templates.push(templ), Ok(templ) => self.templates.push(templ),
Err(e) => { Err(e) => {
self.error_value = Some(e); self.error_value = Some(e);
self.error_modal = true;
} }
} }
}; };
}; };
if self.error_modal { if ui.button("New Template").clicked() {
egui::Modal::new(Id::new("Error Box")).show(ui.ctx(), |ui| { let file = FileDialog::new().add_filter("text", &["txt"]).set_directory("/").save_file();
ui.heading("An Error Has Occurred");
ui.label(format!("{:?}", self.error_value)); if let Some(path) = file {
if ui.button("Okay").clicked() { let handle = std::fs::OpenOptions::new().write(true).create(true).truncate(true).open(path);
self.error_modal = false;
self.error_value = None; match handle {
Ok(mut handle) => {
let templ = Template::new();
let var = ron::ser::to_string(&templ).unwrap();
handle.write(var.as_bytes()).unwrap();
handle.flush().unwrap();
}
Err(e) => {
self.error_value = Some(e.into())
}
} }
}); }
} }
let combo_text = match self.selected_template { let combo_text = match self.selected_template {

View File

@@ -3,7 +3,7 @@ mod template;
fn main() { fn main() {
let native_options = eframe::NativeOptions::default(); let native_options = eframe::NativeOptions::default();
eframe::run_native("test app", native_options, Box::new(|cc| Ok(Box::new(app::MyEguiApp::new(cc))))); eframe::run_native("Bretting Templates", native_options, Box::new(|cc| Ok(Box::new(app::App::new(cc)))));
} }

1
success.txt Normal file
View File

@@ -0,0 +1 @@
(label:"Template",content:[])