Generalize constraints to observables

This commit is contained in:
Aaron Fenyes 2025-01-25 13:00:18 -08:00
parent 46324fecc6
commit fb8e391587
4 changed files with 77 additions and 35 deletions

View file

@ -8,7 +8,16 @@ use web_sys::{
wasm_bindgen::JsCast
};
use crate::{AppState, assembly, assembly::{Constraint, ConstraintKey, ElementKey}};
use crate::{
AppState,
assembly,
assembly::{
Constraint,
ConstraintKey,
ConstraintRole::*,
ElementKey
}
};
// an editable view of the Lorentz product representing a constraint
#[component(inline_props)]
@ -16,16 +25,22 @@ fn LorentzProductInput(constraint: Constraint) -> View {
view! {
input(
r#type="text",
bind:value=constraint.lorentz_prod_text,
placeholder=constraint.measured.with(|result| result.to_string()),
bind:value=constraint.desired_text,
on:change=move |event: Event| {
let target: HtmlInputElement = event.target().unwrap().unchecked_into();
match target.value().parse::<f64>() {
Ok(lorentz_prod) => batch(|| {
constraint.lorentz_prod.set(lorentz_prod);
constraint.lorentz_prod_valid.set(true);
}),
Err(_) => constraint.lorentz_prod_valid.set(false)
};
let value = target.value();
if value.is_empty() {
constraint.role.set(Measure);
} else {
match target.value().parse::<f64>() {
Ok(desired) => batch(|| {
constraint.desired.set(desired);
constraint.role.set(Constrain);
}),
Err(_) => constraint.role.set(Invalid)
};
}
}
)
}
@ -43,12 +58,15 @@ fn ConstraintOutlineItem(constraint_key: ConstraintKey, element_key: ElementKey)
constraint.subjects.0
};
let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone());
let class = constraint.lorentz_prod_valid.map(
|&lorentz_prod_valid| if lorentz_prod_valid { "constraint" } else { "constraint invalid" }
let class = constraint.role.map(
|role| match role {
Measure => "constraint",
Constrain => "constraint constrained",
Invalid => "constraint invalid"
}
);
view! {
li(class=class.get()) {
input(r#type="checkbox", bind:checked=constraint.active)
div(class="constraint-label") { (other_subject_label) }
LorentzProductInput(constraint=constraint)
div(class="status")