Skip to main content

desktop_example/app/routes/
portal.rs

1use std::time::Duration;
2
3use freya::{
4    animation::*,
5    material_design::Ripple,
6    prelude::*,
7};
8
9#[derive(PartialEq)]
10pub struct PortalDemo;
11
12impl Component for PortalDemo {
13    fn render(&self) -> impl IntoElement {
14        let mut show_popup = use_state::<Option<i32>>(|| None);
15
16        ScrollView::new()
17            .width(Size::fill())
18            .height(Size::fill())
19            .child(
20                rect()
21                    .width(Size::fill())
22                    .spacing(12.)
23                    .padding(12.)
24                    .child(portal_popup(show_popup))
25                    .children((0..5).map(|i| {
26                        rect()
27                            .key(i)
28                            .spacing(6.)
29                            .width(Size::fill())
30                            .child(
31                                Portal::new(i)
32                                    .key(show_popup())
33                                    .show(show_popup() != Some(i))
34                                    .width(Size::fill())
35                                    .height(Size::px(120.))
36                                    .function(Function::Expo)
37                                    .duration(Duration::from_millis(500))
38                                    .child(portal_card(i)),
39                            )
40                            .on_press(move |_| show_popup.set(Some(i)))
41                            .into()
42                    })),
43            )
44    }
45}
46
47fn portal_card(i: i32) -> impl IntoElement {
48    Ripple::new().color((255, 255, 255)).child(
49        rect()
50            .expanded()
51            .background((103, 80, 164))
52            .corner_radius(16.)
53            .center()
54            .color(Color::WHITE)
55            .child(format!("Card {}", i)),
56    )
57}
58
59fn portal_popup(mut show_popup: State<Option<i32>>) -> impl IntoElement {
60    let show = show_popup().is_some();
61    let i = show_popup().unwrap_or(0);
62
63    Popup::new()
64        .show(show)
65        .on_close_request(move |_| show_popup.set(None))
66        .width(Size::px(350.))
67        .child(PopupTitle::new(format!("Card {i}")))
68        .child(
69            PopupContent::new().child(
70                Portal::new(i)
71                    .width(Size::px(250.))
72                    .height(Size::px(150.))
73                    .function(Function::Expo)
74                    .duration(Duration::from_millis(500))
75                    .child(portal_card(i)),
76            ),
77        )
78        .child(
79            PopupButtons::new()
80                .child(
81                    Button::new()
82                        .expanded()
83                        .rounded_full()
84                        .on_press(move |_| show_popup.set(None))
85                        .child("Close"),
86                )
87                .child(
88                    Button::new()
89                        .filled()
90                        .expanded()
91                        .rounded_full()
92                        .on_press(move |_| show_popup.set(None))
93                        .child("Accept"),
94                ),
95        )
96}