1use std::cell::RefCell;
14use std::rc::Rc;
15use std::time::Duration;
16
17use repose_core::animation::{AnimatedValue, AnimationSpec, Easing};
18use repose_core::animation_driver;
19use repose_core::{
20 Color, Indication, IndicationDrawNode, IndicationNodeFactory, InteractionSource, PressId, Rect,
21 Scene, SceneNode, Vec2, remember_state_with_key, request_frame,
22};
23
24const FADE_IN_MS: u64 = 75;
25const RADIUS_MS: u64 = 225;
26const FADE_OUT_MS: u64 = 150;
27
28const PRESS_ALPHA: f32 = 0.10;
30
31#[derive(Clone, Debug)]
32pub struct RippleConfig {
33 pub bounded: bool,
34 pub radius: Option<f32>,
35 pub color: Option<Color>,
36 pub enable_press: bool,
37}
38
39impl Default for RippleConfig {
40 fn default() -> Self {
41 Self {
42 bounded: true,
43 radius: None,
44 color: None,
45 enable_press: true,
46 }
47 }
48}
49
50pub fn ripple(config: RippleConfig) -> Rc<dyn IndicationNodeFactory> {
51 Rc::new(RippleNodeFactory { config })
52}
53
54#[derive(Clone, Debug)]
55pub struct RippleNodeFactory {
56 pub config: RippleConfig,
57}
58
59impl Indication for RippleNodeFactory {}
60
61impl IndicationNodeFactory for RippleNodeFactory {
62 fn create(&self, interaction_source: &InteractionSource) -> Box<dyn IndicationDrawNode> {
63 Box::new(RippleDrawNode::new(
64 interaction_source.clone(),
65 self.config.clone(),
66 ))
67 }
68}
69
70struct RippleDrawNode {
71 interaction_source: InteractionSource,
72 config: RippleConfig,
73}
74
75impl RippleDrawNode {
76 fn new(interaction_source: InteractionSource, config: RippleConfig) -> Self {
77 Self {
78 interaction_source,
79 config,
80 }
81 }
82
83 fn anim_base(&self) -> String {
84 format!("rp:{:p}", self.interaction_source.stable_id())
85 }
86
87 fn register_driver(key: &str, anim: Rc<RefCell<AnimatedValue<f32>>>) {
88 animation_driver::register(
89 key.to_string(),
90 Rc::new(RefCell::new(move || anim.borrow_mut().update())),
91 );
92 request_frame();
93 }
94}
95
96impl IndicationDrawNode for RippleDrawNode {
97 fn draw(&self, scene: &mut Scene, rect: Rect, alpha: f32) {
98 if !self.config.enable_press {
99 return;
100 }
101
102 let base = self.anim_base();
103 let bounded = self.config.bounded;
104 let center_scene = Vec2 {
105 x: rect.x + rect.w * 0.5,
106 y: rect.y + rect.h * 0.5,
107 };
108
109 let target_radius = self.config.radius.unwrap_or_else(|| {
110 let diag = (rect.w * rect.w + rect.h * rect.h).sqrt();
111 if bounded {
112 diag * 0.5 + 10.0
113 } else {
114 diag * 0.5
115 }
116 });
117 let start_radius = rect.w.max(rect.h) * 0.3;
118
119 let base_color = self.config.color.unwrap_or(Color(0, 0, 0, 255));
120
121 let is_pressed = self.interaction_source.collect_is_pressed();
122 let press_pos = self.interaction_source.collect_last_press_position();
123 let current_pid = self.interaction_source.collect_last_press_id();
124
125 let k_alpha = format!("{}:a", base);
126 let k_rad = format!("{}:r", base);
127 let k_ctr = format!("{}:c", base);
128
129 let alpha_anim = remember_state_with_key(&k_alpha, || {
130 AnimatedValue::new(
131 0.0f32,
132 AnimationSpec::tween(Duration::from_millis(FADE_IN_MS), Easing::Linear),
133 )
134 });
135 let rad_anim = remember_state_with_key(&k_rad, || {
136 AnimatedValue::new(
137 0.0f32,
138 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::FastOutSlowIn),
139 )
140 });
141 let ctr_anim = remember_state_with_key(&k_ctr, || {
142 AnimatedValue::new(
143 0.0f32,
144 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::Linear),
145 )
146 });
147
148 let k_phase = format!("{}:ph", base);
150 let phase = remember_state_with_key(&k_phase, || 0u8);
151
152 let k_last_pid = format!("{}:lpid", base);
154 let last_pid = remember_state_with_key(&k_last_pid, || None::<PressId>);
155
156 let k_release_pending = format!("{}:rpend", base);
158 let release_pending = remember_state_with_key(&k_release_pending, || false);
159
160 let prev_pid = *last_pid.borrow();
161
162 animation_driver::touch(&format!("{}:drv:a", base));
163 animation_driver::touch(&format!("{}:drv:r", base));
164 animation_driver::touch(&format!("{}:drv:c", base));
165
166 let new_press = current_pid.is_some() && current_pid != prev_pid;
169
170 if new_press {
171 *last_pid.borrow_mut() = current_pid;
172 *phase.borrow_mut() = 1;
173 *release_pending.borrow_mut() = false;
174
175 let spec_in = AnimationSpec::tween(Duration::from_millis(FADE_IN_MS), Easing::Linear);
176 let spec_rad =
177 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::FastOutSlowIn);
178 let spec_ctr = AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::Linear);
179
180 alpha_anim.borrow_mut().set_target(1.0);
181 alpha_anim.borrow_mut().set_spec(spec_in);
182 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
183
184 rad_anim.borrow_mut().set_target(1.0);
185 rad_anim.borrow_mut().set_spec(spec_rad);
186 Self::register_driver(&format!("{}:drv:r", base), rad_anim.clone());
187
188 ctr_anim.borrow_mut().set_target(1.0);
189 ctr_anim.borrow_mut().set_spec(spec_ctr);
190 Self::register_driver(&format!("{}:drv:c", base), ctr_anim.clone());
191 }
192
193 if *phase.borrow() != 0
196 && !is_pressed
197 && current_pid.is_some()
198 && current_pid == *last_pid.borrow()
199 {
200 *release_pending.borrow_mut() = true;
201 }
202
203 let fade_pct = *alpha_anim.borrow().get();
204
205 if *phase.borrow() == 1 && fade_pct >= 1.0 {
206 if *release_pending.borrow() {
208 *phase.borrow_mut() = 3;
209 let spec_out =
210 AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
211 alpha_anim.borrow_mut().set_target(0.0);
212 alpha_anim.borrow_mut().set_spec(spec_out);
213 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
214 } else {
215 *phase.borrow_mut() = 2;
216 }
217 }
218
219 if *phase.borrow() == 2 && *release_pending.borrow() {
220 *phase.borrow_mut() = 3;
222 let spec_out = AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
223 alpha_anim.borrow_mut().set_target(0.0);
224 alpha_anim.borrow_mut().set_spec(spec_out);
225 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
226 }
227
228 if *phase.borrow() == 3 && fade_pct <= 0.01 {
229 *phase.borrow_mut() = 0;
231 *last_pid.borrow_mut() = current_pid;
234 return;
235 }
236
237 if fade_pct <= 0.01 || *phase.borrow() == 0 {
238 return;
239 }
240
241 let draw_alpha = if *release_pending.borrow() && *phase.borrow() == 1 {
244 1.0f32
245 } else {
246 fade_pct
247 };
248
249 let rad_pct = *rad_anim.borrow().get();
250 let ctr_pct = *ctr_anim.borrow().get();
251 let current_radius = start_radius + (target_radius - start_radius) * rad_pct;
252
253 let origin_scene = match press_pos {
254 Some(pos) => {
255 let ox = rect.x + pos.x;
256 let oy = rect.y + pos.y;
257 if bounded {
258 Vec2 {
259 x: ox + (center_scene.x - ox) * ctr_pct,
260 y: oy + (center_scene.y - oy) * ctr_pct,
261 }
262 } else {
263 center_scene
264 }
265 }
266 None => center_scene,
267 };
268
269 let ripple_alpha = PRESS_ALPHA * draw_alpha * alpha;
271 if ripple_alpha <= 0.001 {
272 return;
273 }
274
275 let draw_color = base_color.with_alpha_f32(ripple_alpha);
276
277 if bounded {
278 scene.nodes.push(SceneNode::PushClip {
279 rect,
280 radius: [0.0; 4],
281 op: repose_core::ClipOp::Intersect,
282 });
283 scene.nodes.push(SceneNode::Ellipse {
284 rect: Rect {
285 x: origin_scene.x - current_radius,
286 y: origin_scene.y - current_radius,
287 w: current_radius * 2.0,
288 h: current_radius * 2.0,
289 },
290 brush: draw_color.into(),
291 });
292 scene.nodes.push(SceneNode::PopClip);
293 } else {
294 scene.nodes.push(SceneNode::Ellipse {
295 rect: Rect {
296 x: origin_scene.x - current_radius,
297 y: origin_scene.y - current_radius,
298 w: current_radius * 2.0,
299 h: current_radius * 2.0,
300 },
301 brush: draw_color.into(),
302 });
303 }
304 }
305}