Use pointers, not keys, to refer to elements

Use reference-counted pointers, rather than collection keys, to refer to
elements in tasks like specifying the subjects of regulators, handling
selection, and creating interface components.
This commit is contained in:
Aaron Fenyes 2025-05-04 00:09:30 -07:00
parent 17f30d1312
commit ab01c26415
4 changed files with 108 additions and 99 deletions

View file

@ -91,20 +91,17 @@ fn RegulatorInput(regulator: Rc<dyn Regulator>) -> View {
}
pub trait OutlineItem {
fn outline_item(self: Rc<Self>, element_key: ElementKey) -> View;
fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View;
}
impl OutlineItem for InversiveDistanceRegulator {
fn outline_item(self: Rc<Self>, element_key: ElementKey) -> View {
fn outline_item(self: Rc<Self>, element: Rc<dyn Element>) -> View {
let state = use_context::<AppState>();
let other_subject = if self.subjects[0] == element_key {
self.subjects[1]
let other_subject_label = if self.subjects[0] == element {
self.subjects[1].label()
} else {
self.subjects[0]
};
let other_subject_label = state.assembly.elements.with(
|elts| elts[other_subject].label().clone()
);
self.subjects[0].label()
}.clone();
view! {
li(class="regulator") {
div(class="regulator-label") { (other_subject_label) }
@ -117,7 +114,7 @@ impl OutlineItem for InversiveDistanceRegulator {
}
impl OutlineItem for HalfCurvatureRegulator {
fn outline_item(self: Rc<Self>, _element_key: ElementKey) -> View {
fn outline_item(self: Rc<Self>, _element: Rc<dyn Element>) -> View {
view! {
li(class="regulator") {
div(class="regulator-label") // for spacing
@ -131,21 +128,24 @@ 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_key: ElementKey) -> View {
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_key)
regulator.outline_item(element)
}
// a list item that shows an element in an outline view of an assembly
#[component(inline_props)]
fn ElementOutlineItem(key: ElementKey, element: Rc<dyn Element>) -> View {
let state = use_context::<AppState>();
let class = state.selection.map(
move |sel| if sel.contains(&key) { "selected" } else { "" }
);
let class = {
let element_for_class = element.clone();
state.selection.map(
move |sel| if sel.contains(&element_for_class) { "selected" } else { "" }
)
};
let label = element.label().clone();
let representation = element.representation().clone();
let rep_components = move || {
@ -177,10 +177,11 @@ fn ElementOutlineItem(key: ElementKey, element: Rc<dyn Element>) -> View {
summary(
class=class.get(),
on:keydown={
let element_for_handler = element.clone();
move |event: KeyboardEvent| {
match event.key().as_str() {
"Enter" => {
state.select(key, event.shift_key());
state.select(&element_for_handler, event.shift_key());
event.prevent_default();
},
"ArrowRight" if regulated.get() => {
@ -207,9 +208,10 @@ fn ElementOutlineItem(key: ElementKey, element: Rc<dyn Element>) -> View {
div(
class="element",
on:click={
let state_clone = state.clone();
let state_for_handler = state.clone();
let element_for_handler = element.clone();
move |event: MouseEvent| {
state_clone.select(key, event.shift_key());
state_for_handler.select(&element_for_handler, event.shift_key());
event.stop_propagation();
event.prevent_default();
}
@ -223,11 +225,14 @@ fn ElementOutlineItem(key: ElementKey, element: Rc<dyn Element>) -> View {
ul(class="regulators") {
Keyed(
list=regulator_list,
view=move |reg_key| view! {
RegulatorOutlineItem(
regulator_key=reg_key,
element_key=key
)
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()
)