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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
//! Simplified Animation Engine API
//!
//! This module provides a simplified, user-friendly animation engine API
//! that hides implementation details and provides a clean interface.
use crate::performance::PerformanceReport;
use crate::*;
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::Mutex;
/// Mock engine for non-WASM targets to avoid WASM static access issues
#[cfg(not(target_arch = "wasm32"))]
struct MockEngine {
animations: HashMap<AnimationHandle, bool>,
current_handle: u64,
}
#[cfg(not(target_arch = "wasm32"))]
impl MockEngine {
fn new() -> Self {
Self {
animations: HashMap::new(),
current_handle: 1,
}
}
}
#[cfg(not(target_arch = "wasm32"))]
impl AnimationEngine for MockEngine {
fn is_available(&self) -> bool {
true
}
fn animate(&mut self, _animation: &engine::AnimationConfig) -> Result<AnimationHandle> {
let handle = AnimationHandle(self.current_handle);
self.current_handle += 1;
self.animations.insert(handle, true);
Ok(handle)
}
fn stop(&mut self, handle: AnimationHandle) -> Result<()> {
self.animations.remove(&handle);
Ok(())
}
fn pause(&mut self, _handle: AnimationHandle) -> Result<()> {
Ok(())
}
fn resume(&mut self, _handle: AnimationHandle) -> Result<()> {
Ok(())
}
fn tick(&mut self, _timestamp: f64) -> Result<()> {
Ok(())
}
fn get_state(&self, handle: AnimationHandle) -> Result<PlaybackState> {
if self.animations.contains_key(&handle) {
Ok(PlaybackState::Running)
} else {
Ok(PlaybackState::Completed)
}
}
fn is_running(&self, handle: AnimationHandle) -> bool {
self.animations.contains_key(&handle)
}
fn get_performance_metrics(&self) -> Option<PerformanceReport> {
None
}
}
/// Simplified animation engine that hides implementation details
///
/// This is the main public API for animation operations. It provides
/// a clean, simple interface while hiding the complexity of the
/// underlying hybrid engine implementation.
pub struct SimplifiedAnimationEngine {
/// Internal engine (hidden from public API)
/// Uses different implementations for WASM vs native targets
#[cfg(target_arch = "wasm32")]
internal_engine: Arc<Mutex<OptimizedHybridEngine>>,
#[cfg(not(target_arch = "wasm32"))]
internal_engine: Arc<Mutex<MockEngine>>,
/// Performance metrics cache
performance_cache: Arc<Mutex<Option<PerformanceReport>>>,
/// Global state for batch operations
global_state: Arc<Mutex<GlobalState>>,
}
/// Global state for batch operations
#[derive(Debug, Default)]
struct GlobalState {
/// All active animation handles
active_handles: Vec<AnimationHandle>,
/// Global pause state
globally_paused: bool,
/// Global stop state
globally_stopped: bool,
}
impl SimplifiedAnimationEngine {
/// Create a new simplified animation engine
///
/// This is the main entry point for creating an animation engine.
/// The implementation details are hidden from the user.
pub fn new() -> Self {
// Use conditional compilation to handle WASM vs native targets
#[cfg(target_arch = "wasm32")]
{
Self {
internal_engine: Arc::new(Mutex::new(OptimizedHybridEngine::new())),
performance_cache: Arc::new(Mutex::new(None)),
global_state: Arc::new(Mutex::new(GlobalState::default())),
}
}
#[cfg(not(target_arch = "wasm32"))]
{
// For non-WASM targets, create a mock engine that doesn't access WASM statics
Self {
internal_engine: Arc::new(Mutex::new(MockEngine::new())),
performance_cache: Arc::new(Mutex::new(None)),
global_state: Arc::new(Mutex::new(GlobalState::default())),
}
}
}
/// Check if the animation engine is available
///
/// Returns true if the engine can be used in the current environment.
pub fn is_available(&self) -> bool {
// For now, always available in WASM environment
true
}
/// Start an animation
///
/// # Arguments
/// * `element` - The DOM element to animate
/// * `target` - The target animation values
/// * `transition` - The transition configuration
///
/// # Returns
/// * `Result<AnimationHandle>` - Handle to control the animation
pub fn animate(
&mut self,
#[cfg(feature = "web-sys")] element: &web_sys::Element,
target: &AnimationTarget,
transition: &Transition,
) -> Result<AnimationHandle> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Create animation config for the engine
let config = engine::AnimationConfig {
#[cfg(feature = "web-sys")]
element: element.clone(),
from: HashMap::new(), // Start from current state
to: target.clone(),
transition: transition.clone(),
on_complete_id: None,
on_update_id: None,
};
// Start animation
let handle = engine.animate(&config)?;
// Track handle globally
global_state.active_handles.push(handle);
Ok(handle)
}
/// Stop an animation
///
/// # Arguments
/// * `handle` - The animation handle to stop
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn stop(&mut self, handle: AnimationHandle) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Stop animation
engine.stop(handle)?;
// Remove from global tracking
global_state.active_handles.retain(|&h| h != handle);
Ok(())
}
/// Pause an animation
///
/// # Arguments
/// * `handle` - The animation handle to pause
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn pause(&mut self, handle: AnimationHandle) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
engine.pause(handle)
}
/// Resume a paused animation
///
/// # Arguments
/// * `handle` - The animation handle to resume
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn resume(&mut self, handle: AnimationHandle) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
engine.resume(handle)
}
/// Check if an animation is running
///
/// # Arguments
/// * `handle` - The animation handle to check
///
/// # Returns
/// * `bool` - True if animation is running
pub fn is_running(&self, handle: AnimationHandle) -> bool {
let engine = self.internal_engine.lock().unwrap();
engine.is_running(handle)
}
/// Get the current playback state of an animation
///
/// # Arguments
/// * `handle` - The animation handle to check
///
/// # Returns
/// * `Result<PlaybackState>` - Current state or error
pub fn get_state(&self, handle: AnimationHandle) -> Result<PlaybackState> {
let engine = self.internal_engine.lock().unwrap();
engine.get_state(handle)
}
/// Get performance metrics
///
/// # Returns
/// * `Option<PerformanceReport>` - Performance metrics if available
pub fn get_performance_metrics(&self) -> Option<PerformanceReport> {
let mut cache = self.performance_cache.lock().unwrap();
// Return cached metrics if available
if let Some(ref metrics) = *cache {
return Some(metrics.clone());
}
// Get fresh metrics from internal engine
let engine = self.internal_engine.lock().unwrap();
if let Some(metrics) = engine.get_performance_metrics() {
*cache = Some(metrics.clone());
Some(metrics)
} else {
None
}
}
/// Cleanup all animations and reset engine state
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn cleanup(&mut self) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Stop all active animations
for handle in global_state.active_handles.clone() {
let _ = engine.stop(handle);
}
// Clear global state
global_state.active_handles.clear();
global_state.globally_paused = false;
global_state.globally_stopped = false;
// Clear performance cache
let mut cache = self.performance_cache.lock().unwrap();
*cache = None;
Ok(())
}
/// Stop all active animations
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn stop_all(&mut self) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Stop all active animations
for handle in global_state.active_handles.clone() {
let _ = engine.stop(handle);
}
// Clear global state
global_state.active_handles.clear();
global_state.globally_stopped = true;
Ok(())
}
/// Pause all active animations
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn pause_all(&mut self) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Pause all active animations
for handle in global_state.active_handles.clone() {
let _ = engine.pause(handle);
}
global_state.globally_paused = true;
Ok(())
}
/// Resume all paused animations
///
/// # Returns
/// * `Result<()>` - Success or error
pub fn resume_all(&mut self) -> Result<()> {
let mut engine = self.internal_engine.lock().unwrap();
let mut global_state = self.global_state.lock().unwrap();
// Resume all active animations
for handle in global_state.active_handles.clone() {
let _ = engine.resume(handle);
}
global_state.globally_paused = false;
Ok(())
}
/// Get the number of active animations
///
/// # Returns
/// * `usize` - Number of active animations
pub fn active_animation_count(&self) -> usize {
let global_state = self.global_state.lock().unwrap();
global_state.active_handles.len()
}
/// Check if any animations are running
///
/// # Returns
/// * `bool` - True if any animations are running
pub fn has_active_animations(&self) -> bool {
self.active_animation_count() > 0
}
}
impl Default for SimplifiedAnimationEngine {
fn default() -> Self {
Self::new()
}
}
impl Clone for SimplifiedAnimationEngine {
fn clone(&self) -> Self {
Self {
internal_engine: Arc::clone(&self.internal_engine),
performance_cache: Arc::clone(&self.performance_cache),
global_state: Arc::clone(&self.global_state),
}
}
}
impl std::fmt::Debug for SimplifiedAnimationEngine {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("SimplifiedAnimationEngine")
.field("active_animations", &self.active_animation_count())
.field("has_active_animations", &self.has_active_animations())
.finish()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_simplified_engine_creation() {
let engine = SimplifiedAnimationEngine::new();
assert!(engine.is_available());
assert_eq!(engine.active_animation_count(), 0);
assert!(!engine.has_active_animations());
}
#[test]
fn test_simplified_engine_clone() {
let engine1 = SimplifiedAnimationEngine::new();
let engine2 = engine1.clone();
assert!(engine1.is_available());
assert!(engine2.is_available());
assert_eq!(
engine1.active_animation_count(),
engine2.active_animation_count()
);
}
#[test]
fn test_simplified_engine_debug() {
let engine = SimplifiedAnimationEngine::new();
let debug_str = format!("{:?}", engine);
assert!(debug_str.contains("SimplifiedAnimationEngine"));
assert!(debug_str.contains("active_animations"));
assert!(debug_str.contains("has_active_animations"));
}
}