Skip to main content

makeover_layout/
facet.rs

1use crate::Nesting;
2
3// Names this module's prose links to, resolved for rustdoc.
4#[allow(unused_imports)]
5use crate::{Awaiting, FieldKind};
6
7/// A named dimension a set can be narrowed by.
8///
9/// One word for six things that were six mechanisms. MNW's discover page filters
10/// by free text, a flat any-of over item types, a tree of tags, a numeric range
11/// over price, a nested one-of over AI tier, and a browse position in the tag
12/// tree held separately from the tag selection, and the last two being separate
13/// is the whole reason a filter row there needs a tick box *and* a chevron. The
14/// panel is a mixed bag of hand-written controls because nothing named the thing
15/// they all are.
16///
17/// Deliberately wider than that one page. audiofiles' library browser and
18/// goingson's filters are the same shape, and a word that only fitted discover
19/// would be discover's markup with a neutral name on it.
20///
21/// # What it does not say
22///
23/// **What picking a value calls.** This crate names no address, so a facet is
24/// paired with routes the way a sortable column is paired with what
25/// reordering calls.
26///
27/// **How a tree is drawn.** Indented rows, a column of panes, a breadcrumb and a
28/// list: all four are honest renderings of the same described facet, and a
29/// terminal will not pick the same one a browser does. [`FacetValue::depth`] is
30/// what a renderer needs to draw any of them; the choice is not described.
31///
32/// **Which values to show.** A tag tree has thousands of nodes and a panel shows
33/// a handful. Deciding which handful is the app's, it is the same question as
34/// which rows go in a table, and no table member answers it either.
35#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
36#[non_exhaustive]
37pub struct Facet<'a> {
38    /// What the dimension is called, as the user reads it.
39    pub name: &'a str,
40    /// How many of its values may be in force, and in what shape.
41    pub mode: Selecting,
42    /// The values on offer, in the order they are drawn.
43    ///
44    /// A [`Selecting::Text`] facet has none: the value is whatever was typed,
45    /// and a description that listed the possible strings would be listing the
46    /// corpus. A [`Selecting::Range`] facet has none either, for the reason
47    /// [`FieldKind::Range`] takes bounds rather than options, the ends are the
48    /// question and the values between them are not enumerable.
49    pub values: &'a [FacetValue<'a>],
50}
51
52impl<'a> Facet<'a> {
53    /// A dimension with values to pick from.
54    #[must_use]
55    pub const fn new(name: &'a str, mode: Selecting, values: &'a [FacetValue<'a>]) -> Self {
56        Self { name, mode, values }
57    }
58
59    /// Whether the facet is narrowing the set right now.
60    ///
61    /// The question a renderer asks to decide whether to offer a way out of it,
62    /// and the reason it is derived rather than carried: a facet with nothing
63    /// standing is unengaged by construction, so a member saying so could
64    /// disagree with the values beside it. [`Standing::Inherited`] does not
65    /// count, something further up is what is doing the narrowing, and clearing
66    /// a child that was never picked clears nothing.
67    ///
68    /// Always false for [`Selecting::Text`] and [`Selecting::Range`], which
69    /// carry no values. A host that wants a clear affordance on those knows
70    /// whether its own box is empty; the description does not hold the typed
71    /// string.
72    #[must_use]
73    pub fn engaged(&self) -> bool {
74        self.values.iter().any(|value| value.standing.is_picked())
75    }
76
77    /// The deepest value in the facet, or zero when it is flat.
78    ///
79    /// What an indenting renderer needs to reserve a gutter before it draws the
80    /// first row, which is "First paint is final paint" applied to a tree: a
81    /// gutter widened as deeper values arrive is the reflow that rule forbids.
82    #[must_use]
83    pub fn reach(&self) -> u8 {
84        self.values
85            .iter()
86            .map(|value| value.depth.level)
87            .max()
88            .unwrap_or(0)
89    }
90}
91
92/// How many of a [`Facet`]'s values may be in force, and in what shape.
93///
94/// Five, and the fifth is what made this an enum rather than a bool. `one-of`,
95/// `any-of`, a range and free text are the four a form vocabulary already has in
96/// [`FieldKind`]; a tree's selection is none of them, and describing tags as
97/// any-of was what forced browsing to be a second mechanism beside filtering.
98#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
99#[non_exhaustive]
100pub enum Selecting {
101    /// Exactly one value, and picking another replaces it.
102    ///
103    /// MNW's AI tier, whose three options are nested ranges rather than
104    /// independent values, so two of them at once means nothing.
105    OneOf,
106    /// Any number of values, each independent of the others.
107    AnyOf,
108    /// A low end, a high end, or both.
109    ///
110    /// Carries no values for [`FieldKind::Range`]'s reason: the ends are the
111    /// question.
112    Range,
113    /// Whatever the user types.
114    Text,
115    /// A position in a tree, edited by taking branches in and pruning branches
116    /// out.
117    ///
118    /// The one mode that is not reducible to the others, and the one gesture
119    /// that replaced two. Picking a value narrows the set to it *and* reveals
120    /// its children, so browsing a tree and filtering by it stop being separate
121    /// mechanisms with separate state. What a selection then is: a set of
122    /// branches taken and a set pruned, resolved nearest-ancestor-first, so
123    /// `music` in and `music/synths` out is sayable and no flat mode can say it.
124    ///
125    /// Resolution happens in the app, and what reaches a renderer is the
126    /// [`Standing`] each drawn value ended up with. A renderer walking ancestors
127    /// itself would be a renderer that can disagree with the results beside it.
128    Subtree,
129}
130
131impl Selecting {
132    /// Whether the mode picks from values the description lists.
133    ///
134    /// False for [`Text`](Self::Text) and [`Range`](Self::Range), which are the
135    /// two whose answer is not one of a set. A renderer asks this before it
136    /// looks at [`Facet::values`], the way it asks
137    /// [`FieldKind::offers_options`] before it looks at a field's options.
138    #[must_use]
139    pub const fn offers_values(self) -> bool {
140        matches!(self, Self::OneOf | Self::AnyOf | Self::Subtree)
141    }
142
143    /// Whether a value can be pruned as well as picked.
144    ///
145    /// [`Subtree`](Self::Subtree) alone. Excluding a value from a flat facet is
146    /// the same fact as not picking it, so an exclude affordance there would be
147    /// a second control for a state the first one already holds.
148    #[must_use]
149    pub const fn prunes(self) -> bool {
150        matches!(self, Self::Subtree)
151    }
152
153    /// Whether picking a second value keeps the first.
154    #[must_use]
155    pub const fn accumulates(self) -> bool {
156        matches!(self, Self::AnyOf | Self::Subtree)
157    }
158}
159
160/// One value a [`Facet`] offers, as it currently stands.
161#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
162#[non_exhaustive]
163pub struct FacetValue<'a> {
164    /// What identifies it, and what a host keys its route on.
165    ///
166    /// An option's split between what submits and what reads, and a tree is
167    /// why it is not optional: two leaves under different parents are
168    /// legitimately both called "Ambient",
169    /// and the path is the only thing telling them apart. It is also what
170    /// nearest-ancestor-wins resolves over, so an app that carried only labels
171    /// could not compute the [`standing`](Self::standing) it hands back here.
172    pub value: &'a str,
173    /// What it is called, as the user reads it.
174    ///
175    /// The leaf's own name rather than its path: a facet drawn as an indented
176    /// tree repeats every ancestor on every line otherwise, and one drawn as a
177    /// breadcrumb has the ancestors already.
178    pub label: &'a str,
179    /// How many members of the set carry it.
180    ///
181    /// Optional, and settled that way rather than made mandatory: a count is a
182    /// measured fact the app may not have. Counting a tag subtree under an
183    /// active text search is a second query, and an app that will not pay for it
184    /// should be able to describe the facet anyway rather than write a zero that
185    /// reads as "none of them". That is [`Awaiting::amount`]'s rule in a second
186    /// place, state a number when it was measured, and nothing when it was not.
187    pub count: Option<u64>,
188    /// Whether it is narrowing the set, and how it came to be.
189    pub standing: Standing,
190    /// How far down the tree it sits, counting from zero at the root.
191    ///
192    /// Always zero for a flat facet, which is what makes an indenting renderer
193    /// one code path rather than two. A renderer that draws no tree at all still
194    /// reads this, since a value's depth is what distinguishes two same-named
195    /// leaves under different parents.
196    pub depth: Nesting,
197    /// Whether taking it reveals values under it.
198    ///
199    /// Distinct from having a nonzero [`depth`](Self::depth): a leaf deep in the
200    /// tree branches no further, and a root with children does. Both facts are
201    /// needed and neither implies the other, which is why the pair is two
202    /// members rather than one count.
203    pub branching: bool,
204}
205
206impl<'a> FacetValue<'a> {
207    /// An unpicked value at the root of the facet.
208    #[must_use]
209    pub const fn new(value: &'a str, label: &'a str) -> Self {
210        Self {
211            value,
212            label,
213            count: None,
214            standing: Standing::Open,
215            depth: Nesting::top(),
216            branching: false,
217        }
218    }
219
220    /// A value whose identifier is also what the user reads.
221    ///
222    /// A plain option's convenience, and it is the flat case: a type or a tier
223    /// is its own name, and only a tree needs a path that is not one.
224    #[must_use]
225    pub const fn of(value: &'a str) -> Self {
226        Self::new(value, value)
227    }
228
229    /// How many members carry it, when that was measured.
230    #[must_use]
231    pub const fn counted(mut self, count: u64) -> Self {
232        self.count = Some(count);
233        self
234    }
235
236    /// How it stands in the current selection.
237    #[must_use]
238    pub const fn standing(mut self, standing: Standing) -> Self {
239        self.standing = standing;
240        self
241    }
242
243    /// Where it sits in the tree, and whether anything hangs off it.
244    #[must_use]
245    pub const fn at(mut self, depth: Nesting, branching: bool) -> Self {
246        self.depth = depth;
247        self.branching = branching;
248        self
249    }
250}
251
252/// Whether a [`FacetValue`] is narrowing the set, and how it came to be.
253///
254/// Five rather than a bool, and the extra members are what a tree costs. A
255/// pruned branch and an untaken one are not the same state (one was decided
256/// against and the other was never reached) and a child under a taken parent is
257/// in force without anybody having picked it. A renderer given a bool either
258/// marks every descendant of a taken branch, which reads as forty deliberate
259/// choices, or marks none of them, which reads as unfiltered.
260///
261/// The out side needs the same pair the in side has. A child under a pruned
262/// branch is out without anybody having pruned it: [`PrunedAbove`] to
263/// [`Pruned`]'s decision, as [`Inherited`] is to [`Taken`]'s.
264///
265/// [`PrunedAbove`]: Self::PrunedAbove
266/// [`Pruned`]: Self::Pruned
267/// [`Inherited`]: Self::Inherited
268/// [`Taken`]: Self::Taken
269#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
270#[non_exhaustive]
271pub enum Standing {
272    /// Not picked, and nothing above it is either.
273    #[default]
274    Open,
275    /// Picked here. The set is narrowed to it and whatever hangs off it.
276    Taken,
277    /// In force because something above it was taken.
278    Inherited,
279    /// Pruned out, though something above it was taken.
280    ///
281    /// The state that only [`Selecting::Subtree`] can reach, and the reason
282    /// exclusion is drawn as a visible affordance beside each label rather than
283    /// as a modifier on the ordinary one: a gesture a terminal cannot express is
284    /// a gesture half the renderers would have to leave out, and an affordance
285    /// nothing teaches is one users do not find.
286    Pruned,
287    /// Out because something above it was pruned.
288    ///
289    /// Not picked: clearing it clears nothing, because the decision is further
290    /// up, which is [`Inherited`](Self::Inherited)'s position on the other
291    /// side.
292    PrunedAbove,
293}
294
295impl Standing {
296    /// Whether the user decided this value, either way.
297    ///
298    /// True for [`Taken`](Self::Taken) and [`Pruned`](Self::Pruned), both are
299    /// choices, and both are things a "clear this" affordance has to clear.
300    /// [`Inherited`](Self::Inherited) is not: clearing it clears nothing,
301    /// because the decision is further up.
302    #[must_use]
303    pub const fn is_picked(self) -> bool {
304        matches!(self, Self::Taken | Self::Pruned)
305    }
306
307    /// Whether the value narrows the set in.
308    ///
309    /// [`Taken`](Self::Taken) and [`Inherited`](Self::Inherited): one was picked
310    /// and one came down from above, and to the set they mean the same thing.
311    /// The pair is named here so a renderer colouring in-force values does not
312    /// have to know which is which.
313    #[must_use]
314    pub const fn in_force(self) -> bool {
315        matches!(self, Self::Taken | Self::Inherited)
316    }
317
318    /// The content intent the value takes.
319    ///
320    /// [`Pruned`](Self::Pruned) reads back a step, which is the three-tone rule
321    /// above rather than a new decision: a pruned branch is still a live control
322    /// (pressing it takes the prune off) so it may not wear `content-muted`,
323    /// and it is not the thing itself either.
324    #[must_use]
325    pub const fn intent(self) -> &'static str {
326        match self {
327            Self::Taken | Self::Inherited | Self::Open => "content",
328            Self::Pruned | Self::PrunedAbove => "content-secondary",
329        }
330    }
331
332    /// Whether the value is out of the set by a prune, here or above.
333    ///
334    /// [`in_force`](Self::in_force)'s counterpart, so a renderer reading a
335    /// value back a step does not have to know which of the two it has.
336    #[must_use]
337    pub const fn pruned_out(self) -> bool {
338        matches!(self, Self::Pruned | Self::PrunedAbove)
339    }
340}