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();
123 let press_pos = self.interaction_source.collect_last_press_position();
124 let current_pid = self.interaction_source.collect_last_press_id();
125
126 let k_alpha = format!("{}:a", base);
128 let k_rad = format!("{}:r", base);
129 let k_ctr = format!("{}:c", base);
130
131 let alpha_anim = remember_state_with_key(&k_alpha, || {
132 AnimatedValue::new(
133 0.0f32,
134 AnimationSpec::tween(Duration::from_millis(FADE_IN_MS), Easing::Linear),
135 )
136 });
137 let rad_anim = remember_state_with_key(&k_rad, || {
138 AnimatedValue::new(
139 0.0f32,
140 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::FastOutSlowIn),
141 )
142 });
143 let ctr_anim = remember_state_with_key(&k_ctr, || {
144 AnimatedValue::new(
145 0.0f32,
146 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::Linear),
147 )
148 });
149
150 let k_phase = format!("{}:ph", base);
152 let phase = remember_state_with_key(&k_phase, || 0u8);
153
154 let k_last_pid = format!("{}:lpid", base);
156 let last_pid = remember_state_with_key(&k_last_pid, || None::<PressId>);
157
158 let k_release_pending = format!("{}:rpend", base);
160 let release_pending = remember_state_with_key(&k_release_pending, || false);
161
162 let prev_pid = *last_pid.borrow();
163
164 let new_press = current_pid.is_some() && current_pid != prev_pid;
168
169 if new_press {
170 *last_pid.borrow_mut() = current_pid;
171 *phase.borrow_mut() = 1;
172 *release_pending.borrow_mut() = false;
173
174 let spec_in = AnimationSpec::tween(Duration::from_millis(FADE_IN_MS), Easing::Linear);
175 let spec_rad =
176 AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::FastOutSlowIn);
177 let spec_ctr = AnimationSpec::tween(Duration::from_millis(RADIUS_MS), Easing::Linear);
178
179 alpha_anim.borrow_mut().set_target(1.0);
180 alpha_anim.borrow_mut().set_spec(spec_in);
181 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
182
183 rad_anim.borrow_mut().set_target(1.0);
184 rad_anim.borrow_mut().set_spec(spec_rad);
185 Self::register_driver(&format!("{}:drv:r", base), rad_anim.clone());
186
187 ctr_anim.borrow_mut().set_target(1.0);
188 ctr_anim.borrow_mut().set_spec(spec_ctr);
189 Self::register_driver(&format!("{}:drv:c", base), ctr_anim.clone());
190 }
191
192 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();
205
206 if *phase.borrow() == 1 && fade_pct >= 1.0 {
207 if *release_pending.borrow() {
209 *phase.borrow_mut() = 3;
210 let spec_out =
211 AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
212 alpha_anim.borrow_mut().set_target(0.0);
213 alpha_anim.borrow_mut().set_spec(spec_out);
214 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
215 } else {
216 *phase.borrow_mut() = 2;
217 }
218 }
219
220 if *phase.borrow() == 2 && *release_pending.borrow() {
221 *phase.borrow_mut() = 3;
223 let spec_out = AnimationSpec::tween(Duration::from_millis(FADE_OUT_MS), Easing::Linear);
224 alpha_anim.borrow_mut().set_target(0.0);
225 alpha_anim.borrow_mut().set_spec(spec_out);
226 Self::register_driver(&format!("{}:drv:a", base), alpha_anim.clone());
227 }
228
229 if *phase.borrow() == 3 && fade_pct <= 0.01 {
230 *phase.borrow_mut() = 0;
232 *last_pid.borrow_mut() = current_pid;
235 return;
236 }
237
238 if fade_pct <= 0.01 || *phase.borrow() == 0 {
240 return;
241 }
242
243 let draw_alpha = if *release_pending.borrow() && *phase.borrow() == 1 {
246 1.0f32
247 } else {
248 fade_pct
249 };
250
251 let rad_pct = *rad_anim.borrow().get();
252 let ctr_pct = *ctr_anim.borrow().get();
253 let current_radius = start_radius + (target_radius - start_radius) * rad_pct;
254
255 let origin_scene = match press_pos {
256 Some(pos) => {
257 let ox = rect.x + pos.x;
258 let oy = rect.y + pos.y;
259 if bounded {
260 Vec2 {
261 x: ox + (center_scene.x - ox) * ctr_pct,
262 y: oy + (center_scene.y - oy) * ctr_pct,
263 }
264 } else {
265 center_scene
266 }
267 }
268 None => center_scene,
269 };
270
271 let ripple_alpha = PRESS_ALPHA * draw_alpha * alpha;
273 if ripple_alpha <= 0.001 {
274 return;
275 }
276
277 let draw_color = base_color.with_alpha_f32(ripple_alpha);
278
279 if bounded {
280 scene.nodes.push(SceneNode::PushClip {
281 rect,
282 radius: [0.0; 4],
283 op: repose_core::ClipOp::Intersect,
284 });
285 scene.nodes.push(SceneNode::Ellipse {
286 rect: Rect {
287 x: origin_scene.x - current_radius,
288 y: origin_scene.y - current_radius,
289 w: current_radius * 2.0,
290 h: current_radius * 2.0,
291 },
292 brush: draw_color.into(),
293 });
294 scene.nodes.push(SceneNode::PopClip);
295 } else {
296 scene.nodes.push(SceneNode::Ellipse {
297 rect: Rect {
298 x: origin_scene.x - current_radius,
299 y: origin_scene.y - current_radius,
300 w: current_radius * 2.0,
301 h: current_radius * 2.0,
302 },
303 brush: draw_color.into(),
304 });
305 }
306 }
307}