diff --git a/app-proto/main.css b/app-proto/main.css index 4726a27..d71ce4a 100644 --- a/app-proto/main.css +++ b/app-proto/main.css @@ -133,25 +133,28 @@ details[open]:has(li) .element-switch::after { font-size: 10pt; } -.regulator-input { +.regulator.invalid-constraint { + color: var(--text-invalid); +} + +.regulator > input { color: inherit; background-color: inherit; border: 1px solid var(--border); border-radius: 2px; } -.regulator-input::placeholder { +.regulator > input::placeholder { color: inherit; opacity: 54%; font-style: italic; } -.regulator-input.constraint { +.regulator.valid-constraint > input { background-color: var(--display-background); } -.regulator-input.invalid { - color: var(--text-invalid); +.regulator.invalid-constraint > input { border-color: var(--border-invalid); } @@ -163,7 +166,7 @@ details[open]:has(li) .element-switch::after { font-style: normal; } -.regulator-input.invalid + .status::after, details:has(.invalid):not([open]) .status::after { +.invalid-constraint > .status::after, details:has(.invalid-constraint):not([open]) .status::after { content: '⚠'; color: var(--text-invalid); } diff --git a/app-proto/src/add_remove.rs b/app-proto/src/add_remove.rs index cdc35e6..3280dac 100644 --- a/app-proto/src/add_remove.rs +++ b/app-proto/src/add_remove.rs @@ -6,8 +6,11 @@ use crate::{ AppState, assembly::{ Assembly, + Regulator, + RegulatorRole, Element - } + }, + engine::Q }; /* DEBUG */ @@ -197,8 +200,52 @@ pub fn AddRemove() -> View { (subject_vec[0].clone(), subject_vec[1].clone()) } ); - state.assembly.insert_new_regulator(subjects); + let measurement = state.assembly.elements.map( + move |elts| { + let reps = ( + elts[subjects.0].representation.get_clone(), + elts[subjects.1].representation.get_clone() + ); + reps.0.dot(&(&*Q * reps.1)) + } + ); + let set_point = create_signal(0.0); + let role = create_signal(RegulatorRole::Measurement); + state.assembly.insert_regulator(Regulator { + subjects: subjects, + measurement: measurement, + set_point: set_point, + set_point_text: create_signal(String::new()), + role: role, + }); state.selection.update(|sel| sel.clear()); + + /* DEBUG */ + // print updated regulator list + console::log_1(&JsValue::from("Regulators:")); + state.assembly.regulators.with(|regs| { + for (_, reg) in regs.into_iter() { + console::log_5( + &JsValue::from(" "), + &JsValue::from(reg.subjects.0), + &JsValue::from(reg.subjects.1), + &JsValue::from(":"), + &JsValue::from(reg.set_point.get_untracked()) + ); + } + }); + + // update the realization when the regulator becomes + // a constraint, or is edited while acting as a constraint + create_effect(move || { + console::log_1(&JsValue::from( + format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1) + )); + set_point.track(); + if role.with(|rl| rl.is_valid_constraint()) { + state.assembly.realize(); + } + }); } ) { "🔗" } select(bind:value=assembly_name) { /* DEBUG */ // example assembly chooser diff --git a/app-proto/src/assembly.rs b/app-proto/src/assembly.rs index 2ccfc69..c052a3b 100644 --- a/app-proto/src/assembly.rs +++ b/app-proto/src/assembly.rs @@ -1,15 +1,11 @@ use nalgebra::{DMatrix, DVector, DVectorView, Vector3}; use rustc_hash::FxHashMap; use slab::Slab; -use std::{ - collections::BTreeSet, - num::ParseFloatError, - sync::atomic::{AtomicU64, Ordering} -}; +use std::{collections::BTreeSet, sync::atomic::{AtomicU64, Ordering}}; use sycamore::prelude::*; use web_sys::{console, wasm_bindgen::JsValue}; /* DEBUG */ -use crate::engine::{Q, local_unif_to_std, realize_gram, ConfigSubspace, PartialMatrix}; +use crate::engine::{realize_gram, local_unif_to_std, ConfigSubspace, PartialMatrix}; // the types of the keys we use to access an assembly's elements and regulators pub type ElementKey = usize; @@ -115,76 +111,32 @@ impl Element { } } -// to construct a `SpecifiedValue` that might be `Present`, use the associated -// function `try_from`. this ensures that `spec` is always a valid specification -// of `value` according to the format discussed above the implementation of -// `TryFrom` -pub enum SpecifiedValue { - Absent, - Present { - spec: String, - value: f64 - } +pub enum RegulatorRole { + Measurement, + Constraint(bool) } -use SpecifiedValue::*; - -impl SpecifiedValue { - // get the specification for this value. the associated function `try_from` - // is essentially a left inverse of this method: - // - // SpecifiedValue::try_from(x.spec()) == Ok(x) - // - pub fn spec(&self) -> String { +impl RegulatorRole { + pub fn is_valid_constraint(&self) -> bool { match self { - Absent => String::new(), - Present { spec, .. } => spec.clone() - } - } - - fn is_present(&self) -> bool { - match self { - Absent => false, - Present { .. } => true + RegulatorRole::Measurement => false, + RegulatorRole::Constraint(valid) => *valid } } } -// we can try to turn a specification string into a `SpecifiedValue`. if the -// specification is empty, the `SpecifiedValue` is `Absent`. if the -// specification parses to a floating-point value `x`, the `SpecifiedValue` is -// `Present`, with a `value` of `x`, and the specification is stored in `spec`. -// these are the only valid specifications; any other produces an error -impl TryFrom for SpecifiedValue { - type Error = ParseFloatError; - - fn try_from(spec: String) -> Result { - if spec.is_empty() { - Ok(Absent) - } else { - spec.parse::().map( - |value| Present { spec: spec, value: value } - ) - } - } -} - -#[derive(Clone, Copy)] +#[derive(Clone)] pub struct Regulator { pub subjects: (ElementKey, ElementKey), pub measurement: ReadSignal, - pub set_point: Signal + pub set_point: Signal, + pub set_point_text: Signal, + pub role: Signal } impl Regulator { - pub fn try_set(&self, set_pt_spec: String) -> bool { - match SpecifiedValue::try_from(set_pt_spec) { - Ok(set_pt) => { - self.set_point.set(set_pt); - true - } - Err(_) => false, - } + fn role_is_valid_constraint_untracked(&self) -> bool { + self.role.with_untracked(|role| role.is_valid_constraint()) } } @@ -282,53 +234,6 @@ impl Assembly { subject_regulators.1.update(|regs| regs.insert(key)); } - pub fn insert_new_regulator(self, subjects: (ElementKey, ElementKey)) { - // create and insert a new regulator - let measurement = self.elements.map( - move |elts| { - let reps = ( - elts[subjects.0].representation.get_clone(), - elts[subjects.1].representation.get_clone() - ); - reps.0.dot(&(&*Q * reps.1)) - } - ); - let set_point = create_signal(Absent); - self.insert_regulator(Regulator { - subjects: subjects, - measurement: measurement, - set_point: set_point - }); - - /* DEBUG */ - // print an updated list of regulators - console::log_1(&JsValue::from("Regulators:")); - self.regulators.with(|regs| { - for (_, reg) in regs.into_iter() { - console::log_5( - &JsValue::from(" "), - &JsValue::from(reg.subjects.0), - &JsValue::from(reg.subjects.1), - &JsValue::from(":"), - &JsValue::from(reg.set_point.with_untracked( - |set_pt| set_pt.spec() - )) - ); - } - }); - - // update the realization when the regulator becomes a constraint, or is - // edited while acting as a constraint - create_effect(move || { - console::log_1(&JsValue::from( - format!("Updated constraint with subjects ({}, {})", subjects.0, subjects.1) - )); - if set_point.with(|set_pt| set_pt.is_present()) { - self.realize(); - } - }); - } - // --- realization --- pub fn realize(&self) { @@ -345,17 +250,12 @@ impl Assembly { let mut gram_to_be = PartialMatrix::new(); self.regulators.with_untracked(|regs| { for (_, reg) in regs { - reg.set_point.with_untracked(|set_pt| { - match set_pt { - Absent => (), - Present { value, .. } => { - let subjects = reg.subjects; - let row = elts[subjects.0].column_index.unwrap(); - let col = elts[subjects.1].column_index.unwrap(); - gram_to_be.push_sym(row, col, *value); - } - }; - }); + if reg.role_is_valid_constraint_untracked() { + let subjects = reg.subjects; + let row = elts[subjects.0].column_index.unwrap(); + let col = elts[subjects.1].column_index.unwrap(); + gram_to_be.push_sym(row, col, reg.set_point.get_untracked()); + } } }); diff --git a/app-proto/src/outline.rs b/app-proto/src/outline.rs index 04bda9a..8dcda93 100644 --- a/app-proto/src/outline.rs +++ b/app-proto/src/outline.rs @@ -1,6 +1,8 @@ use itertools::Itertools; use sycamore::prelude::*; use web_sys::{ + Event, + HtmlInputElement, KeyboardEvent, MouseEvent, wasm_bindgen::JsCast @@ -10,60 +12,34 @@ use crate::{ AppState, assembly, assembly::{ - ElementKey, Regulator, RegulatorKey, - SpecifiedValue::* + RegulatorRole::*, + ElementKey } }; // an editable view of a regulator #[component(inline_props)] fn RegulatorInput(regulator: Regulator) -> View { - let valid = create_signal(true); - let value = create_signal( - regulator.set_point.with_untracked(|set_pt| set_pt.spec()) - ); - - // this closure resets the input value to the regulator's set point - // specification - let reset_value = move || { - batch(|| { - valid.set(true); - value.set(regulator.set_point.with(|set_pt| set_pt.spec())); - }) - }; - - // reset the input value whenever the regulator's set point specification - // is updated - create_effect(reset_value); - view! { input( r#type="text", - class=move || { - if valid.get() { - regulator.set_point.with(|set_pt| { - match set_pt { - Absent => "regulator-input", - Present { .. } => "regulator-input constraint" - } - }) - } else { - "regulator-input invalid" - } - }, placeholder=regulator.measurement.with(|result| result.to_string()), - bind:value=value, - on:change=move |_| valid.set( - regulator.try_set(value.get_clone_untracked()) - ), - on:keydown={ - move |event: KeyboardEvent| { - match event.key().as_str() { - "Escape" => reset_value(), - _ => () - } + bind:value=regulator.set_point_text, + on:change=move |event: Event| { + let target: HtmlInputElement = event.target().unwrap().unchecked_into(); + let value = target.value(); + if value.is_empty() { + regulator.role.set(Measurement); + } else { + match target.value().parse::() { + Ok(set_pt) => batch(|| { + regulator.set_point.set(set_pt); + regulator.role.set(Constraint(true)); + }), + Err(_) => regulator.role.set(Constraint(false)) + }; } } ) @@ -75,15 +51,22 @@ fn RegulatorInput(regulator: Regulator) -> View { fn RegulatorOutlineItem(regulator_key: RegulatorKey, element_key: ElementKey) -> View { let state = use_context::(); let assembly = &state.assembly; - let regulator = assembly.regulators.with(|regs| regs[regulator_key]); + let regulator = assembly.regulators.with(|regs| regs[regulator_key].clone()); let other_subject = if regulator.subjects.0 == element_key { regulator.subjects.1 } else { regulator.subjects.0 }; let other_subject_label = assembly.elements.with(|elts| elts[other_subject].label.clone()); + let class = regulator.role.map( + |role| match role { + Measurement => "regulator", + Constraint(true) => "regulator valid-constraint", + Constraint(false) => "regulator invalid-constraint" + } + ); view! { - li(class="regulator") { + li(class=class.get()) { div(class="regulator-label") { (other_subject_label) } div(class="regulator-type") { "Inversive distance" } RegulatorInput(regulator=regulator)