initial commit - v0.1.0
This commit is contained in:
+339
@@ -0,0 +1,339 @@
|
||||
use egui::{RichText, ScrollArea};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use crate::{PROJECT_FOLDER, RightPanelContent, object::ObjectInstance};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum FieldType {
|
||||
Image,
|
||||
SingleLine,
|
||||
MultiLine,
|
||||
Date,
|
||||
Number,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FieldDefinition {
|
||||
pub name: String,
|
||||
pub field_type: FieldType,
|
||||
pub required: bool,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default)]
|
||||
pub enum EditorMode {
|
||||
#[default]
|
||||
View,
|
||||
EditTemplate,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Template {
|
||||
pub name: String,
|
||||
pub description: Option<String>,
|
||||
pub fields: Vec<FieldDefinition>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub path: Option<PathBuf>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub editor_mode: EditorMode,
|
||||
|
||||
#[serde(skip)]
|
||||
pub dialog: Option<egui_file::FileDialog>,
|
||||
}
|
||||
|
||||
impl Default for Template {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "New Template".to_string(),
|
||||
description: Some(String::from("Placeholder description")),
|
||||
fields: Vec::new(),
|
||||
saved: false,
|
||||
path: None,
|
||||
editor_mode: EditorMode::default(),
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Template {
|
||||
pub fn load(path: PathBuf) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let content = std::fs::read_to_string(&path)?;
|
||||
let mut template: Self = serde_json::from_str(&content)?;
|
||||
template.path = Some(path);
|
||||
Ok(template)
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let content = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(self.path.as_ref().ok_or("no path")?, content)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ui(
|
||||
&mut self,
|
||||
ui: &mut egui::Ui,
|
||||
new_instance: &mut RightPanelContent,
|
||||
new_field_name: &mut String,
|
||||
new_field_type: &mut FieldType,
|
||||
new_field_required: &mut bool,
|
||||
new_field_description: &mut String,
|
||||
) {
|
||||
match self.editor_mode {
|
||||
EditorMode::View => {
|
||||
ScrollArea::vertical().show(ui, |ui| {
|
||||
if ui.button("Edit Template").clicked() {
|
||||
self.editor_mode = EditorMode::EditTemplate;
|
||||
}
|
||||
|
||||
if ui.button("New Instance").clicked() {
|
||||
*new_instance = RightPanelContent::Instance {
|
||||
instance: Box::new(ObjectInstance::new(self)),
|
||||
path: None,
|
||||
};
|
||||
}
|
||||
});
|
||||
|
||||
self.viewer_ui(ui, new_instance);
|
||||
|
||||
if self.saved {
|
||||
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
|
||||
} else {
|
||||
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
|
||||
}
|
||||
|
||||
// Show current save path or "Not saved yet"
|
||||
let path_display = self
|
||||
.path
|
||||
.as_ref()
|
||||
.and_then(|p| p.to_str())
|
||||
.unwrap_or("Not saved yet");
|
||||
ui.label(path_display);
|
||||
}
|
||||
EditorMode::EditTemplate => {
|
||||
ScrollArea::vertical().show(ui, |ui| {
|
||||
ui.vertical_centered(|ui| {
|
||||
self.editor_ui(
|
||||
ui,
|
||||
new_field_name,
|
||||
new_field_type,
|
||||
new_field_required,
|
||||
new_field_description,
|
||||
);
|
||||
|
||||
// Save/Cancel buttons
|
||||
ui.horizontal(|ui| {
|
||||
if ui.button("Save Template").clicked() {
|
||||
if let Some(_path) = &self.path {
|
||||
if let Err(e) = self.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
} else {
|
||||
// Open save dialog
|
||||
let mut dialog = egui_file::FileDialog::save_file(Some(
|
||||
PROJECT_FOLDER.clone(),
|
||||
));
|
||||
dialog.open();
|
||||
self.dialog = Some(dialog);
|
||||
}
|
||||
}
|
||||
|
||||
if ui.button("Cancel").clicked() {
|
||||
self.editor_mode = EditorMode::View;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn viewer_ui(&self, ui: &mut egui::Ui, new_instance: &mut RightPanelContent) {
|
||||
// Show template view
|
||||
ui.vertical_centered(|ui| {
|
||||
ui.heading(&self.name);
|
||||
|
||||
if let Some(description) = &self.description {
|
||||
ui.separator();
|
||||
ui.label(description);
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
ui.heading("Fields");
|
||||
|
||||
for field in &self.fields {
|
||||
ui.separator();
|
||||
ui.horizontal(|ui| {
|
||||
ui.strong(&field.name);
|
||||
ui.label(format!("({:?})", field.field_type));
|
||||
if field.required {
|
||||
ui.label("*");
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(desc) = &field.description {
|
||||
ui.label(desc);
|
||||
}
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
if ui.button("Create New Instance").clicked() {
|
||||
*new_instance = RightPanelContent::Instance {
|
||||
instance: Box::new(ObjectInstance::new(self)),
|
||||
path: None,
|
||||
};
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn editor_ui(
|
||||
&mut self,
|
||||
ui: &mut egui::Ui,
|
||||
new_field_name: &mut String,
|
||||
new_field_type: &mut FieldType,
|
||||
new_field_required: &mut bool,
|
||||
new_field_description: &mut String,
|
||||
) {
|
||||
// Template name and description
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Template Name:");
|
||||
ui.text_edit_singleline(&mut self.name);
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Description:");
|
||||
ui.text_edit_multiline(self.description.get_or_insert_with(String::new));
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
ui.heading("Fields");
|
||||
|
||||
// List of fields
|
||||
let mut to_remove = None;
|
||||
for (i, field) in self.fields.iter_mut().enumerate() {
|
||||
let id = ui.make_persistent_id(format!("field_{i}"));
|
||||
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
.show_header(ui, |ui| {
|
||||
ui.label(field.name.clone());
|
||||
if ui.button("❌").clicked() {
|
||||
to_remove = Some(i);
|
||||
}
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Name:");
|
||||
ui.text_edit_singleline(&mut field.name);
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Type:");
|
||||
egui::ComboBox::from_id_salt(format!("field_type_{i}"))
|
||||
.selected_text(format!("{:?}", field.field_type))
|
||||
.show_ui(ui, |ui| {
|
||||
for variant in [
|
||||
FieldType::SingleLine,
|
||||
FieldType::MultiLine,
|
||||
FieldType::Number,
|
||||
FieldType::Date,
|
||||
FieldType::Image,
|
||||
] {
|
||||
ui.selectable_value(
|
||||
&mut field.field_type,
|
||||
variant.clone(),
|
||||
format!("{variant:?}"),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.checkbox(&mut field.required, "Required");
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Description:");
|
||||
ui.text_edit_singleline(field.description.get_or_insert_with(String::new));
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Remove field if needed
|
||||
if let Some(index) = to_remove {
|
||||
self.fields.remove(index);
|
||||
}
|
||||
|
||||
// Add new field
|
||||
ui.separator();
|
||||
ui.heading("Add New Field");
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.vertical(|ui| {
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Name:");
|
||||
ui.text_edit_singleline(new_field_name);
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Type:");
|
||||
egui::ComboBox::from_id_salt("new_field_type")
|
||||
.selected_text(format!("{new_field_type:?}"))
|
||||
.show_ui(ui, |ui| {
|
||||
for variant in [
|
||||
FieldType::SingleLine,
|
||||
FieldType::MultiLine,
|
||||
FieldType::Number,
|
||||
FieldType::Date,
|
||||
FieldType::Image,
|
||||
] {
|
||||
ui.selectable_value(
|
||||
new_field_type,
|
||||
variant.clone(),
|
||||
format!("{variant:?}"),
|
||||
);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.checkbox(new_field_required, "Required");
|
||||
});
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.label("Description:");
|
||||
ui.text_edit_singleline(new_field_description);
|
||||
});
|
||||
|
||||
if ui.button("Add Field").clicked() && !new_field_name.is_empty() {
|
||||
self.fields.push(FieldDefinition {
|
||||
name: new_field_name.clone(),
|
||||
field_type: new_field_type.clone(),
|
||||
required: *new_field_required,
|
||||
description: if new_field_description.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(new_field_description.clone())
|
||||
},
|
||||
});
|
||||
|
||||
// Reset new field form
|
||||
new_field_name.clear();
|
||||
*new_field_required = false;
|
||||
new_field_description.clear();
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
|
||||
pub struct FieldValue {
|
||||
pub value: String,
|
||||
#[serde(skip)]
|
||||
pub modified: bool,
|
||||
}
|
||||
Reference in New Issue
Block a user