Skip to main content

fc_ui/capabilities/effects/
text_highlight.rs

1use crate::capabilities::motion::animations::easings;
2use gpui::{prelude::FluentBuilder as _, *};
3use std::time::Duration;
4
5#[derive(IntoElement)]
6pub struct TextHighlight {
7    id: ElementId,
8    color: Option<Hsla>,
9    duration: Duration,
10    delay: Duration,
11    children: Vec<AnyElement>,
12    style: StyleRefinement,
13}
14
15impl TextHighlight {
16    pub fn new(id: impl Into<ElementId>) -> Self {
17        Self {
18            id: id.into(),
19            color: None,
20            duration: Duration::from_millis(600),
21            delay: Duration::from_millis(0),
22            children: Vec::new(),
23            style: StyleRefinement::default(),
24        }
25    }
26
27    pub fn color(mut self, color: Hsla) -> Self {
28        self.color = Some(color);
29        self
30    }
31
32    pub fn duration(mut self, duration: Duration) -> Self {
33        self.duration = duration;
34        self
35    }
36
37    pub fn delay(mut self, delay: Duration) -> Self {
38        self.delay = delay;
39        self
40    }
41}
42
43impl Styled for TextHighlight {
44    fn style(&mut self) -> &mut StyleRefinement {
45        &mut self.style
46    }
47}
48
49impl ParentElement for TextHighlight {
50    fn extend(&mut self, elements: impl IntoIterator<Item = AnyElement>) {
51        self.children.extend(elements);
52    }
53}
54
55impl RenderOnce for TextHighlight {
56    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
57        let highlight_color = self.color.unwrap_or(hsla(0.15, 0.9, 0.6, 0.3));
58        let anim_id: ElementId = ElementId::Name(format!("{}-sweep", self.id).into());
59        let user_style = self.style;
60
61        let overlay = div()
62            .id(anim_id.clone())
63            .absolute()
64            .top_0()
65            .left_0()
66            .h_full()
67            .bg(highlight_color)
68            .with_animation(
69                anim_id,
70                Animation::new(self.duration)
71                    .with_easing(easings::ease_out_cubic)
72                    .with_delay(self.delay),
73                move |el, t| el.w(relative(t)),
74            );
75
76        let mut container = div().relative().child(overlay).map(|mut el| {
77            el.style().refine(&user_style);
78            el
79        });
80
81        for child in self.children {
82            container = container.child(div().relative().child(child));
83        }
84
85        container
86    }
87}