@@ -0,0 +1,346 @@ |
| 1 | +//! Animation system for notification popups |
| 2 | +//! |
| 3 | +//! Handles slide and fade animations for popup appearance/disappearance. |
| 4 | + |
| 5 | +use std::time::{Duration, Instant}; |
| 6 | + |
| 7 | +/// Animation state |
| 8 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 9 | +pub enum AnimationState { |
| 10 | + /// Popup is animating in (appearing) |
| 11 | + Appearing, |
| 12 | + /// Popup is fully visible |
| 13 | + Visible, |
| 14 | + /// Popup is animating to a new position (reflow) |
| 15 | + Reflowing, |
| 16 | + /// Popup is animating out (disappearing) |
| 17 | + Disappearing, |
| 18 | + /// Popup is hidden (animation complete) |
| 19 | + Hidden, |
| 20 | +} |
| 21 | + |
| 22 | +/// Slide direction for animations |
| 23 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 24 | +pub enum SlideDirection { |
| 25 | + Up, |
| 26 | + Down, |
| 27 | + Left, |
| 28 | + Right, |
| 29 | + None, |
| 30 | +} |
| 31 | + |
| 32 | +impl SlideDirection { |
| 33 | + pub fn from_str(s: &str) -> Self { |
| 34 | + match s.to_lowercase().as_str() { |
| 35 | + "up" => SlideDirection::Up, |
| 36 | + "down" => SlideDirection::Down, |
| 37 | + "left" => SlideDirection::Left, |
| 38 | + "right" => SlideDirection::Right, |
| 39 | + _ => SlideDirection::None, |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + /// Get the offset multiplier for x and y based on direction |
| 44 | + /// Returns (dx, dy) where values are -1, 0, or 1 |
| 45 | + pub fn offset_multiplier(&self) -> (i32, i32) { |
| 46 | + match self { |
| 47 | + SlideDirection::Up => (0, -1), |
| 48 | + SlideDirection::Down => (0, 1), |
| 49 | + SlideDirection::Left => (-1, 0), |
| 50 | + SlideDirection::Right => (1, 0), |
| 51 | + SlideDirection::None => (0, 0), |
| 52 | + } |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +/// Animation configuration |
| 57 | +#[derive(Debug, Clone)] |
| 58 | +pub struct AnimationConfig { |
| 59 | + /// Whether animations are enabled |
| 60 | + pub enabled: bool, |
| 61 | + /// Duration for fade-in animation |
| 62 | + pub fade_in_duration: Duration, |
| 63 | + /// Duration for fade-out animation |
| 64 | + pub fade_out_duration: Duration, |
| 65 | + /// Slide direction |
| 66 | + pub slide_direction: SlideDirection, |
| 67 | + /// Slide distance in pixels |
| 68 | + pub slide_distance: i32, |
| 69 | +} |
| 70 | + |
| 71 | +impl Default for AnimationConfig { |
| 72 | + fn default() -> Self { |
| 73 | + Self { |
| 74 | + enabled: true, |
| 75 | + fade_in_duration: Duration::from_millis(150), |
| 76 | + fade_out_duration: Duration::from_millis(150), |
| 77 | + slide_direction: SlideDirection::Down, |
| 78 | + slide_distance: 20, |
| 79 | + } |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +impl AnimationConfig { |
| 84 | + /// Create from the app's AnimationConfig |
| 85 | + pub fn from_config(config: &crate::config::AnimationConfig) -> Self { |
| 86 | + Self { |
| 87 | + enabled: config.enabled, |
| 88 | + fade_in_duration: Duration::from_millis(config.fade_in as u64), |
| 89 | + fade_out_duration: Duration::from_millis(config.fade_out as u64), |
| 90 | + slide_direction: SlideDirection::from_str(&config.slide), |
| 91 | + slide_distance: config.slide_distance as i32, |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +/// Animator for a single popup |
| 97 | +#[derive(Debug)] |
| 98 | +pub struct Animator { |
| 99 | + /// Current animation state |
| 100 | + state: AnimationState, |
| 101 | + /// Animation configuration |
| 102 | + config: AnimationConfig, |
| 103 | + /// When the current animation started |
| 104 | + start_time: Instant, |
| 105 | + /// Target position (final x, y when animation completes) |
| 106 | + target_x: i32, |
| 107 | + target_y: i32, |
| 108 | + /// Start position for reflow animation |
| 109 | + reflow_start_x: i32, |
| 110 | + reflow_start_y: i32, |
| 111 | +} |
| 112 | + |
| 113 | +impl Animator { |
| 114 | + /// Create a new animator |
| 115 | + pub fn new(config: AnimationConfig, target_x: i32, target_y: i32) -> Self { |
| 116 | + Self { |
| 117 | + state: AnimationState::Hidden, |
| 118 | + config, |
| 119 | + start_time: Instant::now(), |
| 120 | + target_x, |
| 121 | + target_y, |
| 122 | + reflow_start_x: target_x, |
| 123 | + reflow_start_y: target_y, |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + /// Start the appear animation |
| 128 | + pub fn start_appear(&mut self) { |
| 129 | + if !self.config.enabled { |
| 130 | + self.state = AnimationState::Visible; |
| 131 | + return; |
| 132 | + } |
| 133 | + self.state = AnimationState::Appearing; |
| 134 | + self.start_time = Instant::now(); |
| 135 | + } |
| 136 | + |
| 137 | + /// Start the disappear animation |
| 138 | + pub fn start_disappear(&mut self) { |
| 139 | + if !self.config.enabled { |
| 140 | + self.state = AnimationState::Hidden; |
| 141 | + return; |
| 142 | + } |
| 143 | + self.state = AnimationState::Disappearing; |
| 144 | + self.start_time = Instant::now(); |
| 145 | + } |
| 146 | + |
| 147 | + /// Start a reflow animation to a new position |
| 148 | + pub fn start_reflow(&mut self, current_x: i32, current_y: i32, new_target_x: i32, new_target_y: i32) { |
| 149 | + if !self.config.enabled { |
| 150 | + self.target_x = new_target_x; |
| 151 | + self.target_y = new_target_y; |
| 152 | + return; |
| 153 | + } |
| 154 | + self.reflow_start_x = current_x; |
| 155 | + self.reflow_start_y = current_y; |
| 156 | + self.target_x = new_target_x; |
| 157 | + self.target_y = new_target_y; |
| 158 | + self.state = AnimationState::Reflowing; |
| 159 | + self.start_time = Instant::now(); |
| 160 | + } |
| 161 | + |
| 162 | + /// Update the animation state, returns true if animation is still in progress |
| 163 | + pub fn update(&mut self) -> bool { |
| 164 | + match self.state { |
| 165 | + AnimationState::Appearing => { |
| 166 | + let elapsed = self.start_time.elapsed(); |
| 167 | + if elapsed >= self.config.fade_in_duration { |
| 168 | + self.state = AnimationState::Visible; |
| 169 | + false |
| 170 | + } else { |
| 171 | + true |
| 172 | + } |
| 173 | + } |
| 174 | + AnimationState::Reflowing => { |
| 175 | + let elapsed = self.start_time.elapsed(); |
| 176 | + // Use fade_in duration for reflow animation |
| 177 | + if elapsed >= self.config.fade_in_duration { |
| 178 | + self.state = AnimationState::Visible; |
| 179 | + false |
| 180 | + } else { |
| 181 | + true |
| 182 | + } |
| 183 | + } |
| 184 | + AnimationState::Disappearing => { |
| 185 | + let elapsed = self.start_time.elapsed(); |
| 186 | + if elapsed >= self.config.fade_out_duration { |
| 187 | + self.state = AnimationState::Hidden; |
| 188 | + false |
| 189 | + } else { |
| 190 | + true |
| 191 | + } |
| 192 | + } |
| 193 | + _ => false, |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + /// Get the current animation state |
| 198 | + pub fn state(&self) -> AnimationState { |
| 199 | + self.state |
| 200 | + } |
| 201 | + |
| 202 | + /// Check if the popup should be visible (not hidden) |
| 203 | + pub fn is_visible(&self) -> bool { |
| 204 | + self.state != AnimationState::Hidden |
| 205 | + } |
| 206 | + |
| 207 | + /// Check if animation is in progress |
| 208 | + pub fn is_animating(&self) -> bool { |
| 209 | + matches!(self.state, AnimationState::Appearing | AnimationState::Reflowing | AnimationState::Disappearing) |
| 210 | + } |
| 211 | + |
| 212 | + /// Get the current animation progress (0.0 to 1.0) |
| 213 | + pub fn progress(&self) -> f64 { |
| 214 | + match self.state { |
| 215 | + AnimationState::Appearing | AnimationState::Reflowing => { |
| 216 | + let elapsed = self.start_time.elapsed(); |
| 217 | + let duration = self.config.fade_in_duration; |
| 218 | + if duration.is_zero() { |
| 219 | + 1.0 |
| 220 | + } else { |
| 221 | + (elapsed.as_secs_f64() / duration.as_secs_f64()).min(1.0) |
| 222 | + } |
| 223 | + } |
| 224 | + AnimationState::Disappearing => { |
| 225 | + let elapsed = self.start_time.elapsed(); |
| 226 | + let duration = self.config.fade_out_duration; |
| 227 | + if duration.is_zero() { |
| 228 | + 1.0 |
| 229 | + } else { |
| 230 | + (elapsed.as_secs_f64() / duration.as_secs_f64()).min(1.0) |
| 231 | + } |
| 232 | + } |
| 233 | + AnimationState::Visible => 1.0, |
| 234 | + AnimationState::Hidden => 0.0, |
| 235 | + } |
| 236 | + } |
| 237 | + |
| 238 | + /// Get the current opacity (0.0 to 1.0) |
| 239 | + pub fn opacity(&self) -> f64 { |
| 240 | + match self.state { |
| 241 | + AnimationState::Appearing => ease_out_cubic(self.progress()), |
| 242 | + AnimationState::Disappearing => 1.0 - ease_out_cubic(self.progress()), |
| 243 | + AnimationState::Visible | AnimationState::Reflowing => 1.0, |
| 244 | + AnimationState::Hidden => 0.0, |
| 245 | + } |
| 246 | + } |
| 247 | + |
| 248 | + /// Get the current position offset from target (for slide animation) |
| 249 | + pub fn position_offset(&self) -> (i32, i32) { |
| 250 | + let (dx, dy) = self.config.slide_direction.offset_multiplier(); |
| 251 | + let distance = self.config.slide_distance; |
| 252 | + |
| 253 | + match self.state { |
| 254 | + AnimationState::Appearing => { |
| 255 | + // Start offset, move toward target |
| 256 | + let progress = ease_out_cubic(self.progress()); |
| 257 | + let remaining = 1.0 - progress; |
| 258 | + ( |
| 259 | + (dx as f64 * distance as f64 * remaining) as i32, |
| 260 | + (dy as f64 * distance as f64 * remaining) as i32, |
| 261 | + ) |
| 262 | + } |
| 263 | + AnimationState::Disappearing => { |
| 264 | + // Move away from target |
| 265 | + let progress = ease_out_cubic(self.progress()); |
| 266 | + ( |
| 267 | + (dx as f64 * distance as f64 * progress) as i32, |
| 268 | + (dy as f64 * distance as f64 * progress) as i32, |
| 269 | + ) |
| 270 | + } |
| 271 | + _ => (0, 0), |
| 272 | + } |
| 273 | + } |
| 274 | + |
| 275 | + /// Get the current position (target + offset) |
| 276 | + pub fn current_position(&self) -> (i32, i32) { |
| 277 | + match self.state { |
| 278 | + AnimationState::Reflowing => { |
| 279 | + // Interpolate between start and target positions |
| 280 | + let progress = ease_out_cubic(self.progress()); |
| 281 | + let x = self.reflow_start_x + ((self.target_x - self.reflow_start_x) as f64 * progress) as i32; |
| 282 | + let y = self.reflow_start_y + ((self.target_y - self.reflow_start_y) as f64 * progress) as i32; |
| 283 | + (x, y) |
| 284 | + } |
| 285 | + _ => { |
| 286 | + let (offset_x, offset_y) = self.position_offset(); |
| 287 | + (self.target_x + offset_x, self.target_y + offset_y) |
| 288 | + } |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + /// Update the target position (e.g., when stack reflows) |
| 293 | + pub fn set_target_position(&mut self, x: i32, y: i32) { |
| 294 | + self.target_x = x; |
| 295 | + self.target_y = y; |
| 296 | + } |
| 297 | + |
| 298 | + /// Get target position |
| 299 | + pub fn target_position(&self) -> (i32, i32) { |
| 300 | + (self.target_x, self.target_y) |
| 301 | + } |
| 302 | +} |
| 303 | + |
| 304 | +/// Ease-out cubic easing function for smooth animations |
| 305 | +fn ease_out_cubic(t: f64) -> f64 { |
| 306 | + 1.0 - (1.0 - t).powi(3) |
| 307 | +} |
| 308 | + |
| 309 | +#[cfg(test)] |
| 310 | +mod tests { |
| 311 | + use super::*; |
| 312 | + |
| 313 | + #[test] |
| 314 | + fn test_slide_direction() { |
| 315 | + assert_eq!(SlideDirection::from_str("up"), SlideDirection::Up); |
| 316 | + assert_eq!(SlideDirection::from_str("DOWN"), SlideDirection::Down); |
| 317 | + assert_eq!(SlideDirection::from_str("invalid"), SlideDirection::None); |
| 318 | + } |
| 319 | + |
| 320 | + #[test] |
| 321 | + fn test_offset_multiplier() { |
| 322 | + assert_eq!(SlideDirection::Up.offset_multiplier(), (0, -1)); |
| 323 | + assert_eq!(SlideDirection::Down.offset_multiplier(), (0, 1)); |
| 324 | + assert_eq!(SlideDirection::Left.offset_multiplier(), (-1, 0)); |
| 325 | + assert_eq!(SlideDirection::Right.offset_multiplier(), (1, 0)); |
| 326 | + } |
| 327 | + |
| 328 | + #[test] |
| 329 | + fn test_animator_disabled() { |
| 330 | + let config = AnimationConfig { |
| 331 | + enabled: false, |
| 332 | + ..Default::default() |
| 333 | + }; |
| 334 | + let mut animator = Animator::new(config, 100, 100); |
| 335 | + animator.start_appear(); |
| 336 | + assert_eq!(animator.state(), AnimationState::Visible); |
| 337 | + } |
| 338 | + |
| 339 | + #[test] |
| 340 | + fn test_ease_out_cubic() { |
| 341 | + assert!((ease_out_cubic(0.0) - 0.0).abs() < 0.001); |
| 342 | + assert!((ease_out_cubic(1.0) - 1.0).abs() < 0.001); |
| 343 | + // Should be > linear at midpoint |
| 344 | + assert!(ease_out_cubic(0.5) > 0.5); |
| 345 | + } |
| 346 | +} |