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
use gpui::*;
use std::time::Duration;

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

#[derive(IntoElement)]
pub struct ContentTransition {
    id: ElementId,
    old_content: Option<AnyElement>,
    new_content: AnyElement,
    duration: Duration,
    transitioning: bool,
    key: u64,
}

impl ContentTransition {
    pub fn new(id: impl Into<ElementId>, content: impl IntoElement) -> Self {
        Self {
            id: id.into(),
            old_content: None,
            new_content: content.into_any_element(),
            duration: Duration::from_millis(200),
            transitioning: false,
            key: 0,
        }
    }

    /// Tag the transition with the current content key (e.g. from
    /// [`ContentTransitionState::key`]). The key is mixed into the animation
    /// element ids so every content switch spawns fresh animations; without
    /// it the crossfade only plays once per mount.
    pub fn key(mut self, key: u64) -> Self {
        self.key = key;
        self
    }

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

    pub fn crossfade_from(mut self, old: impl IntoElement) -> Self {
        self.old_content = Some(old.into_any_element());
        self.transitioning = true;
        self
    }
}

impl RenderOnce for ContentTransition {
    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
        let duration = self.duration;

        if self.transitioning {
            let mut container = div().relative().size_full();

            if let Some(old) = self.old_content {
                container = container.child(div().absolute().inset_0().child(old).with_animation(
                    ElementId::Name(format!("{}-old-{}", self.id, self.key).into()),
                    Animation::new(duration).with_easing(easings::ease_in_cubic),
                    |el, delta| el.opacity(1.0 - delta),
                ));
            }

            container = container.child(div().size_full().child(self.new_content).with_animation(
                ElementId::Name(format!("{}-new-{}", self.id, self.key).into()),
                Animation::new(duration).with_easing(easings::ease_out_cubic),
                |el, delta| el.opacity(delta),
            ));

            container
        } else {
            div().size_full().child(self.new_content)
        }
    }
}

pub struct ContentTransitionState {
    current_key: u64,
}

impl ContentTransitionState {
    pub fn new() -> Self {
        Self { current_key: 0 }
    }

    pub fn set_key(&mut self, key: u64) -> bool {
        if self.current_key != key {
            self.current_key = key;
            true
        } else {
            false
        }
    }

    pub fn key(&self) -> u64 {
        self.current_key
    }
}

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