1use gpui::{
2 Animation, AnimationExt as _, App, Bounds, ContentMask, Element, ElementId, GlobalElementId,
3 Hsla, InspectorElementId, IntoElement, LayoutId, LineLayout, ParentElement as _, Pixels, Point,
4 RenderOnce, SharedString, StyleRefinement, Styled, StyledText, TextAlign, Window, WrapBoundary,
5 div, point, px, size,
6};
7use instant::Duration;
8
9use crate::{ActiveTheme as _, Colorize as _, StyledExt as _};
10
11const SHIMMER_LAYER_COUNT: usize = 12;
12const DEFAULT_SHIMMER_SPREAD: f32 = 0.3;
13const MIN_HIGHLIGHT_LIGHTNESS_GAP: f32 = 0.1;
15
16#[derive(Clone, Copy, Debug, PartialEq)]
22pub enum ShimmerSpread {
23 Relative(f32),
25 Absolute(Pixels),
27}
28
29impl Default for ShimmerSpread {
30 fn default() -> Self {
31 Self::Relative(DEFAULT_SHIMMER_SPREAD)
32 }
33}
34
35impl From<f32> for ShimmerSpread {
36 fn from(fraction: f32) -> Self {
37 Self::Relative(fraction)
38 }
39}
40
41impl From<Pixels> for ShimmerSpread {
42 fn from(length: Pixels) -> Self {
43 Self::Absolute(length)
44 }
45}
46
47#[derive(Clone, Copy, Debug)]
53pub struct ShimmerStyle {
54 duration: Duration,
55 highlight_color: Option<Hsla>,
56 spread: ShimmerSpread,
57 reverse: bool,
58 once: bool,
59}
60
61impl ShimmerStyle {
62 pub fn new() -> Self {
64 Self::default()
65 }
66
67 pub fn duration(mut self, duration: Duration) -> Self {
71 self.duration = duration.max(Duration::from_millis(1));
72 self
73 }
74
75 pub fn highlight_color(mut self, color: impl Into<Hsla>) -> Self {
77 self.highlight_color = Some(color.into());
78 self
79 }
80
81 pub fn spread(mut self, spread: impl Into<ShimmerSpread>) -> Self {
88 match spread.into() {
89 ShimmerSpread::Relative(fraction) if fraction.is_finite() => {
90 self.spread = ShimmerSpread::Relative(fraction.clamp(0.05, 1.));
91 }
92 ShimmerSpread::Absolute(length) if length.as_f32().is_finite() => {
93 self.spread = ShimmerSpread::Absolute(length.max(px(1.)));
94 }
95 _ => {}
96 }
97 self
98 }
99
100 pub fn reverse(mut self, reverse: bool) -> Self {
102 self.reverse = reverse;
103 self
104 }
105
106 pub fn once(mut self, once: bool) -> Self {
108 self.once = once;
109 self
110 }
111
112 pub(crate) fn animation(self) -> Animation {
113 loading_animation(self.duration, self.once)
114 }
115}
116
117impl Default for ShimmerStyle {
118 fn default() -> Self {
119 Self {
120 duration: Duration::from_secs(2),
121 highlight_color: None,
122 spread: ShimmerSpread::default(),
123 reverse: false,
124 once: false,
125 }
126 }
127}
128
129#[derive(IntoElement)]
141pub struct ShimmerText {
142 text: SharedString,
143 style: StyleRefinement,
144 shimmer_style: ShimmerStyle,
145 id: Option<ElementId>,
146}
147
148impl ShimmerText {
149 pub fn new(text: impl Into<SharedString>) -> Self {
151 Self {
152 text: text.into(),
153 style: StyleRefinement::default(),
154 shimmer_style: ShimmerStyle::default(),
155 id: None,
156 }
157 }
158
159 pub fn id(mut self, id: impl Into<ElementId>) -> Self {
161 self.id = Some(id.into());
162 self
163 }
164
165 pub fn with_shimmer_style(mut self, style: ShimmerStyle) -> Self {
167 self.shimmer_style = style;
168 self
169 }
170
171 pub fn duration(mut self, duration: Duration) -> Self {
173 self.shimmer_style = self.shimmer_style.duration(duration);
174 self
175 }
176
177 pub fn highlight_color(mut self, color: impl Into<Hsla>) -> Self {
179 self.shimmer_style = self.shimmer_style.highlight_color(color);
180 self
181 }
182
183 pub fn spread(mut self, spread: impl Into<ShimmerSpread>) -> Self {
185 self.shimmer_style = self.shimmer_style.spread(spread);
186 self
187 }
188
189 pub fn reverse(mut self, reverse: bool) -> Self {
191 self.shimmer_style = self.shimmer_style.reverse(reverse);
192 self
193 }
194
195 pub fn once(mut self, once: bool) -> Self {
197 self.shimmer_style = self.shimmer_style.once(once);
198 self
199 }
200}
201
202impl Styled for ShimmerText {
203 fn style(&mut self) -> &mut StyleRefinement {
204 &mut self.style
205 }
206}
207
208impl RenderOnce for ShimmerText {
209 fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
210 let id = self.id.unwrap_or_else(|| self.text.clone().into());
211 let container = div().min_w_0().refine_style(&self.style);
212
213 if cx.reduce_motion() {
214 return container
215 .child(StyledText::new(self.text))
216 .into_any_element();
217 }
218
219 let tokens = cx.theme().semantic_tokens();
220 let reverse = self.shimmer_style.reverse;
221 let shimmer = ShimmerGlyphs {
222 text: StyledText::new(self.text),
223 highlight_color: self.shimmer_style.highlight_color,
224 background: tokens.colors.background,
225 foreground: tokens.colors.foreground,
226 dark: cx.theme().is_dark(),
227 spread: self.shimmer_style.spread,
228 phase: 0.,
229 }
230 .with_animation(
231 id,
232 self.shimmer_style.animation(),
233 move |mut this, phase| {
234 this.phase = if reverse { 1. - phase } else { phase };
235 this
236 },
237 );
238
239 container.child(shimmer).into_any_element()
240 }
241}
242
243struct ShimmerGlyphs {
249 text: StyledText,
250 highlight_color: Option<Hsla>,
251 background: Hsla,
252 foreground: Hsla,
253 dark: bool,
254 spread: ShimmerSpread,
255 phase: f32,
256}
257
258impl ShimmerGlyphs {
259 fn paint_highlight(&self, bounds: Bounds<Pixels>, window: &mut Window, cx: &mut App) {
260 let masks = std::array::from_fn::<_, SHIMMER_LAYER_COUNT, _>(|layer| {
261 shimmer_band_bounds(bounds, self.phase, self.spread, layer)
262 .map(|bounds| ContentMask { bounds })
263 });
264
265 if masks.iter().all(Option::is_none) {
266 return;
267 }
268
269 let color = shimmer_highlight_color(
270 window.text_style().color,
271 self.background,
272 self.foreground,
273 self.dark,
274 self.highlight_color,
275 );
276 let layout = self.text.layout();
277 let line_height = layout.line_height();
278 let text_align = window.text_style().text_align;
279 let mut line_origin = bounds.origin;
280
281 let paint = |window: &mut Window| {
282 for wrapped_line in layout.line_layouts() {
283 let line = &wrapped_line.unwrapped_layout;
284 let baseline_offset = point(
285 px(0.),
286 (line_height - line.ascent - line.descent) / 2. + line.ascent,
287 );
288 let mut wraps = wrapped_line.wrap_boundaries.iter().peekable();
289 let mut glyph_origin = point(
290 shimmer_aligned_origin_x(
291 line_origin,
292 bounds.size.width,
293 px(0.),
294 text_align,
295 line,
296 wraps.peek().copied(),
297 ),
298 line_origin.y,
299 );
300 let mut previous_glyph_position = Point::default();
301
302 for (run_index, run) in line.runs.iter().enumerate() {
303 let glyph_size = cx
304 .text_system()
305 .bounding_box(run.font_id, line.font_size)
306 .size;
307
308 for (glyph_index, glyph) in run.glyphs.iter().enumerate() {
309 glyph_origin.x += glyph.position.x - previous_glyph_position.x;
310
311 if wraps.peek().is_some_and(|wrap| {
312 wrap.run_ix == run_index && wrap.glyph_ix == glyph_index
313 }) {
314 wraps.next();
315 glyph_origin.x = shimmer_aligned_origin_x(
316 line_origin,
317 bounds.size.width,
318 glyph.position.x,
319 text_align,
320 line,
321 wraps.peek().copied(),
322 );
323 glyph_origin.y += line_height;
324 }
325
326 previous_glyph_position = glyph.position;
327
328 if glyph.is_emoji {
329 continue;
330 }
331
332 let glyph_bounds = Bounds::new(glyph_origin, glyph_size);
333 let paint_origin =
334 glyph_origin + baseline_offset + point(px(0.), glyph.position.y);
335
336 for mask in masks.iter().flatten() {
337 if !glyph_bounds.intersects(&mask.bounds) {
338 continue;
339 }
340
341 window.with_content_mask(Some(*mask), |window| {
342 let _ = window.paint_glyph(
343 paint_origin,
344 run.font_id,
345 glyph.id,
346 line.font_size,
347 color,
348 );
349 });
350 }
351 }
352 }
353
354 line_origin.y += wrapped_line.size(line_height).height;
355 }
356 };
357 #[cfg(feature = "gpui-fast")]
359 window.paint_layer(bounds, paint);
360 #[cfg(not(feature = "gpui-fast"))]
361 window.paint_layer(paint);
362 }
363}
364
365impl Element for ShimmerGlyphs {
366 type RequestLayoutState = ();
367 type PrepaintState = ();
368
369 fn id(&self) -> Option<ElementId> {
370 None
371 }
372
373 fn source_location(&self) -> Option<&'static std::panic::Location<'static>> {
374 None
375 }
376
377 fn request_layout(
378 &mut self,
379 global_id: Option<&GlobalElementId>,
380 inspector_id: Option<&InspectorElementId>,
381 window: &mut Window,
382 cx: &mut App,
383 ) -> (LayoutId, Self::RequestLayoutState) {
384 self.text
385 .request_layout(global_id, inspector_id, window, cx)
386 }
387
388 fn prepaint(
389 &mut self,
390 global_id: Option<&GlobalElementId>,
391 inspector_id: Option<&InspectorElementId>,
392 bounds: Bounds<Pixels>,
393 layout: &mut Self::RequestLayoutState,
394 window: &mut Window,
395 cx: &mut App,
396 ) -> Self::PrepaintState {
397 self.text
398 .prepaint(global_id, inspector_id, bounds, layout, window, cx);
399 }
400
401 fn paint(
402 &mut self,
403 global_id: Option<&GlobalElementId>,
404 inspector_id: Option<&InspectorElementId>,
405 bounds: Bounds<Pixels>,
406 layout: &mut Self::RequestLayoutState,
407 prepaint: &mut Self::PrepaintState,
408 window: &mut Window,
409 cx: &mut App,
410 ) {
411 self.text.paint(
412 global_id,
413 inspector_id,
414 bounds,
415 layout,
416 prepaint,
417 window,
418 cx,
419 );
420 self.paint_highlight(bounds, window, cx);
421 }
422}
423
424pub(crate) fn loading_animation(duration: Duration, once: bool) -> Animation {
425 if once {
426 Animation::new(duration)
427 } else {
428 Animation::new(duration).repeat_synced()
429 }
430}
431
432fn shimmer_highlight_color(
433 text: Hsla,
434 background: Hsla,
435 foreground: Hsla,
436 dark: bool,
437 override_color: Option<Hsla>,
438) -> Hsla {
439 let highlight = override_color.unwrap_or_else(|| {
440 let (target, opposite) = if dark {
441 (foreground, background)
442 } else {
443 (background, foreground)
444 };
445 let target = if (target.l - text.l).abs() < MIN_HIGHLIGHT_LIGHTNESS_GAP {
448 opposite
449 } else {
450 target
451 };
452 text.mix_oklab(target, 0.2)
453 });
454 let peak_opacity: f32 = if dark { 0.6 } else { 0.75 };
455 let layer_opacity = 1. - (1. - peak_opacity).powf(1. / SHIMMER_LAYER_COUNT as f32);
456
457 highlight.opacity(layer_opacity)
458}
459
460fn shimmer_band_bounds(
461 bounds: Bounds<Pixels>,
462 phase: f32,
463 spread: ShimmerSpread,
464 layer: usize,
465) -> Option<Bounds<Pixels>> {
466 let width = bounds.size.width.as_f32();
467
468 if width <= 0. || bounds.size.height <= px(0.) || layer >= SHIMMER_LAYER_COUNT {
469 return None;
470 }
471
472 let half_width = match spread {
473 ShimmerSpread::Relative(fraction) => width * fraction,
474 ShimmerSpread::Absolute(length) => length.as_f32(),
475 };
476 let padding = half_width / width + 0.05;
477 let center = phase.mul_add(1. + padding * 2., -padding) * width;
478 let radius = half_width * (1. - layer as f32 / SHIMMER_LAYER_COUNT as f32);
479 let left = (center - radius).max(0.);
480 let right = (center + radius).min(width);
481
482 (right > left).then(|| {
483 Bounds::new(
484 point(bounds.origin.x + px(left), bounds.origin.y),
485 size(px(right - left), bounds.size.height),
486 )
487 })
488}
489
490fn shimmer_aligned_origin_x(
491 origin: Point<Pixels>,
492 align_width: Pixels,
493 previous_glyph_x: Pixels,
494 align: TextAlign,
495 layout: &LineLayout,
496 next_wrap: Option<&WrapBoundary>,
497) -> Pixels {
498 let line_end = next_wrap
499 .map(|wrap| layout.runs[wrap.run_ix].glyphs[wrap.glyph_ix].position.x)
500 .unwrap_or(layout.width);
501 let line_width = line_end - previous_glyph_x;
502
503 match align {
504 TextAlign::Left => origin.x,
505 TextAlign::Center => (origin.x * 2. + align_width - line_width) / 2.,
506 TextAlign::Right => origin.x + align_width - line_width,
507 }
508}
509
510#[cfg(test)]
511mod tests {
512 use super::*;
513
514 #[test]
515 fn test_shimmer_builder() {
516 let color = Hsla::white();
517 let style = ShimmerStyle::new()
518 .duration(Duration::from_secs(3))
519 .highlight_color(color)
520 .spread(0.45)
521 .reverse(true)
522 .once(true);
523
524 assert_eq!(style.duration, Duration::from_secs(3));
525 assert_eq!(style.highlight_color, Some(color));
526 assert_eq!(style.spread, ShimmerSpread::Relative(0.45));
527 assert!(style.reverse);
528 assert!(style.once);
529
530 let text = ShimmerText::new("Thinking")
531 .id("thinking")
532 .with_shimmer_style(style)
533 .duration(Duration::from_secs(4))
534 .spread(0.5)
535 .reverse(false)
536 .once(false)
537 .opacity(0.8);
538
539 assert_eq!(text.text.as_ref(), "Thinking");
540 assert_eq!(text.shimmer_style.duration, Duration::from_secs(4));
541 assert_eq!(text.shimmer_style.spread, ShimmerSpread::Relative(0.5));
542 assert!(!text.shimmer_style.reverse);
543 assert!(!text.shimmer_style.once);
544 assert_eq!(text.style.opacity, Some(0.8));
545 assert_eq!(text.id, Some("thinking".into()));
546
547 assert_eq!(
548 ShimmerStyle::new().spread(0.).spread,
549 ShimmerSpread::Relative(0.05)
550 );
551 assert_eq!(
552 ShimmerStyle::new().spread(2.).spread,
553 ShimmerSpread::Relative(1.)
554 );
555 assert_eq!(
556 ShimmerStyle::new().spread(f32::NAN).spread,
557 ShimmerSpread::default()
558 );
559 assert_eq!(
560 ShimmerStyle::new().spread(px(0.)).spread,
561 ShimmerSpread::Absolute(px(1.))
562 );
563 assert_eq!(
564 ShimmerStyle::new().spread(px(48.)).spread,
565 ShimmerSpread::Absolute(px(48.))
566 );
567 assert_eq!(
568 ShimmerStyle::new().spread(px(f32::NAN)).spread,
569 ShimmerSpread::default()
570 );
571 assert_eq!(
572 ShimmerStyle::new().duration(Duration::ZERO).duration,
573 Duration::from_millis(1)
574 );
575 }
576
577 #[test]
578 fn test_shimmer_band_moves_smoothly_across_text() {
579 let bounds = Bounds::new(point(px(10.), px(20.)), size(px(100.), px(18.)));
580 let spread = ShimmerSpread::default();
581
582 assert!(shimmer_band_bounds(bounds, 0., spread, 0).is_none());
583 assert!(shimmer_band_bounds(bounds, 1., spread, 0).is_none());
584
585 let early = shimmer_band_bounds(bounds, 0.35, spread, 0).unwrap();
586 let late = shimmer_band_bounds(bounds, 0.65, spread, 0).unwrap();
587 assert!(early.origin.x < late.origin.x);
588
589 let outer = shimmer_band_bounds(bounds, 0.5, spread, 0).unwrap();
590 let inner = shimmer_band_bounds(bounds, 0.5, spread, SHIMMER_LAYER_COUNT - 1).unwrap();
591 assert!(inner.origin.x > outer.origin.x);
592 assert!(inner.size.width < outer.size.width);
593 assert!(shimmer_band_bounds(bounds, 0.5, spread, SHIMMER_LAYER_COUNT).is_none());
594 assert!(
595 shimmer_band_bounds(
596 Bounds::new(bounds.origin, size(px(0.), px(18.))),
597 0.5,
598 spread,
599 0
600 )
601 .is_none()
602 );
603
604 let narrow = shimmer_band_bounds(bounds, 0.5, ShimmerSpread::Relative(0.1), 0).unwrap();
605 let wide = shimmer_band_bounds(bounds, 0.5, ShimmerSpread::Relative(0.5), 0).unwrap();
606 assert!(narrow.size.width < wide.size.width);
607
608 let absolute = ShimmerSpread::Absolute(px(20.));
610 let band = shimmer_band_bounds(bounds, 0.5, absolute, 0).unwrap();
611 assert_eq!(band.size.width, px(40.));
612 let wider_bounds = Bounds::new(bounds.origin, size(px(200.), px(18.)));
613 let wider_band = shimmer_band_bounds(wider_bounds, 0.5, absolute, 0).unwrap();
614 assert_eq!(wider_band.size.width, px(40.));
615 }
616
617 #[test]
618 fn test_shimmer_highlight_stays_bright_in_both_themes() {
619 let black = Hsla::black();
620 let white = Hsla::white();
621 let muted = white.mix_oklab(black, 0.55);
622 let light = shimmer_highlight_color(black, white, black, false, None);
623 let dark = shimmer_highlight_color(muted, black, white, true, None);
624
625 assert!(light.l > black.l);
626 assert!(dark.l > muted.l);
627 assert!(light.a > dark.a);
628 assert!((1. - (1. - light.a).powi(SHIMMER_LAYER_COUNT as i32) - 0.75).abs() < 0.001);
629 assert!((1. - (1. - dark.a).powi(SHIMMER_LAYER_COUNT as i32) - 0.6).abs() < 0.001);
630
631 let on_dark_foreground = shimmer_highlight_color(white, black, white, true, None);
634 assert!(white.l - on_dark_foreground.l > 0.3);
635 let on_light_background = shimmer_highlight_color(white, white, black, false, None);
636 assert!(white.l - on_light_background.l > 0.3);
637
638 let custom = shimmer_highlight_color(black, white, black, false, Some(muted));
639 assert_eq!(custom.h, muted.h);
640 assert_eq!(custom.s, muted.s);
641 assert_eq!(custom.l, muted.l);
642
643 let animation = loading_animation(Duration::from_secs(3), false);
644 assert_eq!(animation.duration, Duration::from_secs(3));
645 assert!(animation.synced);
646 assert!(!animation.oneshot);
647 assert_eq!(animation.max_fps, None);
648
649 let animation = loading_animation(Duration::from_secs(3), true);
650 assert_eq!(animation.duration, Duration::from_secs(3));
651 assert!(animation.oneshot);
652 assert!(!animation.synced);
653 }
654}