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 let new_press = current_pid.is_some() && current_pid != prev_pid;
165
166 if new_press {
167 *last_pid.borrow_mut() = current_pid;
168 *phase.borrow_mut() = 1;
169 *release_pending.borrow_mut() = false;
170
171 let spec_in = AnimationSpec::tween(Duration::from_millis(FADE_IN_MS), Easing::Linear);
172 let spec_rad =
173 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::FastOutSlowIn);
174 let spec_ctr = AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::Linear);
175
176 alpha_anim.borrow_mut().set_target(1.0);
177 alpha_anim.borrow_mut().set_spec(spec_in);
178 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
179
180 rad_anim.borrow_mut().set_target(1.0);
181 rad_anim.borrow_mut().set_spec(spec_rad);
182 Self::register_driver(&format!("{}:drv:r", base), rad_anim.clone());
183
184 ctr_anim.borrow_mut().set_target(1.0);
185 ctr_anim.borrow_mut().set_spec(spec_ctr);
186 Self::register_driver(&format!("{}:drv:c", base), ctr_anim.clone());
187 }
188
189 if *phase.borrow() != 0
192 && !is_pressed
193 && current_pid.is_some()
194 && current_pid == *last_pid.borrow()
195 {
196 *release_pending.borrow_mut() = true;
197 }
198
199 let fade_pct = *alpha_anim.borrow().get();
200
201 if *phase.borrow() == 1 && fade_pct >= 1.0 {
202 if *release_pending.borrow() {
204 *phase.borrow_mut() = 3;
205 let spec_out =
206 AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
207 alpha_anim.borrow_mut().set_target(0.0);
208 alpha_anim.borrow_mut().set_spec(spec_out);
209 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
210 } else {
211 *phase.borrow_mut() = 2;
212 }
213 }
214
215 if *phase.borrow() == 2 && *release_pending.borrow() {
216 *phase.borrow_mut() = 3;
218 let spec_out = AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
219 alpha_anim.borrow_mut().set_target(0.0);
220 alpha_anim.borrow_mut().set_spec(spec_out);
221 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
222 }
223
224 if *phase.borrow() == 3 && fade_pct <= 0.01 {
225 *phase.borrow_mut() = 0;
227 *last_pid.borrow_mut() = current_pid;
230 return;
231 }
232
233 if fade_pct <= 0.01 || *phase.borrow() == 0 {
234 return;
235 }
236
237 let draw_alpha = if *release_pending.borrow() && *phase.borrow() == 1 {
240 1.0f32
241 } else {
242 fade_pct
243 };
244
245 let rad_pct = *rad_anim.borrow().get();
246 let ctr_pct = *ctr_anim.borrow().get();
247 let current_radius = start_radius + (target_radius - start_radius) * rad_pct;
248
249 let origin_scene = match press_pos {
250 Some(pos) => {
251 let ox = rect.x + pos.x;
252 let oy = rect.y + pos.y;
253 if bounded {
254 Vec2 {
255 x: ox + (center_scene.x - ox) * ctr_pct,
256 y: oy + (center_scene.y - oy) * ctr_pct,
257 }
258 } else {
259 center_scene
260 }
261 }
262 None => center_scene,
263 };
264
265 let ripple_alpha = PRESS_ALPHA * draw_alpha * alpha;
267 if ripple_alpha <= 0.001 {
268 return;
269 }
270
271 let draw_color = base_color.with_alpha_f32(ripple_alpha);
272
273 if bounded {
274 scene.nodes.push(SceneNode::PushClip {
275 rect,
276 radius: [0.0; 4],
277 op: repose_core::ClipOp::Intersect,
278 });
279 scene.nodes.push(SceneNode::Ellipse {
280 rect: Rect {
281 x: origin_scene.x - current_radius,
282 y: origin_scene.y - current_radius,
283 w: current_radius * 2.0,
284 h: current_radius * 2.0,
285 },
286 brush: draw_color.into(),
287 });
288 scene.nodes.push(SceneNode::PopClip);
289 } else {
290 scene.nodes.push(SceneNode::Ellipse {
291 rect: Rect {
292 x: origin_scene.x - current_radius,
293 y: origin_scene.y - current_radius,
294 w: current_radius * 2.0,
295 h: current_radius * 2.0,
296 },
297 brush: draw_color.into(),
298 });
299 }
300 }
301}