lune-tty 0.1.0

A GPU-accelerated Wayland terminal emulator for Linux
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
use std::num::NonZeroU32;

use lune_terminal::{PixelPosition, PixelSize};
use winit::dpi::PhysicalPosition;

use crate::PixelRect;

use super::PaneId;

const MIN_RATIO: f32 = 0.15;
const MAX_RATIO: f32 = 0.85;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Axis {
    /// Side-by-side panes separated by a vertical divider.
    Columns,
    /// Stacked panes separated by a horizontal divider.
    Rows,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) enum Direction {
    Left,
    Right,
    Up,
    Down,
}

impl Direction {
    pub(super) const fn axis(self) -> Axis {
        match self {
            Self::Left | Self::Right => Axis::Columns,
            Self::Up | Self::Down => Axis::Rows,
        }
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct SplitPath {
    bits: u64,
    depth: u8,
}

impl SplitPath {
    const ROOT: Self = Self { bits: 0, depth: 0 };

    fn push(self, second: bool) -> Option<Self> {
        if self.depth >= 64 {
            return None;
        }
        let bit = u64::from(second) << self.depth;
        Some(Self {
            bits: self.bits | bit,
            depth: self.depth + 1,
        })
    }

    fn is_second(self, level: u8) -> bool {
        (self.bits >> level) & 1 != 0
    }
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct PaneRect {
    pub(super) id: PaneId,
    pub(super) rect: PixelRect,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Divider {
    pub(super) path: SplitPath,
    pub(super) axis: Axis,
    pub(super) rect: PixelRect,
    pub(super) parent_start: u32,
    pub(super) parent_span: u32,
}

impl Divider {
    pub(super) fn ratio_for(&self, position: PhysicalPosition<f64>) -> f32 {
        let coord = match self.axis {
            Axis::Columns => position.x,
            Axis::Rows => position.y,
        };
        let span = f64::from(self.parent_span.max(1));
        let ratio = ((coord - f64::from(self.parent_start)) / span) as f32;
        ratio.clamp(MIN_RATIO, MAX_RATIO)
    }
}

#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub(super) struct SplitLayout {
    pub(super) panes: Vec<PaneRect>,
    pub(super) dividers: Vec<Divider>,
}

impl SplitLayout {
    pub(super) fn rect_of(&self, id: PaneId) -> Option<PixelRect> {
        self.panes
            .iter()
            .find(|pane| pane.id == id)
            .map(|pane| pane.rect)
    }

    pub(super) fn pane_at(&self, position: PhysicalPosition<f64>) -> Option<PaneId> {
        self.panes
            .iter()
            .find(|pane| rect_contains(pane.rect, position))
            .map(|pane| pane.id)
    }

    pub(super) fn divider_at(
        &self,
        position: PhysicalPosition<f64>,
        margin: f64,
    ) -> Option<Divider> {
        self.dividers
            .iter()
            .copied()
            .find(|divider| match divider.axis {
                Axis::Columns => {
                    let left = f64::from(divider.rect.origin.x) - margin;
                    let right =
                        f64::from(divider.rect.origin.x + divider.rect.size.width.get()) + margin;
                    let top = f64::from(divider.rect.origin.y);
                    let bottom = f64::from(divider.rect.origin.y + divider.rect.size.height.get());
                    position.x >= left
                        && position.x < right
                        && position.y >= top
                        && position.y < bottom
                }
                Axis::Rows => {
                    let left = f64::from(divider.rect.origin.x);
                    let right = f64::from(divider.rect.origin.x + divider.rect.size.width.get());
                    let top = f64::from(divider.rect.origin.y) - margin;
                    let bottom =
                        f64::from(divider.rect.origin.y + divider.rect.size.height.get()) + margin;
                    position.x >= left
                        && position.x < right
                        && position.y >= top
                        && position.y < bottom
                }
            })
    }
}

fn rect_contains(rect: PixelRect, position: PhysicalPosition<f64>) -> bool {
    let x = f64::from(rect.origin.x);
    let y = f64::from(rect.origin.y);
    position.x >= x
        && position.x < x + f64::from(rect.size.width.get())
        && position.y >= y
        && position.y < y + f64::from(rect.size.height.get())
}

#[derive(Clone, Debug)]
enum Node {
    Leaf(PaneId),
    Split {
        axis: Axis,
        ratio: f32,
        first: Box<Node>,
        second: Box<Node>,
    },
}

#[derive(Clone, Debug)]
pub(super) struct SplitTree {
    root: Node,
}

impl SplitTree {
    pub(super) fn new(root: PaneId) -> Self {
        Self {
            root: Node::Leaf(root),
        }
    }

    pub(super) fn contains(&self, id: PaneId) -> bool {
        self.root.contains(id)
    }

    pub(super) fn split(&mut self, target: PaneId, axis: Axis, new_id: PaneId) -> bool {
        self.root.split(target, axis, new_id)
    }

    /// Removes `target` from the tree, collapsing its parent split into its sibling.
    /// Returns the preferred surviving pane to focus, or `None` if `target` was the root.
    pub(super) fn remove(&mut self, target: PaneId) -> Option<PaneId> {
        match &self.root {
            Node::Leaf(id) if *id == target => None,
            _ => {
                let (next_root, next_focus) =
                    remove_node(std::mem::replace(&mut self.root, Node::Leaf(0)), target);
                self.root = next_root;
                next_focus
            }
        }
    }

    pub(super) fn layout(
        &self,
        area: PixelRect,
        divider: u32,
        zoomed: Option<PaneId>,
    ) -> SplitLayout {
        if let Some(id) = zoomed
            && self.contains(id)
        {
            return SplitLayout {
                panes: vec![PaneRect { id, rect: area }],
                dividers: Vec::new(),
            };
        }
        let mut layout = SplitLayout::default();
        self.root
            .layout(area, divider.max(1), SplitPath::ROOT, &mut layout);
        layout
    }

    pub(super) fn focus_neighbor(
        current: PaneId,
        direction: Direction,
        layout: &SplitLayout,
    ) -> Option<PaneId> {
        let origin = layout.rect_of(current)?;
        let (ox0, oy0, ox1, oy1) = rect_bounds(origin);
        let ocx = (ox0 + ox1) / 2;
        let ocy = (oy0 + oy1) / 2;

        let mut best: Option<(PaneId, (u32, u32, u32))> = None;
        for pane in &layout.panes {
            if pane.id == current {
                continue;
            }
            let (x0, y0, x1, y1) = rect_bounds(pane.rect);
            let (primary_gap, overlap, center_dist) = match direction {
                Direction::Left => {
                    if x1 > ox0 {
                        continue;
                    }
                    let overlap = y1.min(oy1).saturating_sub(y0.max(oy0));
                    if overlap == 0 {
                        continue;
                    }
                    (ox0 - x1, overlap, ocy.abs_diff((y0 + y1) / 2))
                }
                Direction::Right => {
                    if x0 < ox1 {
                        continue;
                    }
                    let overlap = y1.min(oy1).saturating_sub(y0.max(oy0));
                    if overlap == 0 {
                        continue;
                    }
                    (x0 - ox1, overlap, ocy.abs_diff((y0 + y1) / 2))
                }
                Direction::Up => {
                    if y1 > oy0 {
                        continue;
                    }
                    let overlap = x1.min(ox1).saturating_sub(x0.max(ox0));
                    if overlap == 0 {
                        continue;
                    }
                    (oy0 - y1, overlap, ocx.abs_diff((x0 + x1) / 2))
                }
                Direction::Down => {
                    if y0 < oy1 {
                        continue;
                    }
                    let overlap = x1.min(ox1).saturating_sub(x0.max(ox0));
                    if overlap == 0 {
                        continue;
                    }
                    (y0 - oy1, overlap, ocx.abs_diff((x0 + x1) / 2))
                }
            };
            // Prefer smallest gap, then center alignment, then larger overlap.
            let score = (primary_gap, center_dist, u32::MAX - overlap);
            if best.is_none_or(|(_, best_score)| score < best_score) {
                best = Some((pane.id, score));
            }
        }
        best.map(|(id, _)| id)
    }

    pub(super) fn resize(&mut self, target: PaneId, direction: Direction, delta: f32) -> bool {
        let axis = direction.axis();
        let signed = match direction {
            Direction::Left | Direction::Up => -delta,
            Direction::Right | Direction::Down => delta,
        };
        self.root.resize(target, axis, signed)
    }

    pub(super) fn set_ratio(&mut self, path: SplitPath, ratio: f32) -> bool {
        let clamped = ratio.clamp(MIN_RATIO, MAX_RATIO);
        self.root.set_ratio(path, 0, clamped)
    }

    pub(super) fn swap_panes(&mut self, first: PaneId, second: PaneId) -> bool {
        if first == second || !self.contains(first) || !self.contains(second) {
            return false;
        }
        self.root.swap_panes(first, second);
        true
    }
}

impl Node {
    fn contains(&self, target: PaneId) -> bool {
        match self {
            Self::Leaf(id) => *id == target,
            Self::Split { first, second, .. } => first.contains(target) || second.contains(target),
        }
    }

    fn first_leaf(&self) -> PaneId {
        match self {
            Self::Leaf(id) => *id,
            Self::Split { first, .. } => first.first_leaf(),
        }
    }

    fn last_leaf(&self) -> PaneId {
        match self {
            Self::Leaf(id) => *id,
            Self::Split { second, .. } => second.last_leaf(),
        }
    }

    fn split(&mut self, target: PaneId, axis: Axis, new_id: PaneId) -> bool {
        match self {
            Self::Leaf(id) if *id == target => {
                let existing = *id;
                *self = Self::Split {
                    axis,
                    ratio: 0.5,
                    first: Box::new(Self::Leaf(existing)),
                    second: Box::new(Self::Leaf(new_id)),
                };
                true
            }
            Self::Leaf(_) => false,
            Self::Split { first, second, .. } => {
                first.split(target, axis, new_id) || second.split(target, axis, new_id)
            }
        }
    }

    fn swap_panes(&mut self, a: PaneId, b: PaneId) {
        match self {
            Self::Leaf(id) => {
                if *id == a {
                    *id = b;
                } else if *id == b {
                    *id = a;
                }
            }
            Self::Split { first, second, .. } => {
                first.swap_panes(a, b);
                second.swap_panes(a, b);
            }
        }
    }

    fn layout(&self, area: PixelRect, divider: u32, path: SplitPath, out: &mut SplitLayout) {
        match self {
            Self::Leaf(id) => out.panes.push(PaneRect {
                id: *id,
                rect: area,
            }),
            Self::Split {
                axis,
                ratio,
                first,
                second,
            } => match *axis {
                Axis::Columns => {
                    let width = area.size.width.get();
                    if width <= divider + 2 {
                        first.layout(area, divider, path, out);
                        return;
                    }
                    let available = width - divider;
                    let first_w = ((available as f32) * *ratio)
                        .round()
                        .clamp(1.0, (available - 1) as f32)
                        as u32;
                    let second_w = available - first_w;
                    let first_rect = PixelRect {
                        origin: area.origin,
                        size: PixelSize {
                            width: NonZeroU32::new(first_w).unwrap(),
                            height: area.size.height,
                        },
                    };
                    let divider_rect = PixelRect {
                        origin: PixelPosition {
                            x: area.origin.x + first_w,
                            y: area.origin.y,
                        },
                        size: PixelSize {
                            width: NonZeroU32::new(divider).unwrap(),
                            height: area.size.height,
                        },
                    };
                    let second_rect = PixelRect {
                        origin: PixelPosition {
                            x: area.origin.x + first_w + divider,
                            y: area.origin.y,
                        },
                        size: PixelSize {
                            width: NonZeroU32::new(second_w).unwrap(),
                            height: area.size.height,
                        },
                    };
                    out.dividers.push(Divider {
                        path,
                        axis: Axis::Columns,
                        rect: divider_rect,
                        parent_start: area.origin.x,
                        parent_span: available,
                    });
                    if let Some(left_path) = path.push(false) {
                        first.layout(first_rect, divider, left_path, out);
                    }
                    if let Some(right_path) = path.push(true) {
                        second.layout(second_rect, divider, right_path, out);
                    }
                }
                Axis::Rows => {
                    let height = area.size.height.get();
                    if height <= divider + 2 {
                        first.layout(area, divider, path, out);
                        return;
                    }
                    let available = height - divider;
                    let first_h = ((available as f32) * *ratio)
                        .round()
                        .clamp(1.0, (available - 1) as f32)
                        as u32;
                    let second_h = available - first_h;
                    let first_rect = PixelRect {
                        origin: area.origin,
                        size: PixelSize {
                            width: area.size.width,
                            height: NonZeroU32::new(first_h).unwrap(),
                        },
                    };
                    let divider_rect = PixelRect {
                        origin: PixelPosition {
                            x: area.origin.x,
                            y: area.origin.y + first_h,
                        },
                        size: PixelSize {
                            width: area.size.width,
                            height: NonZeroU32::new(divider).unwrap(),
                        },
                    };
                    let second_rect = PixelRect {
                        origin: PixelPosition {
                            x: area.origin.x,
                            y: area.origin.y + first_h + divider,
                        },
                        size: PixelSize {
                            width: area.size.width,
                            height: NonZeroU32::new(second_h).unwrap(),
                        },
                    };
                    out.dividers.push(Divider {
                        path,
                        axis: Axis::Rows,
                        rect: divider_rect,
                        parent_start: area.origin.y,
                        parent_span: available,
                    });
                    if let Some(top_path) = path.push(false) {
                        first.layout(first_rect, divider, top_path, out);
                    }
                    if let Some(bottom_path) = path.push(true) {
                        second.layout(second_rect, divider, bottom_path, out);
                    }
                }
            },
        }
    }

    fn resize(&mut self, target: PaneId, axis: Axis, delta: f32) -> bool {
        match self {
            Self::Leaf(_) => false,
            Self::Split {
                axis: node_axis,
                ratio,
                first,
                second,
            } => {
                if first.contains(target) {
                    if first.resize(target, axis, delta) {
                        return true;
                    }
                    if *node_axis == axis {
                        let next = (*ratio + delta).clamp(MIN_RATIO, MAX_RATIO);
                        let changed = (next - *ratio).abs() > f32::EPSILON;
                        *ratio = next;
                        return changed;
                    }
                } else if second.contains(target) {
                    if second.resize(target, axis, delta) {
                        return true;
                    }
                    if *node_axis == axis {
                        let next = (*ratio + delta).clamp(MIN_RATIO, MAX_RATIO);
                        let changed = (next - *ratio).abs() > f32::EPSILON;
                        *ratio = next;
                        return changed;
                    }
                }
                false
            }
        }
    }

    fn set_ratio(&mut self, path: SplitPath, level: u8, next_ratio: f32) -> bool {
        match self {
            Self::Leaf(_) => false,
            Self::Split {
                ratio,
                first,
                second,
                ..
            } => {
                if level == path.depth {
                    let changed = (next_ratio - *ratio).abs() > f32::EPSILON;
                    *ratio = next_ratio;
                    changed
                } else if path.is_second(level) {
                    second.set_ratio(path, level + 1, next_ratio)
                } else {
                    first.set_ratio(path, level + 1, next_ratio)
                }
            }
        }
    }
}

fn remove_node(node: Node, target: PaneId) -> (Node, Option<PaneId>) {
    match node {
        Node::Leaf(id) => (Node::Leaf(id), None),
        Node::Split {
            axis,
            ratio,
            first,
            second,
        } => {
            if matches!(*first, Node::Leaf(id) if id == target) {
                let focus = second.first_leaf();
                return (*second, Some(focus));
            }
            if matches!(*second, Node::Leaf(id) if id == target) {
                let focus = first.last_leaf();
                return (*first, Some(focus));
            }
            let (first, focus) = remove_node(*first, target);
            if focus.is_some() {
                return (
                    Node::Split {
                        axis,
                        ratio,
                        first: Box::new(first),
                        second,
                    },
                    focus,
                );
            }
            let (second, focus) = remove_node(*second, target);
            (
                Node::Split {
                    axis,
                    ratio,
                    first: Box::new(first),
                    second: Box::new(second),
                },
                focus,
            )
        }
    }
}

fn rect_bounds(rect: PixelRect) -> (u32, u32, u32, u32) {
    (
        rect.origin.x,
        rect.origin.y,
        rect.origin.x + rect.size.width.get(),
        rect.origin.y + rect.size.height.get(),
    )
}

#[cfg(test)]
mod tests {
    use std::num::NonZeroU32;

    use lune_terminal::{PixelPosition, PixelSize};
    use winit::dpi::PhysicalPosition;

    use crate::PixelRect;

    use super::{Axis, Direction, SplitTree};

    fn rect(x: u32, y: u32, width: u32, height: u32) -> PixelRect {
        PixelRect {
            origin: PixelPosition { x, y },
            size: PixelSize {
                width: NonZeroU32::new(width).unwrap(),
                height: NonZeroU32::new(height).unwrap(),
            },
        }
    }

    #[test]
    fn split_layout_navigation_resize_zoom_and_remove() {
        let mut tree = SplitTree::new(1);
        assert!(tree.split(1, Axis::Columns, 2));
        assert!(tree.split(2, Axis::Rows, 3));

        let area = rect(0, 0, 1002, 602);
        let layout = tree.layout(area, 2, None);
        assert_eq!(layout.panes.len(), 3);
        assert_eq!(layout.dividers.len(), 2);
        assert_eq!(layout.rect_of(1), Some(rect(0, 0, 500, 602)));
        assert_eq!(layout.rect_of(2), Some(rect(502, 0, 500, 300)));
        assert_eq!(layout.rect_of(3), Some(rect(502, 302, 500, 300)));

        // Spatial navigation.
        assert_eq!(
            SplitTree::focus_neighbor(1, Direction::Right, &layout),
            Some(2)
        );
        assert_eq!(
            SplitTree::focus_neighbor(2, Direction::Down, &layout),
            Some(3)
        );
        assert_eq!(
            SplitTree::focus_neighbor(3, Direction::Up, &layout),
            Some(2)
        );
        assert_eq!(
            SplitTree::focus_neighbor(3, Direction::Left, &layout),
            Some(1)
        );
        assert_eq!(SplitTree::focus_neighbor(1, Direction::Left, &layout), None);

        // Divider hit test and drag resize.
        let main_divider = layout
            .divider_at(PhysicalPosition::new(501.0, 200.0), 4.0)
            .unwrap();
        assert_eq!(main_divider.axis, Axis::Columns);
        assert!(tree.set_ratio(main_divider.path, 0.7));
        let resized = tree.layout(area, 2, None);
        assert_eq!(resized.rect_of(1).unwrap().size.width.get(), 700);
        assert_eq!(resized.rect_of(2).unwrap().size.width.get(), 300);

        // Keyboard resize adjusts nearest ancestor on that axis.
        assert!(tree.resize(3, Direction::Down, 0.1));
        let after_kb = tree.layout(area, 2, None);
        assert_eq!(after_kb.rect_of(2).unwrap().size.height.get(), 360);
        assert_eq!(after_kb.rect_of(3).unwrap().size.height.get(), 240);

        // Swapping two leaves exchanges their rectangles while preserving split ratios.
        let before_1 = after_kb.rect_of(1);
        let before_3 = after_kb.rect_of(3);
        assert!(tree.swap_panes(1, 3));
        let swapped = tree.layout(area, 2, None);
        assert_eq!(swapped.rect_of(1), before_3);
        assert_eq!(swapped.rect_of(3), before_1);
        assert!(tree.swap_panes(1, 3));

        // Zoom collapses layout to a single full-area pane with no dividers.
        let zoomed = tree.layout(area, 2, Some(3));
        assert_eq!(zoomed.panes.len(), 1);
        assert_eq!(zoomed.rect_of(3), Some(area));
        assert!(zoomed.dividers.is_empty());

        // Removing a pane collapses its parent split and returns sibling leaf.
        assert_eq!(tree.remove(3), Some(2));
        let collapsed = tree.layout(area, 2, None);
        assert_eq!(collapsed.panes.len(), 2);
        assert_eq!(collapsed.rect_of(2).unwrap().size.height.get(), 602);
        assert_eq!(tree.remove(2), Some(1));
        assert_eq!(tree.remove(1), None);
    }
}