1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//! Ripple state — manage ripple animation and hover state for interactive components.
//!
//! ## Usage
//! Provide visual ripple feedback for interactive controls in your app (buttons, surfaces, glass buttons) to indicate clicks and hover interactions.
use std::sync::{Arc, atomic};
/// # RippleState
///
/// Manage ripple animations and hover state for interactive UI components.
/// Recommended use: create a single `RippleState` handle and clone it to share.
///
/// ## Parameters
///
/// - This type has no constructor parameters; create it with [`RippleState::new()`].
///
/// ## Examples
///
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let s = RippleState::new();
/// assert!(!s.is_hovered());
/// s.set_hovered(true);
/// assert!(s.is_hovered());
/// ```
#[derive(Clone)]
pub struct RippleState {
inner: Arc<RippleStateInner>,
}
impl Default for RippleState {
/// Creates a new `RippleState` with all fields initialized to their default values.
fn default() -> Self {
Self::new()
}
}
impl RippleState {
/// Creates a new `RippleState` with default values.
///
/// # Example
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let state = RippleState::new();
/// ```
pub fn new() -> Self {
Self {
inner: Arc::new(RippleStateInner::new()),
}
}
/// Starts a new ripple animation from the given click position.
///
/// # Arguments
///
/// * `click_pos` - The normalized `[x, y]` position (typically in the range [0.0, 1.0]) where the ripple originates.
///
/// # Example
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let state = RippleState::new();
/// state.start_animation([0.5, 0.5]);
/// ```
pub fn start_animation(&self, click_pos: [f32; 2]) {
self.inner.start_animation(click_pos);
}
/// Returns the current progress of the ripple animation and the origin position.
///
/// Returns `Some((progress, [x, y]))` if the animation is active, where:
/// - `progress` is a value in `[0.0, 1.0)` representing the animation progress.
/// - `[x, y]` is the normalized origin of the ripple.
///
/// Returns `None` if the animation is not active or has completed.
///
/// # Example
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let state = RippleState::new();
/// state.start_animation([0.5, 0.5]);
/// if let Some((progress, center)) = state.get_animation_progress() {
/// // Use progress and center for rendering
/// }
/// ```
pub fn get_animation_progress(&self) -> Option<(f32, [f32; 2])> {
self.inner.get_animation_progress()
}
/// Sets the hover state for the ripple.
///
/// # Arguments
///
/// * `hovered` - `true` if the pointer is over the component, `false` otherwise.
///
/// # Example
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let state = RippleState::new();
/// state.set_hovered(true);
/// ```
pub fn set_hovered(&self, hovered: bool) {
self.inner.set_hovered(hovered);
}
/// Returns whether the pointer is currently hovering over the component.
///
/// # Example
/// ```
/// use tessera_ui_basic_components::ripple_state::RippleState;
/// let state = RippleState::new();
/// let hovered = state.is_hovered();
/// ```
pub fn is_hovered(&self) -> bool {
self.inner.is_hovered()
}
}
struct RippleStateInner {
/// Whether the ripple animation is currently active.
is_animating: atomic::AtomicBool,
/// The animation start time, stored as milliseconds since the Unix epoch.
start_time: atomic::AtomicU64,
/// The X coordinate of the click position, stored as fixed-point (multiplied by 1000).
click_pos_x: atomic::AtomicI32,
/// The Y coordinate of the click position, stored as fixed-point (multiplied by 1000).
click_pos_y: atomic::AtomicI32,
/// Whether the pointer is currently hovering over the component.
is_hovered: atomic::AtomicBool,
}
impl RippleStateInner {
fn new() -> Self {
Self {
is_animating: atomic::AtomicBool::new(false),
start_time: atomic::AtomicU64::new(0),
click_pos_x: atomic::AtomicI32::new(0),
click_pos_y: atomic::AtomicI32::new(0),
is_hovered: atomic::AtomicBool::new(false),
}
}
fn start_animation(&self, click_pos: [f32; 2]) {
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
self.start_time.store(now, atomic::Ordering::SeqCst);
self.click_pos_x
.store((click_pos[0] * 1000.0) as i32, atomic::Ordering::SeqCst);
self.click_pos_y
.store((click_pos[1] * 1000.0) as i32, atomic::Ordering::SeqCst);
self.is_animating.store(true, atomic::Ordering::SeqCst);
}
fn get_animation_progress(&self) -> Option<(f32, [f32; 2])> {
let is_animating = self.is_animating.load(atomic::Ordering::SeqCst);
if !is_animating {
return None;
}
let now = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.unwrap()
.as_millis() as u64;
let start = self.start_time.load(atomic::Ordering::SeqCst);
let elapsed_ms = now.saturating_sub(start);
let progress = (elapsed_ms as f32) / 600.0; // 600ms animation
if progress >= 1.0 {
self.is_animating.store(false, atomic::Ordering::SeqCst);
return None;
}
let click_pos = [
self.click_pos_x.load(atomic::Ordering::SeqCst) as f32 / 1000.0,
self.click_pos_y.load(atomic::Ordering::SeqCst) as f32 / 1000.0,
];
Some((progress, click_pos))
}
fn set_hovered(&self, hovered: bool) {
self.is_hovered.store(hovered, atomic::Ordering::SeqCst);
}
fn is_hovered(&self) -> bool {
self.is_hovered.load(atomic::Ordering::SeqCst)
}
}