cella_gui 1.0.0

A GUI for the Cella cellular automata library
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
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
//! Actions: the one way the panels change the simulation.
//!
//! Panels and the toolbar do not reach into playback, the grid or the view
//! and poke fields. They describe what the user wants as an [`Action`] and
//! push it onto a queue; after the frame is drawn, [`CellaApp::drain_actions`]
//! hands each one to [`CellaApp::apply_action`], the single place where state
//! changes. If you know React, this is the reducer: widgets emit, one function
//! applies, and the state is only ever written in one place.
//!
//! Why bother? Three things get easier:
//! - **Testing.** Every behaviour behind a button or key is a plain function
//!   call on a plain struct — no window, no egui.
//! - **Reuse.** A keyboard shortcut and a toolbar button do the same thing by
//!   pushing the same action, so they cannot drift apart.
//! - **Borrowing.** A panel that holds a borrow (the rule editor's working
//!   copy, say) cannot also mutate the grid mid-draw; queuing sidesteps that.
//!
//! Panel bodies may still edit their own *drafts* directly (text being typed,
//! a slider's local copy). Anything that touches `scenario`, `playback`,
//! `view`, the undo stack or a worker goes through an action.

use super::app::{CellaApp, Dim, DrawMode};
use super::explore::ExploreAction;
use super::interact::{GridDims, MAX_BRUSH, MAX_SCALE, MIN_SCALE};
use super::layers::Layer;
use super::state::{ControlTab, Pacing, WorkbenchTab};
use super::theme::ThemeChoice;
use cella_lib::types::interner;
use cella_lib::{CellType, INACTIVE, Neighborhood2D, ParamValue};
use egui::Color32;

/// The built-in demo scenarios.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::gui) enum Demo {
    Life,
    Rule30,
    Radius2,
    ThreeState2D,
    StraightLine2D,
}

/// The user's answer to "this file was saved mid-run — resume, or start
/// over?", from the modal `ui_snapshot_load_modal` draws.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(in crate::gui) enum SnapshotChoice {
    /// Load the grid as it was at `snapshot.step`.
    Resume,
    /// Load `initial` instead, at step 0.
    Initial,
    /// Leave the current scenario untouched.
    Cancel,
}

/// Something the user asked for. Cheap to clone; carries its own data.
#[derive(Clone, Debug, PartialEq)]
pub(in crate::gui) enum Action {
    // ── playback ──
    TogglePlay,
    Step,
    /// Burst `steps` steps ahead of the current step.
    RunTo {
        steps: u64,
    },
    /// Paced playback speed in steps per second (1..=1000).
    SetSpeed {
        steps_per_s: f64,
    },
    /// `true`: run as fast as the machine allows; `false`: back to paced.
    SetMaxSpeed(bool),
    Reset,
    // ── view ──
    ZoomIn,
    ZoomOut,
    /// Pick the largest cell size at which the whole grid fits the viewport.
    ZoomToFit,
    SetScale(usize),
    ToggleGridLines,
    /// Switch an overlay layer on or off (Grid means the grid lines).
    ToggleLayer(Layer),
    /// Opacity of the overlay layers, `0..=1`.
    SetLayerOpacity(f32),
    /// Ages at or above this are not tinted by the age layer.
    SetAgeCap(u32),
    SetFontScale(f32),
    /// Switch the theme. Background and grid-line colours still at the old
    /// theme's defaults follow it; colours the user picked stay.
    SetTheme(ThemeChoice),
    /// Switch to a palette preset (index into `theme::PALETTES`); automatic
    /// colours are re-slotted, config colours re-applied, user picks reset.
    SetPalette(usize),
    /// Hand out palette slots again, dropping any colour the user picked.
    ReslotColors,
    SetGridLineColor(Color32),
    SetInactiveColor(Color32),
    SetTypeColor(CellType, Color32),
    SetHistoryLimit1D(usize),
    // ── edit ──
    Undo,
    SetDrawMode(DrawMode),
    SetDrawType(CellType),
    /// Brush diameter in cells.
    SetBrush(u8),
    /// Brush footprint shape (a rule neighbourhood).
    SetBrushShape(Neighborhood2D),
    /// Brush range one step larger / smaller (`]` / `[`).
    BrushGrow,
    BrushShrink,
    /// Which pattern the Stamp tool drops (index into `patterns::PATTERNS`).
    SelectStamp(usize),
    /// Paint the selected type onto these cells as part of the current stroke.
    PaintCells(Vec<usize>),
    /// The stroke ended: close it into one undo entry.
    EndStroke,
    /// Advance one cell to the next declared type.
    CycleAt(usize),
    /// Drop the selected pattern with its corner on this cell.
    StampAt(usize),
    /// Set a random share of cells to `ty`, reproducibly from `seed`.
    RandomFill {
        density: f32,
        ty: CellType,
        seed: u64,
        clear_first: bool,
    },
    ToggleInspector,
    // ── scenario / rule / model ──
    LoadDemo(Demo),
    LoadConfigDialog,
    /// The modal's answer to a config loaded with a mid-run `snapshot`.
    ResolveSnapshotLoad(SnapshotChoice),
    /// Rebuild the grid at a new size, keeping the cells that overlap.
    Resize {
        w: usize,
        h: usize,
    },
    /// Build the custom 1D scenario from a Wolfram code and radius.
    Build1D {
        code: String,
        n: u8,
    },
    /// Write the rule editor's working copy into the grid.
    ApplyRule,
    ApplyModelParam {
        key: String,
        value: ParamValue,
    },
    /// Declare a new cell type for the rule editor.
    AddType(String),
    // ── chrome / io ──
    SetControlTab(ControlTab),
    SetWorkbenchTab(WorkbenchTab),
    ToggleLeft,
    ToggleRight,
    ToggleShortcuts,
    ExportGif,
    /// The export modal's "Export": start the GIF with the options shown.
    ConfirmExportGif,
    /// The export modal's "Cancel" (or Escape / click outside).
    CancelExportGif,
    SaveFinalState,
    /// Anything that touches the Explore worker or applies a found genome.
    Explore(ExploreAction),
    // ── fun ──
    /// Draw every knob at random within its declared bounds, then random-fill
    /// the grid (30 % of the first non-background type) with the same seed.
    SurpriseMe {
        seed: u64,
    },
    /// Nudge every knob a little (`sigma` = share of its range).
    MutateRule {
        seed: u64,
        sigma: f64,
    },
    /// Put the knobs back the way they were before the last Surprise me,
    /// Mutate rule or Apply genome.
    UndoRule,
    /// Keyboard forms: read the Edit tab's seed, type and sigma, then advance
    /// the seed so the next press gives something new.
    RandomFillDraft,
    SurpriseMeDraft,
    MutateRuleDraft,
    /// Close the notice popup.
    DismissNotice,
}

/// Most actions applied in one frame before the drain gives up; a reducer arm
/// that keeps enqueuing itself would otherwise hang the UI.
const DRAIN_LIMIT: usize = 1024;

/// Paced playback speed for a refresh interval.
pub(in crate::gui) fn speed_of(refresh_ms: u64) -> f64 {
    1000.0 / refresh_ms.max(1) as f64
}

/// Refresh interval for a paced playback speed, kept within 1..=1000 ms
/// (1 to 1000 steps per second).
pub(in crate::gui) fn refresh_ms_of(steps_per_s: f64) -> u64 {
    if !steps_per_s.is_finite() || steps_per_s <= 0.0 {
        return 1000;
    }
    (1000.0 / steps_per_s).round().clamp(1.0, 1000.0) as u64
}

/// The largest whole cell size (pixels) at which a `grid_w × grid_h` grid
/// fits inside `available`, leaving a small margin for scrollbars.
pub(in crate::gui) fn fit_scale(available: egui::Vec2, grid_w: usize, grid_h: usize) -> usize {
    const MARGIN: f32 = 16.0;
    if !available.x.is_finite() || !available.y.is_finite() {
        return MIN_SCALE;
    }
    let w = ((available.x - MARGIN) / grid_w.max(1) as f32).floor();
    let h = ((available.y - MARGIN) / grid_h.max(1) as f32).floor();
    let s = w.min(h);
    if !s.is_finite() || s < MIN_SCALE as f32 {
        return MIN_SCALE;
    }
    (s as usize).clamp(MIN_SCALE, MAX_SCALE)
}

impl CellaApp {
    /// Queue an action for the end of this frame.
    pub(in crate::gui) fn push(&mut self, action: Action) {
        self.actions.push_back(action);
    }

    /// Apply every queued action, in order. Arms may enqueue further actions.
    pub(in crate::gui) fn drain_actions(&mut self) {
        let mut applied = 0;
        while let Some(a) = self.actions.pop_front() {
            self.apply_action(a);
            applied += 1;
            if applied >= DRAIN_LIMIT {
                self.actions.clear();
                self.set_status("Too many actions in one frame; the rest were dropped");
                break;
            }
        }
    }

    /// The reducer: the only place an action becomes a state change.
    pub(in crate::gui) fn apply_action(&mut self, action: Action) {
        match action {
            Action::TogglePlay => self.toggle_play(),
            Action::Step => {
                self.step_once();
                self.set_status(format!("Stepped to {}", self.current_step()));
            }
            Action::RunTo { steps } => {
                self.playback.run_to_steps = steps.max(1);
                self.start_run_to();
            }
            Action::SetSpeed { steps_per_s } => {
                self.playback.refresh_ms = refresh_ms_of(steps_per_s);
            }
            Action::SetMaxSpeed(on) => {
                self.playback.pacing = if on {
                    Pacing::Unbounded
                } else {
                    Pacing::Interval
                };
            }
            Action::Reset => self.reset_to_initial(),
            Action::ZoomIn => self.view.scale = (self.view.scale + 1).min(MAX_SCALE),
            Action::ZoomOut => self.view.scale = self.view.scale.saturating_sub(1).max(MIN_SCALE),
            Action::ZoomToFit => {
                if let (Some(avail), Some(dims)) = (self.view.last_viewport_size, self.grid_dims())
                {
                    let (w, h) = match dims {
                        GridDims::D1 { width, .. } => (width, self.view.history_limit_1d + 1),
                        GridDims::D2 { width, height } => (width, height),
                    };
                    self.view.scale = fit_scale(avail, w, h);
                    self.set_status(format!("Zoom to fit: {} px per cell", self.view.scale));
                }
            }
            Action::SetScale(s) => self.view.scale = s.clamp(MIN_SCALE, MAX_SCALE),
            Action::ToggleGridLines => self.view.show_grid_lines = !self.view.show_grid_lines,
            Action::ToggleLayer(layer) => match layer {
                Layer::Grid => self.view.show_grid_lines = !self.view.show_grid_lines,
                Layer::Age => self.view.layers.age = !self.view.layers.age,
                Layer::Probability => self.view.layers.probability = !self.view.layers.probability,
            },
            Action::SetLayerOpacity(a) => self.view.layers.opacity = a.clamp(0.0, 1.0),
            Action::SetAgeCap(n) => self.view.layers.age_cap = n.clamp(1, 100_000),
            Action::SetFontScale(f) => self.chrome.font_scale = f.clamp(0.5, 3.0),
            Action::SetTheme(theme) => {
                let old = self.chrome.theme;
                if self.view.inactive_color == old.default_inactive() {
                    self.view.inactive_color = theme.default_inactive();
                }
                if self.view.grid_line_color == old.default_grid_line() {
                    self.view.grid_line_color = theme.default_grid_line();
                }
                self.chrome.theme = theme;
            }
            Action::SetPalette(i) => {
                let i = i.min(super::theme::PALETTES.len() - 1);
                self.view.palette = super::theme::PALETTES[i].colors.to_vec();
                self.view.palette_index = i;
                self.reslot_colors();
                self.explore.thumbs.clear();
            }
            Action::ReslotColors => {
                self.reslot_colors();
                self.explore.thumbs.clear();
            }
            Action::SetGridLineColor(c) => self.view.grid_line_color = c,
            Action::SetInactiveColor(c) => {
                self.view.inactive_color = c;
                self.explore.thumbs.clear();
            }
            Action::SetTypeColor(ty, c) => {
                self.set_color_for(&ty, c);
                self.explore.thumbs.clear();
            }
            Action::SetHistoryLimit1D(n) => self.view.history_limit_1d = n.clamp(1, 10_000),
            Action::Undo => self.undo_last_batch(),
            Action::SetDrawMode(mode) => {
                self.finish_stroke();
                self.edit.draw_mode = mode;
            }
            Action::SetDrawType(ty) => self.edit.selected_draw_type = Some(ty),
            Action::SetBrush(b) => self.edit.brush = b.min(MAX_BRUSH),
            Action::SetBrushShape(shape) => self.edit.brush_shape = shape,
            Action::BrushGrow => self.edit.brush = (self.edit.brush + 1).min(MAX_BRUSH),
            Action::BrushShrink => self.edit.brush = self.edit.brush.saturating_sub(1),
            Action::SelectStamp(i) => self.edit.stamp = i.min(super::patterns::PATTERNS.len() - 1),
            Action::PaintCells(cells) => self.paint_cells(&cells),
            Action::EndStroke => self.finish_stroke(),
            Action::CycleAt(idx) => self.cycle_cell(idx),
            Action::StampAt(idx) => self.stamp_at(idx),
            Action::RandomFill {
                density,
                ty,
                seed,
                clear_first,
            } => self.random_fill(density, ty, seed, clear_first),
            Action::ToggleInspector => self.view.inspector = !self.view.inspector,
            Action::LoadDemo(demo) => match demo {
                Demo::Life => self.load_demo_life(),
                Demo::Rule30 => self.load_demo_1d_rule30(),
                Demo::Radius2 => self.load_demo_1d_n2(),
                Demo::ThreeState2D => self.load_demo_2d_three_state_cycle(),
                Demo::StraightLine2D => self.load_demo_2d_straightline(),
            },
            Action::LoadConfigDialog => self.load_config_dialog(),
            Action::ResolveSnapshotLoad(choice) => self.resolve_snapshot_load(choice),
            Action::Resize { w, h } => {
                self.inputs.grid_width = w.max(1);
                self.inputs.grid_height = h.max(1);
                self.resize_grid();
            }
            Action::Build1D { code, n } => {
                self.inputs.custom_code = code;
                self.inputs.custom_n = n.max(1);
                self.load_demo_1d_custom_from_inputs();
            }
            Action::ApplyRule => self.apply_rule_draft(),
            Action::ApplyModelParam { key, value } => self.apply_model_param(&key, value),
            Action::AddType(name) => {
                let name = name.trim();
                if !name.is_empty() && name != INACTIVE {
                    self.editor
                        .custom_types
                        .insert(interner().get_or_intern(name));
                    self.set_status(format!("Added type '{name}'"));
                }
            }
            Action::SetControlTab(tab) => self.chrome.control_tab = tab,
            Action::SetWorkbenchTab(tab) => self.chrome.workbench_tab = tab,
            Action::ToggleLeft => self.chrome.left_open = !self.chrome.left_open,
            Action::ToggleRight => self.chrome.right_open = !self.chrome.right_open,
            Action::ToggleShortcuts => self.chrome.show_shortcuts = !self.chrome.show_shortcuts,
            Action::ExportGif => self.export_gif_dialog(),
            Action::ConfirmExportGif => self.confirm_export(),
            Action::CancelExportGif => self.cancel_export(),
            Action::SaveFinalState => self.save_final_state(),
            Action::Explore(a) => self.apply_explore_action(a),
            Action::SurpriseMe { seed } => self.surprise_me(seed),
            Action::MutateRule { seed, sigma } => self.mutate_rule(seed, sigma),
            Action::UndoRule => self.undo_rule(),
            Action::RandomFillDraft => {
                if let Some(ty) = self.fill_type_or_default() {
                    let seed = self.next_fill_seed();
                    self.push(Action::RandomFill {
                        density: self.edit.fill_density,
                        ty,
                        seed,
                        clear_first: self.edit.fill_clear,
                    });
                }
            }
            Action::SurpriseMeDraft => {
                let seed = self.next_fill_seed();
                self.push(Action::SurpriseMe { seed });
            }
            Action::MutateRuleDraft => {
                let seed = self.next_fill_seed();
                self.push(Action::MutateRule {
                    seed,
                    sigma: self.edit.mutate_sigma,
                });
            }
            Action::DismissNotice => self.chrome.notice = None,
        }
    }

    /// Write the rule editor's working copy into the live grid. A draft the
    /// library refuses stays in the editor with the reason shown; a good one
    /// becomes the grid's rule and the editor re-reads it.
    fn apply_rule_draft(&mut self) {
        let result = match self.scenario.dim {
            Some(Dim::D1) => self
                .editor
                .rule_1d
                .as_ref()
                .map(|e| e.to_rule().map(|r| (Some(r), None))),
            Some(Dim::D2) => self
                .editor
                .rule_2d
                .as_ref()
                .map(|e| e.to_rule().map(|r| (None, Some(r)))),
            None => None,
        };
        match result {
            Some(Ok((r1, r2))) => {
                if let (Some(r), Some(g)) = (r1, self.scenario.d1.as_mut()) {
                    g.rule = r;
                }
                if let (Some(r), Some(g)) = (r2, self.scenario.d2.as_mut()) {
                    g.rule = r;
                }
                self.editor.error_msg = None;
                self.refresh_rule_editor_from_current();
                self.set_status("Applied rule");
            }
            Some(Err(e)) => {
                self.editor.error_msg = Some(e.clone());
                self.set_status(format!("Rule error: {e}"));
            }
            None => {}
        }
    }

    /// Give every type its automatic palette slot again, then put the loaded
    /// config's colours back on top; colours the user picked by hand go.
    fn reslot_colors(&mut self) {
        let config_colors = self.view.config_colors.clone();
        self.reset_colors_for_scenario();
        self.apply_config_colors(&config_colors);
    }

    /// Paint the selected type onto `cells` as part of the current stroke.
    /// A cell is recorded for undo the first time the stroke touches it.
    fn paint_cells(&mut self, cells: &[usize]) {
        if self.playback.playing {
            return;
        }
        let paint_ty = self
            .edit
            .selected_draw_type
            .unwrap_or_else(CellType::inactive);
        for &idx in cells {
            if !self.edit.stroke_touched.insert(idx) {
                continue;
            }
            let Some(prev) = self.cell_type_at(idx) else {
                continue;
            };
            if prev == paint_ty {
                continue;
            }
            self.edit
                .current_paint_batch
                .get_or_insert_with(Vec::new)
                .push((idx, prev));
            self.set_cell(idx, paint_ty, "Paint");
        }
    }

    /// Close the current stroke into one undo entry.
    fn finish_stroke(&mut self) {
        self.edit.stroke_touched.clear();
        if let Some(batch) = self.edit.current_paint_batch.take()
            && !batch.is_empty()
        {
            self.edit.undo_stack.push(batch);
        }
    }

    /// Advance one cell to the next declared type (every *declared* type, not
    /// just the ones on the grid, so a state that died out can come back).
    fn cycle_cell(&mut self, idx: usize) {
        if self.playback.playing {
            return;
        }
        let types = self.declared_types();
        let Some(current) = self.cell_type_at(idx) else {
            return;
        };
        let next = super::types::next_in_cycle(&types, current);
        self.edit.undo_stack.push(vec![(idx, current)]);
        self.set_cell(idx, next, "Cycle edit");
    }

    /// Drop the selected pattern with its top-left corner on `idx` (2D only),
    /// as one undo entry.
    fn stamp_at(&mut self, idx: usize) {
        if self.playback.playing {
            return;
        }
        let Some(GridDims::D2 { width, height }) = self.grid_dims() else {
            return;
        };
        let Some(pattern) = super::patterns::PATTERNS.get(self.edit.stamp) else {
            return;
        };
        let ty = self
            .edit
            .selected_draw_type
            .unwrap_or_else(CellType::inactive);
        let cells =
            super::patterns::stamp_indices(pattern, (idx % width, idx / width), width, height);
        let mut batch = Vec::new();
        for c in cells {
            if let Some(prev) = self.cell_type_at(c)
                && prev != ty
            {
                batch.push((c, prev));
                self.set_cell(c, ty, "Stamp");
            }
        }
        if !batch.is_empty() {
            self.edit.undo_stack.push(batch);
        }
        self.set_status(format!("Stamped {}", pattern.name));
    }

    /// Fill a random `density` share of cells with `ty`. Each cell draws
    /// `cell_rand(seed, 0, index, STREAM_FILL)`, so the same seed always paints
    /// the same picture. With `clear_first` the rest of the grid becomes
    /// Inactive and the result is the new starting state (Reset returns to
    /// it); otherwise only the chosen cells change, as one undo entry.
    fn random_fill(&mut self, density: f32, ty: CellType, seed: u64, clear_first: bool) {
        use cella_lib::rng::{STREAM_FILL, cell_rand};
        if self.playback.playing {
            self.set_status("Pause before filling the grid");
            return;
        }
        if !self.declared_types().contains(&ty) {
            self.set_status(format!(
                "'{}' is not a type this scenario declares",
                ty.as_str()
            ));
            return;
        }
        let Some(dims) = self.grid_dims() else {
            return;
        };
        let total = match dims {
            GridDims::D1 { width, .. } => width,
            GridDims::D2 { width, height } => width * height,
        };
        let inactive = CellType::inactive();
        let density = density.clamp(0.0, 1.0);
        let new_cells: Vec<CellType> = (0..total)
            .map(|i| {
                if cell_rand(seed, 0, i as u64, STREAM_FILL) < density {
                    ty
                } else if clear_first {
                    inactive
                } else {
                    self.cell_type_at(i).unwrap_or(inactive)
                }
            })
            .collect();
        let changed: Vec<(usize, CellType)> = (0..total)
            .filter_map(|i| {
                let prev = self.cell_type_at(i)?;
                (prev != new_cells[i]).then_some((i, prev))
            })
            .collect();
        let n = changed.len();
        if clear_first {
            let result = match self.scenario.dim {
                Some(Dim::D1) => self.scenario.d1.as_mut().map(|g| {
                    let r = g.reset_cells(new_cells.clone());
                    g.set_seed(seed);
                    r
                }),
                Some(Dim::D2) => self.scenario.d2.as_mut().map(|g| {
                    let r = g.reset_cells(new_cells.clone());
                    g.set_seed(seed);
                    r
                }),
                None => None,
            };
            if let Some(Err(e)) = result {
                self.set_status(format!("Fill error: {e}"));
                return;
            }
            self.view.history_1d.clear();
            self.scenario.initial_state = match self.scenario.dim {
                Some(Dim::D1) => self
                    .scenario
                    .d1
                    .as_ref()
                    .map(cella_lib::GridState::from_grid1d),
                Some(Dim::D2) => self
                    .scenario
                    .d2
                    .as_ref()
                    .map(cella_lib::GridState::from_grid2d),
                None => None,
            };
            self.edit.undo_stack.clear();
            self.edit.current_paint_batch = None;
            self.stats_clear_and_init();
        } else {
            for &(i, _) in &changed {
                self.set_cell(i, new_cells[i], "Fill");
            }
            if !changed.is_empty() {
                self.edit.undo_stack.push(changed);
            }
        }
        self.set_status(format!(
            "Filled {n} cells with {} (seed {seed}{})",
            ty.as_str(),
            if clear_first {
                ", grid cleared first"
            } else {
                ""
            }
        ));
    }

    /// Undo the most recent edit batch, while paused.
    fn undo_last_batch(&mut self) {
        if self.playback.playing {
            return;
        }
        let Some(batch) = self.edit.undo_stack.pop() else {
            return;
        };
        let limit = match self.grid_dims() {
            Some(GridDims::D1 { width, .. }) => width,
            Some(GridDims::D2 { width, height }) => width * height,
            None => return,
        };
        for (idx, prev) in batch {
            // Skip out-of-range entries, but stop entirely at the first entry
            // the engine rejects.
            if idx < limit && !self.set_cell(idx, prev, "Undo") {
                break;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::gui::sim::tests::test_app;
    use cella_lib::CellType;

    #[test]
    fn speed_and_refresh_interval_convert_both_ways_within_bounds() {
        assert_eq!(refresh_ms_of(10.0), 100);
        assert_eq!(speed_of(100), 10.0);
        assert_eq!(refresh_ms_of(1.0), 1000);
        assert_eq!(refresh_ms_of(1000.0), 1);
        assert_eq!(refresh_ms_of(5000.0), 1, "capped at 1000 steps/s");
        assert_eq!(refresh_ms_of(0.1), 1000, "floored at 1 step/s");
        assert_eq!(refresh_ms_of(f64::NAN), 1000);
        assert_eq!(refresh_ms_of(-3.0), 1000);
        assert_eq!(
            speed_of(0),
            1000.0,
            "a zero interval reads as the fastest pace"
        );
        for ms in [1u64, 7, 33, 250, 1000] {
            assert_eq!(refresh_ms_of(speed_of(ms)), ms);
        }
    }

    #[test]
    fn fit_scale_takes_the_tighter_axis_and_stays_in_bounds() {
        assert_eq!(fit_scale(egui::vec2(816.0, 616.0), 100, 100), 6);
        assert_eq!(
            fit_scale(egui::vec2(816.0, 216.0), 100, 100),
            2,
            "height is the tighter axis"
        );
        assert_eq!(
            fit_scale(egui::vec2(10.0, 10.0), 100, 100),
            MIN_SCALE,
            "never below 1"
        );
        assert_eq!(
            fit_scale(egui::vec2(10_000.0, 10_000.0), 4, 4),
            MAX_SCALE,
            "never above 64"
        );
        assert_eq!(fit_scale(egui::vec2(f32::NAN, 100.0), 4, 4), MIN_SCALE);
        assert_eq!(
            fit_scale(egui::vec2(116.0, 116.0), 0, 0),
            64.min(MAX_SCALE),
            "empty grids do not divide by zero"
        );
    }

    #[test]
    fn playback_actions_drive_the_clock_fields() {
        let mut app = test_app();
        app.load_demo_life();
        app.apply_action(Action::SetSpeed { steps_per_s: 10.0 });
        assert_eq!(app.playback.refresh_ms, 100);
        app.apply_action(Action::SetMaxSpeed(true));
        assert_eq!(app.playback.pacing, Pacing::Unbounded);
        assert!(app.burst_target().is_none(), "not playing yet");
        app.apply_action(Action::TogglePlay);
        assert!(app.playback.playing);
        assert_eq!(
            app.burst_target(),
            Some(u64::MAX),
            "Max + Play bursts without a target"
        );
        app.apply_action(Action::SetMaxSpeed(false));
        assert_eq!(app.playback.pacing, Pacing::Interval);
        app.apply_action(Action::TogglePlay);
        assert!(!app.playback.playing);
        let before = app.current_step();
        app.apply_action(Action::Step);
        assert_eq!(app.current_step(), before + 1);
        app.apply_action(Action::RunTo { steps: 7 });
        assert_eq!(app.playback.run_to_target, Some(before + 8));
        assert!(app.playback.playing);
        app.apply_action(Action::Reset);
        assert!(!app.playback.playing);
        assert_eq!(app.current_step(), 0);
        assert!(app.playback.run_to_target.is_none());
    }

    #[test]
    fn view_actions_clamp_and_fit() {
        let mut app = test_app();
        app.load_demo_life();
        app.view.scale = MAX_SCALE;
        app.apply_action(Action::ZoomIn);
        assert_eq!(app.view.scale, MAX_SCALE);
        app.view.scale = MIN_SCALE;
        app.apply_action(Action::ZoomOut);
        assert_eq!(app.view.scale, MIN_SCALE);
        app.apply_action(Action::SetScale(999));
        assert_eq!(app.view.scale, MAX_SCALE);
        app.apply_action(Action::SetScale(0));
        assert_eq!(app.view.scale, MIN_SCALE);
        // Zoom to fit needs last frame's viewport size; without it, nothing happens.
        app.view.scale = 3;
        app.apply_action(Action::ZoomToFit);
        assert_eq!(app.view.scale, 3);
        app.view.last_viewport_size = Some(egui::vec2(516.0, 316.0));
        app.apply_action(Action::ZoomToFit);
        assert_eq!(
            app.view.scale, 10,
            "the 50x30 Life demo fits at 10 px per cell"
        );
        let lines = app.view.show_grid_lines;
        app.apply_action(Action::ToggleGridLines);
        assert_eq!(app.view.show_grid_lines, !lines);
        app.apply_action(Action::ToggleLayer(Layer::Grid));
        assert_eq!(app.view.show_grid_lines, lines);
        app.apply_action(Action::ToggleLayer(Layer::Age));
        assert!(app.view.layers.age);
        app.apply_action(Action::ToggleLayer(Layer::Probability));
        assert!(!app.view.layers.probability);
        app.apply_action(Action::SetLayerOpacity(4.0));
        assert_eq!(app.view.layers.opacity, 1.0);
        app.apply_action(Action::SetAgeCap(0));
        assert_eq!(app.view.layers.age_cap, 1);
        app.apply_action(Action::SetFontScale(9.0));
        assert_eq!(app.chrome.font_scale, 3.0);
        app.apply_action(Action::ToggleLeft);
        assert!(!app.chrome.left_open);
        app.apply_action(Action::ToggleRight);
        assert!(!app.chrome.right_open);
        app.apply_action(Action::ToggleShortcuts);
        assert!(app.chrome.show_shortcuts);
        app.apply_action(Action::SetControlTab(ControlTab::Style));
        assert_eq!(app.chrome.control_tab, ControlTab::Style);
        app.apply_action(Action::SetWorkbenchTab(WorkbenchTab::Explore));
        assert_eq!(app.chrome.workbench_tab, WorkbenchTab::Explore);
        let red = Color32::from_rgb(200, 0, 0);
        app.apply_action(Action::SetGridLineColor(red));
        assert_eq!(app.view.grid_line_color, red);
        app.apply_action(Action::SetInactiveColor(red));
        assert_eq!(app.view.inactive_color, red);
        let alive = CellType::from("Alive");
        app.apply_action(Action::SetTypeColor(alive, red));
        assert_eq!(app.color_of(&alive), red);
        app.apply_action(Action::SetHistoryLimit1D(0));
        assert_eq!(app.view.history_limit_1d, 1);
    }

    #[test]
    fn scenario_edit_and_rule_actions_change_the_grid() {
        let mut app = test_app();
        app.apply_action(Action::LoadDemo(Demo::Rule30));
        assert_eq!(app.scenario.dim, Some(Dim::D1));
        app.apply_action(Action::LoadDemo(Demo::Radius2));
        assert_eq!(app.scenario.dim, Some(Dim::D1));
        app.apply_action(Action::LoadDemo(Demo::ThreeState2D));
        assert_eq!(app.scenario.dim, Some(Dim::D2));
        app.apply_action(Action::LoadDemo(Demo::StraightLine2D));
        assert_eq!(app.scenario.dim, Some(Dim::D2));
        app.apply_action(Action::LoadDemo(Demo::Life));
        assert_eq!(app.scenario.dim, Some(Dim::D2));
        app.apply_action(Action::Resize { w: 12, h: 7 });
        let g = app.scenario.d2.as_ref().unwrap();
        assert_eq!((g.width, g.height), (12, 7));
        app.apply_action(Action::Build1D {
            code: "110".into(),
            n: 1,
        });
        assert_eq!(app.scenario.dim, Some(Dim::D1));
        assert_eq!(
            app.scenario.d1.as_ref().unwrap().rule.subrules[0].wolfram_code,
            110
        );
        app.apply_action(Action::SetDrawMode(DrawMode::Paint));
        assert_eq!(app.edit.draw_mode, DrawMode::Paint);
        let x = CellType::from("X");
        app.apply_action(Action::SetDrawType(x));
        assert_eq!(app.edit.selected_draw_type, Some(x));
        app.apply_action(Action::AddType("  Ember ".into()));
        assert!(app.editor.custom_types.contains(&CellType::from("Ember").0));
        app.apply_action(Action::AddType(INACTIVE.into()));
        assert!(!app.editor.custom_types.contains(&CellType::inactive().0));

        // A rule draft goes live through ApplyRule; a bad one stays in the editor.
        app.apply_action(Action::LoadDemo(Demo::Life));
        {
            let draft = app.editor.rule_2d.as_mut().expect("draft mirrors the grid");
            draft.subrules[0].count = 5;
        }
        app.apply_action(Action::ApplyRule);
        assert_eq!(app.scenario.d2.as_ref().unwrap().rule.subrules[0].count, 5);
        assert!(app.editor.error_msg.is_none());
        {
            let draft = app.editor.rule_2d.as_mut().unwrap();
            draft.subrules[0].range = 0;
        }
        app.apply_action(Action::ApplyRule);
        assert!(app.editor.error_msg.is_some(), "range 0 is refused");
        assert_eq!(
            app.scenario.d2.as_ref().unwrap().rule.subrules[0].range,
            1,
            "grid untouched"
        );
        // Model parameters route through the same door.
        app.apply_action(Action::ApplyModelParam {
            key: "p0".into(),
            value: ParamValue::Float(0.1),
        });
        assert!(
            app.chrome
                .status_message
                .as_deref()
                .unwrap()
                .contains("Model parameter error")
                || app.scenario.d2.as_ref().unwrap().model.is_none()
        );
    }

    #[test]
    fn switching_theme_moves_default_colours_but_not_chosen_ones() {
        let mut app = test_app();
        assert_eq!(
            app.view.inactive_color,
            ThemeChoice::Dark.default_inactive()
        );
        app.apply_action(Action::SetTheme(ThemeChoice::Light));
        assert_eq!(app.chrome.theme, ThemeChoice::Light);
        assert_eq!(
            app.view.inactive_color,
            ThemeChoice::Light.default_inactive()
        );
        assert_eq!(
            app.view.grid_line_color,
            ThemeChoice::Light.default_grid_line()
        );
        // A colour the user picked is theirs.
        let mine = egui::Color32::from_rgb(1, 2, 3);
        app.view.inactive_color = mine;
        app.apply_action(Action::SetTheme(ThemeChoice::Dark));
        assert_eq!(app.view.inactive_color, mine);
        assert_eq!(
            app.view.grid_line_color,
            ThemeChoice::Dark.default_grid_line()
        );
    }

    #[test]
    fn undo_restores_the_last_edit_batch_only_while_paused() {
        let mut app = test_app();
        app.load_demo_life();
        let alive = CellType::from("Alive");
        let inactive = CellType::inactive();
        let prev = app.cell_type_at(0).unwrap();
        assert_eq!(prev, inactive);
        assert!(app.set_cell(0, alive, "test"));
        app.edit.undo_stack.push(vec![(0, prev)]);
        app.playback.playing = true;
        app.apply_action(Action::Undo);
        assert_eq!(app.cell_type_at(0), Some(alive), "no undo while playing");
        app.playback.playing = false;
        app.apply_action(Action::Undo);
        assert_eq!(app.cell_type_at(0), Some(inactive));
        app.apply_action(Action::Undo);
        assert_eq!(
            app.cell_type_at(0),
            Some(inactive),
            "an empty stack is a no-op"
        );
    }

    #[test]
    fn palette_presets_reslot_automatic_colours_but_keep_config_colours() {
        use crate::gui::theme::PALETTES;
        let mut app = test_app();
        app.load_demo_life();
        let alive = CellType::from("Alive");
        assert_eq!(
            app.color_of(&alive),
            PALETTES[0].colors[0],
            "first type, first slot"
        );
        // A colour the config asked for survives a palette change; a hand pick does not.
        let mut cfg = std::collections::BTreeMap::new();
        cfg.insert("Alive".to_string(), "#112233".to_string());
        app.apply_config_colors(&cfg);
        assert_eq!(app.color_of(&alive), Color32::from_rgb(0x11, 0x22, 0x33));
        app.apply_action(Action::SetPalette(3));
        assert_eq!(app.view.palette_index, 3);
        assert_eq!(app.view.palette, PALETTES[3].colors.to_vec());
        assert_eq!(
            app.color_of(&alive),
            Color32::from_rgb(0x11, 0x22, 0x33),
            "config colour kept"
        );
        app.view.config_colors.clear();
        app.apply_action(Action::SetTypeColor(alive, Color32::WHITE));
        app.apply_action(Action::SetPalette(1));
        assert_eq!(
            app.color_of(&alive),
            PALETTES[1].colors[0],
            "hand pick dropped, new slot used"
        );
        app.apply_action(Action::SetTypeColor(alive, Color32::WHITE));
        app.apply_action(Action::ReslotColors);
        assert_eq!(app.color_of(&alive), PALETTES[1].colors[0]);
        app.apply_action(Action::SetPalette(99));
        assert_eq!(app.view.palette_index, PALETTES.len() - 1, "clamped");
        // Loading a scenario forgets the previous config's colours.
        app.load_demo_life();
        assert!(app.view.config_colors.is_empty());
    }

    #[test]
    fn strokes_stamps_and_cycles_are_single_undo_entries() {
        let mut app = test_app();
        app.load_demo_life();
        let alive = CellType::from("Alive");
        let inactive = CellType::inactive();
        app.apply_action(Action::SetDrawMode(DrawMode::Paint));
        app.apply_action(Action::SetDrawType(alive));
        app.apply_action(Action::SetBrush(99));
        assert_eq!(app.edit.brush, MAX_BRUSH);
        app.apply_action(Action::BrushGrow);
        assert_eq!(app.edit.brush, MAX_BRUSH);
        app.apply_action(Action::SetBrush(0));
        app.apply_action(Action::BrushShrink);
        assert_eq!(app.edit.brush, 0);
        app.apply_action(Action::BrushGrow);
        assert_eq!(app.edit.brush, 1);
        app.apply_action(Action::SetBrushShape(cella_lib::Neighborhood2D::Knight));
        assert_eq!(app.edit.brush_shape, cella_lib::Neighborhood2D::Knight);
        app.apply_action(Action::SetBrush(3));
        // Two frames of the same stroke, overlapping cells, then release.
        app.apply_action(Action::PaintCells(vec![0, 1, 2]));
        app.apply_action(Action::PaintCells(vec![2, 3]));
        assert_eq!(app.edit.stroke_touched.len(), 4);
        assert_eq!(app.edit.undo_stack.len(), 0, "the stroke is still open");
        app.apply_action(Action::EndStroke);
        assert_eq!(app.edit.undo_stack.len(), 1);
        assert_eq!(app.edit.undo_stack[0].len(), 4, "each cell recorded once");
        assert!(app.edit.stroke_touched.is_empty());
        for i in 0..4 {
            assert_eq!(app.cell_type_at(i), Some(alive));
        }
        app.apply_action(Action::Undo);
        for i in 0..4 {
            assert_eq!(app.cell_type_at(i), Some(inactive));
        }
        // Switching tool mid-stroke closes it.
        app.apply_action(Action::PaintCells(vec![7]));
        app.apply_action(Action::SetDrawMode(DrawMode::Stamp));
        assert_eq!(app.edit.undo_stack.len(), 1);
        // A stamp is one entry too, and undoes as a whole.
        app.apply_action(Action::SelectStamp(0));
        app.apply_action(Action::StampAt(10 * 50 + 10));
        assert_eq!(app.edit.undo_stack.len(), 2);
        assert_eq!(
            app.cell_type_at(12 * 50 + 12),
            Some(alive),
            "glider cell (2,2)"
        );
        app.apply_action(Action::Undo);
        assert_eq!(app.cell_type_at(12 * 50 + 12), Some(inactive));
        app.apply_action(Action::SelectStamp(999));
        assert_eq!(app.edit.stamp, super::super::patterns::PATTERNS.len() - 1);
        // Cycle advances Inactive -> Alive on the Life demo.
        app.apply_action(Action::SetDrawMode(DrawMode::Cycle));
        app.apply_action(Action::CycleAt(5));
        assert_eq!(app.cell_type_at(5), Some(alive));
        app.apply_action(Action::CycleAt(5));
        assert_eq!(app.cell_type_at(5), Some(inactive), "wraps around");
        // Nothing paints while playing.
        app.playback.playing = true;
        app.apply_action(Action::CycleAt(6));
        assert_eq!(app.cell_type_at(6), Some(inactive));
        app.apply_action(Action::PaintCells(vec![6]));
        assert_eq!(app.cell_type_at(6), Some(inactive));
        app.playback.playing = false;
        // Stamping a 1D grid does nothing.
        app.apply_action(Action::LoadDemo(Demo::Rule30));
        app.apply_action(Action::StampAt(3));
        assert!(app.edit.undo_stack.is_empty());
        app.apply_action(Action::ToggleInspector);
        assert!(!app.view.inspector);
    }

    #[test]
    fn random_fill_is_reproducible_and_resets_or_records_undo() {
        let mut app = test_app();
        app.load_demo_life();
        let alive = CellType::from("Alive");
        app.apply_action(Action::RandomFill {
            density: 0.3,
            ty: alive,
            seed: 42,
            clear_first: true,
        });
        let cells: Vec<CellType> = (0..1500).map(|i| app.cell_type_at(i).unwrap()).collect();
        let n = cells.iter().filter(|c| **c == alive).count();
        assert!((350..=550).contains(&n), "about 30 % of 1500: {n}");
        assert_eq!(app.current_step(), 0);
        assert_eq!(
            app.scenario.d2.as_ref().unwrap().seed,
            42,
            "the grid seed follows the fill"
        );
        assert_eq!(
            *app.scenario
                .d2
                .as_ref()
                .unwrap()
                .counts_current
                .get(&alive.0)
                .unwrap(),
            n as u64
        );
        assert!(
            app.edit.undo_stack.is_empty(),
            "a cleared fill is a new start, not an edit"
        );
        // Reset returns to the fill, not the demo.
        app.apply_action(Action::Step);
        app.apply_action(Action::Reset);
        let again: Vec<CellType> = (0..1500).map(|i| app.cell_type_at(i).unwrap()).collect();
        assert_eq!(again, cells);
        // Same seed, same picture; a different seed differs.
        let mut other = test_app();
        other.load_demo_life();
        other.apply_action(Action::RandomFill {
            density: 0.3,
            ty: alive,
            seed: 42,
            clear_first: true,
        });
        assert_eq!(
            (0..1500)
                .map(|i| other.cell_type_at(i).unwrap())
                .collect::<Vec<_>>(),
            cells
        );
        other.apply_action(Action::RandomFill {
            density: 0.3,
            ty: alive,
            seed: 43,
            clear_first: true,
        });
        assert_ne!(
            (0..1500)
                .map(|i| other.cell_type_at(i).unwrap())
                .collect::<Vec<_>>(),
            cells
        );
        // Without clearing, the fill is one undo entry over the existing cells.
        app.apply_action(Action::RandomFill {
            density: 0.2,
            ty: alive,
            seed: 7,
            clear_first: false,
        });
        assert_eq!(app.edit.undo_stack.len(), 1);
        let more = (0..1500)
            .filter(|i| app.cell_type_at(*i) == Some(alive))
            .count();
        assert!(more >= n, "adding cells never removes any");
        app.apply_action(Action::Undo);
        assert_eq!(
            (0..1500)
                .map(|i| app.cell_type_at(i).unwrap())
                .collect::<Vec<_>>(),
            cells
        );
        // Unknown types and playing are refused with a message.
        app.apply_action(Action::RandomFill {
            density: 0.5,
            ty: CellType::from("Ghost"),
            seed: 1,
            clear_first: true,
        });
        assert!(
            app.chrome
                .status_message
                .as_deref()
                .unwrap()
                .contains("Ghost")
        );
        app.playback.playing = true;
        app.apply_action(Action::RandomFill {
            density: 0.5,
            ty: alive,
            seed: 1,
            clear_first: true,
        });
        assert!(
            app.chrome
                .status_message
                .as_deref()
                .unwrap()
                .contains("Pause")
        );
    }

    #[test]
    fn the_queue_drains_in_order_and_stops_at_the_limit() {
        let mut app = test_app();
        app.load_demo_life();
        app.push(Action::SetScale(5));
        app.push(Action::ZoomIn);
        app.push(Action::ToggleGridLines);
        let lines = app.view.show_grid_lines;
        app.drain_actions();
        assert_eq!(app.view.scale, 6);
        assert_eq!(app.view.show_grid_lines, !lines);
        assert!(app.actions.is_empty());
        for _ in 0..(DRAIN_LIMIT + 5) {
            app.push(Action::ZoomIn);
        }
        app.drain_actions();
        assert!(app.actions.is_empty());
        assert!(
            app.chrome
                .status_message
                .as_deref()
                .unwrap()
                .contains("Too many")
        );
    }
}