From 147e2758234b78ccfc2e24f6ca762b40e9f5d77b Mon Sep 17 00:00:00 2001 From: Aaron Fenyes Date: Sun, 22 Sep 2024 02:38:17 -0700 Subject: [PATCH] App: don't bother copying key into element When we access an element, we always have its key, either because the slab iterator yielded it along side the element or because we used it to get the element from the slab. --- app-proto/sketch-outline/src/assembly.rs | 3 +- app-proto/sketch-outline/src/display.rs | 12 ++++---- app-proto/sketch-outline/src/main.rs | 39 +++++++++--------------- app-proto/sketch-outline/src/outline.rs | 21 ++++++------- 4 files changed, 32 insertions(+), 43 deletions(-) diff --git a/app-proto/sketch-outline/src/assembly.rs b/app-proto/sketch-outline/src/assembly.rs index f34d373..79912b9 100644 --- a/app-proto/sketch-outline/src/assembly.rs +++ b/app-proto/sketch-outline/src/assembly.rs @@ -7,8 +7,7 @@ pub struct Element { pub id: String, pub label: String, pub color: [f32; 3], - pub rep: DVector, - pub key: usize + pub rep: DVector } // a complete, view-independent description of an assembly diff --git a/app-proto/sketch-outline/src/display.rs b/app-proto/sketch-outline/src/display.rs index b0b9877..2ac8ebf 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).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.key)) { + let element_iter = (&elements).into_iter(); + let reps_world: Vec<_> = element_iter.clone().map(|(_, elt)| &assembly_to_world * &elt.rep).collect(); + let colors: Vec<_> = element_iter.clone().map(|(key, elt)| + if state.selection.with(|sel| sel.contains(&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.key)) { + let highlights: Vec<_> = element_iter.map(|(key, _)| + if state.selection.with(|sel| sel.contains(&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 f86abaf..a43b020 100644 --- a/app-proto/sketch-outline/src/main.rs +++ b/app-proto/sketch-outline/src/main.rs @@ -25,45 +25,36 @@ fn main() { }, selection: create_signal(FxHashSet::default()) }; - state.assembly.elements.update(|elts| { - let entry = elts.vacant_entry(); - let key = entry.key(); - entry.insert( + state.assembly.elements.update( + |elts| elts.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]), - key: key + rep: DVector::::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]) } - ); - }); - state.assembly.elements.update(|elts| { - let entry = elts.vacant_entry(); - let key = entry.key(); - entry.insert( + ) + ); + state.assembly.elements.update( + |elts| elts.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]), - key: key + rep: DVector::::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]) }, - ); - }); - state.assembly.elements.update(|elts| { - let entry = elts.vacant_entry(); - let key = entry.key(); - entry.insert( + ) + ); + state.assembly.elements.update( + |elts| elts.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]), - key: key + rep: DVector::::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625]) } - ); - }); + ) + ); provide_context(state); view! { diff --git a/app-proto/sketch-outline/src/outline.rs b/app-proto/sketch-outline/src/outline.rs index 9913f32..031f9bc 100644 --- a/app-proto/sketch-outline/src/outline.rs +++ b/app-proto/sketch-outline/src/outline.rs @@ -12,8 +12,7 @@ pub fn Outline() -> View { state.assembly.elements .get_clone() .into_iter() - .map(|(_, elt)| elt) - .sorted_by_key(|elt| elt.id.clone()) + .sorted_by_key(|(_, elt)| elt.id.clone()) .collect() }); @@ -26,11 +25,11 @@ pub fn Outline() -> View { ) { Keyed( list=elements_sorted, - view=|elt| { + view=|(key, elt)| { let state = use_context::(); let class = create_memo({ move || { - if state.selection.with(|sel| sel.contains(&elt.key)) { + if state.selection.with(|sel| sel.contains(&key)) { "selected" } else { "" @@ -52,14 +51,14 @@ pub fn Outline() -> View { move |event: MouseEvent| { if event.shift_key() { state.selection.update(|sel| { - if !sel.remove(&elt.key) { - sel.insert(elt.key); + if !sel.remove(&key) { + sel.insert(key); } }); } else { state.selection.update(|sel| { sel.clear(); - sel.insert(elt.key); + sel.insert(key); }); } event.stop_propagation(); @@ -70,14 +69,14 @@ pub fn Outline() -> View { if event.key() == "Enter" { if event.shift_key() { state.selection.update(|sel| { - if !sel.remove(&elt.key) { - sel.insert(elt.key); + if !sel.remove(&key) { + sel.insert(key); } }); } else { state.selection.update(|sel| { sel.clear(); - sel.insert(elt.key); + sel.insert(key); }); } event.prevent_default(); @@ -90,7 +89,7 @@ pub fn Outline() -> View { } } }, - key=|elt| elt.key + key=|(key, _)| key.clone() ) } }