leptos-helios 0.8.1

High-performance Rust visualization library with Canvas2D, WebGPU, and WebAssembly support
Documentation
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
//! Responsive Breakpoints for leptos-helios
//!
//! This module provides responsive breakpoint management for mobile-first design,
//! including device detection, orientation handling, and breakpoint-based styling.

use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH};
use thiserror::Error;
use tokio::sync::RwLock;

/// Represents a responsive breakpoint
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Breakpoint {
    pub name: String,
    pub min_width: f64,
    pub max_width: Option<f64>,
    pub device_type: DeviceType,
    pub orientation: Orientation,
}

/// Device types for responsive design
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum DeviceType {
    Mobile,
    Tablet,
    Desktop,
    LargeDesktop,
    UltraWide,
}

/// Screen orientation
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum Orientation {
    Portrait,
    Landscape,
}

/// Breakpoint types
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum BreakpointType {
    Mobile,
    Tablet,
    Desktop,
    LargeDesktop,
    UltraWide,
}

/// Breakpoint configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BreakpointConfig {
    pub mobile_breakpoint: f64,
    pub tablet_breakpoint: f64,
    pub desktop_breakpoint: f64,
    pub large_desktop_breakpoint: f64,
    pub ultra_wide_breakpoint: f64,
    pub orientation_detection: bool,
    pub device_detection: bool,
    pub custom_breakpoints: HashMap<String, Breakpoint>,
}

impl Default for BreakpointConfig {
    fn default() -> Self {
        Self {
            mobile_breakpoint: 768.0,
            tablet_breakpoint: 1024.0,
            desktop_breakpoint: 1200.0,
            large_desktop_breakpoint: 1440.0,
            ultra_wide_breakpoint: 1920.0,
            orientation_detection: true,
            device_detection: true,
            custom_breakpoints: HashMap::new(),
        }
    }
}

/// Breakpoint manager for responsive design
pub struct BreakpointManager {
    config: BreakpointConfig,
    current_breakpoint: Arc<RwLock<Breakpoint>>,
    viewport_size: Arc<RwLock<(f64, f64)>>,
    orientation: Arc<RwLock<Orientation>>,
    device_type: Arc<RwLock<DeviceType>>,
    breakpoint_history: Arc<RwLock<Vec<Breakpoint>>>,
    stats: Arc<RwLock<BreakpointStats>>,
}

/// Breakpoint statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BreakpointStats {
    pub total_breakpoint_changes: usize,
    pub mobile_views: usize,
    pub tablet_views: usize,
    pub desktop_views: usize,
    pub large_desktop_views: usize,
    pub ultra_wide_views: usize,
    pub portrait_views: usize,
    pub landscape_views: usize,
    pub last_breakpoint_change: Option<u64>,
    pub average_viewport_width: f64,
    pub average_viewport_height: f64,
}

impl Default for BreakpointStats {
    fn default() -> Self {
        Self {
            total_breakpoint_changes: 0,
            mobile_views: 0,
            tablet_views: 0,
            desktop_views: 0,
            large_desktop_views: 0,
            ultra_wide_views: 0,
            portrait_views: 0,
            landscape_views: 0,
            last_breakpoint_change: None,
            average_viewport_width: 0.0,
            average_viewport_height: 0.0,
        }
    }
}

impl BreakpointManager {
    /// Create a new breakpoint manager
    pub fn new(config: BreakpointConfig) -> Self {
        let default_breakpoint = Self::get_default_mobile_breakpoint();

        Self {
            config,
            current_breakpoint: Arc::new(RwLock::new(default_breakpoint)),
            viewport_size: Arc::new(RwLock::new((375.0, 667.0))), // Default mobile size
            orientation: Arc::new(RwLock::new(Orientation::Portrait)),
            device_type: Arc::new(RwLock::new(DeviceType::Mobile)),
            breakpoint_history: Arc::new(RwLock::new(Vec::new())),
            stats: Arc::new(RwLock::new(BreakpointStats::default())),
        }
    }

    /// Update viewport size and recalculate breakpoint
    pub async fn update_viewport_size(
        &self,
        width: f64,
        height: f64,
    ) -> Result<Breakpoint, BreakpointError> {
        // Validate viewport size
        if width <= 0.0 || height <= 0.0 {
            return Err(BreakpointError::InvalidViewportSize { width, height });
        }

        // Update viewport size
        {
            let mut viewport = self.viewport_size.write().await;
            *viewport = (width, height);
        }

        // Update orientation
        {
            let mut orientation = self.orientation.write().await;
            *orientation = if width > height {
                Orientation::Landscape
            } else {
                Orientation::Portrait
            };
        }

        // Determine device type
        let device_type = self.determine_device_type(width).await;
        {
            let mut device = self.device_type.write().await;
            *device = device_type;
        }

        // Calculate new breakpoint
        let new_breakpoint = self.calculate_breakpoint(width, height, device_type).await;

        // Check if breakpoint changed
        let current_breakpoint = self.current_breakpoint.read().await;
        if *current_breakpoint != new_breakpoint {
            drop(current_breakpoint);

            // Update current breakpoint
            {
                let mut current = self.current_breakpoint.write().await;
                *current = new_breakpoint.clone();
            }

            // Add to history
            {
                let mut history = self.breakpoint_history.write().await;
                history.push(new_breakpoint.clone());
                if history.len() > 100 {
                    history.remove(0);
                }
            }

            // Update stats
            self.update_stats(&new_breakpoint, width, height).await;
        }

        Ok(new_breakpoint)
    }

    /// Get current breakpoint
    pub async fn get_current_breakpoint(&self) -> Breakpoint {
        self.current_breakpoint.read().await.clone()
    }

    /// Get current viewport size
    pub async fn get_viewport_size(&self) -> (f64, f64) {
        self.viewport_size.read().await.clone()
    }

    /// Get current orientation
    pub async fn get_orientation(&self) -> Orientation {
        self.orientation.read().await.clone()
    }

    /// Get current device type
    pub async fn get_device_type(&self) -> DeviceType {
        self.device_type.read().await.clone()
    }

    /// Get breakpoint history
    pub async fn get_breakpoint_history(&self) -> Vec<Breakpoint> {
        self.breakpoint_history.read().await.clone()
    }

    /// Get breakpoint statistics
    pub async fn get_stats(&self) -> BreakpointStats {
        self.stats.read().await.clone()
    }

    /// Update breakpoint configuration
    pub async fn update_config(&mut self, config: BreakpointConfig) -> Result<(), BreakpointError> {
        self.config = config;

        // Recalculate current breakpoint with new config
        let (width, height) = self.get_viewport_size().await;
        let device_type = self.get_device_type().await;
        let new_breakpoint = self.calculate_breakpoint(width, height, device_type).await;

        {
            let mut current = self.current_breakpoint.write().await;
            *current = new_breakpoint;
        }

        Ok(())
    }

    /// Check if current breakpoint matches a specific type
    pub async fn is_breakpoint(&self, breakpoint_type: BreakpointType) -> bool {
        let current = self.get_current_breakpoint().await;
        match breakpoint_type {
            BreakpointType::Mobile => current.device_type == DeviceType::Mobile,
            BreakpointType::Tablet => current.device_type == DeviceType::Tablet,
            BreakpointType::Desktop => current.device_type == DeviceType::Desktop,
            BreakpointType::LargeDesktop => current.device_type == DeviceType::LargeDesktop,
            BreakpointType::UltraWide => current.device_type == DeviceType::UltraWide,
        }
    }

    /// Get breakpoint for a specific width
    pub async fn get_breakpoint_for_width(&self, width: f64) -> Breakpoint {
        let device_type = self.determine_device_type(width).await;
        let (_, height) = self.get_viewport_size().await;
        self.calculate_breakpoint(width, height, device_type).await
    }

    /// Determine device type based on width
    async fn determine_device_type(&self, width: f64) -> DeviceType {
        if width < self.config.mobile_breakpoint {
            DeviceType::Mobile
        } else if width < self.config.tablet_breakpoint {
            DeviceType::Tablet
        } else if width < self.config.desktop_breakpoint {
            DeviceType::Desktop
        } else if width < self.config.large_desktop_breakpoint {
            DeviceType::LargeDesktop
        } else {
            DeviceType::UltraWide
        }
    }

    /// Calculate breakpoint based on dimensions and device type
    async fn calculate_breakpoint(
        &self,
        width: f64,
        height: f64,
        device_type: DeviceType,
    ) -> Breakpoint {
        let orientation = if width > height {
            Orientation::Landscape
        } else {
            Orientation::Portrait
        };

        match device_type {
            DeviceType::Mobile => Breakpoint {
                name: "mobile".to_string(),
                min_width: 0.0,
                max_width: Some(self.config.mobile_breakpoint),
                device_type,
                orientation,
            },
            DeviceType::Tablet => Breakpoint {
                name: "tablet".to_string(),
                min_width: self.config.mobile_breakpoint,
                max_width: Some(self.config.tablet_breakpoint),
                device_type,
                orientation,
            },
            DeviceType::Desktop => Breakpoint {
                name: "desktop".to_string(),
                min_width: self.config.tablet_breakpoint,
                max_width: Some(self.config.desktop_breakpoint),
                device_type,
                orientation,
            },
            DeviceType::LargeDesktop => Breakpoint {
                name: "large-desktop".to_string(),
                min_width: self.config.desktop_breakpoint,
                max_width: Some(self.config.large_desktop_breakpoint),
                device_type,
                orientation,
            },
            DeviceType::UltraWide => Breakpoint {
                name: "ultra-wide".to_string(),
                min_width: self.config.large_desktop_breakpoint,
                max_width: None,
                device_type,
                orientation,
            },
        }
    }

    /// Update breakpoint statistics
    async fn update_stats(&self, breakpoint: &Breakpoint, width: f64, height: f64) {
        let mut stats = self.stats.write().await;
        stats.total_breakpoint_changes += 1;
        stats.last_breakpoint_change = Some(
            SystemTime::now()
                .duration_since(UNIX_EPOCH)
                .unwrap()
                .as_secs(),
        );

        match breakpoint.device_type {
            DeviceType::Mobile => stats.mobile_views += 1,
            DeviceType::Tablet => stats.tablet_views += 1,
            DeviceType::Desktop => stats.desktop_views += 1,
            DeviceType::LargeDesktop => stats.large_desktop_views += 1,
            DeviceType::UltraWide => stats.ultra_wide_views += 1,
        }

        match breakpoint.orientation {
            Orientation::Portrait => stats.portrait_views += 1,
            Orientation::Landscape => stats.landscape_views += 1,
        }

        // Update average viewport dimensions
        let total_changes = stats.total_breakpoint_changes as f64;
        stats.average_viewport_width =
            (stats.average_viewport_width * (total_changes - 1.0) + width) / total_changes;
        stats.average_viewport_height =
            (stats.average_viewport_height * (total_changes - 1.0) + height) / total_changes;
    }

    /// Get default mobile breakpoint
    fn get_default_mobile_breakpoint() -> Breakpoint {
        Breakpoint {
            name: "mobile".to_string(),
            min_width: 0.0,
            max_width: Some(768.0),
            device_type: DeviceType::Mobile,
            orientation: Orientation::Portrait,
        }
    }
}

/// Breakpoint error types
#[derive(Debug, Error)]
pub enum BreakpointError {
    #[error("Invalid viewport size: width={width}, height={height}")]
    InvalidViewportSize { width: f64, height: f64 },

    #[error("Invalid breakpoint configuration: {message}")]
    InvalidConfiguration { message: String },

    #[error("Breakpoint calculation error: {message}")]
    CalculationError { message: String },
}

#[cfg(test)]
mod tests {
    use super::*;

    fn create_test_breakpoint_config() -> BreakpointConfig {
        BreakpointConfig {
            mobile_breakpoint: 768.0,
            tablet_breakpoint: 1024.0,
            desktop_breakpoint: 1200.0,
            large_desktop_breakpoint: 1440.0,
            ultra_wide_breakpoint: 1920.0,
            orientation_detection: true,
            device_detection: true,
            custom_breakpoints: HashMap::new(),
        }
    }

    #[tokio::test]
    async fn test_breakpoint_manager_creation() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        let breakpoint = manager.get_current_breakpoint().await;
        assert_eq!(breakpoint.device_type, DeviceType::Mobile);
        assert_eq!(breakpoint.orientation, Orientation::Portrait);
    }

    #[tokio::test]
    async fn test_mobile_breakpoint() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        let breakpoint = manager.update_viewport_size(375.0, 667.0).await.unwrap();
        assert_eq!(breakpoint.device_type, DeviceType::Mobile);
        assert_eq!(breakpoint.orientation, Orientation::Portrait);
    }

    #[tokio::test]
    async fn test_tablet_breakpoint() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        let breakpoint = manager.update_viewport_size(768.0, 1024.0).await.unwrap();
        assert_eq!(breakpoint.device_type, DeviceType::Tablet);
        assert_eq!(breakpoint.orientation, Orientation::Portrait);
    }

    #[tokio::test]
    async fn test_desktop_breakpoint() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        let breakpoint = manager.update_viewport_size(1100.0, 800.0).await.unwrap();
        assert_eq!(breakpoint.device_type, DeviceType::Desktop);
        assert_eq!(breakpoint.orientation, Orientation::Landscape);
    }

    #[tokio::test]
    async fn test_orientation_detection() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        // Portrait
        let breakpoint = manager.update_viewport_size(375.0, 667.0).await.unwrap();
        assert_eq!(breakpoint.orientation, Orientation::Portrait);

        // Landscape
        let breakpoint = manager.update_viewport_size(667.0, 375.0).await.unwrap();
        assert_eq!(breakpoint.orientation, Orientation::Landscape);
    }

    #[tokio::test]
    async fn test_breakpoint_history() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        // Start with tablet to trigger the first change from default mobile
        manager.update_viewport_size(768.0, 1024.0).await.unwrap();
        manager.update_viewport_size(1100.0, 800.0).await.unwrap();
        manager.update_viewport_size(1300.0, 900.0).await.unwrap();

        let history = manager.get_breakpoint_history().await;
        assert_eq!(history.len(), 3);
        assert_eq!(history[0].device_type, DeviceType::Tablet);
        assert_eq!(history[1].device_type, DeviceType::Desktop);
        assert_eq!(history[2].device_type, DeviceType::LargeDesktop);
    }

    #[tokio::test]
    async fn test_breakpoint_stats() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        // Start with tablet to trigger the first change from default mobile
        manager.update_viewport_size(768.0, 1024.0).await.unwrap();
        manager.update_viewport_size(1100.0, 800.0).await.unwrap();
        manager.update_viewport_size(1300.0, 900.0).await.unwrap();

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_breakpoint_changes, 3);
        assert_eq!(stats.tablet_views, 1);
        assert_eq!(stats.desktop_views, 1);
        assert_eq!(stats.large_desktop_views, 1);
    }

    #[tokio::test]
    async fn test_breakpoint_type_check() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        manager.update_viewport_size(375.0, 667.0).await.unwrap();
        assert!(manager.is_breakpoint(BreakpointType::Mobile).await);
        assert!(!manager.is_breakpoint(BreakpointType::Tablet).await);

        manager.update_viewport_size(768.0, 1024.0).await.unwrap();
        assert!(manager.is_breakpoint(BreakpointType::Tablet).await);
        assert!(!manager.is_breakpoint(BreakpointType::Mobile).await);
    }

    #[tokio::test]
    async fn test_breakpoint_for_width() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        let mobile_breakpoint = manager.get_breakpoint_for_width(375.0).await;
        assert_eq!(mobile_breakpoint.device_type, DeviceType::Mobile);

        let tablet_breakpoint = manager.get_breakpoint_for_width(768.0).await;
        assert_eq!(tablet_breakpoint.device_type, DeviceType::Tablet);

        let desktop_breakpoint = manager.get_breakpoint_for_width(1100.0).await;
        assert_eq!(desktop_breakpoint.device_type, DeviceType::Desktop);
    }

    #[tokio::test]
    async fn test_config_update() {
        let mut config = create_test_breakpoint_config();
        config.mobile_breakpoint = 600.0;

        let mut manager = BreakpointManager::new(config);

        // Update with new config
        let new_config = create_test_breakpoint_config();
        manager.update_config(new_config).await.unwrap();

        // Verify breakpoint is recalculated
        let breakpoint = manager.get_current_breakpoint().await;
        assert_eq!(breakpoint.device_type, DeviceType::Mobile);
    }

    #[tokio::test]
    async fn test_invalid_viewport_size() {
        let config = create_test_breakpoint_config();
        let manager = BreakpointManager::new(config);

        // Test with negative width
        let result = manager.update_viewport_size(-100.0, 600.0).await;
        assert!(result.is_err());

        // Test with zero height
        let result = manager.update_viewport_size(800.0, 0.0).await;
        assert!(result.is_err());
    }
}