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
#![allow(clippy::unwrap_used, clippy::panic)]
use std::sync::Arc;

use panes::runtime::LayoutRuntime;
use panes::{ActivePanelVariant, FocusOutcome, FocusRejection, StrategyKind};

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

fn monocle_runtime(n: usize) -> LayoutRuntime {
    let k = kinds(n);
    LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Monocle,
            bar_height: 0.0,
        },
        &k,
    )
    .unwrap()
}

#[test]
fn monocle_focus_change_is_constraint_only() {
    let mut rt = monocle_runtime(3);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();

    // Initially p0 is visible
    let c0 = rt.tree().panel_constraints(p0).unwrap();
    assert!(c0.grow.is_some());

    // Focus p1
    rt.focus(p1);
    assert_eq!(rt.focused(), Some(p1));

    // p0 should now be hidden, p1 visible
    let c0 = rt.tree().panel_constraints(p0).unwrap();
    assert_eq!(c0.fixed, Some(0.0));
    let c1 = rt.tree().panel_constraints(p1).unwrap();
    assert!(c1.grow.is_some());
}

#[test]
fn monocle_add_hides_previous() {
    let mut rt = monocle_runtime(2);
    let p0 = rt.sequence().get(0).unwrap();
    let new_pid = rt.add_panel(Arc::from("p_new")).unwrap();
    assert_eq!(rt.focused(), Some(new_pid));
    let c0 = rt.tree().panel_constraints(p0).unwrap();
    assert_eq!(c0.fixed, Some(0.0));
}

#[test]
fn monocle_remove_focused_shows_neighbor() {
    let mut rt = monocle_runtime(3);
    let p1 = rt.sequence().get(1).unwrap();
    rt.focus(p1);
    let new_focus = rt.remove_panel(p1).unwrap();
    assert!(new_focus.is_some());
    let focus = new_focus.unwrap();
    let c = rt.tree().panel_constraints(focus).unwrap();
    assert!(c.grow.is_some());
}

#[test]
fn monocle_focus_same_panel_is_noop() {
    let mut rt = monocle_runtime(2);
    let p0 = rt.sequence().get(0).unwrap();
    rt.focus(p0);
    assert_eq!(rt.focused(), Some(p0));
}

// -- Tabbed --

#[test]
fn tabbed_sequence_excludes_tab_panels() {
    let rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Tabbed,
            bar_height: 1.0,
        },
        &[
            Arc::from("editor"),
            Arc::from("terminal"),
            Arc::from("logs"),
        ],
    )
    .unwrap();

    // Sequence should only contain content panels, not decoration panels
    assert_eq!(rt.sequence().len(), 3);
    for pid in rt.sequence().iter() {
        assert!(
            !rt.tree().is_decoration(pid),
            "decoration panel should not be in sequence"
        );
    }
}

#[test]
fn tabbed_focus_cycles_content_only() {
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Tabbed,
            bar_height: 1.0,
        },
        &[
            Arc::from("editor"),
            Arc::from("terminal"),
            Arc::from("logs"),
        ],
    )
    .unwrap();

    let p0 = rt.sequence().get(0).unwrap();
    assert_eq!(rt.tree().panel_kind(p0).unwrap(), "editor");

    // Tab through all panels — each should be a content panel
    rt.focus_next();
    let f1 = rt.focused().unwrap();
    assert_eq!(rt.tree().panel_kind(f1).unwrap(), "terminal");

    rt.focus_next();
    let f2 = rt.focused().unwrap();
    assert_eq!(rt.tree().panel_kind(f2).unwrap(), "logs");

    // Previous focus should be hidden
    let c1 = rt.tree().panel_constraints(f1).unwrap();
    assert_eq!(c1.fixed, Some(0.0));
    // Current focus should be visible
    let c2 = rt.tree().panel_constraints(f2).unwrap();
    assert!(c2.grow.is_some());
}

// -- Stacked --

#[test]
fn stacked_sequence_excludes_title_panels() {
    let rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Stacked,
            bar_height: 1.0,
        },
        &[
            Arc::from("editor"),
            Arc::from("terminal"),
            Arc::from("logs"),
        ],
    )
    .unwrap();

    assert_eq!(rt.sequence().len(), 3);
    for pid in rt.sequence().iter() {
        assert!(
            !rt.tree().is_decoration(pid),
            "decoration panel should not be in sequence"
        );
    }
}

#[test]
fn stacked_focus_cycles_content_only() {
    let mut rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Stacked,
            bar_height: 1.0,
        },
        &[
            Arc::from("editor"),
            Arc::from("terminal"),
            Arc::from("logs"),
        ],
    )
    .unwrap();

    let p0 = rt.sequence().get(0).unwrap();
    assert_eq!(rt.tree().panel_kind(p0).unwrap(), "editor");

    rt.focus_next();
    let f1 = rt.focused().unwrap();
    assert_eq!(rt.tree().panel_kind(f1).unwrap(), "terminal");

    // editor should be hidden, terminal visible
    let c0 = rt.tree().panel_constraints(p0).unwrap();
    assert_eq!(c0.fixed, Some(0.0));
    let c1 = rt.tree().panel_constraints(f1).unwrap();
    assert!(c1.grow.is_some());
}

// -- Decoration node identity (Step 1) --

#[test]
fn tabbed_and_stacked_sequences_still_exclude_decoration_nodes() {
    // Tabbed
    let tabbed_rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Tabbed,
            bar_height: 1.0,
        },
        &[Arc::from("a"), Arc::from("b"), Arc::from("c")],
    )
    .unwrap();
    assert_eq!(tabbed_rt.sequence().len(), 3);
    for pid in tabbed_rt.sequence().iter() {
        assert!(
            !tabbed_rt.tree().is_decoration(pid),
            "tabbed sequence must contain only content panels"
        );
    }

    // Stacked
    let stacked_rt = LayoutRuntime::from_strategy(
        StrategyKind::ActivePanel {
            variant: ActivePanelVariant::Stacked,
            bar_height: 1.0,
        },
        &[Arc::from("a"), Arc::from("b"), Arc::from("c")],
    )
    .unwrap();
    assert_eq!(stacked_rt.sequence().len(), 3);
    for pid in stacked_rt.sequence().iter() {
        assert!(
            !stacked_rt.tree().is_decoration(pid),
            "stacked sequence must contain only content panels"
        );
    }
}

// -- Dirty-state split: focus changes are constraint-only --

#[test]
fn monocle_focus_change_is_constraint_only_under_split_dirty_state() {
    let mut rt = monocle_runtime(3);

    // First resolve: establish baseline
    let _ = rt.resolve(200.0, 200.0).unwrap();
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();
    assert_eq!(rt.last_diff().added.len(), 3);

    // Second resolve: settle into unchanged state
    let _ = rt.resolve(200.0, 200.0).unwrap();
    assert_eq!(rt.last_diff().unchanged.len(), 3);

    // Focus p1 — constraint-only change (hide p0, show p1)
    rt.focus(p1);
    assert_eq!(rt.focused(), Some(p1));

    // Third resolve: visibility changed but no topology mutation
    let frame3 = rt.resolve(200.0, 200.0).unwrap();

    // Visibility updated: p1 visible, p0 hidden (zero-height in col layout)
    let r1 = frame3.layout().get(p1).unwrap();
    assert!(r1.h > 0.0, "focused panel should be visible");
    let r0 = frame3.layout().get(p0).unwrap();
    assert!(
        r0.h < 1.0,
        "unfocused panel should be hidden, got h={}",
        r0.h
    );

    // Kind lookup still works (kind caches preserved)
    assert_eq!(frame3.layout().by_kind("p0"), &[p0]);
    assert_eq!(frame3.layout().by_kind("p1"), &[p1]);

    // Diff should show resized/moved, NOT added/removed (same-panel path)
    let diff = rt.last_diff();
    assert!(
        diff.added.is_empty(),
        "focus change must not report added panels"
    );
    assert!(
        diff.removed.is_empty(),
        "focus change must not report removed panels"
    );
}

#[test]
fn window_focus_slide_updates_visibility_without_topology_rebuild() {
    let mut rt = window_runtime(4);

    // First resolve: establish baseline
    let _ = rt.resolve(400.0, 200.0).unwrap();
    let p0 = rt.sequence().get(0).unwrap();
    let p3 = rt.sequence().get(3).unwrap();
    assert_eq!(rt.last_diff().added.len(), 4);

    // Second resolve: settle into unchanged state
    let _ = rt.resolve(400.0, 200.0).unwrap();
    assert_eq!(rt.last_diff().unchanged.len(), 4);

    // Focus p3 — slides window from [0,1] to [2,3]
    rt.focus(p3);
    assert_eq!(rt.focused(), Some(p3));
    assert!(rt.viewport().window_start >= 2);

    // Third resolve: window shifted, no topology change
    let frame3 = rt.resolve(400.0, 200.0).unwrap();

    // p3 should be visible, p0 should be hidden
    let r3 = frame3.layout().get(p3).unwrap();
    assert!(r3.w > 0.0, "focused panel should be visible");
    let r0 = frame3.layout().get(p0).unwrap();
    assert!(
        r0.w < 1.0,
        "panel outside window should be hidden, got w={}",
        r0.w
    );

    // Kind lookup stable (kind caches preserved)
    assert_eq!(frame3.layout().by_kind("p0"), &[p0]);
    assert_eq!(frame3.layout().by_kind("p3"), &[p3]);

    // Diff: same-panel path (no add/remove churn)
    let diff = rt.last_diff();
    assert!(
        diff.added.is_empty(),
        "window slide must not report added panels"
    );
    assert!(
        diff.removed.is_empty(),
        "window slide must not report removed panels"
    );
}

fn window_runtime(n: usize) -> LayoutRuntime {
    let k = kinds(n);
    LayoutRuntime::from_strategy(
        StrategyKind::Window {
            panel_count: 2,
            gap: 0.0,
        },
        &k,
    )
    .unwrap()
}

#[test]
fn window_focus_within_pair_no_constraint_change() {
    let mut rt = window_runtime(4);
    let p0 = rt.sequence().get(0).unwrap();
    let p1 = rt.sequence().get(1).unwrap();

    // Both p0 and p1 should be visible initially
    let c0 = rt.tree().panel_constraints(p0).unwrap();
    let c1 = rt.tree().panel_constraints(p1).unwrap();
    assert!(c0.grow.is_some());
    assert!(c1.grow.is_some());

    // Focus p1 — still in window, no constraint changes
    rt.focus(p1);
    assert_eq!(rt.focused(), Some(p1));
    assert_eq!(rt.viewport().window_start, 0);
}

#[test]
fn window_focus_past_edge_slides() {
    let mut rt = window_runtime(4);
    let p3 = rt.sequence().get(3).unwrap();

    rt.focus(p3);
    assert_eq!(rt.focused(), Some(p3));

    // Window should have shifted
    let ws = rt.viewport().window_start;
    assert!(ws >= 2);

    // p3 should be visible
    let c3 = rt.tree().panel_constraints(p3).unwrap();
    assert!(c3.grow.is_some());
}

#[test]
fn window_focus_rejects_when_sequence_and_tree_drift() {
    let mut rt = window_runtime(4);
    let initial_focus = rt.focused();
    let p3 = rt.sequence().get(3).unwrap();

    rt.tree_mut().remove_panel(p3).unwrap();

    let outcome = rt.focus(p3);

    assert_eq!(
        outcome,
        FocusOutcome::Rejected(FocusRejection::StrategyRejected)
    );
    assert_eq!(rt.focused(), initial_focus);
    assert_eq!(rt.viewport().window_start, 0);
}

#[test]
fn window_add_shifts_window() {
    let mut rt = window_runtime(3);
    let new_pid = rt.add_panel(Arc::from("p_new")).unwrap();
    assert_eq!(rt.focused(), Some(new_pid));

    // New panel should be visible
    let c = rt.tree().panel_constraints(new_pid).unwrap();
    assert!(c.grow.is_some());
}

#[test]
fn window_remove_adjusts_window() {
    let mut rt = window_runtime(4);
    let p0 = rt.sequence().get(0).unwrap();
    let new_focus = rt.remove_panel(p0).unwrap();
    assert!(new_focus.is_some());
    assert_eq!(rt.sequence().len(), 3);
}