77 lines
1.8 KiB
Rust
77 lines
1.8 KiB
Rust
use abacus_core::Output;
|
|
use druid::{
|
|
im::{vector, Vector},
|
|
Data, Lens,
|
|
};
|
|
|
|
use super::{EditorData, Modals};
|
|
|
|
#[derive(Clone, Data, Lens, Debug)]
|
|
pub struct AppData {
|
|
#[data(same_fn = "PartialEq::eq")]
|
|
pub filename: Option<String>,
|
|
pub blocks: Vector<Block>,
|
|
pub modals: Modals,
|
|
}
|
|
|
|
impl AppData {
|
|
pub fn save(&self) {
|
|
if let Some(ref filepath) = self.filename {
|
|
let _ = abacus_core::SaveFile::new(
|
|
filepath.clone(),
|
|
self.blocks
|
|
.iter()
|
|
.map(|d| abacus_core::SaveBlock::new(&d.name, &d.editor_data.content))
|
|
.collect(),
|
|
)
|
|
.save();
|
|
}
|
|
}
|
|
|
|
pub fn remove_block(&mut self, idx: usize) {
|
|
self.blocks.remove(idx);
|
|
for (idx, block) in self.blocks.iter_mut().enumerate() {
|
|
block.index = idx;
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Default for AppData {
|
|
fn default() -> Self {
|
|
Self {
|
|
filename: None,
|
|
blocks: vector![Block::new("Block #1", 0)],
|
|
modals: Modals::default(),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Data, Lens, Default, PartialEq, Debug)]
|
|
pub struct Block {
|
|
pub name: String,
|
|
pub editor_data: EditorData,
|
|
#[data(same_fn = "PartialEq::eq")]
|
|
pub output: Output,
|
|
pub index: usize,
|
|
}
|
|
|
|
impl Block {
|
|
pub fn new(name: &str, index: usize) -> Self {
|
|
Self {
|
|
name: name.to_string(),
|
|
editor_data: Default::default(),
|
|
output: Default::default(),
|
|
index,
|
|
}
|
|
}
|
|
|
|
pub fn new_with_content(name: &str, index: usize, content: &str) -> Self {
|
|
Self {
|
|
name: name.to_string(),
|
|
editor_data: super::EditorData::new(content),
|
|
output: Default::default(),
|
|
index,
|
|
}
|
|
}
|
|
}
|