From e6d1e0b8658b041e667785a370bc52c2db47f4c4 Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Fri, 13 Sep 2024 00:40:34 -0700 Subject: [PATCH] Outline: encapsulate assembly data --- app-proto/sketch-outline/src/app.rs | 55 +++++++++++------------- app-proto/sketch-outline/src/assembly.rs | 16 +++++++ app-proto/sketch-outline/src/main.rs | 5 ++- 3 files changed, 44 insertions(+), 32 deletions(-) create mode 100644 app-proto/sketch-outline/src/assembly.rs diff --git a/app-proto/sketch-outline/src/app.rs b/app-proto/sketch-outline/src/app.rs index cc63ec0..1435fa9 100644 --- a/app-proto/sketch-outline/src/app.rs +++ b/app-proto/sketch-outline/src/app.rs @@ -2,47 +2,42 @@ use itertools::Itertools; use nalgebra::DVector; use sycamore::{prelude::*, web::tags::div}; -#[derive(Clone, PartialEq)] -struct Element { - id: String, - label: String, - color: [f32; 3], - rep: DVector, -} +use crate::assembly::{Assembly, Element}; struct AppState { - // the order of the elements is arbitrary, and it could change at any time - elements: Signal> + assembly: Assembly } #[component] pub fn App() -> View { let state = AppState { - elements: create_signal(vec![ - Element { - id: String::from("wing_a"), - label: String::from("Wing A"), - color: [1.00_f32, 0.25_f32, 0.00_f32], - rep: DVector::::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]) - }, - Element { - id: String::from("wing_b"), - label: String::from("Wing B"), - color: [1.00_f32, 0.25_f32, 0.00_f32], - rep: DVector::::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]) - }, - Element { - id: String::from("central"), - label: String::from("Central"), - color: [0.75_f32, 0.75_f32, 0.75_f32], - rep: DVector::::from_column_slice(&[0.0, 0.0, 0.0, 0.25, -1.0]) - } - ]) + assembly: Assembly { + elements: create_signal(vec![ + Element { + id: String::from("wing_a"), + label: String::from("Wing A"), + color: [1.00_f32, 0.25_f32, 0.00_f32], + rep: DVector::::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]) + }, + Element { + id: String::from("wing_b"), + label: String::from("Wing B"), + color: [1.00_f32, 0.25_f32, 0.00_f32], + rep: DVector::::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]) + }, + Element { + id: String::from("central"), + label: String::from("Central"), + color: [0.75_f32, 0.75_f32, 0.75_f32], + rep: DVector::::from_column_slice(&[0.0, 0.0, 0.0, 0.25, -1.0]) + } + ]) + } }; // sort the elements alphabetically by ID let elements_sorted = create_memo(move || - state.elements + state.assembly.elements .get_clone() .into_iter() .sorted_by_key(|elt| elt.id.clone()) diff --git a/app-proto/sketch-outline/src/assembly.rs b/app-proto/sketch-outline/src/assembly.rs new file mode 100644 index 0000000..e18242b --- /dev/null +++ b/app-proto/sketch-outline/src/assembly.rs @@ -0,0 +1,16 @@ +use nalgebra::DVector; +use sycamore::reactive::Signal; + +#[derive(Clone, PartialEq)] +pub struct Element { + pub id: String, + pub label: String, + pub color: [f32; 3], + pub rep: DVector, +} + +// a complete, view-independent description of an assembly +pub struct Assembly { + // the order of the elements is arbitrary, and it could change at any time + pub elements: Signal> +} \ No newline at end of file diff --git a/app-proto/sketch-outline/src/main.rs b/app-proto/sketch-outline/src/main.rs index e46fff3..06700e5 100644 --- a/app-proto/sketch-outline/src/main.rs +++ b/app-proto/sketch-outline/src/main.rs @@ -1,6 +1,7 @@ -use sycamore::prelude::*; - mod app; +mod assembly; + +use sycamore::prelude::*; use app::App;