makeover_layout/track.rs
1// Names this module's prose links to, resolved for rustdoc.
2#[allow(unused_imports)]
3use crate::{Region, Share, Tone};
4
5/// What a [`Track`]'s integers count.
6///
7/// `Track::fraction` never needed this -- the arithmetic is the same whatever
8/// the numbers mean -- which is exactly how the ruler came to assume minutes
9/// and print `00:00` over a month. A renderer drawing an axis has to write a
10/// label, and it cannot derive the unit from the numbers.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12#[non_exhaustive]
13pub enum Unit {
14 /// Minutes from the start of a day. A day view.
15 #[default]
16 Minutes,
17 /// Whole days. A month strip, a sprint, a stretch of leave.
18 ///
19 /// A day-granularity axis is a *strip*, not a calendar: one line with
20 /// spans laid along it. What it deliberately does not do is wrap into
21 /// weeks, which is the shape that makes weekday periodicity visible and
22 /// the one job of a month grid that a strip cannot take over. See the
23 /// crate header.
24 Days,
25}
26
27/// A window on an axis, in whatever [`Unit`] its [`Track`] counts.
28///
29/// The axis a [`Track`] draws. Offsets rather than instants, because a
30/// description carrying a `DateTime` would carry a timezone with it and the
31/// vocabulary has no business holding one. The app knows which day or month
32/// this is; the description says how far along it a thing sits.
33///
34/// `to` is exclusive and may exceed the natural period, which is how a span
35/// running past the end is said without a second date: under
36/// [`Unit::Minutes`], `Span::new(1320, 1560)` is 22:00 to 02:00.
37#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
38pub struct Span {
39 from: u16,
40 to: u16,
41}
42
43impl Span {
44 /// Midnight to midnight, the ordinary day.
45 pub const DAY: Self = Self { from: 0, to: 1440 };
46
47 /// A span, clamped to a sane one.
48 ///
49 /// An empty or backwards span is a caller bug that should not cost a
50 /// renderer a division by zero, so `to` is forced at least one minute past
51 /// `from` rather than returning an error nobody can act on. Same reasoning
52 /// as [`Share::percent`], which clamps rather than refuses. `from` is held
53 /// one short of `u16::MAX` so there is always a minute after it.
54 #[must_use]
55 pub const fn new(from: u16, to: u16) -> Self {
56 let from = if from == u16::MAX { u16::MAX - 1 } else { from };
57 Self {
58 from,
59 to: if to > from { to } else { from + 1 },
60 }
61 }
62
63 /// The first minute on the axis.
64 #[must_use]
65 pub const fn from(self) -> u16 {
66 self.from
67 }
68
69 /// One past the last minute on the axis.
70 #[must_use]
71 pub const fn to(self) -> u16 {
72 self.to
73 }
74
75 /// How much the axis covers, in its track's unit. Never zero.
76 #[must_use]
77 pub const fn length(self) -> u16 {
78 self.to - self.from
79 }
80
81 /// Whether an offset falls on this axis.
82 #[must_use]
83 pub const fn holds(self, minute: u16) -> bool {
84 minute >= self.from && minute < self.to
85 }
86}
87
88impl Default for Span {
89 fn default() -> Self {
90 Self::DAY
91 }
92}
93
94/// Where a thing sits on a [`Track`], and for how long.
95///
96/// The one fact a list cannot carry and the whole reason this primitive exists.
97/// A list says what order things come in; a track says a thing starts 135
98/// minutes along and lasts 45, which is a different claim and not derivable
99/// from the first.
100#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
101pub struct Placement {
102 at: u16,
103 length: u16,
104}
105
106impl Placement {
107 /// A placement, clamped to a drawable one.
108 ///
109 /// Zero length becomes one for the same reason [`Span::new`] clamps: a
110 /// zero-height thing is invisible rather than expressive, and every
111 /// renderer would need its own guard.
112 #[must_use]
113 pub const fn new(at: u16, length: u16) -> Self {
114 // Held inside the axis, so `end` cannot pass `u16::MAX` and two
115 // placements at the far end still compare as overlapping.
116 let at = if at == u16::MAX { u16::MAX - 1 } else { at };
117 let room = u16::MAX - at;
118 let length = if length == 0 {
119 1
120 } else if length > room {
121 room
122 } else {
123 length
124 };
125 Self { at, length }
126 }
127
128 /// Offset from the axis origin, matching [`Span`]'s.
129 #[must_use]
130 pub const fn at(self) -> u16 {
131 self.at
132 }
133
134 /// How long it lasts, in its track's unit. Never zero.
135 #[must_use]
136 pub const fn length(self) -> u16 {
137 self.length
138 }
139
140 /// One past its last minute.
141 #[must_use]
142 pub const fn end(self) -> u16 {
143 self.at + self.length
144 }
145
146 /// Whether two placements cover any of the same time.
147 ///
148 /// Geometry, and deliberately not a described field. Whether an overlap is
149 /// a *conflict* is the app's judgment -- a meeting inside a block of free
150 /// time overlaps and is fine -- and that judgment travels the way every
151 /// other judgment does, as a [`Tone`] on the thing itself. What a renderer
152 /// needs in order to lay two things side by side instead of on top of each
153 /// other is this, and it can compute it.
154 ///
155 /// The alternative was a `conflicts: bool` on each entry, which is state
156 /// that can disagree with the times beside it. Two sources for one fact is
157 /// how a screen starts rendering a conflict badge on a thing that no longer
158 /// conflicts.
159 #[must_use]
160 pub const fn overlaps(self, other: Self) -> bool {
161 self.at < other.end() && other.at < self.end()
162 }
163}
164
165/// A time axis: things placed by when they happen, rather than flowed.
166///
167/// # Why this is a primitive
168///
169/// The argument against naming a timeline is that a description expressive
170/// enough to draw one is a component library wearing a description's name. It
171/// does not hold here, and being precise about why matters, because the
172/// reasoning applies to real cases.
173///
174/// What a timeline needs that a [`List`](Region::Pane) does not is **one**
175/// thing: placement. Where a thing sits is a fact about the thing, the way a
176/// row's primary text is, and it is not derivable from order. Everything else a
177/// day view draws -- the labels, the gridlines, the item bodies, the tones --
178/// is furniture this vocabulary already names. Measured against goingson's
179/// `day-planning-render.js`, the only members it needed and could not get were
180/// `at` and `minutes`.
181///
182/// So the timeline was never a component library's worth of vocabulary. It was
183/// two integers, and the refusal was priced as though it were the whole widget.
184/// The test that matters is not "does this shape look complicated" but "how
185/// many members does it actually add, and are they facts or presentation".
186/// Slot heights, gridline colour, how overlaps stack and which hour scrolls
187/// into view on open are all presentation and all stay the renderer's, which is
188/// why they are absent here.
189///
190/// # What it does not carry
191///
192/// No pixel measure, no scroll offset, no drag affordance. A renderer draws the
193/// span at whatever density its host uses; `makeover-geometry` owns that the
194/// way it owns everything else measured in pixels.
195#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
196pub struct Track {
197 /// The window the axis covers.
198 pub span: Span,
199 /// The granularity a thing can be placed on, in minutes.
200 ///
201 /// goingson's day view is 15, giving 96 slots across a day. A renderer uses
202 /// it to decide where gridlines fall and what a drop lands on; it does not
203 /// constrain [`Placement`], because data arriving from a calendar does not
204 /// respect anyone's grid.
205 pub slot: u16,
206 /// How often the axis labels itself, in its own unit.
207 ///
208 /// 60 gives an hourly ruler over a 15-minute grid, which is the common
209 /// shape and the reason this is separate from `slot`. Zero means an
210 /// unlabelled axis.
211 pub tick: u16,
212 /// What `span`, `slot`, `tick` and every [`Placement`] on it count.
213 ///
214 /// The one field here a renderer cannot derive, and the reason it exists:
215 /// [`fraction`](Self::fraction) is unit-agnostic, so a day-granularity
216 /// track produced correct geometry under an hours-and-minutes ruler until
217 /// this was added. Geometry never needed it; a label always did.
218 pub unit: Unit,
219}
220
221impl Track {
222 /// An ordinary day: midnight to midnight, quarter-hour slots, hourly ticks.
223 pub const DAY: Self = Self {
224 span: Span::DAY,
225 slot: 15,
226 tick: 60,
227 unit: Unit::Minutes,
228 };
229
230 /// A track over `span`, with the day's usual granularity.
231 #[must_use]
232 pub const fn over(span: Span) -> Self {
233 Self {
234 span,
235 slot: 15,
236 tick: 60,
237 unit: Unit::Minutes,
238 }
239 }
240
241 /// A strip of whole days: one slot a day, a label a week.
242 ///
243 /// The shape a stretch of leave or a sprint is drawn on. Not a calendar --
244 /// it does not wrap into weeks, and the crate header says why that
245 /// distinction is the whole of what a month grid still has over this.
246 #[must_use]
247 pub const fn days(span: Span) -> Self {
248 Self {
249 span,
250 slot: 1,
251 tick: 7,
252 unit: Unit::Days,
253 }
254 }
255
256 /// How many slots the axis holds.
257 ///
258 /// Rounded up, so a span that does not divide evenly by `slot` still has a
259 /// slot covering its tail rather than dropping it. Never zero: `slot` of 0
260 /// reads as one slot spanning the whole axis rather than a division by
261 /// zero, since a renderer asking this question has already committed to
262 /// drawing something.
263 #[must_use]
264 pub const fn slots(self) -> u16 {
265 if self.slot == 0 {
266 1
267 } else {
268 self.span.length().div_ceil(self.slot)
269 }
270 }
271
272 /// Where a placement sits on the axis, as a fraction from 0.0 to 1.0.
273 ///
274 /// The one calculation every renderer would otherwise write itself, and the
275 /// place the three would drift apart. Clamped, so a placement outside the
276 /// span draws at the edge rather than off it -- an event running past
277 /// midnight is a real thing and truncating it is better than either
278 /// panicking or drawing it somewhere impossible.
279 #[must_use]
280 pub fn fraction(self, minute: u16) -> f32 {
281 let span = f32::from(self.span.length());
282 let offset = f32::from(minute.saturating_sub(self.span.from()));
283 (offset / span).clamp(0.0, 1.0)
284 }
285}
286
287impl Default for Track {
288 fn default() -> Self {
289 Self::DAY
290 }
291}