boxmux 0.240.3373

YAML-driven terminal UI framework for rich, interactive CLI applications and dashboards with PTY 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
use super::{HitRegion, Padding};
use crate::Bounds;

/// ComponentDimensions - Centralizes all UI component mathematical operations
///
/// Eliminates ad hoc math like:
/// - bounds.left() + 1, bounds.right() - 1
/// - width.saturating_sub(2)
/// - (x1 + x2) / 2 for center calculations
/// - Hit testing and coordinate validation
///
/// All component positioning, sizing, and geometric operations centralized here.
#[derive(Debug, Clone)]
pub struct ComponentDimensions {
    bounds: Bounds,
    border_thickness: usize,
    padding: Padding,
    has_scrollbar_vertical: bool,
    has_scrollbar_horizontal: bool,
}

impl ComponentDimensions {
    /// Create new component dimensions
    pub fn new(bounds: Bounds) -> Self {
        Self {
            bounds,
            border_thickness: 1,          // Default border thickness
            padding: Padding::uniform(1), // Default padding
            has_scrollbar_vertical: false,
            has_scrollbar_horizontal: false,
        }
    }

    /// Builder pattern for configuration
    pub fn with_border_thickness(mut self, thickness: usize) -> Self {
        self.border_thickness = thickness;
        self
    }

    pub fn with_padding(mut self, padding: Padding) -> Self {
        self.padding = padding;
        self
    }

    pub fn with_scrollbars(mut self, vertical: bool, horizontal: bool) -> Self {
        self.has_scrollbar_vertical = vertical;
        self.has_scrollbar_horizontal = horizontal;
        self
    }

    /// Get outer bounds (full component including border)
    pub fn outer_bounds(&self) -> Bounds {
        self.bounds
    }

    /// Get border area bounds (just the border region)
    pub fn border_bounds(&self) -> Bounds {
        self.bounds
    }

    /// Get content area bounds (inside border and padding)
    /// Replaces: bounds.left() + 2, bounds.right() - 2, etc.
    pub fn content_bounds(&self) -> Bounds {
        let border = self.border_thickness;
        let left_offset = border + self.padding.left;
        let right_offset = border + self.padding.right;
        let top_offset = border + self.padding.top;
        let bottom_offset = border + self.padding.bottom;

        // Handle scrollbar space
        let scrollbar_vertical_space = if self.has_scrollbar_vertical { 1 } else { 0 };
        let scrollbar_horizontal_space = if self.has_scrollbar_horizontal { 1 } else { 0 };

        Bounds::new(
            self.bounds.x1 + left_offset,
            self.bounds.y1 + top_offset,
            self.bounds
                .x2
                .saturating_sub(right_offset + scrollbar_vertical_space),
            self.bounds
                .y2
                .saturating_sub(bottom_offset + scrollbar_horizontal_space),
        )
    }

    /// Get center point of component
    /// Replaces: (x1 + x2) / 2, (y1 + y2) / 2
    pub fn center(&self) -> (usize, usize) {
        (
            (self.bounds.x1 + self.bounds.x2) / 2,
            (self.bounds.y1 + self.bounds.y2) / 2,
        )
    }

    /// Get content area center
    pub fn content_center(&self) -> (usize, usize) {
        let content = self.content_bounds();
        ((content.x1 + content.x2) / 2, (content.y1 + content.y2) / 2)
    }

    /// Calculate available content width
    /// Replaces: width.saturating_sub(4), bounds.width() - borders
    pub fn content_width(&self) -> usize {
        let content = self.content_bounds();
        content.width()
    }

    /// Calculate available content height
    /// Replaces: height.saturating_sub(4), bounds.height() - borders
    pub fn content_height(&self) -> usize {
        let content = self.content_bounds();
        content.height()
    }

    /// Get border drawing coordinates for each side
    pub fn border_coordinates(&self) -> BorderCoordinates {
        BorderCoordinates {
            top: (
                self.bounds.x1,
                self.bounds.y1,
                self.bounds.x2,
                self.bounds.y1,
            ),
            bottom: (
                self.bounds.x1,
                self.bounds.y2,
                self.bounds.x2,
                self.bounds.y2,
            ),
            left: (
                self.bounds.x1,
                self.bounds.y1,
                self.bounds.x1,
                self.bounds.y2,
            ),
            right: (
                self.bounds.x2,
                self.bounds.y1,
                self.bounds.x2,
                self.bounds.y2,
            ),
        }
    }

    /// Calculate inside border coordinates (border + 1)
    /// Replaces: bounds.left() + 1, bounds.right() - 1, etc.
    pub fn inside_border_bounds(&self) -> Bounds {
        let thickness = self.border_thickness;
        Bounds::new(
            self.bounds.x1 + thickness,
            self.bounds.y1 + thickness,
            self.bounds.x2.saturating_sub(thickness),
            self.bounds.y2.saturating_sub(thickness),
        )
    }

    /// Hit test a point against component regions
    /// Centralizes all coordinate hit testing logic
    pub fn hit_test(&self, x: usize, y: usize) -> HitRegion {
        if !self.bounds.contains_point(x, y) {
            return HitRegion::Outside;
        }

        let content = self.content_bounds();
        if content.contains_point(x, y) {
            return HitRegion::Content;
        }

        // Check scrollbar regions
        if self.has_scrollbar_vertical && x == self.bounds.x2 {
            return HitRegion::ScrollbarVertical;
        }

        if self.has_scrollbar_horizontal && y == self.bounds.y2 {
            return HitRegion::ScrollbarHorizontal;
        }

        // Check tab bar region (top inside border)
        let inside = self.inside_border_bounds();
        if y == inside.y1 && x >= inside.x1 && x <= inside.x2 {
            return HitRegion::TabBar;
        }

        HitRegion::Border
    }

    /// Calculate title positioning within component
    /// Centralizes title positioning math from draw_utils.rs
    pub fn calculate_title_position(
        &self,
        title: &str,
        position: &str,
        padding: usize,
    ) -> TitleLayout {
        let width = self.bounds.width();
        let title_length = title.chars().count();
        let x1 = self.bounds.x1;
        let x2 = self.bounds.x2;

        let (title_start, line_before_length, line_after_length) = match position {
            "left" | "start" => {
                let title_start = x1 + padding;
                let line_before = title_start.saturating_sub(x1);
                let line_after = width.saturating_sub(line_before + title_length);
                (title_start, line_before, line_after)
            }
            "center" => {
                let title_start = x1 + (width.saturating_sub(title_length)) / 2;
                let line_before = title_start.saturating_sub(x1);
                let line_after = width.saturating_sub(line_before + title_length);
                (title_start, line_before, line_after)
            }
            "right" | "end" => {
                let title_start = x2.saturating_sub(title_length + padding);
                let line_before = title_start.saturating_sub(x1);
                let line_after = x2.saturating_sub(title_start + title_length);
                (title_start, line_before, line_after)
            }
            _ => {
                // Default to left
                let title_start = x1 + padding;
                let line_before = title_start.saturating_sub(x1);
                let line_after = width.saturating_sub(line_before + title_length);
                (title_start, line_before, line_after)
            }
        };

        TitleLayout {
            title_x: title_start,
            title_end_x: title_start + title_length - 1,
            line_before_length,
            line_after_length,
        }
    }

    /// Check if point is near corner (for resize detection)
    /// Replaces: x >= bounds.x2.saturating_sub(corner_tolerance) && x <= bounds.x2
    pub fn is_near_corner(&self, x: usize, y: usize, tolerance: usize) -> Option<Corner> {
        let bounds = &self.bounds;

        // Bottom-right corner
        if (x >= bounds.x2.saturating_sub(tolerance) && x <= bounds.x2)
            && (y >= bounds.y2.saturating_sub(tolerance) && y <= bounds.y2)
        {
            return Some(Corner::BottomRight);
        }

        // Top-right corner
        if (x >= bounds.x2.saturating_sub(tolerance) && x <= bounds.x2)
            && (y >= bounds.y1 && y <= bounds.y1 + tolerance)
        {
            return Some(Corner::TopRight);
        }

        // Bottom-left corner
        if (x >= bounds.x1 && x <= bounds.x1 + tolerance)
            && (y >= bounds.y2.saturating_sub(tolerance) && y <= bounds.y2)
        {
            return Some(Corner::BottomLeft);
        }

        // Top-left corner
        if (x >= bounds.x1 && x <= bounds.x1 + tolerance)
            && (y >= bounds.y1 && y <= bounds.y1 + tolerance)
        {
            return Some(Corner::TopLeft);
        }

        None
    }

    /// Calculate space available for child components
    pub fn available_child_space(&self) -> (usize, usize) {
        let content = self.content_bounds();
        (content.width(), content.height())
    }

    /// Validate that dimensions are reasonable
    pub fn validate(&self) -> Result<(), ComponentDimensionError> {
        if self.bounds.width() < self.minimum_width() {
            return Err(ComponentDimensionError::TooNarrow {
                actual: self.bounds.width(),
                minimum: self.minimum_width(),
            });
        }

        if self.bounds.height() < self.minimum_height() {
            return Err(ComponentDimensionError::TooShort {
                actual: self.bounds.height(),
                minimum: self.minimum_height(),
            });
        }

        Ok(())
    }

    fn minimum_width(&self) -> usize {
        2 * self.border_thickness + self.padding.horizontal_total() + 1
    }

    fn minimum_height(&self) -> usize {
        2 * self.border_thickness + self.padding.vertical_total() + 1
    }
}

#[derive(Debug, Clone, PartialEq)]
pub struct BorderCoordinates {
    pub top: (usize, usize, usize, usize), // (x1, y1, x2, y2)
    pub bottom: (usize, usize, usize, usize),
    pub left: (usize, usize, usize, usize),
    pub right: (usize, usize, usize, usize),
}

#[derive(Debug, Clone, PartialEq)]
pub struct TitleLayout {
    pub title_x: usize,
    pub title_end_x: usize,
    pub line_before_length: usize,
    pub line_after_length: usize,
}

#[derive(Debug, Clone, PartialEq)]
pub enum Corner {
    TopLeft,
    TopRight,
    BottomLeft,
    BottomRight,
}

#[derive(Debug, Clone, PartialEq)]
pub enum ComponentDimensionError {
    TooNarrow { actual: usize, minimum: usize },
    TooShort { actual: usize, minimum: usize },
    InvalidPadding,
    InvalidBorder,
}

impl ComponentDimensions {
    /// Get vertical scrollbar track bounds
    /// Replaces: bounds.top() + 1, bounds.bottom() - 1
    pub fn vertical_scrollbar_track_bounds(&self) -> Bounds {
        let x = self.bounds.right();
        let start_y = self.bounds.top() + 1;
        let end_y = self.bounds.bottom().saturating_sub(1);
        Bounds::new(x, start_y, x, end_y)
    }

    /// Get horizontal scrollbar track bounds  
    /// Replaces: bounds.left() + 1, bounds.right() - 1
    pub fn horizontal_scrollbar_track_bounds(&self) -> Bounds {
        let y = self.bounds.bottom();
        let start_x = self.bounds.left() + 1;
        let end_x = self.bounds.right().saturating_sub(1);
        Bounds::new(start_x, y, end_x, y)
    }
}

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

    #[test]
    fn test_content_bounds_calculation() {
        let bounds = Bounds::new(0, 0, 10, 5);
        let dims = ComponentDimensions::new(bounds)
            .with_border_thickness(1)
            .with_padding(Padding::uniform(1));

        let content = dims.content_bounds();
        assert_eq!(content.x1, 2); // border + padding
        assert_eq!(content.y1, 2);
        assert_eq!(content.x2, 8); // 10 - border - padding
        assert_eq!(content.y2, 3); // 5 - border - padding
    }

    #[test]
    fn test_center_calculation() {
        let bounds = Bounds::new(0, 0, 10, 4);
        let dims = ComponentDimensions::new(bounds);

        let (cx, cy) = dims.center();
        assert_eq!(cx, 5); // (0 + 10) / 2
        assert_eq!(cy, 2); // (0 + 4) / 2
    }

    #[test]
    fn test_hit_testing() {
        let bounds = Bounds::new(0, 0, 10, 5);
        let dims = ComponentDimensions::new(bounds);

        // Outside
        assert_eq!(dims.hit_test(15, 15), HitRegion::Outside);

        // Content area
        let content = dims.content_bounds();
        assert_eq!(dims.hit_test(content.x1, content.y1), HitRegion::Content);

        // Border
        assert_eq!(dims.hit_test(0, 0), HitRegion::Border);
    }

    #[test]
    fn test_title_positioning() {
        let bounds = Bounds::new(0, 0, 20, 5);
        let dims = ComponentDimensions::new(bounds);

        let layout = dims.calculate_title_position("Test", "center", 2);

        // Title should be centered: width=21, title=4, so start at (21-4)/2 = 8.5 ≈ 8
        assert_eq!(layout.title_x, 8);
        assert_eq!(layout.title_end_x, 11); // 8 + 4 - 1
    }

    #[test]
    fn test_scrollbar_track_bounds() {
        let bounds = Bounds::new(0, 0, 20, 10);
        let dims = ComponentDimensions::new(bounds);

        // Vertical scrollbar should be at right edge
        let v_track = dims.vertical_scrollbar_track_bounds();
        assert_eq!(v_track.x1, 20); // right edge
        assert_eq!(v_track.y1, 1); // top + 1
        assert_eq!(v_track.y2, 9); // bottom - 1

        // Horizontal scrollbar should be at bottom edge
        let h_track = dims.horizontal_scrollbar_track_bounds();
        assert_eq!(h_track.y1, 10); // bottom edge
        assert_eq!(h_track.x1, 1); // left + 1
        assert_eq!(h_track.x2, 19); // right - 1
    }
}