//! What the reader has done that a walk over the description has to know. //! //! Three walks read a screen and they have to agree exactly: the focus walk //! decides how many stops there are, the height walk decides how many rows each //! node wants, and the drawing counts stops as it paints. A description alone is //! not enough for any of them, because two things about a screen are the //! reader's rather than the handler's: //! //! - which regions and which questions do not apply right now, from //! [`crate::reveal::hidden`] (`079a011e`, `8fdb814c`), and //! - how many slots a repeating question stands in, from [`View::standing`] //! (`60d1753c`). //! //! They are one type because they are one fact — what the reader has done //! since the screen arrived — and because a walk that took one and not the //! other is a walk that can disagree with the one beside it about how many //! stops a screen has, which is a focus ring painted on the wrong control. //! //! [`Local::none`] is the honest answer for a caller with no view: every region //! applies and every repeating question stands in the slots the description //! described. That is what a screen drew before either member existed. use quasi_router::Field; use crate::View; /// What does not apply right now, on one screen, as the reader has left it. /// /// Regions and questions in one type because they are one answer to one /// question and are computed in one walk. A caller holding the regions and not /// the fields would draw a box the caret cannot stop on, which is the same /// disagreement [`Local`] exists to prevent one turn further out. #[derive(Debug, Clone, Default)] pub struct Hidden<'a> { /// The regions that are not out, by [`Slot::id`]. /// /// [`Slot::id`]: quasi_router::Slot::id pub regions: Vec<&'a str>, /// The questions that are not out, by [`Field::name`]. pub fields: Vec<&'a str>, } impl Hidden<'_> { /// Everything applies, which is what a caller with nothing to evaluate /// against hands on and what a walk that must see the whole description /// passes deliberately. #[must_use] pub const fn none() -> Self { Self { regions: Vec::new(), fields: Vec::new(), } } } /// The reader's own half of a screen, as the walks over it need it. #[derive(Debug, Clone, Copy)] pub struct Local<'a> { /// The regions and questions that do not apply right now. hidden: &'a Hidden<'a>, /// What the reader has typed, ticked and added, when there is one to read. view: Option<&'a View>, } impl<'a> Local<'a> { /// Nothing hidden, and every repeating question in the slots the /// description gave it. #[must_use] pub const fn none() -> Self { Self { hidden: &NOTHING, view: None, } } /// These regions out, and the reader's slots counted from this view. #[must_use] pub const fn of(hidden: &'a Hidden<'a>, view: &'a View) -> Self { Self { hidden, view: Some(view), } } /// These regions out, and nothing known about what the reader has added. /// /// What a caller answering "how many stops does this screen describe" /// passes: the two halves are independent, and a walk that wants one of /// them should not have to invent the other. #[must_use] pub const fn hiding(hidden: &'a Hidden<'a>) -> Self { Self { hidden, view: None } } /// The reader's own state, when the caller had one to hand over. #[must_use] pub const fn view(&self) -> Option<&'a View> { self.view } /// Whether this region does not apply right now. #[must_use] pub fn out(&self, id: &str) -> bool { self.hidden.regions.contains(&id) } /// Whether this question does not apply right now. /// /// By name, which is what the description carries and what a submit sends /// the value under. #[must_use] pub fn field_out(&self, name: &str) -> bool { self.hidden.fields.contains(&name) } /// How many slots this question stands in. /// /// `1` for an ordinary field, which is what makes every walk able to loop /// over the slots of anything without asking whether there are any. #[must_use] pub fn standing(&self, field: &Field) -> usize { self.view .map_or_else(|| field.slots(), |view| view.standing(field)) } } /// The empty answer, so [`Local::none`] can stay a `const fn`. static NOTHING: Hidden<'static> = Hidden::none();