egui-cha-ds 0.6.0

Design System for egui-cha (Atoms, Molecules, Theme)
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
//! Dashboard Layout - Three-column layout with top bar
//!
//! A composable layout for dashboard-style applications with:
//! - Fixed-height top bar
//! - Collapsible left/right sidebars
//! - Main content area
//!
//! Works within any `&mut Ui` - can be used inside panels, windows, or anywhere.
//!
//! # Example
//!
//! ```ignore
//! DashboardLayout::new()
//!     .top_bar(48.0, |ui| {
//!         ui.horizontal(|ui| {
//!             Text::h2("MyApp").show(ui);
//!             ui.separator();
//!             // Nav tabs...
//!         });
//!     })
//!     .left_sidebar(SidebarConfig::new(200.0).title("Navigation"), |ui| {
//!         // Sidebar content...
//!     })
//!     .right_sidebar(SidebarConfig::new(280.0).title("Details"), |ui| {
//!         // Details panel...
//!     })
//!     .main(|ui| {
//!         // Main content...
//!     })
//!     .show(ui);
//! ```

use crate::theme::Theme;
use egui::{Color32, Pos2, Rect, Ui, Vec2};

// ============================================================================
// Configuration Types
// ============================================================================

/// Configuration for the top bar
#[derive(Clone, Debug)]
pub struct TopBarConfig {
    pub height: f32,
    pub background: Option<Color32>,
}

impl TopBarConfig {
    pub fn new(height: f32) -> Self {
        Self {
            height,
            background: None,
        }
    }

    pub fn background(mut self, color: Color32) -> Self {
        self.background = Some(color);
        self
    }
}

impl Default for TopBarConfig {
    fn default() -> Self {
        Self::new(48.0)
    }
}

/// Configuration for sidebars
#[derive(Clone, Debug)]
pub struct SidebarConfig {
    pub width: f32,
    pub min_width: f32,
    pub max_width: f32,
    pub title: Option<String>,
    pub collapsible: bool,
    pub collapsed: bool,
    pub resizable: bool,
    pub background: Option<Color32>,
}

impl SidebarConfig {
    pub fn new(width: f32) -> Self {
        Self {
            width,
            min_width: 100.0,
            max_width: 500.0,
            title: None,
            collapsible: false,
            collapsed: false,
            resizable: false, // Disabled by default for Ui-based layout
            background: None,
        }
    }

    pub fn title(mut self, title: impl Into<String>) -> Self {
        self.title = Some(title.into());
        self
    }

    pub fn collapsible(mut self, collapsible: bool) -> Self {
        self.collapsible = collapsible;
        self
    }

    pub fn collapsed(mut self, collapsed: bool) -> Self {
        self.collapsed = collapsed;
        self
    }

    pub fn resizable(mut self, resizable: bool) -> Self {
        self.resizable = resizable;
        self
    }

    pub fn min_width(mut self, width: f32) -> Self {
        self.min_width = width;
        self
    }

    pub fn max_width(mut self, width: f32) -> Self {
        self.max_width = width;
        self
    }

    pub fn background(mut self, color: Color32) -> Self {
        self.background = Some(color);
        self
    }
}

/// Events emitted by DashboardLayout
#[derive(Clone, Debug, PartialEq)]
pub enum DashboardEvent {
    /// Left sidebar collapsed/expanded
    LeftSidebarToggle(bool),
    /// Right sidebar collapsed/expanded
    RightSidebarToggle(bool),
    /// Left sidebar resized
    LeftSidebarResize(f32),
    /// Right sidebar resized
    RightSidebarResize(f32),
}

/// Persistent state for DashboardLayout
#[derive(Clone, Debug)]
pub struct DashboardState {
    pub left_collapsed: bool,
    pub right_collapsed: bool,
    pub left_width: f32,
    pub right_width: f32,
}

impl Default for DashboardState {
    fn default() -> Self {
        Self {
            left_collapsed: false,
            right_collapsed: false,
            left_width: 200.0,
            right_width: 280.0,
        }
    }
}

impl DashboardState {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn with_left_width(mut self, width: f32) -> Self {
        self.left_width = width;
        self
    }

    pub fn with_right_width(mut self, width: f32) -> Self {
        self.right_width = width;
        self
    }

    /// Update state based on event
    pub fn handle_event(&mut self, event: &DashboardEvent) {
        match event {
            DashboardEvent::LeftSidebarToggle(collapsed) => {
                self.left_collapsed = *collapsed;
            }
            DashboardEvent::RightSidebarToggle(collapsed) => {
                self.right_collapsed = *collapsed;
            }
            DashboardEvent::LeftSidebarResize(width) => {
                self.left_width = *width;
            }
            DashboardEvent::RightSidebarResize(width) => {
                self.right_width = *width;
            }
        }
    }
}

// ============================================================================
// DashboardLayout Builder
// ============================================================================

type BoxedUiFn<'a> = Box<dyn FnOnce(&mut Ui) + 'a>;

/// Dashboard layout builder
///
/// Creates a three-column layout within any Ui region.
pub struct DashboardLayout<'a> {
    top_bar: Option<(TopBarConfig, BoxedUiFn<'a>)>,
    left_sidebar: Option<(SidebarConfig, BoxedUiFn<'a>)>,
    right_sidebar: Option<(SidebarConfig, BoxedUiFn<'a>)>,
    main_content: Option<BoxedUiFn<'a>>,
    state: Option<&'a mut DashboardState>,
}

impl<'a> DashboardLayout<'a> {
    pub fn new() -> Self {
        Self {
            top_bar: None,
            left_sidebar: None,
            right_sidebar: None,
            main_content: None,
            state: None,
        }
    }

    /// Set persistent state for the layout
    pub fn state(mut self, state: &'a mut DashboardState) -> Self {
        self.state = Some(state);
        self
    }

    /// Add a top bar with fixed height
    pub fn top_bar(mut self, height: f32, content: impl FnOnce(&mut Ui) + 'a) -> Self {
        self.top_bar = Some((TopBarConfig::new(height), Box::new(content)));
        self
    }

    /// Add a top bar with configuration
    pub fn top_bar_with_config(
        mut self,
        config: TopBarConfig,
        content: impl FnOnce(&mut Ui) + 'a,
    ) -> Self {
        self.top_bar = Some((config, Box::new(content)));
        self
    }

    /// Add a left sidebar with fixed width
    pub fn left_sidebar(mut self, width: f32, content: impl FnOnce(&mut Ui) + 'a) -> Self {
        self.left_sidebar = Some((SidebarConfig::new(width), Box::new(content)));
        self
    }

    /// Add a left sidebar with configuration
    pub fn left_sidebar_with_config(
        mut self,
        config: SidebarConfig,
        content: impl FnOnce(&mut Ui) + 'a,
    ) -> Self {
        self.left_sidebar = Some((config, Box::new(content)));
        self
    }

    /// Add a right sidebar with fixed width
    pub fn right_sidebar(mut self, width: f32, content: impl FnOnce(&mut Ui) + 'a) -> Self {
        self.right_sidebar = Some((SidebarConfig::new(width), Box::new(content)));
        self
    }

    /// Add a right sidebar with configuration
    pub fn right_sidebar_with_config(
        mut self,
        config: SidebarConfig,
        content: impl FnOnce(&mut Ui) + 'a,
    ) -> Self {
        self.right_sidebar = Some((config, Box::new(content)));
        self
    }

    /// Add main content area
    pub fn main(mut self, content: impl FnOnce(&mut Ui) + 'a) -> Self {
        self.main_content = Some(Box::new(content));
        self
    }

    /// Show the layout within the given Ui region
    pub fn show(self, ui: &mut Ui) -> Option<DashboardEvent> {
        let theme = Theme::current(ui.ctx());
        let mut event: Option<DashboardEvent> = None;

        // Get available rect
        let available_rect = ui.available_rect_before_wrap();
        let mut current_y = available_rect.min.y;

        // Calculate collapsed state
        let left_collapsed = self
            .state
            .as_ref()
            .map(|s| s.left_collapsed)
            .unwrap_or(false);
        let right_collapsed = self
            .state
            .as_ref()
            .map(|s| s.right_collapsed)
            .unwrap_or(false);

        // ========================================
        // Top Bar
        // ========================================
        if let Some((config, content)) = self.top_bar {
            let top_bar_rect = Rect::from_min_size(
                Pos2::new(available_rect.min.x, current_y),
                Vec2::new(available_rect.width(), config.height),
            );

            // Use bg_tertiary for better contrast, or custom background
            let bg = config.background.unwrap_or(theme.bg_tertiary);

            // Draw top bar frame
            ui.painter().rect_filled(top_bar_rect, 0.0, bg);

            // Draw bottom border
            ui.painter().hline(
                top_bar_rect.x_range(),
                top_bar_rect.max.y,
                egui::Stroke::new(1.0, theme.border),
            );

            // Create child UI for top bar content
            let mut top_ui = ui.new_child(
                egui::UiBuilder::new()
                    .max_rect(top_bar_rect.shrink2(Vec2::new(theme.spacing_md, theme.spacing_sm))),
            );
            content(&mut top_ui);

            current_y += config.height;
        }

        // ========================================
        // Body (Left + Main + Right)
        // ========================================
        let body_rect = Rect::from_min_max(
            Pos2::new(available_rect.min.x, current_y),
            available_rect.max,
        );

        let mut left_width = 0.0;
        let mut right_width = 0.0;
        let collapsed_width = 28.0;

        // Calculate sidebar widths
        if let Some((ref config, _)) = self.left_sidebar {
            if left_collapsed {
                if config.collapsible {
                    left_width = collapsed_width;
                }
            } else if !config.collapsed {
                left_width = self
                    .state
                    .as_ref()
                    .map(|s| s.left_width)
                    .unwrap_or(config.width);
            }
        }

        if let Some((ref config, _)) = self.right_sidebar {
            if right_collapsed {
                if config.collapsible {
                    right_width = collapsed_width;
                }
            } else if !config.collapsed {
                right_width = self
                    .state
                    .as_ref()
                    .map(|s| s.right_width)
                    .unwrap_or(config.width);
            }
        }

        let main_width = (body_rect.width() - left_width - right_width).max(100.0);

        // ========================================
        // Left Sidebar
        // ========================================
        if let Some((config, content)) = self.left_sidebar {
            let sidebar_rect = Rect::from_min_size(
                Pos2::new(body_rect.min.x, body_rect.min.y),
                Vec2::new(left_width, body_rect.height()),
            );

            let bg = config.background.unwrap_or(theme.bg_secondary);

            // Draw sidebar background
            ui.painter().rect_filled(sidebar_rect, 0.0, bg);
            // Draw right border
            ui.painter().vline(
                sidebar_rect.max.x,
                sidebar_rect.y_range(),
                egui::Stroke::new(1.0, theme.border),
            );

            if left_collapsed && config.collapsible {
                // Collapsed: just show expand button
                let inner_rect = sidebar_rect.shrink(4.0);
                let mut sidebar_ui = ui.new_child(egui::UiBuilder::new().max_rect(inner_rect));
                if sidebar_ui.button("").clicked() {
                    event = Some(DashboardEvent::LeftSidebarToggle(false));
                }
            } else if !config.collapsed {
                // Normal sidebar
                let inner_rect = sidebar_rect.shrink(theme.spacing_sm);
                let mut sidebar_ui = ui.new_child(egui::UiBuilder::new().max_rect(inner_rect));

                // Title and collapse button
                if config.title.is_some() || config.collapsible {
                    sidebar_ui.horizontal(|ui| {
                        if let Some(title) = &config.title {
                            ui.heading(title);
                        }
                        ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
                            if config.collapsible {
                                if ui.small_button("").clicked() {
                                    event = Some(DashboardEvent::LeftSidebarToggle(true));
                                }
                            }
                        });
                    });
                    sidebar_ui.separator();
                }

                // Content with scroll
                egui::ScrollArea::vertical()
                    .id_salt("dashboard_left_sidebar")
                    .show(&mut sidebar_ui, |ui| {
                        content(ui);
                    });
            }
        }

        // ========================================
        // Right Sidebar
        // ========================================
        if let Some((config, content)) = self.right_sidebar {
            let sidebar_rect = Rect::from_min_size(
                Pos2::new(body_rect.max.x - right_width, body_rect.min.y),
                Vec2::new(right_width, body_rect.height()),
            );

            let bg = config.background.unwrap_or(theme.bg_secondary);

            // Draw sidebar background
            ui.painter().rect_filled(sidebar_rect, 0.0, bg);
            // Draw left border
            ui.painter().vline(
                sidebar_rect.min.x,
                sidebar_rect.y_range(),
                egui::Stroke::new(1.0, theme.border),
            );

            if right_collapsed && config.collapsible {
                // Collapsed: just show expand button
                let inner_rect = sidebar_rect.shrink(4.0);
                let mut sidebar_ui = ui.new_child(egui::UiBuilder::new().max_rect(inner_rect));
                if sidebar_ui.button("").clicked() {
                    event = Some(DashboardEvent::RightSidebarToggle(false));
                }
            } else if !config.collapsed {
                // Normal sidebar
                let inner_rect = sidebar_rect.shrink(theme.spacing_sm);
                let mut sidebar_ui = ui.new_child(egui::UiBuilder::new().max_rect(inner_rect));

                // Title and collapse button
                if config.title.is_some() || config.collapsible {
                    sidebar_ui.horizontal(|ui| {
                        if config.collapsible {
                            if ui.small_button("").clicked() {
                                event = Some(DashboardEvent::RightSidebarToggle(true));
                            }
                        }
                        if let Some(title) = &config.title {
                            ui.heading(title);
                        }
                    });
                    sidebar_ui.separator();
                }

                // Content with scroll
                egui::ScrollArea::vertical()
                    .id_salt("dashboard_right_sidebar")
                    .show(&mut sidebar_ui, |ui| {
                        content(ui);
                    });
            }
        }

        // ========================================
        // Main Content
        // ========================================
        if let Some(content) = self.main_content {
            let main_rect = Rect::from_min_size(
                Pos2::new(body_rect.min.x + left_width, body_rect.min.y),
                Vec2::new(main_width, body_rect.height()),
            );

            // Draw main background
            ui.painter().rect_filled(main_rect, 0.0, theme.bg_primary);

            let inner_rect = main_rect.shrink(theme.spacing_md);
            let mut main_ui = ui.new_child(egui::UiBuilder::new().max_rect(inner_rect));
            content(&mut main_ui);
        }

        // Consume the space
        ui.allocate_rect(available_rect, egui::Sense::hover());

        event
    }
}

impl<'a> Default for DashboardLayout<'a> {
    fn default() -> Self {
        Self::new()
    }
}

// ============================================================================
// Convenience Functions
// ============================================================================

/// Create a simple three-column dashboard layout
///
/// # Example
/// ```ignore
/// dashboard_3col(
///     ui,
///     200.0, // left width
///     280.0, // right width
///     |ui| { /* left sidebar */ },
///     |ui| { /* main content */ },
///     |ui| { /* right sidebar */ },
/// );
/// ```
pub fn dashboard_3col(
    ui: &mut Ui,
    left_width: f32,
    right_width: f32,
    left: impl FnOnce(&mut Ui),
    main: impl FnOnce(&mut Ui),
    right: impl FnOnce(&mut Ui),
) {
    DashboardLayout::new()
        .left_sidebar(left_width, left)
        .right_sidebar(right_width, right)
        .main(main)
        .show(ui);
}

/// Create a dashboard with top bar and three columns
///
/// # Example
/// ```ignore
/// dashboard_full(
///     ui,
///     48.0,   // top bar height
///     200.0,  // left width
///     280.0,  // right width
///     |ui| { /* top bar */ },
///     |ui| { /* left sidebar */ },
///     |ui| { /* main content */ },
///     |ui| { /* right sidebar */ },
/// );
/// ```
pub fn dashboard_full(
    ui: &mut Ui,
    top_height: f32,
    left_width: f32,
    right_width: f32,
    top: impl FnOnce(&mut Ui),
    left: impl FnOnce(&mut Ui),
    main: impl FnOnce(&mut Ui),
    right: impl FnOnce(&mut Ui),
) {
    DashboardLayout::new()
        .top_bar(top_height, top)
        .left_sidebar(left_width, left)
        .right_sidebar(right_width, right)
        .main(main)
        .show(ui);
}

// ============================================================================
// Tests
// ============================================================================

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

    #[test]
    fn test_sidebar_config() {
        let config = SidebarConfig::new(200.0)
            .title("Test")
            .collapsible(true)
            .min_width(150.0);

        assert_eq!(config.width, 200.0);
        assert_eq!(config.title, Some("Test".to_string()));
        assert!(config.collapsible);
        assert_eq!(config.min_width, 150.0);
    }

    #[test]
    fn test_dashboard_state() {
        let mut state = DashboardState::new();
        assert!(!state.left_collapsed);

        state.handle_event(&DashboardEvent::LeftSidebarToggle(true));
        assert!(state.left_collapsed);

        state.handle_event(&DashboardEvent::LeftSidebarResize(300.0));
        assert_eq!(state.left_width, 300.0);
    }
}