From d121385c18026bb76176c33ff97669476e1016dc Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 22 Sep 2024 02:21:45 -0700 Subject: [PATCH] App: store assembly elements in slab --- app-proto/sketch-outline/Cargo.toml | 1 + app-proto/sketch-outline/src/assembly.rs | 8 ++-- app-proto/sketch-outline/src/display.rs | 6 +-- app-proto/sketch-outline/src/main.rs | 49 ++++++++++++++---------- app-proto/sketch-outline/src/outline.rs | 21 +++++----- 5 files changed, 45 insertions(+), 40 deletions(-) diff --git a/app-proto/sketch-outline/Cargo.toml b/app-proto/sketch-outline/Cargo.toml index 35d199b..920469a 100644 --- a/app-proto/sketch-outline/Cargo.toml +++ b/app-proto/sketch-outline/Cargo.toml @@ -12,6 +12,7 @@ itertools = "0.13.0" js-sys = "0.3.70" nalgebra = "0.33.0" rustc-hash = "2.0.0" +slab = "0.4.9" sycamore = "0.9.0-beta.3" # The `console_error_panic_hook` crate provides better debugging of panics by diff --git a/app-proto/sketch-outline/src/assembly.rs b/app-proto/sketch-outline/src/assembly.rs index fa7fc3e..f34d373 100644 --- a/app-proto/sketch-outline/src/assembly.rs +++ b/app-proto/sketch-outline/src/assembly.rs @@ -1,5 +1,5 @@ use nalgebra::DVector; -use rustc_hash::FxHashMap; +use slab::Slab; use sycamore::reactive::Signal; #[derive(Clone, PartialEq)] @@ -7,12 +7,12 @@ pub struct Element { pub id: String, pub label: String, pub color: [f32; 3], - pub rep: DVector + pub rep: DVector, + pub key: usize } // a complete, view-independent description of an assembly #[derive(Clone)] pub struct Assembly { - // the order of the elements is arbitrary, and it could change at any time - pub elements: Signal> + pub elements: Signal> } \ No newline at end of file diff --git a/app-proto/sketch-outline/src/display.rs b/app-proto/sketch-outline/src/display.rs index 2d0900d..b0b9877 100644 --- a/app-proto/sketch-outline/src/display.rs +++ b/app-proto/sketch-outline/src/display.rs @@ -288,17 +288,17 @@ pub fn Display() -> View { // get the assembly let elements = state.assembly.elements.get_clone(); - let element_iter = (&elements).values(); + let element_iter = (&elements).into_iter().map(|(_, elt)| elt); let reps_world: Vec<_> = element_iter.clone().map(|elt| &assembly_to_world * &elt.rep).collect(); let colors: Vec<_> = element_iter.clone().map(|elt| - if state.selection.with(|sel| sel.contains(&elt.id)) { + if state.selection.with(|sel| sel.contains(&elt.key)) { elt.color.map(|ch| 0.2 + 0.8*ch) } else { elt.color } ).collect(); let highlights: Vec<_> = element_iter.map(|elt| - if state.selection.with(|sel| sel.contains(&elt.id)) { + if state.selection.with(|sel| sel.contains(&elt.key)) { 1.0_f32 } else { HIGHLIGHT diff --git a/app-proto/sketch-outline/src/main.rs b/app-proto/sketch-outline/src/main.rs index 0d1b0c7..f86abaf 100644 --- a/app-proto/sketch-outline/src/main.rs +++ b/app-proto/sketch-outline/src/main.rs @@ -3,7 +3,8 @@ mod display; mod outline; use nalgebra::DVector; -use rustc_hash::{FxHashMap, FxHashSet}; +use rustc_hash::FxHashSet; +use slab::Slab; use sycamore::prelude::*; use assembly::{Assembly, Element}; @@ -13,50 +14,56 @@ use outline::Outline; #[derive(Clone)] struct AppState { assembly: Assembly, - selection: Signal> + selection: Signal> } fn main() { sycamore::render(|| { let state = AppState { assembly: Assembly { - elements: create_signal(FxHashMap::default()) + elements: create_signal(Slab::new()) }, selection: create_signal(FxHashSet::default()) }; - state.assembly.elements.update( - |elts| elts.insert( - "wing_a".to_string(), + state.assembly.elements.update(|elts| { + let entry = elts.vacant_entry(); + let key = entry.key(); + entry.insert( 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]) + rep: DVector::::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]), + key: key } - ) - ); - state.assembly.elements.update( - |elts| elts.insert( - "wing_b".to_string(), + ); + }); + state.assembly.elements.update(|elts| { + let entry = elts.vacant_entry(); + let key = entry.key(); + entry.insert( Element { id: String::from("wing_b"), label: String::from("Wing B"), color: [0.00_f32, 0.25_f32, 1.00_f32], - rep: DVector::::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]) + rep: DVector::::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]), + key: key }, - ) - ); - state.assembly.elements.update( - |elts| elts.insert( - "central".to_string(), + ); + }); + state.assembly.elements.update(|elts| { + let entry = elts.vacant_entry(); + let key = entry.key(); + entry.insert( 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.4, -0.625]) + rep: DVector::::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625]), + key: key } - ) - ); + ); + }); provide_context(state); view! { diff --git a/app-proto/sketch-outline/src/outline.rs b/app-proto/sketch-outline/src/outline.rs index 8d13df1..9913f32 100644 --- a/app-proto/sketch-outline/src/outline.rs +++ b/app-proto/sketch-outline/src/outline.rs @@ -12,8 +12,8 @@ pub fn Outline() -> View { state.assembly.elements .get_clone() .into_iter() - .sorted_by_key(|(id, _)| id.clone()) .map(|(_, elt)| elt) + .sorted_by_key(|elt| elt.id.clone()) .collect() }); @@ -29,9 +29,8 @@ pub fn Outline() -> View { view=|elt| { let state = use_context::(); let class = create_memo({ - let id = elt.id.clone(); move || { - if state.selection.with(|sel| sel.contains(&id)) { + if state.selection.with(|sel| sel.contains(&elt.key)) { "selected" } else { "" @@ -50,37 +49,35 @@ pub fn Outline() -> View { class=class.get(), tabindex="0", on:click={ - let id = elt.id.clone(); move |event: MouseEvent| { if event.shift_key() { state.selection.update(|sel| { - if !sel.remove(&id) { - sel.insert(id.clone()); + if !sel.remove(&elt.key) { + sel.insert(elt.key); } }); } else { state.selection.update(|sel| { sel.clear(); - sel.insert(id.clone()); + sel.insert(elt.key); }); } event.stop_propagation(); } }, on:keydown={ - let id = elt.id.clone(); move |event: KeyboardEvent| { if event.key() == "Enter" { if event.shift_key() { state.selection.update(|sel| { - if !sel.remove(&id) { - sel.insert(id.clone()); + if !sel.remove(&elt.key) { + sel.insert(elt.key); } }); } else { state.selection.update(|sel| { sel.clear(); - sel.insert(id.clone()); + sel.insert(elt.key); }); } event.prevent_default(); @@ -93,7 +90,7 @@ pub fn Outline() -> View { } } }, - key=|elt| elt.id.clone() + key=|elt| elt.key ) } }