use crate::{Icon, IconName, Sizable, Size}; use gpui::{ Animation, AnimationExt as _, App, Hsla, IntoElement, ParentElement, RenderOnce, Styled as _, Transformation, Window, div, ease_in_out, percentage, prelude::FluentBuilder as _, }; use instant::Duration; /// A cycling loading spinner. #[derive(IntoElement)] pub struct Spinner { size: Size, icon: Icon, speed: Duration, easing: Box f32>, color: Option, } impl Spinner { /// Create a new loading spinner. pub fn new() -> Self { Self { size: Size::Medium, speed: Duration::from_secs_f64(0.8), easing: Box::new(ease_in_out), icon: Icon::new(IconName::Loader), color: None, } } /// Set specified icon for the spinner. /// /// Default is [`IconName::Loader`]. /// /// Please ensure the icon used is suitable for a loading spinner. pub fn icon(mut self, icon: impl Into) -> Self { self.icon = icon.into(); self } /// Set the icon color. pub fn color(mut self, color: Hsla) -> Self { self.color = Some(color); self } /// Set the easing function. pub fn ease(mut self, easing: impl Fn(f32) -> f32 + 'static) -> Self { self.easing = Box::new(easing); self } } impl Sizable for Spinner { fn with_size(mut self, size: impl Into) -> Self { self.size = size.into(); self } } impl RenderOnce for Spinner { fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement { div() .child( self.icon .with_size(self.size) .when_some(self.color, |this, color| this.text_color(color)) .with_animation( "circle", Animation::new(self.speed).repeat().with_easing(self.easing), |this, delta| this.transform(Transformation::rotate(percentage(delta))), ), ) .into_element() } } #[cfg(test)] mod tests { use super::*; use gpui::{Render, TestAppContext, px, size}; struct SpinnerHost; impl Render for SpinnerHost { fn render(&mut self, _: &mut Window, _: &mut gpui::Context) -> impl IntoElement { Spinner::new() } } #[gpui::test] fn reduced_motion_spinner_is_static_and_requests_no_frame(cx: &mut TestAppContext) { cx.update(|cx| cx.set_reduce_motion(true)); let window = cx.open_window(size(px(100.), px(100.)), |_, _| SpinnerHost); cx.run_until_parked(); assert_eq!( window .update(cx, |_, window, cx| window.simulate_next_frame(cx)) .unwrap(), 0 ); } }