added copy and delete to object and templates

This commit is contained in:
2025-07-15 14:56:28 +01:00
parent 76ec44d4e6
commit 1117ce9f13
6 changed files with 92 additions and 37 deletions
+11 -5
View File
@@ -159,16 +159,23 @@ impl eframe::App for Interface {
// Main content area
egui::SidePanel::right("templates").show(ctx, |ui| {
let mut new_instance: RightPanelContent = RightPanelContent::None;
let mut new_instance: Option<RightPanelContent> = None;
match &mut self.right_panel_content {
// an instance of a template
RightPanelContent::Object { object } => {
// load template from path
let mut right_panel = None;
let template = Template::load(&object.template_id).unwrap();
ScrollArea::vertical().show(ui, |ui| {
object.ui(ui, &template);
object.ui(ui, &template, &mut right_panel);
});
if let Some(right_panel) = right_panel {
self.right_panel_content = right_panel;
}
}
// an editable template
@@ -199,9 +206,8 @@ impl eframe::App for Interface {
}
};
if let RightPanelContent::None = new_instance {
} else {
self.right_panel_content = new_instance;
if let Some(new) = new_instance {
self.right_panel_content = new;
}
});
+37 -6
View File
@@ -5,7 +5,7 @@ use egui::{CollapsingHeader, RichText, TextEdit, Ui, vec2};
use serde::{Deserialize, Serialize};
use crate::{
PROJECT_FOLDER,
PROJECT_FOLDER, RightPanelContent,
template::{FieldType, FieldValue, Template},
};
@@ -90,7 +90,13 @@ impl ObjectInstance {
Ok(instance)
}
pub fn ui(&mut self, ui: &mut Ui, template: &Template) {
pub fn ui(
&mut self,
ui: &mut Ui,
template: &Template,
right_panel: &mut Option<RightPanelContent>,
) {
let _ = right_panel;
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
@@ -111,11 +117,36 @@ impl ObjectInstance {
});
});
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
ui.horizontal(|ui| {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.dialog = None;
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
*right_panel = Some(RightPanelContent::Object {
object: Box::new(copy),
});
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("objects")
.join(format!("{}.json", self.id)),
)
.unwrap();
*right_panel = Some(RightPanelContent::None);
}
});
ui.separator();
+23 -5
View File
@@ -99,7 +99,7 @@ impl Template {
pub fn ui(
&mut self,
ui: &mut egui::Ui,
new_instance: &mut RightPanelContent,
new_instance: &mut Option<RightPanelContent>,
new_field_name: &mut String,
new_field_type: &mut FieldType,
new_field_required: &mut bool,
@@ -130,22 +130,40 @@ impl Template {
// Save/Cancel buttons
ui.horizontal(|ui| {
if ui.button("Save Template").clicked() {
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
if ui.button("Create Copy").clicked() {
let mut copy = self.clone();
copy.id = uuid::Uuid::new_v4().to_string();
copy.name = format!("{} (Copy)", self.name);
copy.save().unwrap();
}
if ui.button("Delete").clicked() {
std::fs::remove_file(
PROJECT_FOLDER
.join("templates")
.join(format!("{}.json", self.id)),
)
.unwrap();
*new_instance = Some(RightPanelContent::None);
}
if ui.button("Cancel").clicked() {
// load default state
*self = Self::load(&self.id).unwrap();
}
if ui.button("Create New Instance").clicked() {
if ui.button("Use Template").clicked() {
if self.saved {
*new_instance = RightPanelContent::Object {
*new_instance = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(self)),
};
});
} else {
self.error = Some(Error::new(
"You must save the template before creating a new instance!"