progress
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
use egui::{TextEdit, vec2};
|
||||
|
||||
use crate::{PROJECT_FOLDER, util};
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Asset {
|
||||
pub name: String,
|
||||
pub old_name: String,
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
impl Asset {
|
||||
pub fn open(name: String) -> Self {
|
||||
Self {
|
||||
old_name: name.clone(),
|
||||
name,
|
||||
saved: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self) {
|
||||
let old_path = Self::path(&self.old_name);
|
||||
let new_path = Self::path(&self.name);
|
||||
|
||||
// move from src dir to name path
|
||||
std::fs::rename(&old_path, &new_path).unwrap();
|
||||
self.saved = true;
|
||||
self.old_name = self.name.clone();
|
||||
}
|
||||
|
||||
pub fn path(name: &str) -> std::path::PathBuf {
|
||||
PROJECT_FOLDER.join("assets").join(format!("{name}.png"))
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
ui.vertical(|ui| {
|
||||
util::saved_status(ui, self.saved, &self.name, &self.name);
|
||||
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl)
|
||||
|| ui.button("Save").clicked()
|
||||
{
|
||||
self.save();
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.strong("Filename:");
|
||||
if TextEdit::singleline(&mut self.name)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
if let Ok(bytes) = std::fs::read(Self::path(&self.name)) {
|
||||
let image_source = egui::ImageSource::Bytes {
|
||||
uri: std::borrow::Cow::Owned(self.name.clone()),
|
||||
bytes: bytes.into(),
|
||||
};
|
||||
ui.add(
|
||||
egui::Image::new(image_source)
|
||||
.max_size(vec2(ui.available_width(), f32::INFINITY)),
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,306 @@
|
||||
use egui::TextEdit;
|
||||
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
|
||||
use serde::{self, Deserialize, Serialize};
|
||||
|
||||
use crate::{PROJECT_FOLDER, editors::tags::Tag, util};
|
||||
|
||||
pub struct MainEditor {
|
||||
pub content: ContentSection,
|
||||
pub show_editor: bool,
|
||||
pub show_preview: bool,
|
||||
preview_cache: CommonMarkCache,
|
||||
}
|
||||
|
||||
impl Clone for MainEditor {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
content: self.content.clone(),
|
||||
|
||||
show_editor: self.show_editor,
|
||||
show_preview: self.show_preview,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
pub struct ContentSection {
|
||||
#[serde(default)]
|
||||
pub title: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub id: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub description: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(default)]
|
||||
pub content: String,
|
||||
|
||||
// parent id
|
||||
#[serde(default)]
|
||||
pub parent: Option<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
impl ContentSection {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
title: String::new(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
description: String::new(),
|
||||
tags: Vec::new(),
|
||||
content: String::new(),
|
||||
parent: None,
|
||||
saved: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER
|
||||
.join("documents")
|
||||
.join(format!("{}.json", &self.id));
|
||||
|
||||
let content = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(path, content)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER.join("documents").join(format!("{id}.json"));
|
||||
|
||||
let content = std::fs::read_to_string(&path)?;
|
||||
let mut section: Self = serde_json::from_str(&content)?;
|
||||
section.saved = true;
|
||||
section.id = id.to_string();
|
||||
Ok(section)
|
||||
}
|
||||
|
||||
pub fn create_child(&self) -> Self {
|
||||
let mut child = Self::new();
|
||||
child.title = format!("{} (Child)", self.title);
|
||||
child.parent = Some(self.id.clone());
|
||||
child
|
||||
}
|
||||
}
|
||||
|
||||
impl MainEditor {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
content: ContentSection::new(),
|
||||
show_editor: false, // Start with editor hidden
|
||||
show_preview: true,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn open(content: ContentSection) -> Self {
|
||||
Self {
|
||||
content,
|
||||
show_editor: true,
|
||||
show_preview: true,
|
||||
preview_cache: CommonMarkCache::default(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ctx: &egui::Context) {
|
||||
// Show the editor window if enabled
|
||||
let mut show = self.show_editor;
|
||||
if show {
|
||||
egui::Window::new("Markdown Editor")
|
||||
.resizable(true)
|
||||
.default_width(1000.0)
|
||||
.default_height(800.0)
|
||||
.open(&mut show)
|
||||
.show(ctx, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
// check for Ctrl+S to save
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// display save state
|
||||
util::saved_status(
|
||||
ui,
|
||||
self.content.saved,
|
||||
&self.content.id,
|
||||
&self.content.title,
|
||||
);
|
||||
|
||||
// Save/Cancel buttons
|
||||
ui.horizontal(|ui| {
|
||||
// save button
|
||||
if ui.button("Save").clicked() {
|
||||
if let Err(e) = self.content.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
// create copy button
|
||||
if ui.button("Create Copy").clicked() {
|
||||
let mut copy = self.clone();
|
||||
copy.content.id = uuid::Uuid::new_v4().to_string();
|
||||
copy.content.title = format!("{} (Copy)", self.content.title);
|
||||
copy.content.save().unwrap();
|
||||
}
|
||||
|
||||
// delete button
|
||||
if ui.button("Delete").clicked() {
|
||||
std::fs::remove_file(
|
||||
PROJECT_FOLDER
|
||||
.join("documents")
|
||||
.join(format!("{}.json", self.content.id)),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
*self = Self::new();
|
||||
}
|
||||
|
||||
// revert changes button
|
||||
if ui.button("Revert changes").clicked() {
|
||||
self.content = ContentSection::load(&self.content.id).unwrap();
|
||||
}
|
||||
|
||||
// preview toggle
|
||||
ui.checkbox(&mut self.show_preview, "Preview");
|
||||
});
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
// Name and description grid
|
||||
egui::Grid::new("top_grid")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.strong("Name");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.title)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Description");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.content.description)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Tags");
|
||||
Tag::selector_ui(
|
||||
&mut self.content.tags,
|
||||
ui,
|
||||
Some(&mut self.content.saved),
|
||||
);
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
if self.show_preview {
|
||||
self.preview_ui(ui);
|
||||
}
|
||||
|
||||
self.editor_ui(ui);
|
||||
});
|
||||
}
|
||||
|
||||
self.show_editor = show;
|
||||
}
|
||||
|
||||
fn preview_ui(&mut self, ui: &mut egui::Ui) {
|
||||
// Preview area
|
||||
egui::SidePanel::right("preview_panel")
|
||||
.resizable(true)
|
||||
.default_width(ui.available_width() / 2.0)
|
||||
.show_inside(ui, |ui| {
|
||||
// Preview area with centered content and max width
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("preview_scroll")
|
||||
.show(ui, |ui| {
|
||||
let max_width = 600;
|
||||
let available_width = ui.available_width();
|
||||
let content_width = (max_width as f32).min(available_width);
|
||||
let padding = (available_width - content_width) / 2.0;
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(padding);
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width(content_width);
|
||||
ui.add_space(15.0);
|
||||
|
||||
ui.set_min_width(max_width as f32);
|
||||
|
||||
CommonMarkViewer::new()
|
||||
.default_width(Some(max_width))
|
||||
.max_image_width(Some(512))
|
||||
.show(ui, &mut self.preview_cache, &self.content.content);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn editor_ui(&mut self, ui: &mut egui::Ui) {
|
||||
egui::ScrollArea::both()
|
||||
.auto_shrink([false, false])
|
||||
.id_salt("editor_scroll")
|
||||
.show(ui, |ui| {
|
||||
let max_width = 600;
|
||||
let available_width = ui.available_width();
|
||||
let content_width = (max_width as f32).min(available_width);
|
||||
let padding = (available_width - content_width).max(30.0) / 2.0;
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
ui.add_space(padding);
|
||||
ui.vertical(|ui| {
|
||||
ui.set_width(content_width);
|
||||
ui.add_space(15.0);
|
||||
|
||||
ui.set_min_width(max_width as f32);
|
||||
|
||||
let text_edit = TextEdit::multiline(&mut self.content.content)
|
||||
.id_source("MainEditor_editor")
|
||||
.font(egui::TextStyle::Monospace)
|
||||
.interactive(true)
|
||||
.frame(false)
|
||||
.lock_focus(true)
|
||||
.hint_text("Type here...")
|
||||
.desired_width(max_width as f32);
|
||||
|
||||
if ui
|
||||
.add_sized(
|
||||
egui::vec2(max_width as f32 - 30.0, ui.available_height()),
|
||||
text_edit,
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.content.saved = false;
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
pub mod asset_editor;
|
||||
pub mod content_editor;
|
||||
pub mod note_editor;
|
||||
pub mod object_editor;
|
||||
pub mod tags;
|
||||
pub mod template_editor;
|
||||
@@ -0,0 +1,152 @@
|
||||
use std::fs;
|
||||
|
||||
use egui::TextEdit;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{PROJECT_FOLDER, editors::tags::Tag, util};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize, Clone)]
|
||||
pub struct Note {
|
||||
pub name: String,
|
||||
#[serde(default)]
|
||||
pub content: String,
|
||||
#[serde(default)]
|
||||
pub subject: String,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub id: String,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
}
|
||||
|
||||
impl Default for Note {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: "New Note".to_string(),
|
||||
subject: "".to_string(),
|
||||
content: "".to_string(),
|
||||
tags: Vec::new(),
|
||||
saved: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Note {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: "New Note".to_string(),
|
||||
subject: "".to_string(),
|
||||
content: "".to_string(),
|
||||
tags: Vec::new(),
|
||||
saved: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> std::io::Result<()> {
|
||||
let id = &self.id;
|
||||
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
|
||||
fs::write(path, serde_json::to_string(&self)?)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(id: &str) -> std::io::Result<Self> {
|
||||
let path = PROJECT_FOLDER.join("notes").join(format!("{id}.json"));
|
||||
let content = fs::read_to_string(path)?;
|
||||
let mut note: Note = serde_json::from_str(&content)?;
|
||||
note.id = id.to_string();
|
||||
note.saved = true;
|
||||
Ok(note)
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||
|
||||
if ui.button("Save").clicked() {
|
||||
if let Err(e) = self.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
let id = ui.make_persistent_id("note_name");
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
.show_header(ui, |ui| {
|
||||
ui.strong("Name");
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.separator();
|
||||
if TextEdit::singleline(&mut self.name)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.separator();
|
||||
});
|
||||
|
||||
let id = ui.make_persistent_id("note_tags");
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
.show_header(ui, |ui| {
|
||||
ui.strong("Tags");
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.separator();
|
||||
Tag::selector_ui(&mut self.tags, ui, Some(&mut self.saved));
|
||||
ui.separator();
|
||||
});
|
||||
|
||||
let id = ui.make_persistent_id("note_subject");
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
.show_header(ui, |ui| {
|
||||
ui.strong("Subject");
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.separator();
|
||||
if TextEdit::singleline(&mut self.subject)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.separator();
|
||||
});
|
||||
|
||||
let id = ui.make_persistent_id("note_content");
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(ui.ctx(), id, true)
|
||||
.show_header(ui, |ui| {
|
||||
ui.strong("Content");
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.separator();
|
||||
if TextEdit::multiline(&mut self.content)
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(5)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.separator();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,364 @@
|
||||
use core::f32;
|
||||
use std::path::Path;
|
||||
|
||||
use chrono::NaiveDate;
|
||||
use egui::{CollapsingHeader, Response, RichText, Sense, TextEdit, Ui, UiBuilder, vec2};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER, RightPanelContent,
|
||||
editors::{
|
||||
tags::Tag,
|
||||
template_editor::{FieldDefinition, FieldType, FieldValue, Template},
|
||||
},
|
||||
util,
|
||||
};
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct ObjectInstance {
|
||||
// template info
|
||||
pub id: String,
|
||||
pub template_id: String,
|
||||
|
||||
// instance info
|
||||
pub name: String,
|
||||
pub fields: std::collections::HashMap<String, FieldValue>,
|
||||
|
||||
#[serde(default)]
|
||||
pub tags: Vec<String>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub dialog: Option<egui_file::FileDialog>,
|
||||
}
|
||||
|
||||
impl Clone for ObjectInstance {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
id: self.id.clone(),
|
||||
template_id: self.template_id.clone(),
|
||||
name: self.name.clone(),
|
||||
fields: self.fields.clone(),
|
||||
tags: self.tags.clone(),
|
||||
saved: self.saved,
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ObjectInstance {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
template_id: "new_template_instance".to_string(),
|
||||
name: "new_object".to_string(),
|
||||
fields: std::collections::HashMap::new(),
|
||||
tags: Vec::new(),
|
||||
saved: false,
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ObjectInstance {
|
||||
pub fn new(template: &Template) -> Self {
|
||||
let mut fields = std::collections::HashMap::new();
|
||||
|
||||
for field in &template.fields {
|
||||
fields.insert(field.name.clone(), FieldValue::default());
|
||||
}
|
||||
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
template_id: template.id.clone(),
|
||||
name: "new_object".to_string(),
|
||||
fields,
|
||||
tags: Vec::new(),
|
||||
saved: false,
|
||||
dialog: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER
|
||||
.join("objects")
|
||||
.join(format!("{}.json", &self.id));
|
||||
|
||||
let content = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(&path, content)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER.join("objects").join(format!("{id}.json"));
|
||||
|
||||
let content = std::fs::read_to_string(&path)?;
|
||||
let mut instance: ObjectInstance = serde_json::from_str(&content)?;
|
||||
instance.saved = true;
|
||||
Ok(instance)
|
||||
}
|
||||
|
||||
pub fn ui(
|
||||
&mut self,
|
||||
ui: &mut Ui,
|
||||
template: &Template,
|
||||
right_panel: &mut Option<RightPanelContent>,
|
||||
objects: &mut Vec<ObjectInstance>,
|
||||
) {
|
||||
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}");
|
||||
}
|
||||
}
|
||||
|
||||
ui.vertical(|ui| {
|
||||
// Show save status and button
|
||||
|
||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||
|
||||
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(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();
|
||||
|
||||
egui::ScrollArea::vertical().show(ui, |ui| {
|
||||
// Render each field
|
||||
|
||||
// allow name to be edited
|
||||
CollapsingHeader::new("Name")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
ui.separator();
|
||||
let _ = TextEdit::singleline(&mut self.name)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response;
|
||||
ui.separator();
|
||||
});
|
||||
|
||||
CollapsingHeader::new("Tags")
|
||||
.default_open(true)
|
||||
.show(ui, |ui| {
|
||||
ui.separator();
|
||||
Tag::selector_ui(&mut self.tags, ui, Some(&mut self.saved));
|
||||
ui.separator();
|
||||
});
|
||||
|
||||
for field_def in &template.fields {
|
||||
if let Some(field_value) = self.fields.get_mut(&field_def.name) {
|
||||
let id = ui.make_persistent_id(format!("field_{}", field_def.name));
|
||||
egui::collapsing_header::CollapsingState::load_with_default_open(
|
||||
ui.ctx(),
|
||||
id,
|
||||
true,
|
||||
)
|
||||
.show_header(ui, |ui| {
|
||||
ui.strong(&field_def.name);
|
||||
})
|
||||
.body(|ui| {
|
||||
if let Some(desc) = &field_def.description {
|
||||
ui.label(RichText::new(desc).italics().weak());
|
||||
}
|
||||
|
||||
ui.separator();
|
||||
|
||||
Self::render_field(
|
||||
field_def,
|
||||
field_value,
|
||||
ui,
|
||||
&mut self.saved,
|
||||
objects,
|
||||
);
|
||||
|
||||
ui.separator();
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
fn render_field(
|
||||
field_def: &FieldDefinition,
|
||||
field_value: &mut FieldValue,
|
||||
ui: &mut egui::Ui,
|
||||
saved: &mut bool,
|
||||
objects: &mut Vec<ObjectInstance>,
|
||||
) {
|
||||
match field_def.field_type {
|
||||
FieldType::SingleLine => {
|
||||
if TextEdit::singleline(&mut field_value.value)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
FieldType::MultiLine => {
|
||||
if TextEdit::multiline(&mut field_value.value)
|
||||
.desired_width(f32::INFINITY)
|
||||
.desired_rows(5)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response
|
||||
.changed()
|
||||
{
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
FieldType::Date => {
|
||||
let date_str = &field_value.value;
|
||||
let mut date = NaiveDate::parse_from_str(date_str, "%Y-%m-%d")
|
||||
.unwrap_or_else(|_| chrono::Local::now().date_naive());
|
||||
|
||||
let response = ui.add(egui_extras::DatePickerButton::new(&mut date));
|
||||
|
||||
if response.changed() {
|
||||
field_value.value = date.format("%Y-%m-%d").to_string();
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
FieldType::Number => {
|
||||
let mut num = field_value.value.parse::<f64>().unwrap_or(0.0);
|
||||
let response = ui.add(egui::DragValue::new(&mut num).speed(0.1));
|
||||
|
||||
if response.changed() {
|
||||
field_value.value = num.to_string();
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
FieldType::Image => {
|
||||
ui.scope_builder(UiBuilder::new().sense(Sense::HOVER), |ui| {
|
||||
let id = ui.make_persistent_id("is_hovered");
|
||||
let should_show = field_value.value.is_empty()
|
||||
|| ui.response().hovered()
|
||||
|| ui.memory(|mem| mem.data.get_temp(id).unwrap_or(false));
|
||||
|
||||
// Simple path input for now
|
||||
if should_show {
|
||||
let response = TextEdit::singleline(&mut field_value.value)
|
||||
.hint_text("Path to image")
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false)
|
||||
.show(ui)
|
||||
.response;
|
||||
|
||||
if response.changed() {
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
|
||||
ui.memory_mut(|mem| {
|
||||
*mem.data.get_temp_mut_or_insert_with(id, || true) = response.hovered();
|
||||
});
|
||||
}
|
||||
|
||||
// If we have a valid path, try to display a preview
|
||||
if !field_value.value.is_empty() {
|
||||
if let Ok(bytes) = std::fs::read(&field_value.value) {
|
||||
let path = PROJECT_FOLDER.join(&field_value.value);
|
||||
|
||||
let image_source = egui::ImageSource::Bytes {
|
||||
uri: std::borrow::Cow::Owned(path.to_str().unwrap().to_string()),
|
||||
bytes: bytes.into(),
|
||||
};
|
||||
ui.add(
|
||||
egui::Image::new(image_source).max_size(vec2(256.0, f32::INFINITY)),
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
FieldType::Link => ObjectInstance::selector_ui(field_value, objects, ui, saved),
|
||||
FieldType::Links => {
|
||||
if ui.text_edit_singleline(&mut field_value.value).changed() {
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
fn selector_ui(
|
||||
field_value: &mut FieldValue,
|
||||
objects: &mut Vec<ObjectInstance>,
|
||||
ui: &mut egui::Ui,
|
||||
saved: &mut bool,
|
||||
) {
|
||||
if !field_value.value.is_empty() {
|
||||
if let Ok(object) = ObjectInstance::load(&field_value.value) {
|
||||
ui.strong(&object.name);
|
||||
}
|
||||
}
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let id = ui.make_persistent_id("new_object");
|
||||
|
||||
let ctx = ui.ctx();
|
||||
let mut object_selection: usize =
|
||||
ctx.memory_mut(|mem| *mem.data.get_temp_mut_or_default::<usize>(id));
|
||||
|
||||
if objects.is_empty() {
|
||||
ui.label("No objects available");
|
||||
} else {
|
||||
egui::ComboBox::from_id_salt(id)
|
||||
.selected_text(&objects[object_selection].name)
|
||||
.show_ui(ui, |ui| {
|
||||
for (i, obj) in objects.iter().enumerate() {
|
||||
ui.selectable_value(&mut object_selection, i, &obj.name);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ui.button("Set").clicked() && object_selection < objects.len() {
|
||||
field_value.value = objects[object_selection].id.clone();
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
|
||||
if ui.button("Remove").clicked() {
|
||||
field_value.value.clear();
|
||||
field_value.modified = true;
|
||||
*saved = false;
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
use egui::{Response, RichText, TextEdit, UiBuilder};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{PROJECT_FOLDER, util};
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Tag {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
pub description: String,
|
||||
pub color: egui::Color32,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub error: Option<util::Error>,
|
||||
}
|
||||
|
||||
impl Default for Tag {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
name: String::new(),
|
||||
description: String::new(),
|
||||
color: egui::Color32::from_rgb(20, 20, 20),
|
||||
saved: false,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Tag {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
id: self.id.clone(),
|
||||
name: self.name.clone(),
|
||||
description: self.description.clone(),
|
||||
color: self.color,
|
||||
saved: self.saved,
|
||||
error: None,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Tag {
|
||||
pub fn display_ui(&mut self, ui: &mut egui::Ui) -> bool {
|
||||
let mut remove = false;
|
||||
|
||||
egui::Frame::new()
|
||||
.shadow(egui::Shadow {
|
||||
offset: [2, 2],
|
||||
blur: 16,
|
||||
spread: 0,
|
||||
color: egui::Color32::from_black_alpha(180),
|
||||
})
|
||||
.stroke(egui::Stroke::new(2.0, self.color))
|
||||
.corner_radius(4.0)
|
||||
.show(ui, |ui| {
|
||||
ui.horizontal(|ui| {
|
||||
if ui.add(egui::Button::new("").frame(false)).clicked() {
|
||||
remove = true;
|
||||
}
|
||||
ui.strong(&self.name);
|
||||
});
|
||||
});
|
||||
|
||||
remove
|
||||
}
|
||||
|
||||
pub fn list_ui(&mut self, ui: &mut egui::Ui) -> Response {
|
||||
ui.add(
|
||||
egui::Button::new(RichText::new(self.name.clone()).strong())
|
||||
.frame(false)
|
||||
.stroke(egui::Stroke::new(2.0, self.color))
|
||||
.corner_radius(4.0),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui) {
|
||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||
|
||||
if let Some(error) = &mut self.error {
|
||||
error.show(ui);
|
||||
}
|
||||
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.save() {
|
||||
self.error = Some(util::Error::new(format!("Failed to save tag: {e}")));
|
||||
}
|
||||
}
|
||||
|
||||
egui::Grid::new("tag_grid")
|
||||
.striped(true)
|
||||
.num_columns(2)
|
||||
.show(ui, |ui| {
|
||||
ui.strong("Name");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.name)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Description");
|
||||
if ui
|
||||
.add(
|
||||
TextEdit::singleline(&mut self.description)
|
||||
.desired_width(f32::INFINITY)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.strong("Color");
|
||||
if ui.color_edit_button_srgba(&mut self.color).changed() {
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
});
|
||||
}
|
||||
|
||||
pub fn selector_ui(tag_ids: &mut Vec<String>, ui: &mut egui::Ui, saved: Option<&mut bool>) {
|
||||
// remove duplicate tag ids
|
||||
tag_ids.sort();
|
||||
tag_ids.dedup();
|
||||
|
||||
let mut remove: Vec<usize> = Vec::new();
|
||||
let mut modified = false;
|
||||
let id = ui.make_persistent_id("new_tag");
|
||||
let available_tags = Self::load_all();
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
let ctx = ui.ctx();
|
||||
let mut tag_selection: usize =
|
||||
ctx.memory_mut(|mem| *mem.data.get_temp_mut_or_default::<usize>(id));
|
||||
|
||||
if available_tags.is_empty() {
|
||||
ui.label("No tags available");
|
||||
} else {
|
||||
egui::ComboBox::from_id_salt(id)
|
||||
.selected_text(&available_tags[tag_selection].name)
|
||||
.show_ui(ui, |ui| {
|
||||
for (i, tag) in available_tags.iter().enumerate() {
|
||||
if ui
|
||||
.add(
|
||||
egui::Button::new(RichText::new(tag.name.clone()).strong())
|
||||
.frame(false)
|
||||
.stroke(egui::Stroke::new(2.0, tag.color))
|
||||
.corner_radius(4.0),
|
||||
)
|
||||
.clicked()
|
||||
{
|
||||
tag_selection = i;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
if ui.button("Add").clicked() && tag_selection < available_tags.len() {
|
||||
tag_ids.push(available_tags[tag_selection].id.clone());
|
||||
tag_selection = 0;
|
||||
modified = true;
|
||||
}
|
||||
|
||||
let ctx = ui.ctx();
|
||||
ctx.memory_mut(|mem| {
|
||||
*mem.data.get_temp_mut_or_default::<usize>(id) = tag_selection;
|
||||
});
|
||||
|
||||
for (i, tag_id) in tag_ids.iter().enumerate() {
|
||||
if let Ok(mut tag) = Self::load(tag_id) {
|
||||
if tag.display_ui(ui) {
|
||||
remove.push(i)
|
||||
}
|
||||
} else {
|
||||
// if the tag doesn't exist (AKA it's been deleted)
|
||||
remove.push(i)
|
||||
}
|
||||
}
|
||||
|
||||
if !remove.is_empty() {
|
||||
modified = true;
|
||||
for i in remove {
|
||||
tag_ids.remove(i);
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(saved) = saved {
|
||||
if modified {
|
||||
*saved = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER.join("tags").join(format!("{id}.json"));
|
||||
Ok(serde_json::from_str(&std::fs::read_to_string(path)?)?)
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
if self.name.is_empty() {
|
||||
self.error = Some(util::Error::new("Tag name cannot be empty".to_string()));
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
self.error = None;
|
||||
|
||||
let path = PROJECT_FOLDER
|
||||
.join("tags")
|
||||
.join(format!("{}.json", &self.id));
|
||||
let content = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(path, content)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_all() -> Vec<Self> {
|
||||
let mut tags = Vec::new();
|
||||
|
||||
// scan tags folder. load tag json files
|
||||
let tags_folder = PROJECT_FOLDER.join("tags");
|
||||
if tags_folder.exists() {
|
||||
for entry in std::fs::read_dir(tags_folder).unwrap() {
|
||||
let path = entry.unwrap().path();
|
||||
if path.is_file() && path.extension().unwrap() == "json" {
|
||||
let tag =
|
||||
serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap();
|
||||
tags.push(tag);
|
||||
}
|
||||
}
|
||||
}
|
||||
tags
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,413 @@
|
||||
use core::fmt;
|
||||
|
||||
use egui::ScrollArea;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
PROJECT_FOLDER, RightPanelContent,
|
||||
editors::object_editor::ObjectInstance,
|
||||
util::{self, Error},
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
pub enum FieldType {
|
||||
Image,
|
||||
SingleLine,
|
||||
MultiLine,
|
||||
Date,
|
||||
Number,
|
||||
Link,
|
||||
Links,
|
||||
}
|
||||
|
||||
impl Default for FieldType {
|
||||
fn default() -> Self {
|
||||
Self::SingleLine
|
||||
}
|
||||
}
|
||||
|
||||
impl FieldType {
|
||||
fn types() -> Vec<FieldType> {
|
||||
vec![
|
||||
FieldType::Image,
|
||||
FieldType::SingleLine,
|
||||
FieldType::MultiLine,
|
||||
FieldType::Date,
|
||||
FieldType::Number,
|
||||
FieldType::Link,
|
||||
FieldType::Links,
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
pub struct FieldDefinition {
|
||||
pub name: String,
|
||||
pub field_type: FieldType,
|
||||
pub required: bool,
|
||||
pub description: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Serialize, Deserialize)]
|
||||
pub struct Template {
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
|
||||
pub description: Option<String>,
|
||||
pub fields: Vec<FieldDefinition>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub saved: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub error: Option<Error>,
|
||||
|
||||
#[serde(skip)]
|
||||
pub new_field_name: String,
|
||||
|
||||
#[serde(skip)]
|
||||
pub new_field_type: FieldType,
|
||||
|
||||
#[serde(skip)]
|
||||
pub new_field_required: bool,
|
||||
|
||||
#[serde(skip)]
|
||||
pub new_field_description: String,
|
||||
}
|
||||
|
||||
impl fmt::Debug for Template {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
f.debug_struct("Template")
|
||||
.field("name", &self.name)
|
||||
.field("id", &self.id)
|
||||
.field("description", &self.description)
|
||||
.field("fields", &self.fields)
|
||||
.field("saved", &self.saved)
|
||||
.field("new_field_name", &self.new_field_name)
|
||||
.field("new_field_type", &self.new_field_type)
|
||||
.field("new_field_required", &self.new_field_required)
|
||||
.field("new_field_description", &self.new_field_description)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl Clone for Template {
|
||||
fn clone(&self) -> Self {
|
||||
Self {
|
||||
name: self.name.clone(),
|
||||
id: self.id.clone(),
|
||||
description: self.description.clone(),
|
||||
fields: self.fields.clone(),
|
||||
saved: self.saved,
|
||||
error: None,
|
||||
|
||||
new_field_name: "".to_string(),
|
||||
new_field_type: FieldType::default(),
|
||||
new_field_required: false,
|
||||
new_field_description: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Template {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
name: "New Template".to_string(),
|
||||
id: uuid::Uuid::new_v4().to_string(),
|
||||
description: Some(String::from("Placeholder description")),
|
||||
fields: Vec::new(),
|
||||
saved: false,
|
||||
error: None,
|
||||
|
||||
new_field_name: "".to_string(),
|
||||
new_field_type: FieldType::default(),
|
||||
new_field_required: false,
|
||||
new_field_description: "".to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Template {
|
||||
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER.join("templates").join(format!("{id}.json"));
|
||||
|
||||
let content = std::fs::read_to_string(&path)?;
|
||||
let mut template: Self = serde_json::from_str(&content)?;
|
||||
template.saved = true;
|
||||
Ok(template)
|
||||
}
|
||||
|
||||
pub fn save(&mut self) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path = PROJECT_FOLDER
|
||||
.join("templates")
|
||||
.join(format!("{}.json", &self.id));
|
||||
|
||||
let content = serde_json::to_string_pretty(self)?;
|
||||
std::fs::write(path, content)?;
|
||||
self.saved = true;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn ui(&mut self, ui: &mut egui::Ui, new_instance: &mut Option<RightPanelContent>) {
|
||||
if let Some(error) = &mut self.error {
|
||||
error.show(ui);
|
||||
}
|
||||
|
||||
if ui.input(|i| i.key_pressed(egui::Key::S) && i.modifiers.ctrl) {
|
||||
if let Err(e) = self.save() {
|
||||
eprintln!("Failed to save: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
ScrollArea::vertical().show(ui, |ui| {
|
||||
ui.vertical(|ui| {
|
||||
// ui.group(|ui| {
|
||||
// ui.horizontal(|ui| {
|
||||
// if self.saved {
|
||||
// ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
|
||||
// } else {
|
||||
// ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
|
||||
// }
|
||||
// ui.label(format!("id: {}", self.id));
|
||||
// });
|
||||
// });
|
||||
util::saved_status(ui, self.saved, &self.id, &self.name);
|
||||
|
||||
// Save/Cancel buttons
|
||||
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.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("Use Template").clicked() {
|
||||
if self.saved {
|
||||
*new_instance = Some(RightPanelContent::Object(Box::new(
|
||||
ObjectInstance::new(self),
|
||||
)));
|
||||
} else {
|
||||
self.error = Some(Error::new(
|
||||
"You must save the template before creating a new instance!"
|
||||
.to_string(),
|
||||
));
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
self.editor_ui(ui);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
pub fn editor_ui(&mut self, ui: &mut egui::Ui) {
|
||||
egui::Grid::new("template_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Template Name:");
|
||||
if ui
|
||||
.add(egui::TextEdit::singleline(&mut self.name).frame(false))
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Description:");
|
||||
if ui
|
||||
.add(
|
||||
egui::TextEdit::multiline(self.description.get_or_insert_with(String::new))
|
||||
.desired_rows(1)
|
||||
.frame(false),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
});
|
||||
|
||||
ui.separator();
|
||||
|
||||
egui::CollapsingHeader::new("Name").show(ui, |ui: &mut egui::Ui| {
|
||||
egui::Grid::new("field_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Name:");
|
||||
ui.label("Name");
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Type:");
|
||||
ui.label("SingleLine");
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Required");
|
||||
ui.label("✓");
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Description:");
|
||||
ui.label("Object name");
|
||||
ui.end_row();
|
||||
});
|
||||
});
|
||||
|
||||
// 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| {
|
||||
if ui.button("❌").clicked() {
|
||||
to_remove = Some(i);
|
||||
}
|
||||
ui.strong(field.name.clone());
|
||||
})
|
||||
.body(|ui| {
|
||||
ui.separator();
|
||||
egui::Grid::new("field_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Name:");
|
||||
if ui.text_edit_singleline(&mut field.name).changed() {
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
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::types() {
|
||||
if ui
|
||||
.selectable_value(
|
||||
&mut field.field_type,
|
||||
variant.clone(),
|
||||
format!("{variant:?}"),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
}
|
||||
});
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Required:");
|
||||
if ui.checkbox(&mut field.required, "").clicked() {
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Description:");
|
||||
if ui
|
||||
.text_edit_singleline(
|
||||
field.description.get_or_insert_with(String::new),
|
||||
)
|
||||
.changed()
|
||||
{
|
||||
self.saved = false;
|
||||
}
|
||||
ui.end_row();
|
||||
ui.separator();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// Remove field if needed
|
||||
if let Some(index) = to_remove {
|
||||
self.fields.remove(index);
|
||||
self.saved = false;
|
||||
}
|
||||
|
||||
// Add new field
|
||||
ui.separator();
|
||||
ui.heading("Add New Field");
|
||||
|
||||
ui.horizontal(|ui| {
|
||||
egui::Grid::new("field_grid")
|
||||
.num_columns(2)
|
||||
.striped(true)
|
||||
.show(ui, |ui| {
|
||||
ui.label("Name:");
|
||||
ui.text_edit_singleline(&mut self.new_field_name);
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Type:");
|
||||
egui::ComboBox::from_id_salt("new_field_type")
|
||||
.selected_text(format!("{:?}", self.new_field_type))
|
||||
.show_ui(ui, |ui| {
|
||||
for variant in FieldType::types() {
|
||||
ui.selectable_value(
|
||||
&mut self.new_field_type,
|
||||
variant.clone(),
|
||||
format!("{variant:?}"),
|
||||
);
|
||||
}
|
||||
});
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Required:");
|
||||
ui.checkbox(&mut self.new_field_required, "");
|
||||
ui.end_row();
|
||||
|
||||
ui.label("Description:");
|
||||
ui.text_edit_singleline(&mut self.new_field_description);
|
||||
ui.end_row();
|
||||
|
||||
if ui.button("Add Field").clicked() && !self.new_field_name.is_empty() {
|
||||
self.fields.push(FieldDefinition {
|
||||
name: self.new_field_name.clone(),
|
||||
field_type: self.new_field_type.clone(),
|
||||
required: self.new_field_required,
|
||||
description: if self.new_field_description.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(self.new_field_description.clone())
|
||||
},
|
||||
});
|
||||
|
||||
self.saved = false;
|
||||
|
||||
// Reset new field form
|
||||
self.new_field_name.clear();
|
||||
self.new_field_required = false;
|
||||
self.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