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 label: String,
pub color: [f32; 3],
pub rep: DVector<f64>,
pub key: usize
pub rep: DVector<f64>
}
// a complete, view-independent description of an assembly

View File

@ -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

View File

@ -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::<f64>::from_column_slice(&[0.5, 0.5, 0.0, 0.5, -0.25]),
key: key
rep: DVector::<f64>::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::<f64>::from_column_slice(&[-0.5, -0.5, 0.0, 0.5, -0.25]),
key: key
rep: DVector::<f64>::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::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625]),
key: key
rep: DVector::<f64>::from_column_slice(&[0.0, 0.0, 0.0, 0.4, -0.625])
}
)
);
});
provide_context(state);
view! {

View File

@ -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::<AppState>();
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()
)
}
}