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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
//! Responsive Navigation for leptos-helios
//!
//! This module provides responsive navigation management for charts and visualizations,
//! including mobile navigation patterns, adaptive menus, and touch-friendly controls.

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;

use super::breakpoints::{Breakpoint, DeviceType, Orientation};

/// Navigation types for responsive design
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum NavigationType {
    Horizontal,
    Vertical,
    Dropdown,
    Hamburger,
    Tab,
    Breadcrumb,
    Pagination,
    Sidebar,
}

/// Navigation item configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationItem {
    pub id: String,
    pub label: String,
    pub icon: Option<String>,
    pub url: Option<String>,
    pub children: Vec<NavigationItem>,
    pub visible: bool,
    pub responsive_visibility: HashMap<String, bool>,
    pub touch_target_size: f64,
    pub spacing: f64,
}

/// Responsive navigation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponsiveNavigation {
    pub id: String,
    pub navigation_type: NavigationType,
    pub items: Vec<NavigationItem>,
    pub breakpoint_overrides: HashMap<String, NavigationOverride>,
    pub touch_friendly: bool,
    pub accessibility_enabled: bool,
    pub auto_collapse: bool,
    pub collapse_threshold: f64,
}

/// Navigation override for specific breakpoints
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationOverride {
    pub navigation_type: Option<NavigationType>,
    pub touch_friendly: Option<bool>,
    pub auto_collapse: Option<bool>,
    pub collapse_threshold: Option<f64>,
    pub item_overrides: HashMap<String, NavigationItem>,
}

/// Navigation manager for responsive design
pub struct NavigationManager {
    config: NavigationConfig,
    navigations: Arc<RwLock<HashMap<String, ResponsiveNavigation>>>,
    stats: Arc<RwLock<NavigationStats>>,
}

/// Navigation configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationConfig {
    pub default_navigation_type: NavigationType,
    pub touch_target_min_size: f64,
    pub mobile_navigation_type: NavigationType,
    pub tablet_navigation_type: NavigationType,
    pub desktop_navigation_type: NavigationType,
    pub auto_collapse_enabled: bool,
    pub collapse_threshold: f64,
    pub accessibility_enabled: bool,
    pub touch_friendly_default: bool,
}

impl Default for NavigationConfig {
    fn default() -> Self {
        Self {
            default_navigation_type: NavigationType::Horizontal,
            touch_target_min_size: 44.0,
            mobile_navigation_type: NavigationType::Hamburger,
            tablet_navigation_type: NavigationType::Tab,
            desktop_navigation_type: NavigationType::Horizontal,
            auto_collapse_enabled: true,
            collapse_threshold: 768.0,
            accessibility_enabled: true,
            touch_friendly_default: true,
        }
    }
}

/// Navigation statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NavigationStats {
    pub total_navigations: usize,
    pub responsive_navigations: usize,
    pub mobile_navigations: usize,
    pub tablet_navigations: usize,
    pub desktop_navigations: usize,
    pub horizontal_navigations: usize,
    pub vertical_navigations: usize,
    pub hamburger_navigations: usize,
    pub tab_navigations: usize,
    pub navigation_collapses: usize,
    pub touch_interactions: usize,
}

impl Default for NavigationStats {
    fn default() -> Self {
        Self {
            total_navigations: 0,
            responsive_navigations: 0,
            mobile_navigations: 0,
            tablet_navigations: 0,
            desktop_navigations: 0,
            horizontal_navigations: 0,
            vertical_navigations: 0,
            hamburger_navigations: 0,
            tab_navigations: 0,
            navigation_collapses: 0,
            touch_interactions: 0,
        }
    }
}

impl NavigationManager {
    /// Create a new navigation manager
    pub fn new(config: NavigationConfig) -> Self {
        Self {
            config,
            navigations: Arc::new(RwLock::new(HashMap::new())),
            stats: Arc::new(RwLock::new(NavigationStats::default())),
        }
    }

    /// Create a new responsive navigation
    pub async fn create_navigation(
        &self,
        id: String,
        navigation_type: NavigationType,
    ) -> Result<ResponsiveNavigation, ResponsiveNavigationError> {
        let navigation = ResponsiveNavigation {
            id: id.clone(),
            navigation_type,
            items: Vec::new(),
            breakpoint_overrides: HashMap::new(),
            touch_friendly: self.config.touch_friendly_default,
            accessibility_enabled: self.config.accessibility_enabled,
            auto_collapse: self.config.auto_collapse_enabled,
            collapse_threshold: self.config.collapse_threshold,
        };

        // Store navigation
        {
            let mut navigations = self.navigations.write().await;
            navigations.insert(id, navigation.clone());
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.total_navigations += 1;
            stats.responsive_navigations += 1;

            match navigation_type {
                NavigationType::Horizontal => stats.horizontal_navigations += 1,
                NavigationType::Vertical => stats.vertical_navigations += 1,
                NavigationType::Hamburger => stats.hamburger_navigations += 1,
                NavigationType::Tab => stats.tab_navigations += 1,
                _ => {}
            }
        }

        Ok(navigation)
    }

    /// Add navigation item
    pub async fn add_navigation_item(
        &self,
        navigation_id: &str,
        item: NavigationItem,
    ) -> Result<(), ResponsiveNavigationError> {
        let mut navigations = self.navigations.write().await;

        if let Some(navigation) = navigations.get_mut(navigation_id) {
            navigation.items.push(item);
        } else {
            return Err(ResponsiveNavigationError::NavigationNotFound {
                navigation_id: navigation_id.to_string(),
            });
        }

        Ok(())
    }

    /// Apply responsive navigation to a layout
    pub async fn apply_responsive_navigation(
        &self,
        layout: &mut super::layout::ResponsiveLayout,
        breakpoint: &Breakpoint,
    ) -> Result<(), ResponsiveNavigationError> {
        // Determine navigation type for breakpoint
        let navigation_type = self.get_navigation_type_for_breakpoint(breakpoint).await;

        // Apply navigation adjustments to layout items
        for item in &mut layout.items {
            if let Some(navigation_id) = item.responsive_properties.get("navigation_id") {
                if let Some(navigation_id_str) = navigation_id.as_str() {
                    if let Some(navigation) = self.get_navigation(navigation_id_str).await {
                        let responsive_navigation = self
                            .adapt_navigation_for_breakpoint(
                                &navigation,
                                breakpoint,
                                navigation_type,
                            )
                            .await;

                        // Apply navigation properties to item
                        self.apply_navigation_to_item(item, &responsive_navigation, breakpoint)
                            .await?;
                    }
                }
            }
        }

        // Update stats
        {
            let mut stats = self.stats.write().await;

            match breakpoint.device_type {
                DeviceType::Mobile => stats.mobile_navigations += 1,
                DeviceType::Tablet => stats.tablet_navigations += 1,
                DeviceType::Desktop => stats.desktop_navigations += 1,
                _ => {}
            }
        }

        Ok(())
    }

    /// Get navigation type for breakpoint
    async fn get_navigation_type_for_breakpoint(&self, breakpoint: &Breakpoint) -> NavigationType {
        match breakpoint.device_type {
            DeviceType::Mobile => self.config.mobile_navigation_type,
            DeviceType::Tablet => self.config.tablet_navigation_type,
            DeviceType::Desktop => self.config.desktop_navigation_type,
            _ => self.config.default_navigation_type,
        }
    }

    /// Adapt navigation for breakpoint
    async fn adapt_navigation_for_breakpoint(
        &self,
        navigation: &ResponsiveNavigation,
        breakpoint: &Breakpoint,
        navigation_type: NavigationType,
    ) -> ResponsiveNavigation {
        let mut adapted_navigation = navigation.clone();

        // Apply breakpoint-specific overrides
        if let Some(override_config) = navigation
            .breakpoint_overrides
            .get(breakpoint.name.as_str())
        {
            if let Some(override_type) = override_config.navigation_type {
                adapted_navigation.navigation_type = override_type;
            }

            if let Some(override_touch_friendly) = override_config.touch_friendly {
                adapted_navigation.touch_friendly = override_touch_friendly;
            }

            if let Some(override_auto_collapse) = override_config.auto_collapse {
                adapted_navigation.auto_collapse = override_auto_collapse;
            }

            if let Some(override_threshold) = override_config.collapse_threshold {
                adapted_navigation.collapse_threshold = override_threshold;
            }
        } else {
            // Apply default navigation type for breakpoint
            adapted_navigation.navigation_type = navigation_type;
        }

        // Apply touch-friendly adjustments for mobile
        if breakpoint.device_type == DeviceType::Mobile {
            adapted_navigation.touch_friendly = true;
            adapted_navigation.auto_collapse = true;
        }

        adapted_navigation
    }

    /// Apply navigation to layout item
    async fn apply_navigation_to_item(
        &self,
        item: &mut super::layout::LayoutItem,
        navigation: &ResponsiveNavigation,
        breakpoint: &Breakpoint,
    ) -> Result<(), ResponsiveNavigationError> {
        // Apply navigation type
        item.responsive_properties.insert(
            "navigation_type".to_string(),
            serde_json::Value::String(format!("{:?}", navigation.navigation_type)),
        );

        // Apply touch-friendly sizing
        if navigation.touch_friendly {
            let touch_target_size = self.config.touch_target_min_size;
            item.min_width = Some(touch_target_size);
            item.min_height = Some(touch_target_size);

            // Ensure adequate spacing for touch targets
            if let Some(margin) = &mut item.margin {
                margin.top = margin.top.max(8.0);
                margin.right = margin.right.max(8.0);
                margin.bottom = margin.bottom.max(8.0);
                margin.left = margin.left.max(8.0);
            }
        }

        // Apply auto-collapse behavior
        if navigation.auto_collapse && breakpoint.device_type == DeviceType::Mobile {
            item.responsive_properties
                .insert("auto_collapse".to_string(), serde_json::Value::Bool(true));
            item.responsive_properties.insert(
                "collapse_threshold".to_string(),
                serde_json::Value::Number(
                    serde_json::Number::from_f64(navigation.collapse_threshold).unwrap(),
                ),
            );
        }

        // Apply accessibility features
        if navigation.accessibility_enabled {
            item.responsive_properties.insert(
                "accessibility_enabled".to_string(),
                serde_json::Value::Bool(true),
            );
            item.responsive_properties.insert(
                "aria_label".to_string(),
                serde_json::Value::String("Navigation item".to_string()),
            );
        }

        Ok(())
    }

    /// Get navigation by ID
    pub async fn get_navigation(&self, id: &str) -> Option<ResponsiveNavigation> {
        let navigations = self.navigations.read().await;
        navigations.get(id).cloned()
    }

    /// Update navigation
    pub async fn update_navigation(
        &self,
        id: &str,
        navigation: ResponsiveNavigation,
    ) -> Result<(), ResponsiveNavigationError> {
        let mut navigations = self.navigations.write().await;
        navigations.insert(id.to_string(), navigation);
        Ok(())
    }

    /// Update navigation configuration
    pub async fn update_config(
        &mut self,
        config: NavigationConfig,
    ) -> Result<(), ResponsiveNavigationError> {
        self.config = config;
        Ok(())
    }

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

    /// Handle navigation collapse
    pub async fn handle_navigation_collapse(
        &self,
        navigation_id: &str,
    ) -> Result<(), ResponsiveNavigationError> {
        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.navigation_collapses += 1;
        }

        Ok(())
    }

    /// Handle touch interaction
    pub async fn handle_touch_interaction(
        &self,
        navigation_id: &str,
        item_id: &str,
    ) -> Result<(), ResponsiveNavigationError> {
        // Update stats
        {
            let mut stats = self.stats.write().await;
            stats.touch_interactions += 1;
        }

        Ok(())
    }

    /// Remove navigation
    pub async fn remove_navigation(&self, id: &str) -> Result<(), ResponsiveNavigationError> {
        let mut navigations = self.navigations.write().await;
        navigations.remove(id);
        Ok(())
    }

    /// Get all navigations
    pub async fn get_all_navigations(&self) -> HashMap<String, ResponsiveNavigation> {
        self.navigations.read().await.clone()
    }
}

/// Responsive navigation error types
#[derive(Debug, Error)]
pub enum ResponsiveNavigationError {
    #[error("Navigation not found: {navigation_id}")]
    NavigationNotFound { navigation_id: String },

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

    #[error("Navigation item error: {message}")]
    NavigationItemError { message: String },

    #[error("Touch interaction error: {message}")]
    TouchInteractionError { message: String },

    #[error("Accessibility error: {message}")]
    AccessibilityError { message: String },
}

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

    fn create_test_navigation_config() -> NavigationConfig {
        NavigationConfig {
            default_navigation_type: NavigationType::Horizontal,
            touch_target_min_size: 44.0,
            mobile_navigation_type: NavigationType::Hamburger,
            tablet_navigation_type: NavigationType::Tab,
            desktop_navigation_type: NavigationType::Horizontal,
            auto_collapse_enabled: true,
            collapse_threshold: 768.0,
            accessibility_enabled: true,
            touch_friendly_default: true,
        }
    }

    fn create_test_navigation_item(id: &str, label: &str) -> NavigationItem {
        NavigationItem {
            id: id.to_string(),
            label: label.to_string(),
            icon: None,
            url: None,
            children: Vec::new(),
            visible: true,
            responsive_visibility: HashMap::new(),
            touch_target_size: 44.0,
            spacing: 8.0,
        }
    }

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

    #[tokio::test]
    async fn test_navigation_manager_creation() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_navigations, 0);
    }

    #[tokio::test]
    async fn test_create_horizontal_navigation() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let navigation = manager
            .create_navigation("test-navigation".to_string(), NavigationType::Horizontal)
            .await
            .unwrap();

        assert_eq!(navigation.id, "test-navigation");
        assert_eq!(navigation.navigation_type, NavigationType::Horizontal);
        assert!(navigation.touch_friendly);
        assert!(navigation.accessibility_enabled);

        let stats = manager.get_stats().await;
        assert_eq!(stats.total_navigations, 1);
        assert_eq!(stats.horizontal_navigations, 1);
    }

    #[tokio::test]
    async fn test_create_hamburger_navigation() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let navigation = manager
            .create_navigation("mobile-navigation".to_string(), NavigationType::Hamburger)
            .await
            .unwrap();

        assert_eq!(navigation.navigation_type, NavigationType::Hamburger);

        let stats = manager.get_stats().await;
        assert_eq!(stats.hamburger_navigations, 1);
    }

    #[tokio::test]
    async fn test_add_navigation_item() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        manager
            .create_navigation("test-navigation".to_string(), NavigationType::Horizontal)
            .await
            .unwrap();
        let item = create_test_navigation_item("item-1", "Home");

        manager
            .add_navigation_item("test-navigation", item)
            .await
            .unwrap();

        let navigation = manager.get_navigation("test-navigation").await.unwrap();
        assert_eq!(navigation.items.len(), 1);
        assert_eq!(navigation.items[0].id, "item-1");
        assert_eq!(navigation.items[0].label, "Home");
    }

    #[tokio::test]
    async fn test_navigation_type_for_breakpoint() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let mobile_breakpoint = Breakpoint {
            name: "mobile".to_string(),
            min_width: 0.0,
            max_width: Some(768.0),
            device_type: DeviceType::Mobile,
            orientation: Orientation::Portrait,
        };

        let desktop_breakpoint = Breakpoint {
            name: "desktop".to_string(),
            min_width: 1200.0,
            max_width: None,
            device_type: DeviceType::Desktop,
            orientation: Orientation::Landscape,
        };

        let mobile_type = manager
            .get_navigation_type_for_breakpoint(&mobile_breakpoint)
            .await;
        let desktop_type = manager
            .get_navigation_type_for_breakpoint(&desktop_breakpoint)
            .await;

        assert_eq!(mobile_type, NavigationType::Hamburger);
        assert_eq!(desktop_type, NavigationType::Horizontal);
    }

    #[tokio::test]
    async fn test_adapt_navigation_for_breakpoint() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let navigation = manager
            .create_navigation("test-navigation".to_string(), NavigationType::Horizontal)
            .await
            .unwrap();
        let mobile_breakpoint = create_test_breakpoint();

        let adapted_navigation = manager
            .adapt_navigation_for_breakpoint(
                &navigation,
                &mobile_breakpoint,
                NavigationType::Hamburger,
            )
            .await;

        assert_eq!(
            adapted_navigation.navigation_type,
            NavigationType::Hamburger
        );
        assert!(adapted_navigation.touch_friendly);
        assert!(adapted_navigation.auto_collapse);
    }

    #[tokio::test]
    async fn test_handle_navigation_collapse() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        manager
            .handle_navigation_collapse("test-navigation")
            .await
            .unwrap();

        let stats = manager.get_stats().await;
        assert_eq!(stats.navigation_collapses, 1);
    }

    #[tokio::test]
    async fn test_handle_touch_interaction() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        manager
            .handle_touch_interaction("test-navigation", "item-1")
            .await
            .unwrap();

        let stats = manager.get_stats().await;
        assert_eq!(stats.touch_interactions, 1);
    }

    #[tokio::test]
    async fn test_navigation_not_found() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        let item = create_test_navigation_item("item-1", "Home");
        let result = manager.add_navigation_item("nonexistent", item).await;

        assert!(matches!(
            result,
            Err(ResponsiveNavigationError::NavigationNotFound { .. })
        ));
    }

    #[tokio::test]
    async fn test_remove_navigation() {
        let config = create_test_navigation_config();
        let manager = NavigationManager::new(config);

        manager
            .create_navigation("test-navigation".to_string(), NavigationType::Horizontal)
            .await
            .unwrap();

        let navigation = manager.get_navigation("test-navigation").await;
        assert!(navigation.is_some());

        manager.remove_navigation("test-navigation").await.unwrap();

        let navigation = manager.get_navigation("test-navigation").await;
        assert!(navigation.is_none());
    }

    #[tokio::test]
    async fn test_config_update() {
        let mut config = create_test_navigation_config();
        config.touch_target_min_size = 48.0;

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

        let new_config = create_test_navigation_config();
        manager.update_config(new_config).await.unwrap();

        // Verify config was updated
        let navigation = manager
            .create_navigation("test-navigation".to_string(), NavigationType::Horizontal)
            .await
            .unwrap();
        assert!(navigation.touch_friendly);
    }
}