fc-ui 0.9.7

A comprehensive, professional UI component library for GPUI inspired by shadcn/ui. 85+ polished components for building beautiful desktop applications.
Documentation
// This module is deprecated as a whole (see the type-level attributes);
// silence deprecation warnings for its internal cross-references.
#![allow(deprecated)]

use gpui::*;
use std::time::Duration;

use crate::capabilities::motion::animations::easings;

#[derive(Clone)]
#[deprecated(
    note = "unused within the crate and examples; superseded by gpui::Animation + motion::animations helpers, will be removed in 0.10"
)]
pub struct AnimationPreset {
    duration: Duration,
    easing: fn(f32) -> f32,
    delay: Duration,
}

impl AnimationPreset {
    pub fn new(duration: Duration, easing: fn(f32) -> f32) -> Self {
        Self {
            duration,
            easing,
            delay: Duration::ZERO,
        }
    }

    /// Set the duration of the animation.
    #[deprecated(note = "use with_duration; will be removed in 0.10")]
    pub fn duration(self, duration: Duration) -> Self {
        self.with_duration(duration)
    }

    /// Set the easing function used by the animation.
    #[deprecated(note = "use with_easing; will be removed in 0.10")]
    pub fn easing(self, easing: fn(f32) -> f32) -> Self {
        self.with_easing(easing)
    }

    /// Set a start delay. The animation holds its initial value for the delay
    /// before starting (see [`AnimationPreset::to_animation`]).
    #[deprecated(note = "use with_delay; will be removed in 0.10")]
    pub fn delay(self, delay: Duration) -> Self {
        self.with_delay(delay)
    }

    /// Set a start delay. The animation holds its initial value for the delay
    /// before starting (see [`AnimationPreset::to_animation`]).
    pub fn with_delay(mut self, delay: Duration) -> Self {
        self.delay = delay;
        self
    }

    pub fn with_duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    pub fn with_easing(mut self, easing: fn(f32) -> f32) -> Self {
        self.easing = easing;
        self
    }

    pub fn get_duration(&self) -> Duration {
        self.duration
    }

    pub fn get_easing(&self) -> fn(f32) -> f32 {
        self.easing
    }

    /// Returns the start delay applied by [`AnimationPreset::to_animation`].
    pub fn get_delay(&self) -> Duration {
        self.delay
    }

    pub fn to_animation(&self) -> Animation {
        Animation::new(self.duration)
            .with_easing(self.easing)
            .with_delay(self.delay)
    }
}

pub fn fade_in() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(200), easings::ease_out_cubic)
}

pub fn fade_out() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(200), easings::ease_in_cubic)
}

pub fn slide_up() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
}

pub fn slide_down() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
}

pub fn scale_in() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(200), easings::ease_out_back)
}

pub fn bounce_in() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(400), easings::elastic)
}

pub fn slide_in_left() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
}

pub fn slide_in_right() -> AnimationPreset {
    AnimationPreset::new(Duration::from_millis(250), easings::ease_out_cubic)
}

#[derive(Clone, Debug, PartialEq)]
#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
pub enum AnimationRepeat {
    Once,
    /// Finite repeat count. A count of zero is treated as one pass.
    Count(u32),
    Infinite,
}

#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
pub struct KeyframeAnimation {
    id: SharedString,
    keyframes: Vec<(f32, f32)>,
    duration: Duration,
    repeat: AnimationRepeat,
    easing: fn(f32) -> f32,
}

impl KeyframeAnimation {
    pub fn new(id: impl Into<SharedString>) -> Self {
        Self {
            id: id.into(),
            keyframes: vec![(0.0, 0.0), (1.0, 1.0)],
            duration: Duration::from_millis(300),
            repeat: AnimationRepeat::Once,
            easing: easings::linear,
        }
    }

    /// Add a finite keyframe, ignoring non-finite positions or values.
    pub fn at(mut self, pct: f32, value: f32) -> Self {
        if !pct.is_finite() || !value.is_finite() {
            return self;
        }

        let pct = pct.clamp(0.0, 1.0);
        if let Some(pos) = self
            .keyframes
            .iter()
            .position(|(p, _)| (*p - pct).abs() < f32::EPSILON)
        {
            self.keyframes[pos] = (pct, value);
        } else {
            self.keyframes.push((pct, value));
            self.keyframes
                .sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap());
        }
        self
    }

    pub fn duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    pub fn repeat(mut self, repeat: AnimationRepeat) -> Self {
        self.repeat = repeat;
        self
    }

    pub fn easing(mut self, easing: fn(f32) -> f32) -> Self {
        self.easing = easing;
        self
    }

    pub fn id(&self) -> &SharedString {
        &self.id
    }

    pub fn get_duration(&self) -> Duration {
        self.duration
    }

    pub fn get_repeat(&self) -> &AnimationRepeat {
        &self.repeat
    }

    pub fn interpolate(&self, progress: f32) -> f32 {
        let progress = progress.clamp(0.0, 1.0);

        if self.keyframes.len() < 2 {
            return if self.keyframes.is_empty() {
                0.0
            } else {
                self.keyframes[0].1
            };
        }

        let eased = (self.easing)(progress);

        let mut prev = &self.keyframes[0];
        for kf in &self.keyframes[1..] {
            if eased <= kf.0 {
                let range = kf.0 - prev.0;
                if range <= f32::EPSILON {
                    return kf.1;
                }
                let local_t = (eased - prev.0) / range;
                return prev.1 + (kf.1 - prev.1) * local_t;
            }
            prev = kf;
        }

        self.keyframes.last().map(|kf| kf.1).unwrap_or(1.0)
    }

    pub fn to_animation(&self) -> Animation {
        let anim = Animation::new(self.duration).with_easing(self.easing);
        match &self.repeat {
            AnimationRepeat::Once => anim,
            AnimationRepeat::Infinite => anim.repeat(),
            AnimationRepeat::Count(n) => anim.repeat_n(*n),
        }
    }
}

#[derive(Clone)]
#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
pub struct StaggerConfig {
    delay_per_child: Duration,
    preset: AnimationPreset,
}

impl StaggerConfig {
    pub fn new() -> Self {
        Self {
            delay_per_child: Duration::from_millis(50),
            preset: fade_in(),
        }
    }

    pub fn delay_per_child(mut self, delay: Duration) -> Self {
        self.delay_per_child = delay;
        self
    }

    pub fn animation(mut self, preset: AnimationPreset) -> Self {
        self.preset = preset;
        self
    }

    pub fn delay_for_index(&self, index: usize) -> Duration {
        self.preset.delay + self.delay_per_child * index as u32
    }

    pub fn preset_for_index(&self, index: usize) -> AnimationPreset {
        let mut preset = self.preset.clone();
        preset.delay = self.delay_for_index(index);
        preset
    }

    pub fn get_preset(&self) -> &AnimationPreset {
        &self.preset
    }
}

impl Default for StaggerConfig {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    #![allow(deprecated)]
    use super::{easings, AnimationPreset, KeyframeAnimation};
    use std::time::Duration;

    #[test]
    fn deprecated_animation_preset_builders_remain_compatible() {
        let preset = AnimationPreset::new(Duration::from_millis(100), easings::linear)
            .duration(Duration::from_millis(200))
            .easing(easings::ease_in_cubic)
            .delay(Duration::from_millis(25));

        assert_eq!(preset.get_duration(), Duration::from_millis(200));
        assert_eq!(preset.get_delay(), Duration::from_millis(25));
    }

    #[test]
    fn non_finite_keyframes_are_ignored() {
        let animation = KeyframeAnimation::new("finite-keyframes")
            .at(f32::NAN, 0.5)
            .at(0.5, f32::INFINITY);

        assert_eq!(animation.keyframes, vec![(0.0, 0.0), (1.0, 1.0)]);
    }
}

#[derive(Clone)]
#[deprecated(note = "unused within the crate and examples; will be removed in 0.10")]
pub struct Transition {
    property: SharedString,
    duration: Duration,
    easing: fn(f32) -> f32,
    delay: Duration,
}

impl Transition {
    pub fn new(property: impl Into<SharedString>) -> Self {
        Self {
            property: property.into(),
            duration: Duration::from_millis(200),
            easing: easings::ease_out_cubic,
            delay: Duration::ZERO,
        }
    }

    pub fn duration(mut self, duration: Duration) -> Self {
        self.duration = duration;
        self
    }

    pub fn easing(mut self, easing: fn(f32) -> f32) -> Self {
        self.easing = easing;
        self
    }

    /// Set a start delay. The animation holds its initial value for the delay
    /// before starting (see [`Transition::to_animation`]).
    pub fn delay(mut self, delay: Duration) -> Self {
        self.delay = delay;
        self
    }

    pub fn get_property(&self) -> &SharedString {
        &self.property
    }

    pub fn get_duration(&self) -> Duration {
        self.duration
    }

    pub fn get_easing(&self) -> fn(f32) -> f32 {
        self.easing
    }

    /// Returns the start delay applied by [`Transition::to_animation`].
    pub fn get_delay(&self) -> Duration {
        self.delay
    }

    pub fn to_animation(&self) -> Animation {
        Animation::new(self.duration)
            .with_easing(self.easing)
            .with_delay(self.delay)
    }
}