panes 0.19.0

Renderer-agnostic layout engine with declarative ergonomics
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
#![allow(clippy::unwrap_used, clippy::panic)]
use std::sync::Arc;

use panes::runtime::LayoutRuntime;
use panes::{
    ActivePanelVariant, Axis, FocusDirection, FocusOutcome, FocusRejection, LayoutTree, PaneError,
    StrategyKind, grow,
};

fn kinds(n: usize) -> Vec<Arc<str>> {
    (0..n).map(|i| Arc::from(format!("p{i}"))).collect()
}

fn row_runtime(n: usize) -> LayoutRuntime {
    let k = kinds(n);
    LayoutRuntime::from_strategy(
        StrategyKind::Sequence {
            axis: Axis::Row,
            gap: 0.0,
            ratio: None,
        },
        &k,
    )
    .unwrap()
}

fn col_runtime(n: usize) -> LayoutRuntime {
    let k = kinds(n);
    LayoutRuntime::from_strategy(
        StrategyKind::Sequence {
            axis: Axis::Col,
            gap: 0.0,
            ratio: None,
        },
        &k,
    )
    .unwrap()
}

#[test]
fn right_from_leftmost() {
    let mut rt = row_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();
    rt.focus(p0);
    let frame = rt.resolve(300.0, 100.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(outcome, FocusOutcome::Applied);
    assert_eq!(rt.focused(), Some(p1));
}

#[test]
fn left_from_rightmost() {
    let mut rt = row_runtime(3);
    let p1 = rt.sequence().get(1).unwrap();
    let p2 = rt.sequence().get(2).unwrap();
    rt.focus(p2);
    let frame = rt.resolve(300.0, 100.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Left)
        .unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(outcome, FocusOutcome::Applied);
    assert_eq!(rt.focused(), Some(p1));
}

#[test]
fn no_candidate_in_direction() {
    let mut rt = row_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    rt.focus(p0);
    let frame = rt.resolve(300.0, 100.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Left)
        .unwrap();
    assert_eq!(target, None);
    assert_eq!(outcome, FocusOutcome::Unchanged);
    assert_eq!(rt.focused(), Some(p0));
}

#[test]
fn down_in_column() {
    let mut rt = col_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();
    rt.focus(p0);
    let frame = rt.resolve(100.0, 300.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Down)
        .unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(outcome, FocusOutcome::Applied);
}

#[test]
fn up_in_column() {
    let mut rt = col_runtime(3);
    let p1 = rt.sequence().get(1).unwrap();
    let p2 = rt.sequence().get(2).unwrap();
    rt.focus(p2);
    let frame = rt.resolve(100.0, 300.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Up)
        .unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(outcome, FocusOutcome::Applied);
}

#[test]
fn grid_navigation() {
    // 2x2 grid: row[ col[p0, p1], col[p2, p3] ]
    let mut tree = LayoutTree::new();
    let (p0, n0) = tree.add_panel("p0", grow(1.0)).unwrap();
    let (p1, n1) = tree.add_panel("p1", grow(1.0)).unwrap();
    let (p2, n2) = tree.add_panel("p2", grow(1.0)).unwrap();
    let (p3, n3) = tree.add_panel("p3", grow(1.0)).unwrap();
    let left_col = tree.add_col(0.0, vec![n0, n1]).unwrap();
    let right_col = tree.add_col(0.0, vec![n2, n3]).unwrap();
    let root = tree.add_row(0.0, vec![left_col, right_col]).unwrap();
    tree.set_root(root);

    let k: Vec<Arc<str>> = ["p0", "p1", "p2", "p3"]
        .iter()
        .map(|s| Arc::from(*s))
        .collect();
    let mut rt = LayoutRuntime::from_tree_and_strategy(
        tree,
        StrategyKind::Sequence {
            axis: Axis::Row,
            gap: 0.0,
            ratio: None,
        },
        &k,
    );
    rt.focus(p0);
    let frame = rt.resolve(200.0, 200.0).unwrap();

    // Right from p0 -> p2 (top-right)
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, Some(p2));

    // Down from p2 -> p3
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Down)
        .unwrap();
    assert_eq!(target, Some(p3));

    // Left from p3 -> p1
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Left)
        .unwrap();
    assert_eq!(target, Some(p1));

    // Up from p1 -> p0
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Up)
        .unwrap();
    assert_eq!(target, Some(p0));
}

#[test]
fn master_stack_right() {
    let k = kinds(3);
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::MasterStack {
            master_ratio: 0.5,
            gap: 0.0,
        },
        &k,
    )
    .unwrap();
    let master = rt.sequence().get(0).unwrap();
    rt.focus(master);
    let frame = rt.resolve(200.0, 200.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert!(target.is_some());
    assert_ne!(target.unwrap(), master);
    assert_eq!(outcome, FocusOutcome::Applied);
}

#[test]
fn master_stack_left() {
    let k = kinds(3);
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::MasterStack {
            master_ratio: 0.5,
            gap: 0.0,
        },
        &k,
    )
    .unwrap();
    let master = rt.sequence().get(0).unwrap();
    let stack0 = rt.sequence().get(1).unwrap();
    rt.focus(stack0);
    let frame = rt.resolve(200.0, 200.0).unwrap();

    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Left)
        .unwrap();
    assert_eq!(target, Some(master));
}

#[test]
fn no_focused_panel() {
    let mut tree = LayoutTree::new();
    let mut nids = Vec::new();
    for i in 0..3 {
        let (_, nid) = tree.add_panel(format!("p{i}"), grow(1.0)).unwrap();
        nids.push(nid);
    }
    let root = tree.add_row(0.0, nids).unwrap();
    tree.set_root(root);
    let mut rt = LayoutRuntime::from(tree);
    // No focus set — no candidate, unchanged.
    let frame = rt.resolve(300.0, 100.0).unwrap();

    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, None);
    assert_eq!(outcome, FocusOutcome::Unchanged);
}

#[test]
fn returns_rejected_when_target_cannot_be_focused() {
    let mut tree = LayoutTree::new();
    let (p0, n0) = tree.add_panel("p0", grow(1.0)).unwrap();
    let (p1, n1) = tree.add_panel("p1", grow(1.0)).unwrap();
    let root = tree.add_row(0.0, vec![n0, n1]).unwrap();
    tree.set_root(root);

    let mut sequence = panes::PanelSequence::default();
    sequence.push(p0);
    sequence.push(p1);

    let mut rt = LayoutRuntime::from_tree_and_sequence(tree, sequence);
    let frame = rt.resolve(200.0, 100.0).unwrap();
    rt.tree_mut().remove_panel(p1).unwrap();

    // Candidate found (p1 still in layout snapshot) but focus rejected.
    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();

    assert_eq!(target, Some(p1));
    assert_eq!(
        outcome,
        FocusOutcome::Rejected(FocusRejection::PanelNotFound)
    );
    assert_eq!(rt.focused(), Some(p0));
}

#[test]
fn single_panel() {
    let mut rt = row_runtime(1);
    let p0 = rt.sequence().get(0).unwrap();
    rt.focus(p0);
    let frame = rt.resolve(100.0, 100.0).unwrap();

    for dir in [
        FocusDirection::Left,
        FocusDirection::Right,
        FocusDirection::Up,
        FocusDirection::Down,
    ] {
        let (target, outcome) = rt.focus_direction(frame.layout(), dir).unwrap();
        assert_eq!(target, None);
        assert_eq!(outcome, FocusOutcome::Unchanged);
    }
}

#[test]
fn active_panel_returns_spatial_nav_error() {
    let k = kinds(3);
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Monocle,
            bar_height: 0.0,
        },
        &k,
    )
    .unwrap();
    let frame = rt.resolve(100.0, 100.0).unwrap();

    let result = rt.focus_direction(frame.layout(), FocusDirection::Right);
    assert!(matches!(
        result,
        Err(PaneError::InvalidMutation(
            panes::MutationError::SpatialNavUnsupported
        ))
    ));
}

#[test]
fn window_returns_spatial_nav_error() {
    let k = kinds(3);
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::Window {
            panel_count: 2,
            gap: 0.0,
        },
        &k,
    )
    .unwrap();
    let frame = rt.resolve(100.0, 100.0).unwrap();

    let result = rt.focus_direction(frame.layout(), FocusDirection::Right);
    assert!(matches!(
        result,
        Err(PaneError::InvalidMutation(
            panes::MutationError::SpatialNavUnsupported
        ))
    ));
}

#[test]
fn diagonal_tiebreak() {
    // row[ left_col[s0], right_col[s1, s2, s3] ]
    // 200x300: s0 center (50, 150)
    // s1 center (150, 50), s2 center (150, 150), s3 center (150, 250)
    // Going right from s0: s2 has secondary=0, s1 and s3 have secondary=100
    // s2 wins on secondary distance
    let mut tree = LayoutTree::new();
    let (s0, sn0) = tree.add_panel("s0", grow(1.0)).unwrap();
    let (_, sn1) = tree.add_panel("s1", grow(1.0)).unwrap();
    let (s2, sn2) = tree.add_panel("s2", grow(1.0)).unwrap();
    let (_, sn3) = tree.add_panel("s3", grow(1.0)).unwrap();
    let left = tree.add_col(0.0, vec![sn0]).unwrap();
    let right = tree.add_col(0.0, vec![sn1, sn2, sn3]).unwrap();
    let root = tree.add_row(0.0, vec![left, right]).unwrap();
    tree.set_root(root);

    let k: Vec<Arc<str>> = ["s0", "s1", "s2", "s3"]
        .iter()
        .map(|s| Arc::from(*s))
        .collect();
    let mut rt = LayoutRuntime::from_tree_and_strategy(
        tree,
        StrategyKind::Sequence {
            axis: Axis::Row,
            gap: 0.0,
            ratio: None,
        },
        &k,
    );
    rt.focus(s0);
    let frame = rt.resolve(200.0, 300.0).unwrap();

    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, Some(s2));
}

#[test]
fn collapsed_middle_skipped() {
    // Three panels in a row, collapse the middle one.
    // Right from p0 should skip p1 (zero area) and land on p2.
    let mut rt = row_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();
    let p2 = rt.sequence().get(2).unwrap();
    rt.focus(p0);
    rt.toggle_collapsed(p1).unwrap();
    let frame = rt.resolve(300.0, 100.0).unwrap();

    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, Some(p2));
}

#[test]
fn focus_direction_current_uses_cached_layout() {
    let mut rt = row_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();
    rt.focus(p0);
    rt.resolve(300.0, 100.0).unwrap();

    let (target, outcome) = rt.focus_direction_current(FocusDirection::Right).unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(outcome, FocusOutcome::Applied);
    assert_eq!(rt.focused(), Some(p1));
}

#[test]
fn focus_direction_current_without_resolve_returns_none() {
    let mut rt = row_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    rt.focus(p0);

    let (target, outcome) = rt.focus_direction_current(FocusDirection::Right).unwrap();
    assert_eq!(target, None);
    assert_eq!(outcome, FocusOutcome::Unchanged);
}

#[test]
fn master_stack_down_prefers_cross_axis_overlap() {
    // Issue #22: Down from a mid-stack panel should go to the panel
    // directly below, not jump to the master panel whose center
    // happens to be vertically closer.
    let k: Vec<Arc<str>> = ["editor", "terminal", "logs", "panel-7", "panel-8"]
        .iter()
        .map(|s| Arc::from(*s))
        .collect();
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::MasterStack {
            master_ratio: 0.6,
            gap: 1.0,
        },
        &k,
    )
    .unwrap();

    let logs = rt.sequence().get(2).unwrap();
    let panel7 = rt.sequence().get(3).unwrap();
    let panel8 = rt.sequence().get(4).unwrap();
    let frame = rt.resolve(180.0, 56.0).unwrap();

    // Down from logs → panel-7 (not editor)
    rt.focus(logs);
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Down)
        .unwrap();
    assert_eq!(target, Some(panel7));

    // Down from panel-7 → panel-8
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Down)
        .unwrap();
    assert_eq!(target, Some(panel8));

    // Up from panel-8 → panel-7 (not editor)
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Up)
        .unwrap();
    assert_eq!(target, Some(panel7));

    // Up from panel-7 → logs
    let (target, _) = rt
        .focus_direction(frame.layout(), FocusDirection::Up)
        .unwrap();
    assert_eq!(target, Some(logs));
}

#[test]
fn focus_direction_reports_rejected_when_target_cannot_be_applied() {
    // Build a runtime, resolve, then remove the only directional target.
    // Directional focus should surface the rejection, not hide it behind None.
    let mut tree = LayoutTree::new();
    let (p0, n0) = tree.add_panel("p0", grow(1.0)).unwrap();
    let (p1, n1) = tree.add_panel("p1", grow(1.0)).unwrap();
    let root = tree.add_row(0.0, vec![n0, n1]).unwrap();
    tree.set_root(root);

    let mut sequence = panes::PanelSequence::default();
    sequence.push(p0);
    sequence.push(p1);

    let mut rt = LayoutRuntime::from_tree_and_sequence(tree, sequence);
    rt.focus(p0);
    let frame = rt.resolve(200.0, 100.0).unwrap();

    // Remove p1 from the tree — it's the only rightward candidate.
    rt.tree_mut().remove_panel(p1).unwrap();

    // Candidate was found (p1 still in the layout snapshot) but rejected.
    let (target, outcome) = rt
        .focus_direction(frame.layout(), FocusDirection::Right)
        .unwrap();
    assert_eq!(target, Some(p1));
    assert_eq!(
        outcome,
        FocusOutcome::Rejected(FocusRejection::PanelNotFound)
    );
    assert_eq!(rt.focused(), Some(p0));
}