Skip to main content

fc_ui/capabilities/effects/
marquee.rs

1use gpui::{prelude::FluentBuilder as _, *};
2use std::cell::Cell;
3use std::rc::Rc;
4use std::time::Duration;
5
6#[derive(Copy, Clone, Debug, PartialEq, Eq)]
7pub enum MarqueeDirection {
8    Left,
9    Right,
10}
11
12#[derive(IntoElement)]
13pub struct Marquee {
14    id: ElementId,
15    base: Div,
16    speed: f32,
17    direction: MarqueeDirection,
18    pause_on_hover: bool,
19    gap: Pixels,
20    content_width: Pixels,
21    render_content: Rc<dyn Fn() -> AnyElement>,
22}
23
24impl Marquee {
25    pub fn new(
26        id: impl Into<ElementId>,
27        render_content: impl Fn() -> AnyElement + 'static,
28    ) -> Self {
29        Self {
30            id: id.into(),
31            base: div(),
32            speed: 50.0,
33            direction: MarqueeDirection::Left,
34            pause_on_hover: false,
35            gap: px(32.0),
36            content_width: px(1000.0),
37            render_content: Rc::new(render_content),
38        }
39    }
40
41    pub fn speed(mut self, pixels_per_second: f32) -> Self {
42        self.speed = pixels_per_second.max(1.0);
43        self
44    }
45
46    pub fn direction(mut self, direction: MarqueeDirection) -> Self {
47        self.direction = direction;
48        self
49    }
50
51    pub fn pause_on_hover(mut self, pause: bool) -> Self {
52        self.pause_on_hover = pause;
53        self
54    }
55
56    pub fn gap(mut self, gap: Pixels) -> Self {
57        self.gap = gap;
58        self
59    }
60
61    pub fn content_width(mut self, width: Pixels) -> Self {
62        self.content_width = width;
63        self
64    }
65}
66
67impl Styled for Marquee {
68    fn style(&mut self) -> &mut StyleRefinement {
69        self.base.style()
70    }
71}
72
73impl RenderOnce for Marquee {
74    fn render(self, _window: &mut Window, _cx: &mut App) -> impl IntoElement {
75        let total_travel = self.content_width + self.gap;
76        let duration_ms = (total_travel / px(1.0) / self.speed * 1000.0) as u64;
77        let duration = Duration::from_millis(duration_ms.max(100));
78        let gap = self.gap;
79        let content_width = self.content_width;
80        let direction = self.direction;
81        let pause_on_hover = self.pause_on_hover;
82        let anim_id = ElementId::Name(format!("{}-scroll", self.id).into());
83
84        let copy_one = (self.render_content)();
85        let copy_two = (self.render_content)();
86
87        let hovered = Rc::new(Cell::new(false));
88        let hovered_cb = hovered.clone();
89
90        let scroll = div()
91            .id(self.id)
92            .flex()
93            .flex_row()
94            .items_center()
95            .w(content_width * 2.0 + gap * 2.0)
96            .child(
97                div()
98                    .flex()
99                    .flex_row()
100                    .items_center()
101                    .flex_shrink_0()
102                    .w(content_width)
103                    .mr(gap)
104                    .child(copy_one),
105            )
106            .child(
107                div()
108                    .flex()
109                    .flex_row()
110                    .items_center()
111                    .flex_shrink_0()
112                    .w(content_width)
113                    .mr(gap)
114                    .child(copy_two),
115            );
116
117        let scroll = scroll.when(pause_on_hover, |el| {
118            el.on_hover(move |hovering: &bool, window, _cx| {
119                hovered_cb.set(*hovering);
120                window.refresh();
121            })
122        });
123
124        let scroll = scroll.map(|el| {
125            if !pause_on_hover || !hovered.get() {
126                el.with_animation(
127                    anim_id,
128                    Animation::new(duration).repeat().with_easing(gpui::linear),
129                    move |el, delta| {
130                        let offset = total_travel * delta;
131                        match direction {
132                            MarqueeDirection::Left => el.left(-offset),
133                            MarqueeDirection::Right => el.left(offset - total_travel),
134                        }
135                    },
136                )
137                .into_any_element()
138            } else {
139                el.into_any_element()
140            }
141        });
142
143        self.base.overflow_hidden().child(scroll)
144    }
145}