revue 2.71.1

A Vue-style TUI framework for Rust with CSS styling
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
//! Dock manager for IDE-style layouts
//!
//! Provides a flexible docking system for creating complex, resizable
//! multi-pane UIs similar to IDEs (VS Code, IntelliJ, etc.).

// Allow dead code for public API exports that aren't used yet
#![allow(dead_code)]
//!
//! # Example
//!
//! ```text
//! use revue::widget::layout::dock::{DockManager, DockArea, Panel};
//!
//! DockManager::new()
//!     .left(
//!         DockArea::new("explorer")
//!             .min_width(200)
//!             .panel(explorer_view)
//!     )
//!     .center(
//!         DockArea::new("editor")
//!             .tab("main.rs", editor1)
//!             .tab("lib.rs", editor2)
//!     )
//!     .right(
//!         DockArea::new("properties")
//!             .min_width(200)
//!             .collapsible()
//!             .panel(properties_view)
//!     )
//! ```;

use crate::widget::layout::splitter::Pane;
use crate::widget::layout::tabs::Tabs;
use crate::widget::theme::PLACEHOLDER_FG;
use crate::widget::traits::{RenderContext, View, WidgetProps};
use crate::{impl_props_builders, impl_styled_view};

/// Dock area position
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DockPosition {
    /// Left side
    Left,
    /// Right side
    Right,
    /// Top
    Top,
    /// Bottom
    Bottom,
    /// Center (main content area)
    Center,
}

/// A dockable area (panel with tabs)
pub struct DockArea {
    /// Area identifier
    id: String,
    /// Tabs in this area
    tabs: Vec<TabContent>,
    /// Active tab index
    active_tab: usize,
    /// Minimum size
    min_size: u16,
    /// Maximum size (0 = unlimited)
    max_size: u16,
    /// Initial size ratio (0.0 - 1.0)
    ratio: f32,
    /// Whether collapsible
    collapsible: bool,
    /// Whether collapsed
    collapsed: bool,
    /// Position
    position: DockPosition,
    /// Minimum width constraint (0 = no constraint)
    min_width: u16,
    /// Minimum height constraint (0 = no constraint)
    min_height: u16,
    /// Maximum width constraint (0 = no constraint)
    max_width: u16,
    /// Maximum height constraint (0 = no constraint)
    max_height: u16,
    /// Widget props
    props: WidgetProps,
}

/// Tab content (label + widget)
///
/// Stores a widget that can be rendered within a dock area tab.
pub struct TabContent {
    /// Tab label
    label: String,
    /// Widget to render
    widget: Option<Box<dyn View>>,
}

impl TabContent {
    /// Create a new tab content
    pub fn new(label: impl Into<String>) -> Self {
        Self {
            label: label.into(),
            widget: None,
        }
    }

    /// Set widget
    pub fn widget<W: View + 'static>(mut self, widget: W) -> Self {
        self.widget = Some(Box::new(widget));
        self
    }
}

impl DockArea {
    /// Create a new dock area
    pub fn new(id: impl Into<String>) -> Self {
        Self {
            id: id.into(),
            tabs: Vec::new(),
            active_tab: 0,
            min_size: 100,
            max_size: 0,
            ratio: 0.2,
            collapsible: false,
            collapsed: false,
            position: DockPosition::Left,
            min_width: 0,
            min_height: 0,
            max_width: 0,
            max_height: 0,
            props: WidgetProps::new(),
        }
    }

    /// Set position
    pub fn position(mut self, position: DockPosition) -> Self {
        self.position = position;
        self
    }

    /// Set minimum size
    pub fn min_size(mut self, size: u16) -> Self {
        self.min_size = size;
        self
    }

    /// Set maximum size
    pub fn max_size(mut self, size: u16) -> Self {
        self.max_size = size;
        self
    }

    /// Set size ratio
    pub fn ratio(mut self, ratio: f32) -> Self {
        self.ratio = ratio.clamp(0.0, 1.0);
        self
    }

    /// Set collapsible
    pub fn collapsible(mut self) -> Self {
        self.collapsible = true;
        self
    }

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

    /// Add a tab
    pub fn tab(mut self, label: impl Into<String>) -> Self {
        self.tabs.push(TabContent::new(label.into()));
        self
    }

    /// Add a tab with widget
    pub fn tab_with<W: View + 'static>(mut self, label: impl Into<String>, widget: W) -> Self {
        self.tabs.push(TabContent::new(label.into()).widget(widget));
        self
    }

    /// Add panel content (single widget, no tabs)
    pub fn panel<W: View + 'static>(mut self, widget: W) -> Self {
        let label = self.id.clone();
        self.tabs.push(TabContent::new(label).widget(widget));
        self
    }

    /// Set minimum width constraint
    pub fn min_width(mut self, width: u16) -> Self {
        self.min_width = width;
        self
    }

    /// Set minimum height constraint
    pub fn min_height(mut self, height: u16) -> Self {
        self.min_height = height;
        self
    }

    /// Set maximum width constraint (0 = no limit)
    pub fn max_width(mut self, width: u16) -> Self {
        self.max_width = width;
        self
    }

    /// Set maximum height constraint (0 = no limit)
    pub fn max_height(mut self, height: u16) -> Self {
        self.max_height = height;
        self
    }

    /// Set both min width and height
    pub fn min_dimensions(self, width: u16, height: u16) -> Self {
        self.min_width(width).min_height(height)
    }

    /// Set both max width and height (0 = no limit)
    pub fn max_dimensions(self, width: u16, height: u16) -> Self {
        self.max_width(width).max_height(height)
    }

    /// Set all size constraints at once
    pub fn constrain(self, min_w: u16, min_h: u16, max_w: u16, max_h: u16) -> Self {
        self.min_width(min_w)
            .min_height(min_h)
            .max_width(max_w)
            .max_height(max_h)
    }

    /// Convert to splitter pane
    fn to_pane(&self) -> Pane {
        let mut pane = Pane::new(&self.id)
            .min_size(self.min_size)
            .max_size(self.max_size)
            .ratio(self.ratio);

        if self.collapsible {
            pane = pane.collapsible();
        }
        pane.collapsed = self.collapsed;
        pane
    }
}

impl Clone for DockArea {
    fn clone(&self) -> Self {
        Self {
            id: self.id.clone(),
            tabs: Vec::new(), // Widgets can't be cloned, start with empty tabs
            active_tab: self.active_tab,
            min_size: self.min_size,
            max_size: self.max_size,
            ratio: self.ratio,
            collapsible: self.collapsible,
            collapsed: self.collapsed,
            position: self.position,
            min_width: self.min_width,
            min_height: self.min_height,
            max_width: self.max_width,
            max_height: self.max_height,
            props: self.props.clone(),
        }
    }
}

impl View for DockArea {
    fn render(&self, ctx: &mut RenderContext) {
        if self.collapsed {
            return;
        }

        let rect = ctx.area;

        // Draw tab headers if multiple tabs
        if self.tabs.len() > 1 {
            let tabs_labels: Vec<String> = self.tabs.iter().map(|t| t.label.clone()).collect();
            let tabs = Tabs::new()
                .tabs(tabs_labels)
                .fg(PLACEHOLDER_FG)
                .bg(crate::style::Color::rgb(0, 0, 0));

            // Reserve 1 row for tabs
            if rect.height > 1 {
                let tab_rect = crate::layout::Rect::new(rect.x, rect.y, rect.width, 1);
                let mut tab_ctx = RenderContext::new(ctx.buffer, tab_rect);
                tabs.render(&mut tab_ctx);

                // Render active tab content below
                if let Some(active_tab) = self.tabs.get(self.active_tab) {
                    if let Some(widget) = &active_tab.widget {
                        let content_rect = crate::layout::Rect::new(
                            rect.x,
                            rect.y + 1,
                            rect.width,
                            rect.height.saturating_sub(1),
                        );
                        let mut content_ctx = RenderContext::new(ctx.buffer, content_rect);
                        widget.render(&mut content_ctx);
                    }
                }
            }
        } else if let Some(tab) = self.tabs.first() {
            // Single tab - just render content
            if let Some(widget) = &tab.widget {
                widget.render(ctx);
            }
        }
    }
}

impl_props_builders!(DockArea);
impl_styled_view!(DockArea);

/// Dock manager - orchestrates multiple dock areas
pub struct DockManager {
    /// Left dock area
    left: Option<DockArea>,
    /// Right dock area
    right: Option<DockArea>,
    /// Top dock area
    top: Option<DockArea>,
    /// Bottom dock area
    bottom: Option<DockArea>,
    /// Center dock area (main content)
    center: Option<DockArea>,
    /// Minimum width constraint (0 = no constraint)
    min_width: u16,
    /// Minimum height constraint (0 = no constraint)
    min_height: u16,
    /// Maximum width constraint (0 = no constraint)
    max_width: u16,
    /// Maximum height constraint (0 = no constraint)
    max_height: u16,
    /// Widget props
    props: WidgetProps,
}

impl DockManager {
    /// Create a new dock manager
    pub fn new() -> Self {
        Self {
            left: None,
            right: None,
            top: None,
            bottom: None,
            center: None,
            min_width: 0,
            min_height: 0,
            max_width: 0,
            max_height: 0,
            props: WidgetProps::new(),
        }
    }

    /// Set left dock area
    pub fn left(mut self, area: DockArea) -> Self {
        self.left = Some(area.position(DockPosition::Left));
        self
    }

    /// Set right dock area
    pub fn right(mut self, area: DockArea) -> Self {
        self.right = Some(area.position(DockPosition::Right));
        self
    }

    /// Set top dock area
    pub fn top(mut self, area: DockArea) -> Self {
        self.top = Some(area.position(DockPosition::Top));
        self
    }

    /// Set bottom dock area
    pub fn bottom(mut self, area: DockArea) -> Self {
        self.bottom = Some(area.position(DockPosition::Bottom));
        self
    }

    /// Set center dock area
    pub fn center(mut self, area: DockArea) -> Self {
        self.center = Some(area.position(DockPosition::Center));
        self
    }

    /// Set minimum width constraint
    pub fn min_width(mut self, width: u16) -> Self {
        self.min_width = width;
        self
    }

    /// Set minimum height constraint
    pub fn min_height(mut self, height: u16) -> Self {
        self.min_height = height;
        self
    }

    /// Set maximum width constraint (0 = no limit)
    pub fn max_width(mut self, width: u16) -> Self {
        self.max_width = width;
        self
    }

    /// Set maximum height constraint (0 = no limit)
    pub fn max_height(mut self, height: u16) -> Self {
        self.max_height = height;
        self
    }

    /// Set both min width and height
    pub fn min_size(self, width: u16, height: u16) -> Self {
        self.min_width(width).min_height(height)
    }

    /// Set both max width and height (0 = no limit)
    pub fn max_size(self, width: u16, height: u16) -> Self {
        self.max_width(width).max_height(height)
    }

    /// Set all size constraints at once
    pub fn constrain(self, min_w: u16, min_h: u16, max_w: u16, max_h: u16) -> Self {
        self.min_width(min_w)
            .min_height(min_h)
            .max_width(max_w)
            .max_height(max_h)
    }

    /// Calculate layout based on available areas
    fn calculate_layout(&self, rect: crate::layout::Rect) -> Vec<(DockArea, crate::layout::Rect)> {
        let mut layout = Vec::new();
        let mut current = rect;

        // Reserve top area
        if let Some(ref top) = self.top {
            if !top.collapsed {
                let top_height =
                    (current.height as f32 * top.ratio).max(top.min_size as f32) as u16;
                let top_rect = crate::layout::Rect::new(
                    current.x,
                    current.y,
                    current.width,
                    top_height.min(current.height),
                );
                layout.push(((*top).clone(), top_rect));
                current.y += top_height;
                current.height = current.height.saturating_sub(top_height);
            }
        }

        // Reserve bottom area
        if let Some(ref bottom) = self.bottom {
            if !bottom.collapsed {
                let bottom_height =
                    (current.height as f32 * bottom.ratio).max(bottom.min_size as f32) as u16;
                let bottom_rect = crate::layout::Rect::new(
                    current.x,
                    current.y
                        + current
                            .height
                            .saturating_sub(bottom_height.min(current.height)),
                    current.width,
                    bottom_height.min(current.height),
                );
                layout.push(((*bottom).clone(), bottom_rect));
                current.height = current
                    .height
                    .saturating_sub(bottom_height.min(current.height));
            }
        }

        // Reserve left area
        let mut middle = current;
        if let Some(ref left) = self.left {
            if !left.collapsed {
                let left_width =
                    (middle.width as f32 * left.ratio).max(left.min_size as f32) as u16;
                let left_rect = crate::layout::Rect::new(
                    middle.x,
                    middle.y,
                    left_width.min(middle.width),
                    middle.height,
                );
                layout.push(((*left).clone(), left_rect));
                middle.x += left_width;
                middle.width = middle.width.saturating_sub(left_width);
            }
        }

        // Reserve right area
        if let Some(ref right) = self.right {
            if !right.collapsed {
                let right_width =
                    (middle.width as f32 * right.ratio).max(right.min_size as f32) as u16;
                let right_rect = crate::layout::Rect::new(
                    middle.x + middle.width.saturating_sub(right_width.min(middle.width)),
                    middle.y,
                    right_width.min(middle.width),
                    middle.height,
                );
                layout.push(((*right).clone(), right_rect));
                middle.width = middle.width.saturating_sub(right_width);
            }
        }

        // Center area gets remaining space
        if let Some(ref center) = self.center {
            layout.push(((*center).clone(), middle));
        }

        layout
    }
}

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

impl View for DockManager {
    fn render(&self, ctx: &mut RenderContext) {
        let rect = ctx.area;
        let layout = self.calculate_layout(rect);

        for (area, area_rect) in layout {
            let mut area_ctx = RenderContext::new(ctx.buffer, area_rect);
            area.render(&mut area_ctx);
        }
    }
}

impl_props_builders!(DockManager);
impl_styled_view!(DockManager);

/// Create a new dock manager
pub fn dock() -> DockManager {
    DockManager::new()
}

/// Create a new dock area
pub fn dock_area(id: impl Into<String>) -> DockArea {
    DockArea::new(id)
}

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

    #[test]
    fn test_dock_area_new() {
        let area = DockArea::new("test");
        assert_eq!(area.id, "test");
        assert!(area.tabs.is_empty());
        assert_eq!(area.position, DockPosition::Left);
        assert!(!area.collapsible);
        assert!(!area.collapsed);
    }

    #[test]
    fn test_dock_area_builder() {
        let area = DockArea::new("sidebar")
            .position(DockPosition::Right)
            .min_size(50)
            .ratio(0.3)
            .collapsible()
            .tab("Files")
            .tab("Search");
        assert_eq!(area.position, DockPosition::Right);
        assert_eq!(area.min_size, 50);
        assert_eq!(area.ratio, 0.3);
        assert!(area.collapsible);
        assert_eq!(area.tabs.len(), 2);
    }

    #[test]
    fn test_dock_area_tab_with_widget() {
        let area = DockArea::new("editor").tab_with("main.rs", Text::new("code"));
        assert_eq!(area.tabs.len(), 1);
        assert!(area.tabs[0].widget.is_some());
    }

    #[test]
    fn test_dock_position_variants() {
        assert_eq!(DockPosition::Left, DockPosition::Left);
        assert_ne!(DockPosition::Left, DockPosition::Right);
    }

    #[test]
    fn test_dock_area_helper() {
        let a = dock_area("test");
        assert_eq!(a.id, "test");
    }
}