pandora-kit 0.6.6

Interactive TUI toolkit for the Hefesto framework
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
use ratatui::layout::Rect;

/// Minimum columns the popup must retain when dragged to an edge.
const MIN_VISIBLE_W: u16 = 20;

/// Minimum rows the popup must retain when dragged to an edge.
const MIN_VISIBLE_H: u16 = 5;

/// Minimum popup width when resizing.
const MIN_RESIZE_W: u16 = 20;

/// Minimum popup height when resizing.
const MIN_RESIZE_H: u16 = 5;

/// Which edges of the popup are being resized.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct EdgeSet {
    pub left: bool,
    pub right: bool,
    pub top: bool,
    pub bottom: bool,
}

/// The interactive zone at a given screen position.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Zone {
    None,
    Move,
    Resize(EdgeSet),
}

/// Result of a drag update.
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum DragUpdate {
    None,
    Moved { x: u16, y: u16 },
    Resized { x: u16, y: u16, w: u16, h: u16 },
}

// ── Geometry helpers ──

pub fn contains(rect: Rect, col: u16, row: u16) -> bool {
    col >= rect.x
        && col < rect.x + rect.width
        && row >= rect.y
        && row < rect.y + rect.height
}

pub fn is_on_border(popup: Rect, col: u16, row: u16) -> bool {
    if !contains(popup, col, row) {
        return false;
    }
    let inner = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    !contains(inner, col, row)
}

/// Default zone detection for standard popups:
/// - Border → Resize (with appropriate edges)
/// - Header (rows y+1 to y+3) → Move
/// - Else → None
pub fn default_zone_at(popup: Rect, col: u16, row: u16) -> Zone {
    if !contains(popup, col, row) {
        return Zone::None;
    }

    if is_on_border(popup, col, row) {
        let mut edges = EdgeSet { left: false, right: false, top: false, bottom: false };
        if row == popup.y { edges.top = true; }
        if row == popup.y + popup.height - 1 { edges.bottom = true; }
        if col == popup.x { edges.left = true; }
        if col == popup.x + popup.width - 1 { edges.right = true; }
        return Zone::Resize(edges);
    }

    // Header area (title + divider line)
    let header_top = popup.y + 1;
    let header_bottom = header_top + 2;
    if row >= header_top && row < header_bottom {
        return Zone::Move;
    }

    Zone::None
}

// ── Drag state machine ──

enum DragMode {
    Idle,
    Move,
    Resize(EdgeSet),
}

impl Default for DragMode {
    fn default() -> Self {
        Self::Idle
    }
}

/// State for tracking a popup drag or resize operation.
#[derive(Default)]
pub struct DragState {
    cursor_offset: (u16, u16),
    start_popup: Rect,
    mode: DragMode,
}

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

    /// Start a drag based on the zone at the click position.
    pub fn begin(&mut self, popup: Rect, col: u16, row: u16, zone: Zone) {
        match zone {
            Zone::None => {}
            Zone::Move => {
                let dx = col.saturating_sub(popup.x);
                let dy = row.saturating_sub(popup.y);
                self.cursor_offset = (dx, dy);
                self.start_popup = popup;
                self.mode = DragMode::Move;
            }
            Zone::Resize(edges) => {
                self.cursor_offset = (col, row);
                self.start_popup = popup;
                self.mode = DragMode::Resize(edges);
            }
        }
    }

    /// Update the drag and return the new popup state.
    /// Returns `None`/`DragUpdate::None` if no drag is in progress.
    pub fn update(&self, area: Rect, col: u16, row: u16) -> DragUpdate {
        match &self.mode {
            DragMode::Idle => DragUpdate::None,
            DragMode::Move => {
                let (dx, dy) = self.cursor_offset;
                let max_w = area.width.saturating_sub(MIN_VISIBLE_W);
                let max_h = area.height.saturating_sub(MIN_VISIBLE_H);
                let nx = (col as i16 - dx as i16).clamp(0, max_w as i16) as u16;
                let ny = (row as i16 - dy as i16).clamp(0, max_h as i16) as u16;
                DragUpdate::Moved { x: nx, y: ny }
            }
            DragMode::Resize(edges) => {
                let p = self.start_popup;
                let mut x = p.x;
                let mut y = p.y;
                let mut w = p.width;
                let mut h = p.height;

                if edges.right {
                    w = col.saturating_sub(p.x).max(MIN_RESIZE_W);
                }
                if edges.bottom {
                    h = row.saturating_sub(p.y).max(MIN_RESIZE_H);
                }
                if edges.left {
                    let right = p.x + p.width;
                    let new_x = col.min(right.saturating_sub(MIN_RESIZE_W));
                    w = right.saturating_sub(new_x);
                    x = new_x;
                }
                if edges.top {
                    let bottom = p.y + p.height;
                    let new_y = row.min(bottom.saturating_sub(MIN_RESIZE_H));
                    h = bottom.saturating_sub(new_y);
                    y = new_y;
                }

                DragUpdate::Resized { x, y, w, h }
            }
        }
    }

    /// Whether a drag is in progress.
    pub fn is_dragging(&self) -> bool {
        !matches!(self.mode, DragMode::Idle)
    }

    /// End the drag (called on mouse up).
    pub fn end(&mut self) {
        self.mode = DragMode::Idle;
    }
}

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

    // ── contains / is_on_border / default_zone_at ──

    #[test]
    fn contains_inside() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(contains(r, 15, 8));
    }

    #[test]
    fn contains_outside() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(!contains(r, 5, 8));
        assert!(!contains(r, 35, 8));
    }

    #[test]
    fn contains_excludes_right_and_bottom_edge() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(!contains(r, 30, 8));
        assert!(!contains(r, 15, 15));
    }

    #[test]
    fn is_on_border_top_edge() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(is_on_border(r, 15, 5));
    }

    #[test]
    fn is_on_border_left_edge() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(is_on_border(r, 10, 8));
    }

    #[test]
    fn is_on_border_inside() {
        let r = Rect::new(10, 5, 20, 10);
        assert!(!is_on_border(r, 15, 8));
    }

    #[test]
    fn default_zone_at_header_is_move() {
        let r = Rect::new(0, 0, 60, 20);
        assert_eq!(default_zone_at(r, 5, 1), Zone::Move);
    }

    #[test]
    fn default_zone_at_border_is_resize() {
        let r = Rect::new(0, 0, 60, 20);
        assert!(matches!(default_zone_at(r, 0, 0), Zone::Resize(_)));
    }

    #[test]
    fn default_zone_at_body_is_none() {
        let r = Rect::new(0, 0, 60, 20);
        assert_eq!(default_zone_at(r, 30, 10), Zone::None);
    }

    #[test]
    fn default_zone_at_outside_is_none() {
        let r = Rect::new(0, 0, 60, 20);
        assert_eq!(default_zone_at(r, 100, 5), Zone::None);
    }

    #[test]
    fn default_zone_at_right_edge_is_resize_right() {
        let r = Rect::new(10, 5, 40, 30);
        let zone = default_zone_at(r, 49, 15);
        assert!(matches!(zone, Zone::Resize(e) if e.right && !e.left && !e.top && !e.bottom));
    }

    #[test]
    fn default_zone_at_bottom_edge_is_resize_bottom() {
        let r = Rect::new(10, 5, 40, 30);
        let zone = default_zone_at(r, 20, 34);
        assert!(matches!(zone, Zone::Resize(e) if e.bottom && !e.top && !e.left && !e.right));
    }

    #[test]
    fn default_zone_at_top_left_corner_is_resize_both() {
        let r = Rect::new(10, 5, 40, 30);
        let zone = default_zone_at(r, 10, 5);
        assert!(matches!(zone, Zone::Resize(e) if e.top && e.left));
    }

    // ── begin ──

    #[test]
    fn begin_move_sets_offset() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        assert!(drag.is_dragging());
    }

    #[test]
    fn begin_none_does_not_drag() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::None);
        assert!(!drag.is_dragging());
    }

    #[test]
    fn begin_move_computes_correct_offset() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 25, 15, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 25, 15);
        assert_eq!(result, DragUpdate::Moved { x: 10, y: 5 });
    }

    #[test]
    fn begin_resize_starts_dragging() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, default_zone_at(popup, 49, 15));
        assert!(drag.is_dragging());
    }

    // ── update (move) ──

    #[test]
    fn update_returns_none_when_not_dragging() {
        let drag = DragState::new();
        assert_eq!(drag.update(Rect::new(0, 0, 100, 50), 50, 25), DragUpdate::None);
    }

    #[test]
    fn update_move_returns_origin() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 40, 30);
        assert_eq!(result, DragUpdate::Moved { x: 35, y: 25 });
    }

    #[test]
    fn update_move_clamps_nx_to_area_width_minus_min_w() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 999, 30);
        if let DragUpdate::Moved { x, .. } = result {
            assert!(x <= 80);
        } else {
            panic!("expected Move");
        }
    }

    #[test]
    fn update_move_clamps_ny_to_area_height_minus_min_h() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 30, 999);
        if let DragUpdate::Moved { y, .. } = result {
            assert!(y <= 45);
        } else {
            panic!("expected Move");
        }
    }

    #[test]
    fn update_move_keeps_minimum_visible_size() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 999, 999);
        assert_eq!(result, DragUpdate::Moved { x: 80, y: 45 });
    }

    #[test]
    fn update_move_clamps_to_zero_when_negative() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 0, 0);
        assert_eq!(result, DragUpdate::Moved { x: 0, y: 0 });
    }

    #[test]
    fn update_move_allows_drag_below_middle() {
        let popup = Rect::new(0, 0, 40, 35);
        let mut drag = DragState::new();
        drag.begin(popup, 5, 1, Zone::Move);
        let result = drag.update(Rect::new(0, 0, 100, 50), 5, 26);
        if let DragUpdate::Moved { y, .. } = result {
            assert!(y >= 25, "ny should reach middle of screen, got ny={}", y);
        } else {
            panic!("expected Move");
        }
    }

    // ── update (resize) ──

    #[test]
    fn update_resize_right_increases_width() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, Zone::Resize(EdgeSet { left: false, right: true, top: false, bottom: false }));
        let result = drag.update(Rect::new(0, 0, 100, 50), 60, 15);
        assert_eq!(result, DragUpdate::Resized { x: 10, y: 5, w: 50, h: 30 });
    }

    #[test]
    fn update_resize_bottom_increases_height() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 20, 34, Zone::Resize(EdgeSet { left: false, right: false, top: false, bottom: true }));
        let result = drag.update(Rect::new(0, 0, 100, 50), 20, 40);
        assert_eq!(result, DragUpdate::Resized { x: 10, y: 5, w: 40, h: 35 });
    }

    #[test]
    fn update_resize_left_moves_and_shrinks() {
        let popup = Rect::new(20, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 20, 15, Zone::Resize(EdgeSet { left: true, right: false, top: false, bottom: false }));
        let result = drag.update(Rect::new(0, 0, 100, 50), 30, 15);
        // left edge moved right → smaller width, x changes
        if let DragUpdate::Resized { x, w, .. } = result {
            assert_eq!(x, 30);
            assert!(w <= 30);
        } else {
            panic!("expected Resized");
        }
    }

    #[test]
    fn update_resize_enforces_min_width() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, Zone::Resize(EdgeSet { left: false, right: true, top: false, bottom: false }));
        let result = drag.update(Rect::new(0, 0, 100, 50), 20, 15);
        if let DragUpdate::Resized { w, .. } = result {
            assert!(w >= MIN_RESIZE_W);
        } else {
            panic!("expected Resized");
        }
    }

    // ── multi-step resize ──

    fn unpack_resized(result: DragUpdate) -> Rect {
        match result {
            DragUpdate::Resized { x, y, w, h } => Rect::new(x, y, w, h),
            _ => panic!("expected Resized"),
        }
    }

    #[test]
    fn resize_right_then_right_again() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(10, 5, 40, 30);

        // First drag: resize right to w=50
        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, Zone::Resize(EdgeSet { right: true, ..Default::default() }));
        let r1 = unpack_resized(drag.update(area, 60, 15));
        assert_eq!(r1, Rect::new(10, 5, 50, 30));
        drag.end();

        // Second drag: resize right from new rect to w=55
        drag.begin(r1, r1.x + r1.width - 1, 15, Zone::Resize(EdgeSet { right: true, ..Default::default() }));
        let r2 = unpack_resized(drag.update(area, 65, 15));
        assert_eq!(r2, Rect::new(10, 5, 55, 30));

        // Right edge should be at 10+50=60, now at 10+55=65
        assert_eq!(r2.x + r2.width, 65);
    }

    #[test]
    fn resize_right_then_left_keeps_right_edge() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(10, 5, 40, 30);
        // First drag: resize right to w=50
        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, Zone::Resize(EdgeSet { right: true, ..Default::default() }));
        let r1 = unpack_resized(drag.update(area, 60, 15));
        assert_eq!(r1, Rect::new(10, 5, 50, 30));
        drag.end();

        // The right edge is now at 60
        let new_right = r1.x + r1.width; // 60

        // Second drag: resize left edge to col=5
        // Left click at x=10, drag to x=5
        drag.begin(r1, r1.x, 15, Zone::Resize(EdgeSet { left: true, ..Default::default() }));
        let r2 = unpack_resized(drag.update(area, 5, 15));
        // x should be 5, width should be 60-5=55
        assert_eq!(r2.x, 5);
        assert_eq!(r2.x + r2.width, new_right); // right edge preserved
    }

    #[test]
    fn resize_top_then_bottom_keeps_bottom_edge() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(10, 5, 40, 30);
        let bottom_edge = popup.y + popup.height; // 35

        // First drag: resize top to row=10
        let mut drag = DragState::new();
        drag.begin(popup, 15, 5, Zone::Resize(EdgeSet { top: true, ..Default::default() }));
        let r1 = unpack_resized(drag.update(area, 15, 10));
        // y=10, h=35-10=25, bottom still at 35
        assert_eq!(r1, Rect::new(10, 10, 40, 25));
        assert_eq!(r1.y + r1.height, bottom_edge);
        drag.end();

        // Second drag: resize bottom from row=34 to row=40
        drag.begin(r1, 15, 34, Zone::Resize(EdgeSet { bottom: true, ..Default::default() }));
        let r2 = unpack_resized(drag.update(area, 15, 40));
        // y=10, h=40-10=30
        assert_eq!(r2, Rect::new(10, 10, 40, 30));
        assert_eq!(r2.y + r2.height, 40);
    }

    #[test]
    fn resize_top_then_right_independent_axes() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(10, 5, 40, 30);

        // First drag: resize top to row=10
        let mut drag = DragState::new();
        drag.begin(popup, 15, 5, Zone::Resize(EdgeSet { top: true, ..Default::default() }));
        let r1 = unpack_resized(drag.update(area, 15, 10));
        assert_eq!(r1, Rect::new(10, 10, 40, 25));
        drag.end();

        // Second drag: resize right to col=70
        drag.begin(r1, 49, 15, Zone::Resize(EdgeSet { right: true, ..Default::default() }));
        let r2 = unpack_resized(drag.update(area, 70, 15));
        // y=10 (unchanged), w=70-10=60
        assert_eq!(r2, Rect::new(10, 10, 60, 25));
    }

    #[test]
    fn resize_left_then_top_corner_sequence() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(20, 10, 40, 30);

        // First: resize left edge to col=10, makes popup wider
        let mut drag = DragState::new();
        drag.begin(popup, 20, 15, Zone::Resize(EdgeSet { left: true, ..Default::default() }));
        let r1 = unpack_resized(drag.update(area, 10, 15));
        // x=10, w=60-10=50 (right edge at 20+40=60)
        assert_eq!(r1, Rect::new(10, 10, 50, 30));
        drag.end();

        // Second: resize top to row=15
        drag.begin(r1, 15, 10, Zone::Resize(EdgeSet { top: true, ..Default::default() }));
        let r2 = unpack_resized(drag.update(area, 15, 15));
        // y=15, h=40-15=25 (bottom edge at 10+30=40)
        assert_eq!(r2, Rect::new(10, 15, 50, 25));
    }

    #[test]
    fn resize_single_drag_back_and_forth() {
        let area = Rect::new(0, 0, 100, 50);
        let popup = Rect::new(10, 5, 40, 30);

        let mut drag = DragState::new();
        drag.begin(popup, 49, 15, Zone::Resize(EdgeSet { right: true, ..Default::default() }));

        // Drag wide to w=60
        let r1 = unpack_resized(drag.update(area, 70, 15));
        assert_eq!(r1, Rect::new(10, 5, 60, 30));

        // Drag back narrower to w=45 (same drag, mouse moved left)
        let r2 = unpack_resized(drag.update(area, 55, 15));
        assert_eq!(r2, Rect::new(10, 5, 45, 30));

        // Drag wide again to w=55
        let r3 = unpack_resized(drag.update(area, 65, 15));
        assert_eq!(r3, Rect::new(10, 5, 55, 30));
    }

    // ── end ──

    #[test]
    fn end_clears_state() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        assert!(drag.is_dragging());
        drag.end();
        assert!(!drag.is_dragging());
        assert_eq!(drag.update(Rect::new(0, 0, 100, 50), 40, 30), DragUpdate::None);
    }

    // ── is_dragging ──

    #[test]
    fn is_dragging_false_by_default() {
        let drag = DragState::new();
        assert!(!drag.is_dragging());
    }

    #[test]
    fn is_dragging_true_after_move_begin() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        assert!(drag.is_dragging());
    }

    #[test]
    fn is_dragging_true_after_resize_begin() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Resize(EdgeSet { left: false, right: true, top: false, bottom: false }));
        assert!(drag.is_dragging());
    }

    #[test]
    fn is_dragging_false_after_end() {
        let popup = Rect::new(10, 5, 40, 30);
        let mut drag = DragState::new();
        drag.begin(popup, 15, 10, Zone::Move);
        drag.end();
        assert!(!drag.is_dragging());
    }
}