Use pointers, not keys, to refer to regulators

In the process, move the code that used to handle serial numbering for
elements into the `Serial` trait, where it can provide serial numbers
for regulators too.
This commit is contained in:
Aaron Fenyes 2025-05-04 10:59:28 -07:00
parent fbd6177a07
commit 8a86038de0
3 changed files with 106 additions and 80 deletions

View file

@ -13,8 +13,7 @@ use crate::{
Element,
HalfCurvatureRegulator,
InversiveDistanceRegulator,
Regulator,
RegulatorKey
Regulator
},
specified::SpecifiedValue
};
@ -90,12 +89,12 @@ fn RegulatorInput(regulator: Rc<dyn Regulator>) -> View {
}
pub trait OutlineItem {
fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View;
fn outline_item(self: Rc<Self>, element: &Rc<dyn Element>) -> View;
}
impl OutlineItem for InversiveDistanceRegulator {
fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View {
let other_subject_label = if self.subjects[0] == element {
fn outline_item(self: Rc<Self>, element: &Rc<dyn Element>) -> View {
let other_subject_label = if self.subjects[0] == element.clone() {
self.subjects[1].label()
} else {
self.subjects[0].label()
@ -112,7 +111,7 @@ impl OutlineItem for InversiveDistanceRegulator {
}
impl OutlineItem for HalfCurvatureRegulator {
fn outline_item(self: Rc<Self>, _element: Rc<dyn Element>) -> View {
fn outline_item(self: Rc<Self>, _element: &Rc<dyn Element>) -> View {
view! {
li(class="regulator") {
div(class="regulator-label") // for spacing
@ -124,16 +123,6 @@ impl OutlineItem for HalfCurvatureRegulator {
}
}
// a list item that shows a regulator in an outline view of an element
#[component(inline_props)]
fn RegulatorOutlineItem(regulator_key: RegulatorKey, element: Rc<dyn Element>) -> View {
let state = use_context::<AppState>();
let regulator = state.assembly.regulators.with(
|regs| regs[regulator_key].clone()
);
regulator.outline_item(element)
}
// a list item that shows an element in an outline view of an assembly
#[component(inline_props)]
fn ElementOutlineItem(element: Rc<dyn Element>) -> View {
@ -158,14 +147,10 @@ fn ElementOutlineItem(element: Rc<dyn Element>) -> View {
};
let regulated = element.regulators().map(|regs| regs.len() > 0);
let regulator_list = element.regulators().map(
move |elt_reg_keys| elt_reg_keys
|regs| regs
.clone()
.into_iter()
.sorted_by_key(
|&reg_key| state.assembly.regulators.with(
|regs| regs[reg_key].subjects().len()
)
)
.sorted_by_key(|reg| reg.subjects().len())
.collect()
);
let details_node = create_node_ref();
@ -223,16 +208,8 @@ fn ElementOutlineItem(element: Rc<dyn Element>) -> View {
ul(class="regulators") {
Keyed(
list=regulator_list,
view=move |reg_key| {
let element_for_view = element.clone();
view! {
RegulatorOutlineItem(
regulator_key=reg_key,
element=element_for_view
)
}
},
key=|reg_key| reg_key.clone()
view=move |reg| reg.outline_item(&element),
key=|reg| reg.serial()
)
}
}