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, Choice, Column, Field, 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 column's [`sortable`](Column::sortable) flag is
25/// paired with what 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 [`Field::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 /// [`Choice::value`]'s split, and a tree is why it is not optional: two
167 /// leaves under different parents are legitimately both called "Ambient",
168 /// and the path is the only thing telling them apart. It is also what
169 /// nearest-ancestor-wins resolves over, so an app that carried only labels
170 /// could not compute the [`standing`](Self::standing) it hands back here.
171 pub value: &'a str,
172 /// What it is called, as the user reads it.
173 ///
174 /// The leaf's own name rather than its path: a facet drawn as an indented
175 /// tree repeats every ancestor on every line otherwise, and one drawn as a
176 /// breadcrumb has the ancestors already.
177 pub label: &'a str,
178 /// How many members of the set carry it.
179 ///
180 /// Optional, and settled that way rather than made mandatory: a count is a
181 /// measured fact the app may not have. Counting a tag subtree under an
182 /// active text search is a second query, and an app that will not pay for it
183 /// should be able to describe the facet anyway rather than write a zero that
184 /// reads as "none of them". That is [`Awaiting::amount`]'s rule in a second
185 /// place, state a number when it was measured, and nothing when it was not.
186 pub count: Option<u64>,
187 /// Whether it is narrowing the set, and how it came to be.
188 pub standing: Standing,
189 /// How far down the tree it sits, counting from zero at the root.
190 ///
191 /// Always zero for a flat facet, which is what makes an indenting renderer
192 /// one code path rather than two. A renderer that draws no tree at all still
193 /// reads this, since a value's depth is what distinguishes two same-named
194 /// leaves under different parents.
195 pub depth: Nesting,
196 /// Whether taking it reveals values under it.
197 ///
198 /// Distinct from having a nonzero [`depth`](Self::depth): a leaf deep in the
199 /// tree branches no further, and a root with children does. Both facts are
200 /// needed and neither implies the other, which is why the pair is two
201 /// members rather than one count.
202 pub branching: bool,
203}
204
205impl<'a> FacetValue<'a> {
206 /// An unpicked value at the root of the facet.
207 #[must_use]
208 pub const fn new(value: &'a str, label: &'a str) -> Self {
209 Self {
210 value,
211 label,
212 count: None,
213 standing: Standing::Open,
214 depth: Nesting::top(),
215 branching: false,
216 }
217 }
218
219 /// A value whose identifier is also what the user reads.
220 ///
221 /// [`Choice::plain`]'s convenience, and it is the flat case: a type or a tier
222 /// is its own name, and only a tree needs a path that is not one.
223 #[must_use]
224 pub const fn of(value: &'a str) -> Self {
225 Self::new(value, value)
226 }
227
228 /// How many members carry it, when that was measured.
229 #[must_use]
230 pub const fn counted(mut self, count: u64) -> Self {
231 self.count = Some(count);
232 self
233 }
234
235 /// How it stands in the current selection.
236 #[must_use]
237 pub const fn standing(mut self, standing: Standing) -> Self {
238 self.standing = standing;
239 self
240 }
241
242 /// Where it sits in the tree, and whether anything hangs off it.
243 #[must_use]
244 pub const fn at(mut self, depth: Nesting, branching: bool) -> Self {
245 self.depth = depth;
246 self.branching = branching;
247 self
248 }
249}
250
251/// Whether a [`FacetValue`] is narrowing the set, and how it came to be.
252///
253/// Five rather than a bool, and the extra members are what a tree costs. A
254/// pruned branch and an untaken one are not the same state (one was decided
255/// against and the other was never reached) and a child under a taken parent is
256/// in force without anybody having picked it. A renderer given a bool either
257/// marks every descendant of a taken branch, which reads as forty deliberate
258/// choices, or marks none of them, which reads as unfiltered.
259///
260/// The out side needs the same pair the in side has. A child under a pruned
261/// branch is out without anybody having pruned it: [`PrunedAbove`] to
262/// [`Pruned`]'s decision, as [`Inherited`] is to [`Taken`]'s.
263///
264/// [`PrunedAbove`]: Self::PrunedAbove
265/// [`Pruned`]: Self::Pruned
266/// [`Inherited`]: Self::Inherited
267/// [`Taken`]: Self::Taken
268#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
269#[non_exhaustive]
270pub enum Standing {
271 /// Not picked, and nothing above it is either.
272 #[default]
273 Open,
274 /// Picked here. The set is narrowed to it and whatever hangs off it.
275 Taken,
276 /// In force because something above it was taken.
277 Inherited,
278 /// Pruned out, though something above it was taken.
279 ///
280 /// The state that only [`Selecting::Subtree`] can reach, and the reason
281 /// exclusion is drawn as a visible affordance beside each label rather than
282 /// as a modifier on the ordinary one: a gesture a terminal cannot express is
283 /// a gesture half the renderers would have to leave out, and an affordance
284 /// nothing teaches is one users do not find.
285 Pruned,
286 /// Out because something above it was pruned.
287 ///
288 /// Not picked: clearing it clears nothing, because the decision is further
289 /// up, which is [`Inherited`](Self::Inherited)'s position on the other
290 /// side.
291 PrunedAbove,
292}
293
294impl Standing {
295 /// Whether the user decided this value, either way.
296 ///
297 /// True for [`Taken`](Self::Taken) and [`Pruned`](Self::Pruned), both are
298 /// choices, and both are things a "clear this" affordance has to clear.
299 /// [`Inherited`](Self::Inherited) is not: clearing it clears nothing,
300 /// because the decision is further up.
301 #[must_use]
302 pub const fn is_picked(self) -> bool {
303 matches!(self, Self::Taken | Self::Pruned)
304 }
305
306 /// Whether the value narrows the set in.
307 ///
308 /// [`Taken`](Self::Taken) and [`Inherited`](Self::Inherited): one was picked
309 /// and one came down from above, and to the set they mean the same thing.
310 /// The pair is named here so a renderer colouring in-force values does not
311 /// have to know which is which.
312 #[must_use]
313 pub const fn in_force(self) -> bool {
314 matches!(self, Self::Taken | Self::Inherited)
315 }
316
317 /// The content intent the value takes.
318 ///
319 /// [`Pruned`](Self::Pruned) reads back a step, which is the three-tone rule
320 /// above rather than a new decision: a pruned branch is still a live control
321 /// (pressing it takes the prune off) so it may not wear `content-muted`,
322 /// and it is not the thing itself either.
323 #[must_use]
324 pub const fn intent(self) -> &'static str {
325 match self {
326 Self::Taken | Self::Inherited | Self::Open => "content",
327 Self::Pruned | Self::PrunedAbove => "content-secondary",
328 }
329 }
330
331 /// Whether the value is out of the set by a prune, here or above.
332 ///
333 /// [`in_force`](Self::in_force)'s counterpart, so a renderer reading a
334 /// value back a step does not have to know which of the two it has.
335 #[must_use]
336 pub const fn pruned_out(self) -> bool {
337 matches!(self, Self::Pruned | Self::PrunedAbove)
338 }
339}