egui_stack 0.25.6

Docking system for egui, continuing egui_dock
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
use egui::{Rect, pos2, vec2};

use super::*;

fn rect(width: f32) -> Rect {
    Rect::from_min_size(pos2(0.0, 0.0), vec2(width, 100.0))
}

fn width(laid: &Laid, name: &str) -> f32 {
    laid.rect(name).unwrap().width()
}

fn side(name: &str, default: f32, min: f32) -> Zone {
    Zone::leaf(name, Content::Dock(name.into())).with(|c| {
        c.default = Some(Size::Points(default));
        c.min = Some(Size::Points(min));
    })
}

fn centre(min: f32) -> Zone {
    Zone::leaf("centre", Content::Dock("centre".into())).with(|c| {
        c.grow = true;
        c.min = Some(Size::Points(min));
    })
}

fn row(children: Vec<Zone>) -> Layout {
    Layout::new(Zone::split("root", Dir::Horizontal, children))
}

/// The usual shell: two sides around a centre.
fn shell() -> Layout {
    row(vec![
        side("left", 200.0, 50.0),
        centre(300.0),
        side("right", 200.0, 50.0),
    ])
}

#[test]
fn the_sides_get_their_wanted_size_and_the_centre_the_rest() {
    let laid = shell().solve(rect(1000.0), &Solve::default());
    assert_eq!(width(&laid, "left"), 200.0);
    assert_eq!(width(&laid, "centre"), 600.0);
    assert_eq!(width(&laid, "right"), 200.0);
    assert_eq!(laid.rect("right").unwrap().min.x, 800.0);
}

#[test]
fn the_centre_wins_over_the_sides_wanted_size_and_max() {
    let mut layout = shell();
    layout.constrain("left", |c| c.max = Some(Size::Points(400.0)));
    layout.zone_mut("left").unwrap().state.size = Some(400.0);
    let laid = layout.solve(rect(600.0), &Solve::default());
    assert_eq!(width(&laid, "centre"), 300.0);
    // Of two sides as near and of the same priority, the first gives first;
    // both stay at or over their least size.
    assert!(width(&laid, "left") >= 50.0 && width(&laid, "right") >= 50.0);
    assert_eq!(width(&laid, "left") + width(&laid, "right"), 300.0);
    // The user's size is kept for when the room comes back.
    assert_eq!(layout.zone("left").unwrap().state.size, Some(400.0));
}

#[test]
fn the_lowest_priority_gives_way_first() {
    let mut layout = shell();
    layout.constrain("left", |c| c.priority = Priority::High);
    layout.constrain("right", |c| c.priority = Priority::Low);
    let laid = layout.solve(rect(600.0), &Solve::default());
    // 100 short: the right side gives it all.
    assert_eq!(width(&laid, "left"), 200.0);
    assert_eq!(width(&laid, "right"), 100.0);
    assert_eq!(width(&laid, "centre"), 300.0);
    // 250 short: the right side down to its least, then the left.
    let laid = layout.solve(rect(450.0), &Solve::default());
    assert_eq!(width(&laid, "right"), 50.0);
    assert_eq!(width(&laid, "left"), 100.0);
}

#[test]
fn the_nearest_to_the_centre_gives_way_first() {
    let layout = row(vec![
        side("far", 100.0, 20.0),
        side("near", 100.0, 20.0),
        centre(300.0),
    ]);
    let laid = layout.solve(rect(450.0), &Solve::default());
    assert_eq!(width(&laid, "near"), 50.0);
    assert_eq!(width(&laid, "far"), 100.0);
    let laid = layout.solve(rect(360.0), &Solve::default());
    assert_eq!(width(&laid, "near"), 20.0);
    assert_eq!(width(&laid, "far"), 40.0);
}

#[test]
fn least_sizes_are_never_violated() {
    let layout = shell();
    for room in [1000.0, 600.0, 450.0, 400.0] {
        let laid = layout.solve(rect(room), &Solve::default());
        assert!(width(&laid, "left") >= 50.0, "room {room}");
        assert!(width(&laid, "right") >= 50.0, "room {room}");
        assert!(width(&laid, "centre") >= 300.0, "room {room}");
    }
    // Short even at the least sizes: each zone is given its least size and
    // the layout is cut at the edge.
    let laid = layout.solve(rect(380.0), &Solve::default());
    assert_eq!(width(&laid, "left"), 50.0);
    assert_eq!(width(&laid, "centre"), 300.0);
    assert_eq!(width(&laid, "right"), 30.0);
}

#[test]
fn fit_content_raises_the_least_size() {
    let mut layout = shell();
    layout.constrain("right", |c| c.fit_content = true);
    let mut solve = Solve::default();
    solve.content.insert("right".into(), 150.0);
    let laid = layout.solve(rect(500.0), &solve);
    assert_eq!(width(&laid, "right"), 150.0);
    assert_eq!(width(&laid, "left"), 50.0);
}

#[test]
fn share_sizes_follow_the_parent() {
    let layout = row(vec![
        Zone::leaf("left", Content::App).with(|c| {
            c.default = Some(Size::Share(0.25));
            c.min = Some(Size::Share(0.1));
        }),
        Zone::leaf("centre", Content::App).with(|c| c.grow = true),
    ]);
    let laid = layout.solve(rect(800.0), &Solve::default());
    assert_eq!(width(&laid, "left"), 200.0);
    assert_eq!(width(&laid, "centre"), 600.0);
    let laid = layout.solve(rect(400.0), &Solve::default());
    assert_eq!(width(&laid, "left"), 100.0);
}

#[test]
fn a_split_of_equals_shares_its_length() {
    let layout = row(vec![
        Zone::leaf("a", Content::Empty),
        Zone::leaf("b", Content::Empty),
        Zone::leaf("c", Content::Empty),
        Zone::leaf("d", Content::Empty),
    ]);
    let laid = layout.solve(rect(400.0), &Solve::default());
    for name in ["a", "b", "c", "d"] {
        assert_eq!(width(&laid, name), 100.0);
    }
}

#[test]
fn nested_splits_are_solved_in_their_parent_rect() {
    let layout = Layout::new(Zone::split(
        "root",
        Dir::Horizontal,
        vec![
            side("left", 200.0, 50.0),
            Zone::split(
                "middle",
                Dir::Vertical,
                vec![
                    centre(50.0),
                    Zone::leaf("bottom", Content::App)
                        .with(|c| c.default = Some(Size::Share(0.25))),
                ],
            )
            .with(|c| c.grow = true),
        ],
    ));
    let laid = layout.solve(rect(1000.0), &Solve::default());
    let bottom = laid.rect("bottom").unwrap();
    assert_eq!(
        bottom,
        Rect::from_min_max(pos2(200.0, 75.0), pos2(1000.0, 100.0))
    );
    assert_eq!(laid.rect("centre").unwrap().height(), 75.0);
}

#[test]
fn folded_zones_take_their_strip_and_keep_their_size() {
    let mut layout = shell();
    layout.zone_mut("left").unwrap().state.folded = true;
    layout.zone_mut("left").unwrap().state.size = Some(250.0);
    let solve = Solve::default();
    let laid = layout.solve(rect(1000.0), &solve);
    let left = laid.get("left").unwrap();
    assert_eq!(left.shown, Shown::Folded);
    assert_eq!(left.strip, Some(left.rect));
    assert_eq!(left.rect.width(), solve.strip);
    assert_eq!(width(&laid, "centre"), 1000.0 - solve.strip - 200.0);
    // Unfolded, it gets the size the user gave back.
    layout.zone_mut("left").unwrap().state.folded = false;
    assert_eq!(width(&layout.solve(rect(1000.0), &solve), "left"), 250.0);
}

#[test]
fn a_zone_lying_over_keeps_its_strip_in_the_flow() {
    let mut layout = shell();
    layout.zone_mut("right").unwrap().state.over = true;
    let laid = layout.solve(rect(1000.0), &Solve::default());
    let right = laid.get("right").unwrap();
    assert_eq!(right.shown, Shown::Over);
    let strip = right.strip.unwrap();
    assert_eq!(strip.min.x, 976.0);
    // Its body opens towards the centre, over it.
    assert_eq!(
        right.rect,
        Rect::from_min_max(pos2(776.0, 0.0), pos2(976.0, 100.0))
    );
    assert_eq!(width(&laid, "centre"), 776.0);
}

#[test]
fn hidden_zones_take_no_room() {
    let mut layout = shell();
    layout.zone_mut("left").unwrap().state.hidden = true;
    let laid = layout.solve(rect(1000.0), &Solve::default());
    assert_eq!(laid.get("left").unwrap().shown, Shown::Hidden);
    assert_eq!(width(&laid, "left"), 0.0);
    assert_eq!(width(&laid, "centre"), 800.0);
}

#[test]
fn zones_fold_by_themselves_only_when_they_opt_in() {
    let mut layout = shell();
    for name in ["left", "right"] {
        layout.constrain(name, |c| c.fold = Fold::ToStrip);
    }
    layout.constrain("right", |c| c.priority = Priority::Low);
    let solve = Solve::default();
    // Short even at the least sizes (400), but nobody opted in: cut.
    let laid = layout.solve(rect(380.0), &solve);
    assert_eq!(laid.get("right").unwrap().shown, Shown::Open);
    assert_eq!(width(&laid, "centre"), 300.0);

    for name in ["left", "right"] {
        layout.constrain(name, |c| c.auto_fold = true);
    }
    let laid = layout.solve(rect(380.0), &solve);
    // The lowest priority folds first; the left side gets the room back.
    assert_eq!(laid.get("right").unwrap().shown, Shown::AutoFolded);
    assert_eq!(width(&laid, "right"), solve.strip);
    assert_eq!(laid.get("left").unwrap().shown, Shown::Open);
    assert_eq!(width(&laid, "left"), 380.0 - 300.0 - solve.strip);
    // The state is not touched: with the room back, it comes back.
    assert!(!layout.zone("right").unwrap().state.folded);
    let laid = layout.solve(rect(1000.0), &solve);
    assert_eq!(laid.get("right").unwrap().shown, Shown::Open);
    // Shorter still: both fold.
    let laid = layout.solve(rect(330.0), &solve);
    assert_eq!(laid.get("left").unwrap().shown, Shown::AutoFolded);
}

#[test]
fn the_growing_zone_never_folds() {
    let mut layout = shell();
    layout.zone_mut("centre").unwrap().state.folded = true;
    let laid = layout.solve(rect(1000.0), &Solve::default());
    assert_eq!(laid.get("centre").unwrap().shown, Shown::Open);
    assert_eq!(width(&laid, "centre"), 600.0);
}

fn shell_with_bottom() -> Layout {
    Layout::new(Zone::split(
        "root",
        Dir::Horizontal,
        vec![
            side("left", 200.0, 50.0),
            Zone::split(
                "middle",
                Dir::Vertical,
                vec![centre(100.0), side("bottom", 100.0, 40.0)],
            )
            .with(|c| c.grow = true),
            side("right", 200.0, 50.0),
        ],
    ))
}

#[test]
fn reconcile_takes_the_constraints_from_code() {
    let code = shell();
    let mut loaded = shell();
    loaded.constrain("centre", |c| c.min = None);
    loaded.zone_mut("left").unwrap().state.size = Some(321.0);
    let repair = loaded.reconcile(&code);
    assert!(repair.is_empty());
    assert_eq!(
        loaded.zone("centre").unwrap().constraints,
        code.zone("centre").unwrap().constraints
    );
    assert_eq!(loaded.zone("left").unwrap().state.size, Some(321.0));
}

#[test]
fn reconcile_adds_the_zones_code_added() {
    let mut loaded = shell();
    loaded.zone_mut("right").unwrap().state.folded = true;
    let code = shell_with_bottom();
    let repair = loaded.reconcile(&code);
    assert_eq!(
        repair.added,
        vec![ZoneName::from("middle"), ZoneName::from("bottom")]
    );
    // "centre" moved under "middle" in the code but stays where it was
    // saved: the loaded shape wins; "middle" comes after "left".
    assert_eq!(
        loaded
            .root
            .children()
            .iter()
            .map(|z| z.name.0.as_str())
            .collect::<Vec<_>>(),
        ["left", "middle", "centre", "right"]
    );
    assert_eq!(loaded.zone("middle").unwrap().children().len(), 1);
    assert!(loaded.zone("right").unwrap().state.folded);
    assert_eq!(loaded.names().len(), code.names().len());
}

#[test]
fn reconcile_drops_the_zones_code_removed() {
    let mut loaded = shell_with_bottom();
    loaded.zone_mut("left").unwrap().state.size = Some(123.0);
    let code = row(vec![side("left", 200.0, 50.0), centre(300.0)]);
    let repair = loaded.reconcile(&code);
    assert_eq!(
        repair.removed,
        vec![
            ZoneName::from("middle"),
            ZoneName::from("bottom"),
            ZoneName::from("right")
        ]
    );
    // "centre" was under "middle": it takes its place, with its state.
    assert!(repair.added.is_empty());
    assert_eq!(loaded.names(), code.names());
    assert_eq!(loaded.zone("left").unwrap().state.size, Some(123.0));
}

#[test]
fn reconcile_follows_a_leaf_turned_split() {
    let mut loaded = row(vec![side("left", 200.0, 50.0), centre(300.0)]);
    loaded.zone_mut("left").unwrap().state.size = Some(222.0);
    let code = row(vec![
        Zone::split(
            "left",
            Dir::Vertical,
            vec![
                Zone::leaf("top", Content::App),
                Zone::leaf("low", Content::App),
            ],
        ),
        centre(300.0),
    ]);
    let repair = loaded.reconcile(&code);
    assert_eq!(
        repair.added,
        vec![ZoneName::from("top"), ZoneName::from("low")]
    );
    assert_eq!(loaded.zone("left").unwrap().children().len(), 2);
    assert_eq!(loaded.zone("left").unwrap().state.size, Some(222.0));
}

#[test]
fn reconcile_replaces_a_layout_of_another_root() {
    let mut loaded = Layout::single("main");
    let code = shell();
    let repair = loaded.reconcile(&code);
    assert_eq!(loaded, code);
    assert_eq!(repair.removed, vec![ZoneName::from("main")]);
}

#[cfg(feature = "serde")]
mod serde {
    use super::*;

    #[test]
    fn a_round_trip_keeps_the_tree_and_the_state_not_the_constraints() {
        let mut layout = shell_with_bottom();
        layout.zone_mut("left").unwrap().state.size = Some(260.0);
        layout.zone_mut("bottom").unwrap().state.folded = true;
        layout.zone_mut("right").unwrap().state.over = true;
        let json = serde_json::to_string(&layout).unwrap();
        assert!(json.contains("\"version\":1"), "{json}");
        assert!(!json.contains("priority"), "{json}");
        let mut loaded: Layout = serde_json::from_str(&json).unwrap();
        assert_eq!(loaded.names(), layout.names());
        assert_eq!(
            loaded.zone("centre").unwrap().constraints,
            Constraints::default()
        );
        assert!(loaded.reconcile(&shell_with_bottom()).is_empty());
        assert_eq!(loaded, layout);
    }

    #[test]
    fn a_later_version_does_not_load() {
        let json = serde_json::to_string(&Layout::single("main"))
            .unwrap()
            .replace("\"version\":1", "\"version\":99");
        let error = serde_json::from_str::<Layout>(&json).unwrap_err();
        assert!(error.to_string().contains("99"), "{error}");
    }
}