Skip to main content

makeover_layout/
depth.rs

1use crate::Intent;
2
3/// Which way the light falls across a two-tone edge.
4///
5/// The whole content of a bevel, once colour and thickness are deferred. The
6/// light is always assumed to come from the top left: every consumer measured
7/// agreed on that and none of them ever varied it, so it is an invariant here
8/// rather than a parameter.
9///
10/// # The two corners that belong to both edges
11///
12/// Top-right and bottom-left are where the lit run meets the shaded one, and
13/// the description's claim is that they belong to *both*. How a renderer says
14/// that is its own business, because the answer is bounded by resolution and
15/// not by taste:
16///
17/// - A terminal cell is roughly 8x17 device pixels, so giving the whole corner
18///   to one tone thickens that edge by a cell and reads as one run overrunning
19///   the other. A half-cell glyph divides the cell already, so `makeover-tui`
20///   splits it and recovers real information. Its box-drawing fallback cannot:
21///   a single stroke has no half to give, so there both corners go to dark.
22/// - A pixel bevel is a one-point stroke by default, which makes the corner a
23///   one-point square. There is nothing to divide, a diagonal seam across one
24///   point is sub-pixel, and antialiasing renders it as the blend a mitred join
25///   already produces. So `makeover-immediate` mitres and is *not* diverging;
26///   it is the same rule at a resolution where the split degenerates.
27///
28/// Stated here so the difference reads as a decision rather than as drift. A
29/// renderer with room to divide the corner should; one without should mitre or
30/// pick the shaded tone, and neither is a bug.
31/// `#[non_exhaustive]` for [`Depth`]'s reason: an edge this renderer has no
32/// drawing for should cost it a wildcard arm rather than a compile error and a
33/// wait on someone else's publish.
34#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
35#[non_exhaustive]
36pub enum Bevel {
37    /// Lit from the top left: light on top and left, dark on bottom and right.
38    Raised,
39    /// The same edge inverted, which is also the pressed state of anything
40    /// that draws itself [`Bevel::Raised`].
41    Inset,
42    /// [`Raised`](Self::Raised) with the bottom run left off, so the surface
43    /// continues into whatever sits directly under it.
44    ///
45    /// The chosen folder tab, and the reason this is an edge rather than a
46    /// stylesheet trick in one renderer. A tab that comes forward but keeps a
47    /// closed bottom reads as a chip resting near its pane; the open run is
48    /// what makes it read as the front of the pane itself. Every renderer can
49    /// say it: a browser drops the vertical offset on the dark shadow, a
50    /// terminal draws three sides of a box, and a pixel renderer draws its
51    /// shaded polyline as two points instead of three.
52    ///
53    /// It carries no inverse. Pressing one inverts to [`Inset`](Self::Inset)
54    /// like any other raised thing, because a control held down is not joined
55    /// to anything.
56    RaisedOpen,
57}
58
59impl Bevel {
60    /// The edge intents, as `(top_left, bottom_right)`.
61    ///
62    /// Split out from any painting because the inversion *is* the idea, and
63    /// it is the one part every renderer implements identically.
64    #[must_use]
65    pub const fn edges(self) -> (Edge, Edge) {
66        match self {
67            // An open edge is lit the way a raised one is. Which runs are
68            // *drawn* is [`Bevel::draws_bottom`]'s question, kept apart from
69            // this one so that a renderer reading only the tones keeps
70            // working and a renderer that never asks draws a closed box,
71            // which is the old drawing rather than a wrong one.
72            Self::Raised | Self::RaisedOpen => (Edge::Light, Edge::Dark),
73            Self::Inset => (Edge::Dark, Edge::Light),
74        }
75    }
76
77    /// Whether the shaded run closes the bottom of the box.
78    ///
79    /// True for every edge but [`RaisedOpen`](Self::RaisedOpen), so a renderer
80    /// that does not ask draws what it always drew.
81    #[must_use]
82    pub const fn draws_bottom(self) -> bool {
83        !matches!(self, Self::RaisedOpen)
84    }
85
86    /// Pressing inverts. A raised control reads as inset while held.
87    ///
88    /// Stated here rather than left to each consumer because a cascade can
89    /// carry a pressed state and an immediate-mode renderer cannot: audiofiles
90    /// resolves this per call site, eighteen times.
91    #[must_use]
92    pub const fn pressed(self) -> Self {
93        match self {
94            // A tab held down is not joined to anything, so the open run
95            // closes as it inverts.
96            Self::Raised | Self::RaisedOpen => Self::Inset,
97            Self::Inset => Self::Raised,
98        }
99    }
100}
101
102/// One side of a bevel, named by the intent it takes.
103#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
104#[non_exhaustive]
105pub enum Edge {
106    /// The lit side.
107    Light,
108    /// The shadowed side.
109    Dark,
110}
111
112impl Intent for Edge {
113    fn token(self) -> &'static str {
114        match self {
115            Self::Light => "bevel-light",
116            Self::Dark => "bevel-dark",
117        }
118    }
119}
120
121/// A surface intent a region is filled with.
122///
123/// `#[non_exhaustive]`, so a renderer must carry a wildcard arm and a new
124/// member is additive rather than breaking. The vocabulary exists to grow and
125/// the renderers exist to disagree about how much of it they answer, so growth
126/// must not be a lockstep event. The renderer's wildcard is not a hole:
127/// [`Fill`] is resolved through a fallible lookup, and a missing intent is
128/// answered with structure rather than with a substituted colour.
129///
130/// [`Sunken`]: Fill::Sunken
131#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
132#[non_exhaustive]
133pub enum Fill {
134    /// The page behind everything.
135    Page,
136    /// A surface lifted off the page: cards, controls, menus, toasts.
137    Raised,
138    /// A surface floating above the page rather than resting on it.
139    Overlay,
140    /// The inside of a well.
141    Well,
142    /// A surface set back from the one it sits on, by colour and nothing else.
143    ///
144    /// Not a well. A well is a hole with an edge, and the two are authored in
145    /// opposite directions: `makeover` derives `surface-well` by inverting
146    /// against the theme's own content colour, while `surface-sunken` is
147    /// authored and free to sit darker than raised (goingson's does). Naming
148    /// only the well left the recessed-with-no-edge surface unsayable, which is
149    /// what an unchosen tab is: it recedes so the chosen one can come forward,
150    /// and it carries no bevel of its own.
151    Sunken,
152}
153
154// No `fallback` here, deliberately. An earlier cut had `Fill::Well` fall back
155// to `Fill::Page` so a consumer on makeover 2.2.0, which has no `surface-well`,
156// had something to paint. makeover-tui found that wrong within a day: page is
157// the surface a well is usually cut into, so on a terminal that substitution
158// produces exactly the invisibility it was meant to prevent, and the right
159// answer there is a drawn edge rather than a different colour.
160//
161// Substituting one intent for another is renderer policy. The description says
162// what the region is and stops.
163
164impl Intent for Fill {
165    fn token(self) -> &'static str {
166        match self {
167            Self::Page => "surface-page",
168            Self::Raised => "surface-raised",
169            Self::Overlay => "surface-overlay",
170            Self::Well => "surface-well",
171            Self::Sunken => "surface-sunken",
172        }
173    }
174}
175
176/// How a region sits relative to the surface behind it.
177///
178/// Fill and bevel are named together because naming them apart is what let
179/// them disagree. Every consumer measured had at least one region carrying a
180/// raised bevel over a recessed fill: audiofiles fixed it in `raised_frame`
181/// and recorded the bug in its doc comment, and Balanced Breakfast still had
182/// twelve of them a year later. A single name for the pair makes that
183/// unrepresentable.
184/// `#[non_exhaustive]` for the same reason as [`Fill`], and in the same
185/// release: a depth this renderer has no drawing for should cost it a
186/// wildcard arm, not a compile error and a wait on someone else's publish.
187#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
188#[non_exhaustive]
189pub enum Depth {
190    /// Level with its surroundings. No edge.
191    Flat,
192    /// A card laid on the panel it sits in.
193    Raised,
194    /// A hole in the panel, with content down inside it. For anything the
195    /// user looks *into*: a table body, a tag tree, a text field.
196    Well,
197    /// Set back from what it sits on, by colour alone. No edge.
198    ///
199    /// The one member carrying a fill without a bevel, so a renderer cannot
200    /// assume the two arrive together. That is deliberate and it is still the
201    /// pairing rule: both halves come off the same `Depth`, so they cannot
202    /// disagree, and here one half is legitimately absent.
203    ///
204    /// Distinct from [`Depth::Flat`], which has no fill either and inherits.
205    /// Recessed and level-with are different claims, and only one of them
206    /// needs a colour.
207    Sunken,
208    /// A surface sitting *over* the page rather than in it. A modal, a popover,
209    /// a menu.
210    ///
211    /// Takes elevation and no bevel: a surface overlaying the page is lifted
212    /// off it, and a surface in the page is cut into it. That is the same
213    /// pairing rule the rest of the enum holds, applied to the one case where
214    /// the separation is not an edge at all, the lift and the scrim behind it
215    /// are already saying where the surface is.
216    ///
217    /// Every renderer already has the surface: `makeover-tui` carries
218    /// `Palette::overlay`, `makeover-immediate` `Palette::elevation`, and
219    /// `makeover-webview` emits `--elevation-overlay`. This variant is the
220    /// route from a description to any of them, which is why it is one variant
221    /// rather than a feature.
222    Overlay,
223}
224
225impl Depth {
226    /// The edge this depth is drawn with, if it has one.
227    #[must_use]
228    pub const fn bevel(self) -> Option<Bevel> {
229        match self {
230            // Sunken joins Flat here, for the opposite reason: Flat has no edge
231            // because nothing separates it from its surroundings, and Sunken has
232            // none because its colour is already doing the separating.
233            Self::Flat | Self::Sunken => None,
234            // A third reason to have no edge, which is why it gets its own arm
235            // rather than joining the two above: an overlay is separated by the
236            // lift and by the scrim behind it, so an edge would be a second
237            // answer to a question already answered.
238            Self::Overlay => None,
239            Self::Raised => Some(Bevel::Raised),
240            Self::Well => Some(Bevel::Inset),
241        }
242    }
243
244    /// The surface this depth is filled with.
245    ///
246    /// [`Depth::Flat`] has no fill of its own: it inherits whatever it sits on,
247    /// which is the difference between level-with and painted-the-same-colour.
248    #[must_use]
249    pub const fn fill(self) -> Option<Fill> {
250        match self {
251            Self::Flat => None,
252            Self::Raised => Some(Fill::Raised),
253            Self::Well => Some(Fill::Well),
254            Self::Sunken => Some(Fill::Sunken),
255            Self::Overlay => Some(Fill::Overlay),
256        }
257    }
258
259    /// Pressing a raised region reads as a well, and nothing else moves.
260    ///
261    /// [`Depth::Overlay`] is untouched along with the rest: an overlay is a
262    /// surface, not a control, so there is nothing there to press.
263    #[must_use]
264    pub const fn pressed(self) -> Self {
265        match self {
266            Self::Raised => Self::Well,
267            other => other,
268        }
269    }
270}
271
272/// An interaction state a region can be in, beside whatever [`Depth`] it is.
273///
274/// Orthogonal to depth on purpose. A disabled button is still [`Depth::Raised`]
275/// and a disabled field is still a [`Depth::Well`], so folding either member
276/// into `Depth` would make [`Depth::bevel`] and [`Depth::fill`] answer for
277/// something that is not a depth, and would leave disabled-button and
278/// disabled-field sharing one variant that cannot tell them apart.
279///
280/// # Why hover and pressed are not members
281///
282/// The line is whether every renderer has the state to express, not whether CSS
283/// does. Hover is renderer policy and `makeover-webview` says so in its own
284/// header: a terminal and an immediate-mode painter have no pointer hovering
285/// over anything, and pressed already arrives through [`Bevel::pressed`] and
286/// [`Depth::pressed`], where it belongs, because pressing is a depth inversion
287/// rather than a separate condition.
288///
289/// Focus and disabled are different in kind. A TUI has a focused widget and a
290/// greyed-out one; so does egui. Both were unsayable here, so all three webview
291/// consumers supplied them from outside the primitive by out-specifying rules
292/// they did not own: goingson alone carries 19 of them, and the MNW server
293/// another 21. That is the divergence this crate exists to end, arriving one
294/// layer down.
295///
296/// # The principle this encodes
297///
298/// A primitive owns every state it implies. A renderer that emits a hover rule
299/// for a thing owes disabled and the capability answer for that same thing,
300/// because anything less exports the completion work to N consumers who will
301/// each do it differently.
302///
303/// Focus is not on that list and is not on this axis. It is the renderer's,
304/// decided after the description; see the crate header, "Reach,
305/// focus and the focus ring", for the three terms and who owns each.
306///
307/// `#[non_exhaustive]` for the reason [`Fill`] and [`Depth`] carry it: growth
308/// must not be a lockstep event across the three renderers.
309#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
310#[non_exhaustive]
311pub enum State {
312    /// Present, visible, and not answering.
313    ///
314    /// Not the same as absent, and deliberately not a [`Fill`]: a disabled
315    /// control keeps the surface it always had and stops responding, so what
316    /// changes is its content and its interactivity rather than what it is.
317    Disabled,
318}
319
320impl State {
321    /// Whether a region in this state stops answering the pointer.
322    ///
323    /// Stated in the description rather than left to each renderer, on the same
324    /// reasoning as [`Bevel::pressed`]: a cascade carries it for free and an
325    /// immediate-mode renderer resolves it per call site, so leaving it unsaid
326    /// means resolving it once per consumer and disagreeing.
327    #[must_use]
328    pub const fn suppresses_interaction(self) -> bool {
329        // A match rather than a bare `true`, so a member added to this
330        // `#[non_exhaustive]` axis has to answer the question rather than
331        // inheriting an answer.
332        match self {
333            Self::Disabled => true,
334        }
335    }
336}
337
338impl Intent for State {
339    fn token(self) -> &'static str {
340        match self {
341            // Reusing the muted content intent rather than minting a
342            // `disabled` colour. Disabled is a reduction and not a status, and
343            // `makeover-webview`'s progress rules already record the reading
344            // that `content-muted` is what disabled looks like.
345            Self::Disabled => "content-muted",
346        }
347    }
348}