| 1 | //! Calm loading placeholders with motion-gated shimmer. |
| 2 | |
| 3 | use std::time::Duration; |
| 4 | |
| 5 | use ratatui::{ |
| 6 | buffer::Buffer, |
| 7 | layout::Rect, |
| 8 | text::{Line, Span}, |
| 9 | widgets::Widget, |
| 10 | }; |
| 11 | |
| 12 | use crate::{MotionMode, Paint, Role, Theme}; |
| 13 | |
| 14 | /// Cadence of one shimmer step when [`MotionMode::Full`] is active. |
| 15 | pub const SKELETON_FRAME_INTERVAL: Duration = Duration::from_millis(120); |
| 16 | |
| 17 | /// Layout shape of a [`Skeleton`] placeholder. |
| 18 | #[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] |
| 19 | pub enum SkeletonKind { |
| 20 | /// Uniform full-width placeholder rows. |
| 21 | #[default] |
| 22 | Rows, |
| 23 | /// Staggered prose-like line widths with a shorter trailing line. |
| 24 | Text, |
| 25 | } |
| 26 | |
| 27 | /// Calm loading placeholder matching Desktop `packages/ui` `<Skeleton>`. |
| 28 | /// |
| 29 | /// Under [`MotionMode::Full`], a gentle highlight band steps across each bar |
| 30 | /// according to the caller-supplied `tick` (or `elapsed` duration). Under |
| 31 | /// [`MotionMode::Reduced`] and [`MotionMode::Still`], the placeholder stays |
| 32 | /// strictly static. |
| 33 | #[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 34 | pub struct Skeleton { |
| 35 | /// Number of placeholder rows to paint. |
| 36 | pub rows: u16, |
| 37 | /// Visual shape (`Rows` vs `Text`). |
| 38 | pub kind: SkeletonKind, |
| 39 | /// Motion policy governing shimmer animation. |
| 40 | pub motion: MotionMode, |
| 41 | /// Caller-supplied animation tick. |
| 42 | pub tick: u64, |
| 43 | } |
| 44 | |
| 45 | impl Skeleton { |
| 46 | /// Create a block of `rows` uniform loading bars. |
| 47 | #[must_use] |
| 48 | pub const fn rows(rows: u16) -> Self { |
| 49 | Self { |
| 50 | rows, |
| 51 | kind: SkeletonKind::Rows, |
| 52 | motion: MotionMode::Full, |
| 53 | tick: 0, |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | /// Create `lines` staggered text-shaped placeholder lines. |
| 58 | #[must_use] |
| 59 | pub const fn text(lines: u16) -> Self { |
| 60 | Self { |
| 61 | rows: lines, |
| 62 | kind: SkeletonKind::Text, |
| 63 | motion: MotionMode::Full, |
| 64 | tick: 0, |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | /// Set the [`MotionMode`] governing whether the shimmer animates. |
| 69 | #[must_use] |
| 70 | pub const fn motion(mut self, motion: MotionMode) -> Self { |
| 71 | self.motion = motion; |
| 72 | self |
| 73 | } |
| 74 | |
| 75 | /// Set the caller-driven shimmer frame tick. |
| 76 | #[must_use] |
| 77 | pub const fn tick(mut self, tick: u64) -> Self { |
| 78 | self.tick = tick; |
| 79 | self |
| 80 | } |
| 81 | |
| 82 | /// Derive the shimmer tick from an `elapsed` duration. |
| 83 | #[must_use] |
| 84 | pub fn elapsed(mut self, elapsed: Duration) -> Self { |
| 85 | let interval_ms = SKELETON_FRAME_INTERVAL.as_millis(); |
| 86 | self.tick = u64::try_from(elapsed.as_millis() / interval_ms).unwrap_or(u64::MAX); |
| 87 | self |
| 88 | } |
| 89 | |
| 90 | fn line_width(&self, row_index: u16, max_width: u16) -> usize { |
| 91 | let max = usize::from(max_width); |
| 92 | if max == 0 { |
| 93 | return 0; |
| 94 | } |
| 95 | match self.kind { |
| 96 | SkeletonKind::Rows => max, |
| 97 | SkeletonKind::Text => { |
| 98 | const RATIOS: [usize; 4] = [100, 88, 94, 72]; |
| 99 | let pct = if row_index + 1 == self.rows && self.rows > 1 { |
| 100 | 60 |
| 101 | } else { |
| 102 | RATIOS[usize::from(row_index) % RATIOS.len()] |
| 103 | }; |
| 104 | ((max * pct) / 100).max(1).min(max) |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | impl Paint for Skeleton { |
| 111 | fn height(&self, _width: u16, _theme: &Theme) -> u16 { |
| 112 | self.rows |
| 113 | } |
| 114 | |
| 115 | fn paint(&self, area: Rect, buf: &mut Buffer, theme: &Theme) { |
| 116 | let area = area.intersection(buf.area); |
| 117 | if area.is_empty() || self.rows == 0 { |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | let ascii = theme.ascii(); |
| 122 | let base_glyph = if ascii { '-' } else { '░' }; |
| 123 | let shimmer_glyph = if ascii { '=' } else { '▒' }; |
| 124 | let base_style = theme.fg(Role::Dim); |
| 125 | let shimmer_style = theme.fg(Role::Muted); |
| 126 | let animates = self.motion.animates(); |
| 127 | |
| 128 | let count = self.rows.min(area.height); |
| 129 | for row_idx in 0..count { |
| 130 | let width = self.line_width(row_idx, area.width); |
| 131 | if width == 0 { |
| 132 | continue; |
| 133 | } |
| 134 | |
| 135 | let y = area.y + row_idx; |
| 136 | let row_rect = Rect { |
| 137 | x: area.x, |
| 138 | y, |
| 139 | width: area.width, |
| 140 | height: 1, |
| 141 | }; |
| 142 | |
| 143 | if !animates || width < 3 { |
| 144 | let bar: String = std::iter::repeat_n(base_glyph, width).collect(); |
| 145 | Line::from(Span::styled(bar, base_style)).render(row_rect, buf); |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | let band = (width / 5).clamp(2, 5); |
| 150 | let cycle = width + band; |
| 151 | let phase = usize::try_from( |
| 152 | (self.tick.saturating_add(u64::from(row_idx) * 2)) % (cycle as u64), |
| 153 | ) |
| 154 | .unwrap_or(0); |
| 155 | let hi_start = phase.saturating_sub(band); |
| 156 | let hi_end = phase.min(width); |
| 157 | |
| 158 | let mut spans = Vec::with_capacity(3); |
| 159 | if hi_start > 0 { |
| 160 | let left: String = std::iter::repeat_n(base_glyph, hi_start).collect(); |
| 161 | spans.push(Span::styled(left, base_style)); |
| 162 | } |
| 163 | if hi_end > hi_start { |
| 164 | let mid: String = std::iter::repeat_n(shimmer_glyph, hi_end - hi_start).collect(); |
| 165 | spans.push(Span::styled(mid, shimmer_style)); |
| 166 | } |
| 167 | if hi_end < width { |
| 168 | let right: String = std::iter::repeat_n(base_glyph, width - hi_end).collect(); |
| 169 | spans.push(Span::styled(right, base_style)); |
| 170 | } |
| 171 | Line::from(spans).render(row_rect, buf); |
| 172 | } |
| 173 | } |
| 174 | } |
| 175 |