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.
This commit is contained in:
Aaron Fenyes 2024-09-22 02:38:17 -07:00
parent d121385c18
commit 147e275823
4 changed files with 32 additions and 43 deletions

View File

@ -7,8 +7,7 @@ pub struct Element {
pub id: String, pub id: String,
pub label: String, pub label: String,
pub color: [f32; 3], pub color: [f32; 3],
pub rep: DVector<f64>, pub rep: DVector<f64>
pub key: usize
} }
// a complete, view-independent description of an assembly // a complete, view-independent description of an assembly

View File

@ -288,17 +288,17 @@ pub fn Display() -> View {
// get the assembly // get the assembly
let elements = state.assembly.elements.get_clone(); let elements = state.assembly.elements.get_clone();
let element_iter = (&elements).into_iter().map(|(_, elt)| elt); let element_iter = (&elements).into_iter();
let reps_world: Vec<_> = element_iter.clone().map(|elt| &assembly_to_world * &elt.rep).collect(); let reps_world: Vec<_> = element_iter.clone().map(|(_, elt)| &assembly_to_world * &elt.rep).collect();
let colors: Vec<_> = element_iter.clone().map(|elt| let colors: Vec<_> = element_iter.clone().map(|(key, elt)|
if state.selection.with(|sel| sel.contains(&elt.key)) { if state.selection.with(|sel| sel.contains(&key)) {
elt.color.map(|ch| 0.2 + 0.8*ch) elt.color.map(|ch| 0.2 + 0.8*ch)
} else { } else {
elt.color elt.color
} }
).collect(); ).collect();
let highlights: Vec<_> = element_iter.map(|elt| let highlights: Vec<_> = element_iter.map(|(key, _)|
if state.selection.with(|sel| sel.contains(&elt.key)) { if state.selection.with(|sel| sel.contains(&key)) {
1.0_f32 1.0_f32
} else { } else {
HIGHLIGHT HIGHLIGHT

View File

@ -25,45 +25,36 @@ fn main() {
}, },
selection: create_signal(FxHashSet::default()) selection: create_signal(FxHashSet::default())
}; };
state.assembly.elements.update(|elts| { state.assembly.elements.update(
let entry = elts.vacant_entry(); |elts| elts.insert(
let key = entry.key();
entry.insert(
Element { Element {
id: String::from("wing_a"), id: String::from("wing_a"),
label: String::from("Wing A"), label: String::from("Wing A"),
color: [1.00_f32, 0.25_f32, 0.00_f32], color: [1.00_f32, 0.25_f32, 0.00_f32],
rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]), rep: DVector::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25])
key: key
} }
); )
}); );
state.assembly.elements.update(|elts| { state.assembly.elements.update(
let entry = elts.vacant_entry(); |elts| elts.insert(
let key = entry.key();
entry.insert(
Element { Element {
id: String::from("wing_b"), id: String::from("wing_b"),
label: String::from("Wing B"), label: String::from("Wing B"),
color: [0.00_f32, 0.25_f32, 1.00_f32], color: [0.00_f32, 0.25_f32, 1.00_f32],
rep: DVector::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]), rep: DVector::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25])
key: key
}, },
); )
}); );
state.assembly.elements.update(|elts| { state.assembly.elements.update(
let entry = elts.vacant_entry(); |elts| elts.insert(
let key = entry.key();
entry.insert(
Element { Element {
id: String::from("central"), id: String::from("central"),
label: String::from("Central"), label: String::from("Central"),
color: [0.75_f32, 0.75_f32, 0.75_f32], color: [0.75_f32, 0.75_f32, 0.75_f32],
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625]), rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625])
key: key
} }
); )
}); );
provide_context(state); provide_context(state);
view! { view! {

View File

@ -12,8 +12,7 @@ pub fn Outline() -> View {
state.assembly.elements state.assembly.elements
.get_clone() .get_clone()
.into_iter() .into_iter()
.map(|(_, elt)| elt) .sorted_by_key(|(_, elt)| elt.id.clone())
.sorted_by_key(|elt| elt.id.clone())
.collect() .collect()
}); });
@ -26,11 +25,11 @@ pub fn Outline() -> View {
) { ) {
Keyed( Keyed(
list=elements_sorted, list=elements_sorted,
view=|elt| { view=|(key, elt)| {
let state = use_context::<AppState>(); let state = use_context::<AppState>();
let class = create_memo({ let class = create_memo({
move || { move || {
if state.selection.with(|sel| sel.contains(&elt.key)) { if state.selection.with(|sel| sel.contains(&key)) {
"selected" "selected"
} else { } else {
"" ""
@ -52,14 +51,14 @@ pub fn Outline() -> View {
move |event: MouseEvent| { move |event: MouseEvent| {
if event.shift_key() { if event.shift_key() {
state.selection.update(|sel| { state.selection.update(|sel| {
if !sel.remove(&elt.key) { if !sel.remove(&key) {
sel.insert(elt.key); sel.insert(key);
} }
}); });
} else { } else {
state.selection.update(|sel| { state.selection.update(|sel| {
sel.clear(); sel.clear();
sel.insert(elt.key); sel.insert(key);
}); });
} }
event.stop_propagation(); event.stop_propagation();
@ -70,14 +69,14 @@ pub fn Outline() -> View {
if event.key() == "Enter" { if event.key() == "Enter" {
if event.shift_key() { if event.shift_key() {
state.selection.update(|sel| { state.selection.update(|sel| {
if !sel.remove(&elt.key) { if !sel.remove(&key) {
sel.insert(elt.key); sel.insert(key);
} }
}); });
} else { } else {
state.selection.update(|sel| { state.selection.update(|sel| {
sel.clear(); sel.clear();
sel.insert(elt.key); sel.insert(key);
}); });
} }
event.prevent_default(); event.prevent_default();
@ -90,7 +89,7 @@ pub fn Outline() -> View {
} }
} }
}, },
key=|elt| elt.key key=|(key, _)| key.clone()
) )
} }
} }