freya_components/
portal.rs1use std::{
2 collections::HashMap,
3 fmt::Debug,
4 time::Duration,
5};
6
7use freya_animation::prelude::*;
8use freya_core::{
9 prelude::*,
10 scope_id::ScopeId,
11};
12use torin::{
13 prelude::{
14 Area,
15 Position,
16 },
17 size::Size,
18};
19
20#[derive(PartialEq)]
21pub struct Portal<T> {
22 key: DiffKey,
23 children: Vec<Element>,
24 id: T,
25 function: Function,
26 duration: Duration,
27 ease: Ease,
28 width: Size,
29 height: Size,
30 show: bool,
31}
32
33impl<T> ChildrenExt for Portal<T> {
34 fn get_children(&mut self) -> &mut Vec<Element> {
35 &mut self.children
36 }
37}
38
39impl<T> Portal<T> {
40 pub fn new(id: T) -> Self {
41 Self {
42 key: DiffKey::None,
43 children: vec![],
44 id,
45 function: Function::default(),
46 duration: Duration::from_millis(750),
47 ease: Ease::default(),
48 width: Size::auto(),
49 height: Size::auto(),
50 show: true,
51 }
52 }
53
54 pub fn function(mut self, function: Function) -> Self {
55 self.function = function;
56 self
57 }
58
59 pub fn duration(mut self, duration: Duration) -> Self {
60 self.duration = duration;
61 self
62 }
63
64 pub fn ease(mut self, ease: Ease) -> Self {
65 self.ease = ease;
66 self
67 }
68
69 pub fn width(mut self, width: Size) -> Self {
70 self.width = width;
71 self
72 }
73
74 pub fn height(mut self, height: Size) -> Self {
75 self.height = height;
76 self
77 }
78
79 pub fn show(mut self, show: bool) -> Self {
80 self.show = show;
81 self
82 }
83}
84
85impl<T> KeyExt for Portal<T> {
86 fn write_key(&mut self) -> &mut DiffKey {
87 &mut self.key
88 }
89}
90
91impl<T: PartialEq + 'static + Clone + std::hash::Hash + Eq + Debug> Render for Portal<T> {
92 fn render(&self) -> impl IntoElement {
93 let mut positions = use_hook(|| match try_consume_context::<PortalsMap<T>>() {
94 Some(ctx) => ctx,
95 None => {
96 let ctx = PortalsMap {
97 ids: State::create_in_scope(HashMap::default(), ScopeId::ROOT),
98 };
99 provide_context_for_scope_id(ctx.clone(), ScopeId::ROOT);
100 ctx
101 }
102 });
103 let id = self.id.clone();
104 let init_size = use_hook(move || positions.ids.write().remove(&id));
105 let mut previous_size = use_state::<Option<Area>>(|| None);
106 let mut current_size = use_state::<Option<Area>>(|| None);
107
108 let mut animation = use_animation_with_dependencies(
109 &(self.function, self.duration, self.ease),
110 move |_conf, (function, duration, ease)| {
111 let from_size = previous_size
112 .read()
113 .unwrap_or(init_size.unwrap_or_default());
114 let to_size = current_size.read().unwrap_or_default();
115 (
116 AnimNum::new(from_size.origin.x, to_size.origin.x)
117 .duration(*duration)
118 .ease(*ease)
119 .function(*function),
120 AnimNum::new(from_size.origin.y, to_size.origin.y)
121 .duration(*duration)
122 .ease(*ease)
123 .function(*function),
124 AnimNum::new(from_size.size.width, to_size.size.width)
125 .duration(*duration)
126 .ease(*ease)
127 .function(*function),
128 AnimNum::new(from_size.size.height, to_size.size.height)
129 .duration(*duration)
130 .ease(*ease)
131 .function(*function),
132 )
133 },
134 );
135
136 let (offset_x, offset_y, width, height) = animation.get().value();
137 let id = self.id.clone();
138 let show = self.show;
139
140 rect()
141 .a11y_focusable(false)
142 .on_sized(move |e: Event<SizedEventData>| {
143 if *current_size.peek() != Some(e.area) && show {
144 previous_size.set(current_size());
145 current_size.set(Some(e.area));
146 positions.ids.write().insert(id.clone(), e.area);
147
148 spawn(async move {
149 let has_init_size = init_size.is_some();
150 let has_previous_size = previous_size.peek().is_some();
151
152 if !*animation.has_run_yet().read() && !has_init_size {
153 animation.finish();
155 } else if has_init_size || has_previous_size {
156 animation.start();
158 }
159 });
160 }
161 })
162 .width(self.width.clone())
163 .height(self.height.clone())
164 .child(
165 rect()
166 .offset_x(offset_x)
167 .offset_y(offset_y)
168 .position(Position::new_global())
169 .child(
170 rect()
171 .width(Size::px(width))
172 .height(Size::px(height))
173 .opacity(
175 if init_size.is_some()
176 || previous_size.read().is_some()
177 || current_size.read().is_some()
178 {
179 1.
180 } else {
181 0.
182 },
183 )
184 .children(if self.show {
185 self.children.clone()
186 } else {
187 vec![]
188 }),
189 ),
190 )
191 }
192
193 fn render_key(&self) -> DiffKey {
194 self.key.clone().or(self.default_key())
195 }
196}
197
198#[derive(Clone)]
199pub struct PortalsMap<T: Clone + PartialEq + 'static> {
200 pub ids: State<HashMap<T, Area>>,
201}