This commit is contained in:
2025-07-17 02:18:29 +01:00
parent fe8bbae68e
commit 224300f3ea
29 changed files with 1880 additions and 1439 deletions
Generated
+196 -412
View File
File diff suppressed because it is too large Load Diff
+5 -5
View File
@@ -4,22 +4,22 @@ version = "0.1.0"
edition = "2024"
[dependencies]
eframe = "0.31.1"
egui = "0.31.1"
eframe = "0.32.0"
egui = { version = "0.32.0", features = ["serde"] }
editor = { path = "./editor" }
egui_extras = { version = "0.31.1", features = [
egui_extras = { version = "0.32.0", features = [
"chrono",
"datepicker",
"file",
"image",
] }
egui_file = "0.22.1"
egui_file = "0.23.0"
image = { version = "0.25.6", features = ["jpeg", "png"] }
serde = { version = "1.0.219", features = ["derive"] }
serde_json = "1.0.140"
chrono = { version = "0.4.41", features = ["serde"] }
thiserror = "2.0.12"
egui_commonmark = { version = "0.20.0", features = ["embedded_image"] }
egui_commonmark = { version = "0.21.1", features = ["embedded_image"] }
walkdir = "2.5.0"
uuid = { version = "1.17.0", features = ["v4"] }
+1 -1
View File
@@ -5,7 +5,7 @@ edition = "2024"
description = "a basic text editor widget with line numbers"
[dependencies]
egui = "0.31.1"
egui = "0.32.0"
serde = "1"
[lib]
+11 -19
View File
@@ -1,7 +1,6 @@
use egui::{text::LayoutJob, Color32};
use egui::TextBuffer;
use egui::widgets::text_edit::TextEditOutput;
use egui::{Color32, text::LayoutJob};
use std::hash::{Hash, Hasher};
@@ -137,9 +136,10 @@ impl CodeEditor {
text.lines().count()
} as isize;
let max_indent = total.to_string().len().max(
!self.numlines_only_natural as usize * self.numlines_shift.to_string().len(),
);
let max_indent = total
.to_string()
.len()
.max(!self.numlines_only_natural as usize * self.numlines_shift.to_string().len());
let mut counter = (1..=total)
.map(|i| {
let num = i + self.numlines_shift;
@@ -160,16 +160,12 @@ impl CodeEditor {
let width = max_indent as f32
* self.fontsize
* 0.5
* !(total + self.numlines_shift <= 0 && self.numlines_only_natural) as u8
as f32;
* !(total + self.numlines_shift <= 0 && self.numlines_only_natural) as u8 as f32;
let mut layouter = |ui: &egui::Ui, string: &str, _wrap_width: f32| {
let mut layouter = |ui: &egui::Ui, string: &dyn TextBuffer, _wrap_width: f32| {
let layout_job = egui::text::LayoutJob::single_section(
string.to_string(),
egui::TextFormat::simple(
egui::FontId::monospace(self.fontsize),
Color32::WHITE,
),
string.as_str().to_string(), // Convert TextBuffer to String
egui::TextFormat::simple(egui::FontId::monospace(self.fontsize), Color32::WHITE),
);
ui.fonts(|f| f.layout_job(layout_job))
};
@@ -186,11 +182,7 @@ impl CodeEditor {
}
/// Show Code Editor
pub fn show(
&mut self,
ui: &mut egui::Ui,
text: &mut dyn egui::TextBuffer,
) -> TextEditOutput {
pub fn show(&mut self, ui: &mut egui::Ui, text: &mut dyn egui::TextBuffer) -> TextEditOutput {
let mut text_edit_output: Option<TextEditOutput> = None;
let code_editor = |ui: &mut egui::Ui| {
ui.horizontal_top(|h| {
Binary file not shown.

After

Width:  |  Height:  |  Size: 145 KiB

@@ -1,4 +0,0 @@
{
"name": "New Document",
"text": "# The effects of Whimsum dust:\nmore aggression, higher damage, higher rate of fire, lower accuracy"
}
@@ -1 +0,0 @@
{"name":"Note","content":"this is the note! gfjh gfdhgj fgfjhghfd iughuifghuifghuifghuifghuifdg"}
@@ -1,10 +0,0 @@
{
"id": "0078bb24-2fb0-4ecb-b5cb-20d29c5f2f77",
"template_id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"name": "The SPOONS!",
"fields": {
"description": {
"value": "full of smilers\n"
}
}
}
@@ -1,10 +0,0 @@
{
"id": "20beeb2f-363c-49bf-9621-f156d7c7cdd7",
"template_id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"name": "the brewdog.",
"fields": {
"description": {
"value": "full of smilers\n"
}
}
}
@@ -0,0 +1,23 @@
{
"id": "8d76fdcd-0c3e-41a9-abc4-66fe21c0cb73",
"template_id": "d1223e6b-ade0-405a-8c3b-657c743a21cc",
"name": "Prophet",
"fields": {
"Age": {
"value": ""
},
"Appearance": {
"value": ""
},
"Species": {
"value": ""
},
"DOB": {
"value": ""
},
"PFP": {
"value": "./project/assets/the prophet.png"
}
},
"tags": []
}
@@ -1,16 +0,0 @@
{
"id": "bd13d252-3f19-4618-bb10-cc45e9f7d301",
"template_id": "c96f5e87-7517-44cc-a5ab-42ffd537801d",
"name": "Cast Iron Pan",
"fields": {
"durability": {
"value": "9999999"
},
"Icon": {
"value": ""
},
"description": {
"value": "An unburnt pan for bitchslapping, comes with a free punchcard."
}
}
}
@@ -1,37 +0,0 @@
{
"name": "Character",
"id": "a24b3ab7-2572-4af4-8457-df26937fd773",
"description": "character",
"fields": [
{
"name": "Portrait / Image",
"field_type": "Image",
"required": true,
"description": "image of character"
},
{
"name": "Date of Birth",
"field_type": "Date",
"required": false,
"description": "date of birth"
},
{
"name": "Age",
"field_type": "Number",
"required": false,
"description": "age"
},
{
"name": "Appearance",
"field_type": "MultiLine",
"required": false,
"description": "character's appearance"
},
{
"name": "Personality",
"field_type": "MultiLine",
"required": false,
"description": "character's personality"
}
]
}
@@ -1,13 +0,0 @@
{
"name": "Location",
"id": "b5745688-3c1c-40de-bc3a-2a3e354dd19d",
"description": "a place",
"fields": [
{
"name": "description",
"field_type": "MultiLine",
"required": true,
"description": "what is it like?"
}
]
}
@@ -1,25 +0,0 @@
{
"name": "Item",
"id": "c96f5e87-7517-44cc-a5ab-42ffd537801d",
"description": "an in-game item",
"fields": [
{
"name": "durability",
"field_type": "Number",
"required": false,
"description": "the item's durability"
},
{
"name": "description",
"field_type": "MultiLine",
"required": true,
"description": "the item's description"
},
{
"name": "Icon",
"field_type": "Image",
"required": false,
"description": "yes"
}
]
}
@@ -0,0 +1,37 @@
{
"name": "Character",
"id": "d1223e6b-ade0-405a-8c3b-657c743a21cc",
"description": "desc",
"fields": [
{
"name": "Appearance",
"field_type": "MultiLine",
"required": true,
"description": "app"
},
{
"name": "Age",
"field_type": "Number",
"required": true,
"description": "age"
},
{
"name": "DOB",
"field_type": "Date",
"required": false,
"description": "dob"
},
{
"name": "PFP",
"field_type": "Image",
"required": true,
"description": ""
},
{
"name": "Species",
"field_type": "Link",
"required": false,
"description": ""
}
]
}
+74
View File
@@ -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)),
);
}
});
}
}
+306
View File
@@ -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;
}
});
});
});
}
}
+6
View File
@@ -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;
+152
View File
@@ -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();
});
}
}
+364
View File
@@ -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;
}
});
}
}
+244
View File
@@ -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
}
}
@@ -1,9 +1,13 @@
use core::fmt;
use egui::{RichText, ScrollArea};
use egui::ScrollArea;
use serde::{Deserialize, Serialize};
use crate::{PROJECT_FOLDER, RightPanelContent, error::Error, object::ObjectInstance};
use crate::{
PROJECT_FOLDER, RightPanelContent,
editors::object_editor::ObjectInstance,
util::{self, Error},
};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub enum FieldType {
@@ -12,6 +16,28 @@ pub enum FieldType {
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)]
@@ -35,6 +61,18 @@ pub struct Template {
#[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 {
@@ -45,6 +83,10 @@ impl fmt::Debug for Template {
.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()
}
}
@@ -58,6 +100,11 @@ impl Clone for Template {
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(),
}
}
}
@@ -71,6 +118,11 @@ impl Default for Template {
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(),
}
}
}
@@ -96,15 +148,7 @@ impl Template {
Ok(())
}
pub fn ui(
&mut self,
ui: &mut egui::Ui,
new_instance: &mut Option<RightPanelContent>,
new_field_name: &mut String,
new_field_type: &mut FieldType,
new_field_required: &mut bool,
new_field_description: &mut String,
) {
pub fn ui(&mut self, ui: &mut egui::Ui, new_instance: &mut Option<RightPanelContent>) {
if let Some(error) = &mut self.error {
error.show(ui);
}
@@ -117,16 +161,17 @@ impl Template {
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));
});
});
// 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| {
@@ -161,9 +206,9 @@ impl Template {
if ui.button("Use Template").clicked() {
if self.saved {
*new_instance = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(self)),
});
*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!"
@@ -173,25 +218,12 @@ impl Template {
}
});
self.editor_ui(
ui,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
);
self.editor_ui(ui);
});
});
}
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,
) {
pub fn editor_ui(&mut self, ui: &mut egui::Ui) {
egui::Grid::new("template_grid")
.num_columns(2)
.striped(true)
@@ -254,7 +286,7 @@ impl Template {
if ui.button("").clicked() {
to_remove = Some(i);
}
ui.label(field.name.clone());
ui.strong(field.name.clone());
})
.body(|ui| {
ui.separator();
@@ -272,13 +304,7 @@ impl Template {
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,
] {
for variant in FieldType::types() {
if ui
.selectable_value(
&mut field.field_type,
@@ -330,22 +356,16 @@ impl Template {
.striped(true)
.show(ui, |ui| {
ui.label("Name:");
ui.text_edit_singleline(new_field_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!("{new_field_type:?}"))
.selected_text(format!("{:?}", self.new_field_type))
.show_ui(ui, |ui| {
for variant in [
FieldType::SingleLine,
FieldType::MultiLine,
FieldType::Number,
FieldType::Date,
FieldType::Image,
] {
for variant in FieldType::types() {
ui.selectable_value(
new_field_type,
&mut self.new_field_type,
variant.clone(),
format!("{variant:?}"),
);
@@ -354,31 +374,31 @@ impl Template {
ui.end_row();
ui.label("Required:");
ui.checkbox(new_field_required, "");
ui.checkbox(&mut self.new_field_required, "");
ui.end_row();
ui.label("Description:");
ui.text_edit_singleline(new_field_description);
ui.text_edit_singleline(&mut self.new_field_description);
ui.end_row();
if ui.button("Add Field").clicked() && !new_field_name.is_empty() {
if ui.button("Add Field").clicked() && !self.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() {
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(new_field_description.clone())
Some(self.new_field_description.clone())
},
});
self.saved = false;
// Reset new field form
new_field_name.clear();
*new_field_required = false;
new_field_description.clear();
self.new_field_name.clear();
self.new_field_required = false;
self.new_field_description.clear();
}
});
});
-22
View File
@@ -1,22 +0,0 @@
pub struct Error {
message: String,
visible: bool,
}
impl Error {
pub fn new(message: String) -> Self {
Self {
message,
visible: true,
}
}
pub fn show(&mut self, ui: &mut egui::Ui) {
egui::Window::new("Error")
.open(&mut self.visible)
.fixed_size([200.0, 100.0])
.show(ui.ctx(), |ui| {
ui.label(self.message.clone());
});
}
}
+192 -56
View File
@@ -1,18 +1,36 @@
use egui::RichText;
use crate::{
PROJECT_FOLDER, RightPanelContent,
main_editor::MainEditor,
note::Note,
object::ObjectInstance,
template::{FieldType, Template},
content_editor::MainEditor,
editors::{
asset_editor::Asset, content_editor::ContentSection, object_editor::ObjectInstance,
tags::Tag, template_editor::Template,
},
note_editor::Note,
};
pub struct Explorer {}
pub struct Explorer {
templates: Vec<Template>,
objects: Vec<ObjectInstance>,
notes: Vec<Note>,
documents: Vec<MainEditor>,
tags: Vec<Tag>,
assets: Vec<Asset>,
}
impl Explorer {
pub fn new() -> Self {
Self {}
Self {
templates: Vec::new(),
objects: Vec::new(),
notes: Vec::new(),
documents: Vec::new(),
tags: Vec::new(),
assets: Vec::new(),
}
}
pub fn objects(&self) -> Vec<ObjectInstance> {
self.objects.clone()
}
pub fn ui(
@@ -21,14 +39,11 @@ impl Explorer {
load_doc: &mut Option<MainEditor>,
ui: &mut egui::Ui,
) {
let (templates, objects) = match Self::load_templates() {
Ok((templates, objects)) => (templates, objects),
Err(e) => {
eprintln!("Failed to load project: {e}");
ui.label(RichText::new("Failed to load project").color(egui::Color32::RED));
return;
}
};
self.load_templates().expect("Failed to load templates");
self.load_objects().expect("Failed to load objects");
self.load_notes().expect("Failed to load notes");
self.load_documents().expect("Failed to load documents");
self.load_assets().expect("Failed to load assets");
ui.vertical(|ui| {
egui::collapsing_header::CollapsingState::load_with_default_open(
@@ -40,18 +55,12 @@ impl Explorer {
ui.horizontal(|ui| {
ui.label("Templates");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Template {
template: Box::new(Template::default()),
new_field_name: Default::default(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: Default::default(),
});
*to_load = Some(RightPanelContent::template(Some(Template::default())));
}
});
})
.body(|ui| {
for template in &templates {
for template in &self.templates {
let id = ui.make_persistent_id(template.name.clone());
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
@@ -66,22 +75,21 @@ impl Explorer {
// create a new object based on this template
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Object {
object: Box::new(ObjectInstance::new(template)),
});
*to_load = Some(RightPanelContent::object(Some(ObjectInstance::new(
template,
))));
}
})
.body(|ui| {
for object in &objects {
for object in &self.objects {
if object.template_id == template.id {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the object
if ui.selectable_label(false, &object.name).clicked() {
*to_load = Some(RightPanelContent::Object {
object: Box::new(object.clone()),
});
*to_load =
Some(RightPanelContent::object(Some(object.clone())));
}
});
}
@@ -90,8 +98,6 @@ impl Explorer {
}
});
let notes = Self::load_notes().unwrap();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("notes"),
@@ -101,37 +107,93 @@ impl Explorer {
ui.horizontal(|ui| {
ui.label("Notes");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Note {
note: Box::new(Note::default()),
});
*to_load = Some(RightPanelContent::note(Some(Note::default())));
}
});
})
.body(|ui| {
for note in &notes {
for note in &self.notes {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the note
if ui.selectable_label(false, &note.name).clicked() {
*to_load = Some(RightPanelContent::Note {
note: Box::new(note.clone()),
});
*to_load = Some(RightPanelContent::note(Some(note.clone())));
}
});
}
});
let documents = Self::load_documents().unwrap();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("projects"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Projects");
if ui.button("+").clicked() {
*load_doc = Some(MainEditor::open(ContentSection::new()));
}
});
})
.body(|ui| {
// Convert MainEditor vec to ContentSection vec
let content_sections: Vec<ContentSection> = self
.documents
.iter()
.map(|doc| doc.content.clone())
.collect();
egui::CollapsingHeader::new("Projects").show(ui, |ui| {
for document in &documents {
Self::render_document_tree(ui, &content_sections, None, load_doc);
});
self.tags = Tag::load_all();
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("tags"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Tags");
if ui.button("+").clicked() {
*to_load = Some(RightPanelContent::Tag(Tag::default()));
}
});
})
.body(|ui| {
for tag in &mut self.tags {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the document
if ui.selectable_label(false, &document.name).clicked() {
*load_doc = Some(document.clone());
// load the tag
if tag.list_ui(ui).clicked() {
*to_load = Some(RightPanelContent::Tag(tag.clone()));
}
});
}
});
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id("assets"),
true,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
ui.label("Assets");
});
})
.body(|ui| {
for asset in &mut self.assets {
ui.horizontal(|ui| {
ui.add_space(10.0);
// load the asset
if ui.selectable_label(false, &asset.name).clicked() {
*to_load = Some(RightPanelContent::Asset(Box::new(asset.clone())));
}
});
}
@@ -139,10 +201,56 @@ impl Explorer {
});
}
fn load_templates() -> std::io::Result<(Vec<Template>, Vec<ObjectInstance>)> {
let mut templates = Vec::new();
let mut objects = Vec::new();
/// Recursively renders a tree of documents.
///
/// Each document is represented by a single element in the `documents` array.
/// The `parent_id` parameter is used to filter out documents that do not have the current
/// parent. If `parent_id` is `None`, all documents are rendered.
///
/// `load_doc` is a mutable reference to a `MainEditor`. When a document is clicked, it
/// is loaded into the `MainEditor` and returned as `Some`.
fn render_document_tree(
ui: &mut egui::Ui,
documents: &[ContentSection],
parent_id: Option<&str>,
load_doc: &mut Option<MainEditor>,
) {
// Filter documents that have the current parent (or no parent if this is the root)
let child_docs: Vec<&ContentSection> = documents
.iter()
.filter(|doc| doc.parent.as_deref() == parent_id)
.collect();
for doc in child_docs {
egui::collapsing_header::CollapsingState::load_with_default_open(
ui.ctx(),
ui.make_persistent_id(&doc.id),
false,
)
.show_header(ui, |ui| {
ui.horizontal(|ui| {
// Document title
if ui.selectable_label(false, &doc.title).clicked() {
*load_doc = Some(MainEditor::open(doc.clone()));
}
// Add child button
if ui.button("+").clicked() {
let child = doc.create_child();
*load_doc = Some(MainEditor::open(child));
}
});
})
.body(|ui| {
// recursive call to render the next level of documents
Self::render_document_tree(ui, documents, Some(&doc.id), load_doc);
});
}
}
// load templates from the templates folder
fn load_templates(&mut self) -> std::io::Result<()> {
let mut templates = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("templates")).unwrap() {
let path = entry.unwrap().path();
match Template::load(path.file_stem().unwrap().to_str().unwrap()) {
@@ -150,7 +258,14 @@ impl Explorer {
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.templates = templates;
Ok(())
}
// load objects from the objects folder
fn load_objects(&mut self) -> std::io::Result<()> {
let mut objects = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("objects")).unwrap() {
let path = entry.unwrap().path();
match ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap()) {
@@ -158,11 +273,13 @@ impl Explorer {
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
self.objects = objects;
Ok((templates, objects))
Ok(())
}
fn load_notes() -> std::io::Result<Vec<Note>> {
// load notes from the notes folder
fn load_notes(&mut self) -> std::io::Result<()> {
let mut notes = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("notes")).unwrap() {
@@ -173,20 +290,39 @@ impl Explorer {
}
}
Ok(notes)
self.notes = notes;
Ok(())
}
fn load_documents() -> std::io::Result<Vec<MainEditor>> {
// load documents from the documents folder
fn load_documents(&mut self) -> std::io::Result<()> {
let mut documents = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("documents")).unwrap() {
let path = entry.unwrap().path();
match MainEditor::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(document),
match ContentSection::load(path.file_stem().unwrap().to_str().unwrap()) {
Ok(document) => documents.push(MainEditor::open(document)),
Err(err) => eprintln!("Could not parse file {path:?}: {err}"),
}
}
Ok(documents)
self.documents = documents;
Ok(())
}
fn load_assets(&mut self) -> std::io::Result<()> {
let mut assets = Vec::new();
for entry in std::fs::read_dir(PROJECT_FOLDER.join("assets")).unwrap() {
let path = entry.unwrap().path();
assets.push(Asset::open(
path.file_stem().unwrap().to_str().unwrap().to_string(),
));
}
self.assets = assets;
Ok(())
}
}
+119 -144
View File
@@ -1,20 +1,22 @@
use std::{fs, path::PathBuf, sync::LazyLock};
use std::{path::PathBuf, sync::LazyLock};
use egui::{RichText, ScrollArea};
use egui::ScrollArea;
mod error;
mod editors;
mod explorer;
mod main_editor;
mod note;
mod object;
mod scene;
mod template;
use egui_file::DialogType;
use object::ObjectInstance;
use template::{FieldType, Template};
use crate::{explorer::Explorer, main_editor::MainEditor, note::Note};
mod util;
use crate::{
editors::{
asset_editor::Asset, content_editor, note_editor, object_editor::ObjectInstance, tags::Tag,
template_editor::Template,
},
explorer::Explorer,
};
static VERSION: &str = "0.1.0";
static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
let mut path = std::env::current_dir().unwrap();
path.push("project");
@@ -23,7 +25,6 @@ static PROJECT_FOLDER: LazyLock<PathBuf> = LazyLock::new(|| {
fn main() {
let app = Interface::new();
let options = eframe::NativeOptions {
viewport: egui::ViewportBuilder::default().with_inner_size([800.0, 600.0]),
..Default::default()
@@ -33,111 +34,74 @@ fn main() {
}
pub struct Interface {
dialog: Option<egui_file::FileDialog>,
// dialog: Option<egui_file::FileDialog>,
right_panel_content: RightPanelContent,
editor: main_editor::MainEditor,
editor: content_editor::MainEditor,
scene: scene::EditorScene,
explorer: Explorer,
}
impl eframe::App for Interface {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui_extras::install_image_loaders(ctx);
self.configure_appearance(ctx);
self.render_top_panel(ctx);
self.render_left_panel(ctx);
self.render_right_panel(ctx);
self.render_main_content(ctx);
}
}
pub enum RightPanelContent {
Template(Box<Template>),
Object(Box<ObjectInstance>),
Note(Box<note_editor::Note>),
Tag(Tag),
Asset(Box<Asset>),
None,
}
impl RightPanelContent {
fn template(template: Option<Template>) -> Self {
Self::Template(Box::new(template.unwrap_or_default()))
}
fn object(instance: Option<ObjectInstance>) -> Self {
Self::Object(Box::new(instance.unwrap_or_default()))
}
fn note(note: Option<note_editor::Note>) -> Self {
Self::Note(Box::new(note.unwrap_or_default()))
}
}
impl Interface {
#[must_use]
pub fn new() -> Self {
Self {
dialog: None,
right_panel_content: RightPanelContent::None,
editor: main_editor::MainEditor::new(),
editor: content_editor::MainEditor::new(),
scene: scene::EditorScene::new(),
explorer: Explorer::new(),
}
}
}
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}
// /home/zxq5/Pictures/logos and pfps/YT profile picture background.png
impl eframe::App for Interface {
fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) {
egui_extras::install_image_loaders(ctx);
{
let mut visuals = egui::Visuals::dark();
visuals.window_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.panel_fill = egui::Color32::from_rgb(20, 20, 20);
ctx.set_visuals(visuals);
}
if let Some(dialog) = &mut self.dialog {
if dialog.show(ctx).selected() {
if let Some(path) = dialog.path() {
if dialog.dialog_type() == DialogType::OpenFile {
// Handle file dialog for loading templates/instances
if let Ok(object) =
ObjectInstance::load(path.file_stem().unwrap().to_str().unwrap())
{
// Instance
self.right_panel_content = RightPanelContent::instance(Some(object));
self.dialog = None;
} else if let Ok(template) =
Template::load(path.file_stem().unwrap().to_str().unwrap())
{
// Template
self.right_panel_content = RightPanelContent::template(Some(template));
self.dialog = None;
}
} else if dialog.dialog_type() == DialogType::SaveFile {
// Handle file dialog for saving templates/instances
if let RightPanelContent::Template { template, .. } =
&mut self.right_panel_content
{
// set the save location and save
if template.save().is_err() {
eprintln!("Failed to save template");
} else {
self.dialog = None;
}
}
}
}
}
}
fn render_top_panel(&self, ctx: &egui::Context) {
// Top bar with actions
egui::TopBottomPanel::top("top").show(ctx, |ui| {
ui.horizontal(|ui| {
// Open Markdown Editor button
if ui.button("📝 Open Editor").clicked() {
self.editor.show_editor = true;
}
// title widget
ui.heading("WorldCoder");
ui.separator();
// create new template
if ui.button("New Template").clicked() {
self.right_panel_content = RightPanelContent::Template {
template: Box::new(Template::default()),
new_field_name: String::new(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: String::new(),
};
}
// load instance or template from file
if ui.button("Load Template/Instance").clicked() {
self.dialog = Some(egui_file::FileDialog::open_file(Some(
PROJECT_FOLDER.clone(),
)));
self.dialog.as_mut().unwrap().open();
}
// version
ui.label(VERSION)
});
});
}
fn render_left_panel(&mut self, ctx: &egui::Context) {
// Left panel - File browser
egui::SidePanel::left("file_browser")
.resizable(true)
@@ -147,7 +111,7 @@ impl eframe::App for Interface {
ui.separator();
let mut to_load: Option<RightPanelContent> = None;
let mut load_doc: Option<MainEditor> = None;
let mut load_doc: Option<content_editor::MainEditor> = None;
ScrollArea::vertical().show(ui, |ui| {
self.explorer.ui(&mut to_load, &mut load_doc, ui);
@@ -163,21 +127,28 @@ impl eframe::App for Interface {
self.editor.show_preview = true;
}
});
}
fn render_right_panel(&mut self, ctx: &egui::Context) {
// Main content area
egui::SidePanel::right("templates").show(ctx, |ui| {
let mut new_instance: Option<RightPanelContent> = None;
match &mut self.right_panel_content {
// an instance of a template
RightPanelContent::Object { object } => {
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, &mut right_panel);
object.ui(
ui,
&template,
&mut right_panel,
&mut self.explorer.objects(),
);
});
if let Some(right_panel) = right_panel {
@@ -186,22 +157,15 @@ impl eframe::App for Interface {
}
// an editable template
RightPanelContent::Template {
template,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
} => template.ui(
ui,
&mut new_instance,
new_field_name,
new_field_type,
new_field_required,
new_field_description,
),
RightPanelContent::Template(template) => {
template.ui(ui, &mut new_instance);
}
RightPanelContent::Note { note } => note.ui(ui),
RightPanelContent::Note(note) => note.ui(ui),
RightPanelContent::Tag(tag) => tag.ui(ui),
RightPanelContent::Asset(asset) => asset.ui(ui),
RightPanelContent::None => {
ui.centered_and_justified(|ui| {
@@ -217,44 +181,55 @@ impl eframe::App for Interface {
self.right_panel_content = new;
}
});
}
fn render_main_content(&mut self, ctx: &egui::Context) {
self.editor.ui(ctx);
self.scene.ui(ctx);
}
}
pub enum RightPanelContent {
Template {
template: Box<Template>,
// fields to edit
new_field_name: String,
new_field_type: FieldType,
new_field_required: bool,
new_field_description: String,
},
Object {
object: Box<ObjectInstance>,
},
Note {
note: Box<Note>,
},
None,
}
fn configure_appearance(&self, ctx: &egui::Context) {
let mut visuals = egui::Visuals::dark();
visuals.window_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.panel_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.fg_stroke =
egui::Stroke::from((2.0, egui::Color32::from_rgb(255, 255, 255)));
visuals.widgets.inactive.bg_stroke =
egui::Stroke::from((2.0, egui::Color32::from_rgb(60, 60, 60)));
visuals.widgets.inactive.corner_radius = egui::CornerRadius::from(4);
visuals.widgets.inactive.bg_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.weak_bg_fill = egui::Color32::from_rgb(20, 20, 20);
visuals.widgets.inactive.expansion = 2.0;
impl RightPanelContent {
fn template(template: Option<Template>) -> Self {
Self::Template {
template: Box::new(template.unwrap_or_default()),
new_field_name: String::new(),
new_field_type: FieldType::SingleLine,
new_field_required: false,
new_field_description: String::new(),
}
}
ctx.set_visuals(visuals);
fn instance(instance: Option<ObjectInstance>) -> Self {
Self::Object {
object: Box::new(instance.unwrap_or_default()),
}
let mut fonts = egui::FontDefinitions::default();
fonts.font_data.insert(
"JetBrains Mono Nerd Font".to_string(),
std::sync::Arc::new(egui::FontData::from_static(include_bytes!(
"/usr/local/share/fonts/j/JetBrainsMonoNerdFont_Regular.ttf",
))),
);
fonts
.families
.entry(egui::FontFamily::Proportional)
.or_default()
.insert(0, "JetBrains Mono Nerd Font".to_string());
fonts
.families
.entry(egui::FontFamily::Monospace)
.or_default()
.insert(0, "JetBrains Mono Nerd Font".to_string());
ctx.set_fonts(fonts);
}
}
impl Default for Interface {
fn default() -> Self {
Self::new()
}
}
-211
View File
@@ -1,211 +0,0 @@
use egui::{RichText, TextEdit, Ui};
use egui_commonmark::{CommonMarkCache, CommonMarkViewer};
use serde::{self, Deserialize, Serialize};
use crate::PROJECT_FOLDER;
#[derive(Serialize, Deserialize)]
pub struct MainEditor {
pub name: String,
pub text: String,
#[serde(skip)]
pub id: String,
#[serde(skip)]
saved: bool,
#[serde(skip)]
pub show_editor: bool,
#[serde(skip)]
pub show_preview: bool,
#[serde(skip)]
preview_cache: CommonMarkCache,
}
impl Clone for MainEditor {
fn clone(&self) -> Self {
Self {
name: self.name.clone(),
text: self.text.clone(),
id: self.id.clone(),
saved: self.saved,
show_editor: self.show_editor,
show_preview: self.show_preview,
preview_cache: CommonMarkCache::default(),
}
}
}
impl MainEditor {
pub fn new() -> Self {
Self {
text: String::new(),
id: uuid::Uuid::new_v4().to_string(),
name: "New Document".to_string(),
saved: false,
show_editor: false, // Start with editor hidden
show_preview: true,
preview_cache: CommonMarkCache::default(),
}
}
pub fn load(id: &str) -> Result<Self, Box<dyn std::error::Error>> {
let path = PROJECT_FOLDER
.join("documents")
.join(format!("{}.json", id));
let content = std::fs::read_to_string(&path)?;
let mut editor: Self = serde_json::from_str(&content)?;
editor.saved = true;
editor.id = id.to_string();
Ok(editor)
}
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 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| {
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));
});
});
// 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("documents")
.join(format!("{}.json", self.id)),
)
.unwrap();
*self = Self::new();
}
if ui.button("Revert changes").clicked() {
// load default state
*self = Self::load(&self.id).unwrap();
}
});
});
ui.horizontal(|ui| {
ui.label("Content Editor");
ui.checkbox(&mut self.show_preview, "Preview");
});
ui.separator();
if self.show_preview {
// 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.text);
});
});
});
});
}
// Editor area with centered content and max width
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) / 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.text)
.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);
ui.add_sized(
egui::vec2(max_width as f32, ui.available_height()),
text_edit,
);
});
});
});
});
}
self.show_editor = show;
}
}
-109
View File
@@ -1,109 +0,0 @@
use std::fs;
use egui::RichText;
use serde::{Deserialize, Serialize};
use crate::PROJECT_FOLDER;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Note {
pub name: String,
pub content: 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(),
content: "".to_string(),
saved: false,
}
}
}
impl Note {
pub fn new(name: String, content: String) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
name,
content,
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}");
}
}
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));
});
});
if ui.button("Save").clicked() {
if let Err(e) = self.save() {
eprintln!("Failed to save: {e}");
}
}
egui::Grid::new("note_grid")
.striped(true)
.num_columns(2)
.show(ui, |ui| {
ui.label("Name:");
if ui
.add(egui::TextEdit::singleline(&mut self.name).frame(false))
.changed()
{
self.saved = false;
}
ui.end_row();
ui.label("Content:");
if ui
.add(
egui::TextEdit::multiline(&mut self.content)
.desired_rows(1)
.frame(false),
)
.changed()
{
self.saved = false;
}
ui.end_row();
});
}
}
-276
View File
@@ -1,276 +0,0 @@
use core::f32;
use chrono::NaiveDate;
use egui::{CollapsingHeader, RichText, TextEdit, Ui, vec2};
use serde::{Deserialize, Serialize};
use crate::{
PROJECT_FOLDER, RightPanelContent,
template::{FieldType, FieldValue, Template},
};
#[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(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(),
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(),
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,
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>,
) {
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
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));
});
});
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();
egui::ScrollArea::vertical().show(ui, |ui| {
// Render each field
// allow name to be edited
CollapsingHeader::new("Name")
.default_open(true)
.show(ui, |ui| {
ui.heading(RichText::new("Name").size(14.0).strong());
ui.separator();
let _ = TextEdit::singleline(&mut self.name)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
ui.separator();
});
for field_def in &template.fields {
if let Some(field_value) = self.fields.get_mut(&field_def.name) {
CollapsingHeader::new(&field_def.name)
.default_open(true)
.show(ui, |ui| {
ui.heading(RichText::new(&field_def.name).size(14.0).strong());
if let Some(desc) = &field_def.description {
ui.label(RichText::new(desc).italics().weak());
}
ui.separator();
let response = match field_def.field_type {
FieldType::SingleLine => {
TextEdit::singleline(&mut field_value.value)
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response
}
FieldType::MultiLine => {
TextEdit::multiline(&mut field_value.value)
.desired_width(f32::INFINITY)
.desired_rows(5)
.frame(false)
.show(ui)
.response
}
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;
self.saved = false;
}
response
}
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;
self.saved = false;
}
response
}
FieldType::Image => {
// Simple path input for now
let response = TextEdit::singleline(&mut field_value.value)
.hint_text("Path to image")
.desired_width(f32::INFINITY)
.frame(false)
.show(ui)
.response;
// 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 image_source = egui::ImageSource::Bytes {
uri: std::borrow::Cow::Owned(
field_value.value.clone(),
),
bytes: bytes.into(),
};
ui.add(
egui::Image::new(image_source)
.max_size(vec2(256.0, f32::INFINITY)),
);
}
}
response
}
};
if response.changed() {
field_value.modified = true;
self.saved = false;
}
ui.separator();
});
}
}
});
});
}
}
+62
View File
@@ -0,0 +1,62 @@
use egui::{
RichText,
scroll_area::{ScrollBarVisibility, ScrollSource},
};
pub struct Error {
message: String,
visible: bool,
}
impl Error {
pub fn new(message: String) -> Self {
Self {
message,
visible: true,
}
}
pub fn show(&mut self, ui: &mut egui::Ui) {
egui::Window::new("Error")
.open(&mut self.visible)
.fixed_size([200.0, 100.0])
.show(ui.ctx(), |ui| {
ui.label(self.message.clone());
});
}
}
pub fn saved_status(ui: &mut egui::Ui, saved: bool, id: &str, name: &str) {
ui.group(|ui| {
ui.set_max_width(ui.available_width());
// Create a container that will take up the full width
ui.horizontal(|ui| {
// Add a ScrollArea that will contain our content
egui::ScrollArea::horizontal()
.scroll_source(ScrollSource::ALL)
.scroll_bar_visibility(ScrollBarVisibility::AlwaysHidden)
.auto_shrink([false, false]) // Don't shrink in either direction
.show(ui, |ui| {
// Create a horizontal layout that will contain our content
ui.horizontal(|ui| {
// Now add your content
ui.strong(name);
if saved {
ui.label(RichText::new("✓ Saved").color(egui::Color32::GREEN));
} else {
ui.label(RichText::new("* Unsaved").color(egui::Color32::YELLOW));
}
if ui
.add(egui::Button::new(format!("[id: {id}]")).frame(false))
.on_hover_text(" Click id to copy!")
.clicked()
{
ui.ctx().copy_text(id.to_string());
}
});
});
});
});
}