1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
//! A skeleton: a placeholder in the shape of what is loading.
use std::{sync::Arc, time::Duration};
use super::{
names,
part_style::Length,
recipe::{Case, Name, Recipe, ResolvedPart, ResolvedRecipe},
theme::{Themed, current_theme},
};
use crate::{
animation::{AnimatableProperty, AnimatableValue, Animation, Easing, Keyframes, KeyframesId},
app::theme::{PaletteColor, RadiusSize, Theme},
components::{Column, Row, Stack},
layout::StackAlignment,
node::{BackgroundColor, Element, Gradient, GradientStop, dimension::Dimension},
};
/// The keyframes of a skeleton that pulses, which every window has: whole,
/// then half, then whole again.
pub const SKELETON_PULSE: KeyframesId = KeyframesId::new(0xFF33);
/// The keyframes of the sheen of a skeleton that shimmers, which every window
/// has: the width of what is before the sheen in a row three times the
/// skeleton's width, from nothing to two thirds of it. The sheen, a third of
/// the row and as wide as the skeleton, goes from wholly before the
/// skeleton's start to wholly past its end.
pub const SKELETON_SHIMMER: KeyframesId = KeyframesId::new(0xFF34);
/// How long a skeleton takes to pulse once.
pub const SKELETON_PULSE_PERIOD: Duration = Duration::from_millis(2000);
/// How long the sheen of a skeleton takes to cross it once, whatever its width.
pub const SKELETON_SHIMMER_PERIOD: Duration = Duration::from_millis(2000);
/// How wide the row the sheen crosses in is, in shares of the skeleton: from one width before it to one past it.
const SWEEP: f32 = 300.0;
/// How long the lines of a paragraph are before its last: all of the width, and the one before the last a
/// little less; the last is shorter still. The design's 300, 300, 276 and 180.
const NEXT_TO_LAST: f32 = 92.0;
const LAST: f32 = 60.0;
/// The keyframes of a skeleton: its pulse and its sheen. They play in phase, so every skeleton on screen
/// pulses as one, and the sheen crosses every one at once.
pub(crate) fn keyframes() -> [Keyframes; 2] {
let pulse = Keyframes::new(SKELETON_PULSE)
.frame(0.0, |frame| {
frame.set(AnimatableProperty::Opacity, 1.0);
})
.frame(0.5, |frame| {
frame.set(AnimatableProperty::Opacity, 0.5);
})
.frame(1.0, |frame| {
frame.set(AnimatableProperty::Opacity, 1.0);
});
let shimmer = Keyframes::new(SKELETON_SHIMMER)
.frame(0.0, |frame| {
frame.set(AnimatableProperty::Width, AnimatableValue::Percent(0.0));
})
.frame(1.0, |frame| {
frame.set(AnimatableProperty::Width, AnimatableValue::Percent(200.0 / 3.0));
});
[pulse, shimmer]
}
/// The shape of a skeleton.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SkeletonShape {
/// A line of text: as wide as the room it has, 12 high.
#[default]
Line,
/// A face, an icon: round.
Circle,
/// An image, a chart, a card: more rounded than a line.
Block,
/// Lines of text one under another, the last shorter.
Paragraph,
}
names!(SkeletonShape {
Line => "line",
Circle => "circle",
Block => "block",
Paragraph => "paragraph",
});
/// How a skeleton moves while it waits. Where motion is reduced it stands still, whatever it is given.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum SkeletonAnimation {
/// It fades to half and back.
#[default]
Pulse,
/// A lighter sheen crosses it: the design's shimmer.
Shimmer,
/// It stands still.
None,
}
/// The parts of a skeleton a recipe styles.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum SkeletonPart {
/// What holds the shapes: the column of a paragraph's lines, and the box of a single shape.
Root,
/// A shape: a line, the circle, the block. Its fill is where the sheen starts and ends.
Bone,
/// What crosses a shape that shimmers: its width, and its fill, which is the middle of the sheen.
Sheen,
}
names!(SkeletonPart {
Root => "root",
Bone => "bone",
Sheen => "sheen",
});
/// What builds a skeleton's nodes: a theme's in place of the default one.
pub type SkeletonRenderer = Arc<dyn Fn(SkeletonView) -> Element + Send + Sync>;
/// A skeleton: a placeholder in the shape of what is loading.
///
/// ```
/// use lurq::{components::{Column, Row}, ui::{Skeleton, SkeletonAnimation}};
///
/// // A row of a list: a face, and two lines beside it.
/// let row = Row::new()
/// .spacing(12.0)
/// .child(Skeleton::circle(40.0))
/// .child(
/// Column::new()
/// .flex(1.0)
/// .spacing(8.0)
/// .child(Skeleton::line())
/// .child(Skeleton::line().width(140.0).height(10.0)),
/// );
/// let text = Skeleton::paragraph(4).animation(SkeletonAnimation::Shimmer);
/// // What tooling is told: one status around them all.
/// let loading = Skeleton::status("Loading messages", row);
/// ```
///
/// A line and a paragraph are as wide as the room they have, and a circle
/// and a block are the size they are given; any of them takes a width and a
/// height of the app's. A skeleton is nothing to tooling that reads the
/// tree: what is loading is said once, by a status an app puts around a
/// group of them ([`Skeleton::status`]).
#[derive(Clone)]
pub struct Skeleton {
shape: SkeletonShape,
lines: usize,
animation: SkeletonAnimation,
width: Option<Length>,
height: Option<Length>,
states: Vec<Name>,
rules: Recipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl Skeleton {
fn shaped(shape: SkeletonShape) -> Self {
Self {
shape,
lines: 1,
animation: SkeletonAnimation::default(),
width: None,
height: None,
states: Vec::new(),
rules: Recipe::new(),
id: None,
key: None,
}
}
/// A line of text, as wide as the room it has and 12 high.
pub fn line() -> Self {
Self::shaped(SkeletonShape::Line)
}
/// A circle of `size` across: a face, an icon.
pub fn circle(size: impl Into<Length>) -> Self {
let size = size.into();
let mut skeleton = Self::shaped(SkeletonShape::Circle);
skeleton.width = Some(size);
skeleton.height = Some(size);
skeleton
}
/// A block of `width` by `height`: an image, a chart, a card. `Length::Fill` is all the room it has.
pub fn block(width: impl Into<Length>, height: impl Into<Length>) -> Self {
let mut skeleton = Self::shaped(SkeletonShape::Block);
skeleton.width = Some(width.into());
skeleton.height = Some(height.into());
skeleton
}
/// `lines` lines of text one under another, as wide as the room they have; the one before the last is a
/// little shorter, and the last shorter still.
pub fn paragraph(lines: usize) -> Self {
let mut skeleton = Self::shaped(SkeletonShape::Paragraph);
skeleton.lines = lines.max(1);
skeleton
}
/// A status around `content`, called `label`, for tooling that reads the tree: what the skeletons in it
/// stand for, said once.
pub fn status(label: &str, content: impl Into<Element>) -> Column {
Column::new()
.describe("a11y_role", "status")
.describe("a11y_name", label)
.child(content)
}
/// How the skeleton moves while it waits.
pub fn animation(mut self, animation: SkeletonAnimation) -> Self {
self.animation = animation;
self
}
/// How wide the skeleton is: a number of logical pixels, or `Length::Fill`. For a paragraph, the width of
/// all of it.
pub fn width(mut self, width: impl Into<Length>) -> Self {
self.width = Some(width.into());
self
}
/// How high the skeleton is. For a paragraph, the height of each line.
pub fn height(mut self, height: impl Into<Length>) -> Self {
self.height = Some(height.into());
self
}
/// Puts the skeleton in a state the theme's recipe names, or takes it out.
pub fn state(mut self, state: impl Into<Name>, on: bool) -> Self {
let state = state.into();
self.states.retain(|held| *held != state);
if on {
self.states.push(state);
}
self
}
/// Styles one part of this skeleton over what the theme gives it.
pub fn style(mut self, part: impl Into<Name>, style: impl FnOnce(super::PartStyle) -> super::PartStyle) -> Self {
self.rules = self.rules.base(part, style);
self
}
/// Lays rules over the theme's recipe for this skeleton.
pub fn rules(mut self, rules: &Recipe) -> Self {
self.rules = self.rules.extend(rules);
self
}
/// The id of the root.
pub fn id(mut self, id: impl Into<Arc<str>>) -> Self {
self.id = Some(id.into());
self
}
pub fn key(mut self, key: impl Into<Arc<str>>) -> Self {
self.key = Some(key.into());
self
}
/// Builds the skeleton in `theme`. Converting a skeleton into an element
/// does this in the theme of what is being rendered.
pub fn build(self, theme: &Theme) -> Element {
let case = Case {
variant: Some(self.shape.into()),
size: None,
density: None,
breakpoint: super::current_breakpoint(),
states: self.states.clone(),
};
// The app's sizes are over the shape's and under what the app styles.
let mut sizes = Recipe::new();
let paragraph = self.shape == SkeletonShape::Paragraph;
if let Some(width) = self.width {
sizes = sizes.base(SkeletonPart::Root, |s| s.width(width));
if !paragraph {
sizes = sizes.base(SkeletonPart::Bone, |s| s.width(width));
}
}
if let Some(height) = self.height {
sizes = sizes.base(SkeletonPart::Bone, |s| s.height(height));
}
let rules = sizes.extend(&self.rules);
let view = SkeletonView {
shape: self.shape,
lines: self.lines,
// Where motion is reduced, a skeleton stands still.
animation: if theme.reduced_motion() {
SkeletonAnimation::None
} else {
self.animation
},
recipe: theme
.resolve::<Skeleton>(&case, Some(&rules))
.owned_by(self.id.as_ref()),
id: self.id,
key: self.key,
};
match theme.renderer::<Skeleton>() {
Some(renderer) => renderer(view),
None => view.render(),
}
}
}
impl From<Skeleton> for Element {
fn from(skeleton: Skeleton) -> Self {
skeleton.build(¤t_theme())
}
}
/// A skeleton as a renderer sees it.
pub struct SkeletonView {
pub shape: SkeletonShape,
/// How many lines it has: one, but for a paragraph.
pub lines: usize,
/// How it moves: [`SkeletonAnimation::None`] where motion is reduced.
pub animation: SkeletonAnimation,
/// The style of each part of this skeleton.
pub recipe: ResolvedRecipe,
id: Option<Arc<str>>,
key: Option<Arc<str>>,
}
impl SkeletonView {
/// The style of a part, as this skeleton is to be drawn.
pub fn part(&self, part: impl Into<Name>) -> ResolvedPart {
self.recipe.part(part)
}
pub fn theme(&self) -> &Theme {
self.recipe.theme()
}
/// What crosses a shape that shimmers: a band whose fill runs from the shape's to the sheen's and back, as
/// the design draws it, a third of the row it crosses in (see [`Self::sweep`]): as wide as the shape.
pub fn sheen(&self) -> Row {
let (bone, part) = (self.part(SkeletonPart::Bone), self.part(SkeletonPart::Sheen));
let (edge, middle) = (bone.rest().background, part.rest().background);
let mut sheen = Row::new()
.width(Dimension::Pct(100.0 * 100.0 / SWEEP))
.height(Dimension::Pct(100.0))
.part(&part);
if let (Some(edge), Some(middle)) = (edge, middle) {
let stop = |color: &BackgroundColor, at: f32| GradientStop::at(color.clone(), at);
sheen = sheen.background_gradient(Gradient::linear(
90.0,
[stop(&edge, 0.0), stop(&middle, 0.5), stop(&edge, 1.0)],
));
}
sheen
}
/// What a shape that shimmers holds: a row three times its width, centred on it, so from one width before
/// its start to one past its end; in the row, before the sheen, a room whose width runs from nothing to two
/// thirds of the row ([`SKELETON_SHIMMER`]). The sheen goes from wholly before the shape to wholly past it in
/// the same time whatever the shape's width, and the shape clips it.
pub fn sweep(&self) -> Stack {
let full = Dimension::Pct(100.0);
let room = Row::new().width(Dimension::Pct(0.0)).height(full).animation(
Animation::new(SKELETON_SHIMMER)
.duration(SKELETON_SHIMMER_PERIOD)
.easing(Easing::Linear)
.infinite(),
);
// A stack lays a child wider than itself with its middle on its own: the row starts a width before it.
Stack::new().stack_align(StackAlignment::Center).size(full, full).child(
Row::new()
.width(Dimension::Pct(SWEEP))
.height(full)
.child(room)
.child(self.sheen()),
)
}
/// One shape, in the style of its part, moving as the skeleton does.
pub fn bone(&self) -> Row {
let bone = Row::new().part(&self.part(SkeletonPart::Bone));
match self.animation {
SkeletonAnimation::Pulse => bone.animation(
Animation::new(SKELETON_PULSE)
.duration(SKELETON_PULSE_PERIOD)
.easing(Easing::EASE_IN_OUT)
.infinite(),
),
// The bone clips the sheen to its corners.
SkeletonAnimation::Shimmer => bone.child(self.sweep()),
SkeletonAnimation::None => bone,
}
}
/// How the default renderer builds a skeleton: its root with a bone in it, or a paragraph's lines one under
/// another, the next to last and the last shorter. Nothing of it is described, so tooling that reads the
/// tree leaves it out, as it leaves out any box with no text, no id and no handler.
pub fn render(self) -> Element {
let mut root = Column::new().part(&self.part(SkeletonPart::Root));
let lines = if self.shape == SkeletonShape::Paragraph {
self.lines
} else {
1
};
for index in 0..lines {
let mut bone = self.bone();
if lines > 1 && index + 1 == lines {
bone = bone.width(Dimension::Pct(LAST));
} else if lines > 2 && index + 2 == lines {
bone = bone.width(Dimension::Pct(NEXT_TO_LAST));
}
root = root.child(bone);
}
if let Some(id) = &self.id {
root = root.id(id.clone());
}
if let Some(key) = &self.key {
root = root.key(key.clone());
}
root.into()
}
}
impl crate::ui::Styled for Skeleton {
type Component = Self;
fn rules(self, rules: &crate::ui::Recipe) -> Self {
Self::rules(self, rules)
}
fn state(self, state: impl Into<crate::ui::Name>, on: bool) -> Self {
Self::state(self, state, on)
}
}
impl Themed for Skeleton {
const NAME: &'static str = "skeleton";
type Renderer = SkeletonRenderer;
fn parts() -> crate::ui::Declared {
SkeletonPart::declared()
}
/// The design's skeleton (`pencil/lurq-design-system.pen`, Skeleton/Line,
/// Circle, Block, Shimmer, Paragraph): shapes in the muted fill. A line is
/// 12 high and `radius-sm` round, a circle 40 across and round all the
/// way, a block 200 by 112 and `radius-lg` round; a paragraph's lines are
/// 10 apart. The shimmer's sheen is the secondary fill under the pointer
/// between two of the muted fill.
fn recipe() -> Recipe {
use SkeletonPart::{Bone, Root, Sheen};
use SkeletonShape::{Block, Circle, Line, Paragraph};
Recipe::new()
.base(Bone, |s| s.background(PaletteColor::Muted).radius(RadiusSize::Sm))
.base(Sheen, |s| s.background(PaletteColor::SecondaryHover))
.variant(Line, Root, |s| s.width(Length::Fill))
.variant(Line, Bone, |s| s.width(Length::Fill).height(12.0))
.variant(Paragraph, Root, |s| s.width(Length::Fill).gap(10.0))
.variant(Paragraph, Bone, |s| s.width(Length::Fill).height(12.0))
.variant(Circle, Bone, |s| s.square(40.0).radius(RadiusSize::Full))
.variant(Block, Bone, |s| s.width(200.0).height(112.0).radius(RadiusSize::Lg))
}
}