Initial commit
This commit is contained in:
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
|||||||
|
/target
|
||||||
4792
Cargo.lock
generated
Normal file
4792
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
[package]
|
||||||
|
name = "bretting-templates"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow_serde = "1.0.101"
|
||||||
|
eframe = "0.33.3"
|
||||||
|
evalexpr = "13.1.0"
|
||||||
|
image = { version = "0.25.9", default-features = false, features = ["bmp", "rayon"] }
|
||||||
|
minijinja = "2.14.0"
|
||||||
|
rfd = "0.16.0"
|
||||||
|
ron = "0.12.0"
|
||||||
|
serde = { version = "1.0.228", features = ["derive"] }
|
||||||
66
src/app.rs
Normal file
66
src/app.rs
Normal file
@@ -0,0 +1,66 @@
|
|||||||
|
use eframe::egui::{self, Id};
|
||||||
|
use rfd::FileDialog;
|
||||||
|
|
||||||
|
use crate::template::{self, Template};
|
||||||
|
|
||||||
|
pub struct MyEguiApp {
|
||||||
|
templates: Vec<Template>,
|
||||||
|
selected_template: Option<usize>,
|
||||||
|
error_modal: bool,
|
||||||
|
error_value: Option<anyhow_serde::Error>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MyEguiApp {
|
||||||
|
pub fn new(cc: &eframe::CreationContext<'_>) -> Self {
|
||||||
|
MyEguiApp { templates: Vec::new(), selected_template: None, error_modal: false, error_value: None}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl eframe::App for MyEguiApp {
|
||||||
|
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
|
||||||
|
egui::CentralPanel::default().show(ctx, |ui| {
|
||||||
|
ui.heading("Hello, World!");
|
||||||
|
|
||||||
|
if ui.button("Add Template").clicked() {
|
||||||
|
let file = FileDialog::new().add_filter("text", &["txt"]).set_directory("/").pick_file();
|
||||||
|
|
||||||
|
if let Some(path) = file {
|
||||||
|
match template::Template::from_file(path) {
|
||||||
|
Ok(templ) => self.templates.push(templ),
|
||||||
|
Err(e) => {
|
||||||
|
self.error_value = Some(e);
|
||||||
|
self.error_modal = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
if self.error_modal {
|
||||||
|
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_modal = false;
|
||||||
|
self.error_value = None;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let combo_text = match self.selected_template {
|
||||||
|
Some(num) => {&self.templates[num].get_label()}
|
||||||
|
None => "Select a template"
|
||||||
|
};
|
||||||
|
|
||||||
|
egui::ComboBox::from_label("Template").selected_text(combo_text).show_ui(ui, |ui| {
|
||||||
|
if self.templates.is_empty() {
|
||||||
|
ui.label("No templates loaded");
|
||||||
|
} else {
|
||||||
|
for i in self.templates.iter().enumerate() {
|
||||||
|
ui.selectable_value(&mut self.selected_template, Some(i.0), i.1.get_label());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/main.rs
Normal file
10
src/main.rs
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
mod app;
|
||||||
|
mod template;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let native_options = eframe::NativeOptions::default();
|
||||||
|
eframe::run_native("test app", native_options, Box::new(|cc| Ok(Box::new(app::MyEguiApp::new(cc)))));
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
24
src/template.rs
Normal file
24
src/template.rs
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use anyhow_serde::{Context, Result};
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize)]
|
||||||
|
pub struct Template {
|
||||||
|
label: String,
|
||||||
|
content: Vec<u8>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Template {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
Template { label: "Template".to_string(), content: Vec::new() }
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn from_file<P: AsRef<std::path::Path>>(path: P) -> Result<Self> {
|
||||||
|
let errfile = path.as_ref().to_string_lossy().to_string();
|
||||||
|
let file = std::fs::File::open(path).with_context(|| format!("Failed to open file at location: {:?}", errfile))?;
|
||||||
|
ron::de::from_reader(file).context("Template Contents Invalid")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_label(&self) -> &str {
|
||||||
|
self.label.as_str()
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user