Skip to main content

hover_position/
hover_position.rs

1use gpui::{
2    App, Application, Bounds, Context, Window, WindowBounds, WindowOptions, div, prelude::*, px,
3    size,
4};
5use gpui_animation::{
6    animation::TransitionExt,
7    transition::{self},
8};
9
10struct Hover;
11
12impl Render for Hover {
13    fn render(&mut self, _window: &mut Window, _: &mut Context<Self>) -> impl IntoElement {
14        let linear = std::sync::Arc::new(transition::general::Linear);
15
16        div()
17            .id("Hoverable2")
18            .flex()
19            .flex_col()
20            .gap_3()
21            .size(px(500.0))
22            .justify_center()
23            .items_center()
24            .child(
25                div()
26                    .id("Hoverable")
27                    .child("Hover over rectangle")
28                    .bg(gpui::white())
29                    .text_color(gpui::red())
30                    .flex()
31                    .text_xl()
32                    .h_10()
33                    .justify_center()
34                    .items_center(),
35            )
36            .child(
37                div().flex().gap_2().child(
38                    div()
39                        .id("Hoverable1")
40                        .size_16()
41                        .with_transition("Hoverable1")
42                        .transition_on_hover(
43                            std::time::Duration::from_millis(250),
44                            linear.clone(),
45                            |hovered, state| {
46                                if *hovered {
47                                    state.size_32().ml_24()
48                                } else {
49                                    state.size_16().ml_0()
50                                }
51                            },
52                        )
53                        .bg(gpui::red())
54                        .ml_0(),
55                ),
56            )
57    }
58}
59
60fn main() {
61    Application::new().run(|cx: &mut App| {
62        let bounds = Bounds::centered(None, size(px(500.), px(500.0)), cx);
63        cx.open_window(
64            WindowOptions {
65                window_bounds: Some(WindowBounds::Windowed(bounds)),
66                ..Default::default()
67            },
68            |_, cx| cx.new(|_| Hover),
69        )
70        .unwrap();
71    });
72}