From 69fd6fdda2e3eabcbfc89b7dbb42b46b8bbacb74 Mon Sep 17 00:00:00 2001 From: Penwing Date: Fri, 2 Feb 2024 20:10:41 +0100 Subject: [PATCH] project to code --- src/core/ui.rs | 18 ++++++++++---- src/panels/project_mode.rs | 51 ++++++++++++++++++++++++++++++-------- 2 files changed, 53 insertions(+), 16 deletions(-) diff --git a/src/core/ui.rs b/src/core/ui.rs index d1077d8..bb19278 100644 --- a/src/core/ui.rs +++ b/src/core/ui.rs @@ -81,7 +81,7 @@ impl Calcifer { ui.separator(); }); }); - self.n_file_displayed = n_files.clone(); + self.n_file_displayed = n_files; } pub fn draw_bottom_tray(&mut self, ctx: &egui::Context) { @@ -288,16 +288,24 @@ impl Calcifer { } fn draw_project_file(&mut self, ui: &mut egui::Ui) { - panels::draw_project(ui, self.theme.clone(), &mut self.project_content); + let current_tab = &mut self.tabs[self.selected_tab.to_index()]; + + self.project_content + .update_from_code(current_tab.code.clone()); + panels::draw_project(ui, self.theme, &mut self.project_content); + + match self.project_content.save_to_code() { + Ok(code) => current_tab.code = code, + Err(_err) => (), + } } pub fn draw_windows(&mut self, ctx: &egui::Context) { if self.project_content.item_window.visible { if self.project_content.categories.len() > 1 - && self.project_content.categories[self.project_content.selected_item.category] + && !self.project_content.categories[self.project_content.selected_item.category] .content - .len() - > 0 + .is_empty() { self.project_content.item_window.show( ctx, diff --git a/src/panels/project_mode.rs b/src/panels/project_mode.rs index 1ddcba4..fca4142 100644 --- a/src/panels/project_mode.rs +++ b/src/panels/project_mode.rs @@ -1,4 +1,5 @@ use eframe::egui; +use serde::{Deserialize, Serialize}; use std::{ cmp::min, sync::atomic::{AtomicUsize, Ordering}, @@ -9,6 +10,19 @@ use crate::editor::ColorTheme; use crate::sub_windows; use crate::MAX_PROJECT_COLUMNS; +#[derive(Serialize, Deserialize)] +pub struct ProjectSave { + pub categories: Vec, +} + +impl ProjectSave { + pub fn from_project(project: &Project) -> Self { + Self { + categories: project.categories.clone(), + } + } +} + pub struct Project { pub categories: Vec, pub selected_item: Location, @@ -26,6 +40,17 @@ impl Project { } } + pub fn update_from_code(&mut self, json: String) { + match serde_json::from_str::(&json) { + Ok(project_save) => self.categories = project_save.categories, + Err(_err) => self.categories = vec![Category::create()], + } + } + + pub fn save_to_code(&self) -> Result { + Ok(serde_json::to_string(&ProjectSave::from_project(self))?) + } + fn add_category(&mut self) { let last = self.categories.len() - 1; self.categories[last].initialize(); @@ -43,7 +68,7 @@ impl Project { } } -#[derive(Clone)] +#[derive(Clone, Serialize, Deserialize)] pub struct Category { name: String, pub content: Vec, @@ -62,7 +87,7 @@ impl Category { } } -#[derive(Clone, Hash)] +#[derive(Clone, Hash, Serialize, Deserialize)] pub struct Item { pub name: String, pub description: String, @@ -147,20 +172,24 @@ pub fn draw_project(ui: &mut egui::Ui, theme: ColorTheme, project: &mut Project) } } } - - if category.name != "+" { - if ui.add(egui::Button::new("+")).clicked() { - project.categories[category_index] - .content - .push(Item::new("item")); - } + if category.name != "+" && ui.add(egui::Button::new("+")).clicked() { + project.categories[category_index] + .content + .push(Item::new("item")); } + // if category.name != "+" { + // if ui.add(egui::Button::new("+")).clicked() { + // project.categories[category_index] + // .content + // .push(Item::new("item")); + // } + // } } }); let mut moved = false; - let category = project.selected_item.category.clone(); - let row = project.selected_item.row.clone(); + let category = project.selected_item.category; + let row = project.selected_item.row; if ui.input(|i| i.key_pressed(egui::Key::ArrowLeft) && i.modifiers.shift) && project.selected_item.category > 0