project to code

This commit is contained in:
Penwing 2024-02-02 20:10:41 +01:00
parent bb35ce3295
commit 69fd6fdda2
2 changed files with 53 additions and 16 deletions

View file

@ -81,7 +81,7 @@ impl Calcifer {
ui.separator(); 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) { 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) { 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) { pub fn draw_windows(&mut self, ctx: &egui::Context) {
if self.project_content.item_window.visible { if self.project_content.item_window.visible {
if self.project_content.categories.len() > 1 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 .content
.len() .is_empty()
> 0
{ {
self.project_content.item_window.show( self.project_content.item_window.show(
ctx, ctx,

View file

@ -1,4 +1,5 @@
use eframe::egui; use eframe::egui;
use serde::{Deserialize, Serialize};
use std::{ use std::{
cmp::min, cmp::min,
sync::atomic::{AtomicUsize, Ordering}, sync::atomic::{AtomicUsize, Ordering},
@ -9,6 +10,19 @@ use crate::editor::ColorTheme;
use crate::sub_windows; use crate::sub_windows;
use crate::MAX_PROJECT_COLUMNS; use crate::MAX_PROJECT_COLUMNS;
#[derive(Serialize, Deserialize)]
pub struct ProjectSave {
pub categories: Vec<Category>,
}
impl ProjectSave {
pub fn from_project(project: &Project) -> Self {
Self {
categories: project.categories.clone(),
}
}
}
pub struct Project { pub struct Project {
pub categories: Vec<Category>, pub categories: Vec<Category>,
pub selected_item: Location, pub selected_item: Location,
@ -26,6 +40,17 @@ impl Project {
} }
} }
pub fn update_from_code(&mut self, json: String) {
match serde_json::from_str::<ProjectSave>(&json) {
Ok(project_save) => self.categories = project_save.categories,
Err(_err) => self.categories = vec![Category::create()],
}
}
pub fn save_to_code(&self) -> Result<String, std::io::Error> {
Ok(serde_json::to_string(&ProjectSave::from_project(self))?)
}
fn add_category(&mut self) { fn add_category(&mut self) {
let last = self.categories.len() - 1; let last = self.categories.len() - 1;
self.categories[last].initialize(); self.categories[last].initialize();
@ -43,7 +68,7 @@ impl Project {
} }
} }
#[derive(Clone)] #[derive(Clone, Serialize, Deserialize)]
pub struct Category { pub struct Category {
name: String, name: String,
pub content: Vec<Item>, pub content: Vec<Item>,
@ -62,7 +87,7 @@ impl Category {
} }
} }
#[derive(Clone, Hash)] #[derive(Clone, Hash, Serialize, Deserialize)]
pub struct Item { pub struct Item {
pub name: String, pub name: String,
pub description: String, pub description: String,
@ -147,20 +172,24 @@ pub fn draw_project(ui: &mut egui::Ui, theme: ColorTheme, project: &mut Project)
} }
} }
} }
if category.name != "+" && ui.add(egui::Button::new("+")).clicked() {
if category.name != "+" { project.categories[category_index]
if ui.add(egui::Button::new("+")).clicked() { .content
project.categories[category_index] .push(Item::new("item"));
.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 mut moved = false;
let category = project.selected_item.category.clone(); let category = project.selected_item.category;
let row = project.selected_item.row.clone(); let row = project.selected_item.row;
if ui.input(|i| i.key_pressed(egui::Key::ArrowLeft) && i.modifiers.shift) if ui.input(|i| i.key_pressed(egui::Key::ArrowLeft) && i.modifiers.shift)
&& project.selected_item.category > 0 && project.selected_item.category > 0