Skip to content

Interactive 2D wave optics simulator. Turn any image into a refractive scene, add sources, watch waves refract, diffract and interfere. Probes, intensity maps, video export.

C#

A simulation edit stops playback and any active run; the live follow reaches the last frame

Alexander Graef6806413403e8
diff --git a/CLAUDE.md b/CLAUDE.md
index 28d7c94..d782850 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -133,7 +133,11 @@ WPF shell as it stands:
and curve pens (2.3 ms to 1.1 per draw); and **the shown position advances only while the presenter is free**
(`FramePresenter.IsBusy`, in the playback tick and the live follow): the clock keeps counting, so time stays right,
and the overlays and the bar are redrawn once per picture that can be shown instead of sixty times a second for
- pictures that never appear (this canvas presented 11 to 14 a second under the harness).
+ pictures that never appear (this canvas presented 11 to 14 a second under the harness). **The end of a run
+ follows to the last frame regardless** (2026-10-16, after the live follow stopped at 90 of 99 on a short run
+ and Live went off there): the run's final post sets the frame count and then the state, the frame count
+ handler skips the follow while a picture is drawn and counts on the next heartbeat, and none comes after the
+ end, so the state handler does that last follow itself before switching Live off.
**The colouring, measured properly** (2026-10-16, later): `library_check -- bench-render [w h]` times the
renderer alone, parallel and on one thread, scalar against vector, with the warm-up the "Bench numbers need
the JIT warm" rule above demands (60 calls and a 300 ms pause; a first version with 20 calls reported 4.6 to
@@ -178,9 +182,10 @@ WPF shell as it stands:
on), redoes the stored run after 1.5 s of quiet. An automatic run (`SimulationRun.IsAutomatic`)
never moves the preview: the position stays (the reachable end of the axis stays the planned run
while it fills, so nothing coerces it), Live is switched off whatever the button said, and the
- scratch frame stays on screen until the store has that frame. An edit during an automatic run
- stops it at once (its snapshot is obsolete) and the quiet period starts a fresh one; a run the
- user started keeps going and is just marked stale. Discarding a run keeps the position too; a replaced project (New, Open, Import, Close) stops playback and
+ scratch frame stays on screen until the store has that frame. An edit during a run, **whoever started it**,
+ stops it at once (its snapshot is obsolete; the user's rule, 2026-10-16: an edit makes the whole run moot; until
+ then a run the user had started went on and was only marked stale) and the quiet period starts a fresh one,
+ automatic, with the position kept. Discarding a run keeps the position too; a replaced project (New, Open, Import, Close) stops playback and
puts the timeline back to frame 0 (`PreviewPane.OnProjectReplaced`, the user's rule, 2026-10-15).
**Play means "show me the simulation"**: Play/Space issues `SimulationCommands.EnsureRun`, which
starts an automatic run when there is none or it is stale (position kept, Live off, playback not
@@ -212,7 +217,15 @@ WPF shell as it stands:
as a replay from frame 0 (2026-10-10). That rule also holds without a run: the presenter is
cleared only when both the run and the scratch frame are gone (the project replaced), not on
every scrub step, which flashed the empty picture between frames (2026-10-13). A skipped scratch frame (predicted cost over
- `ScratchRunner.Budget`, 6 s) is said in the status bar once per frame.
+ `ScratchRunner.Budget`, 6 s) is said in the status bar once per frame. **No scratch frame while the preview
+ plays** (2026-10-16, after an edit during playback of a finished run kept playing the old source until playback
+ ended): every playback step restarted the 120 ms scratch timer, so it never fired, and the automatic re-run,
+ which waits behind a pending scratch frame, never started. `ScheduleScratch` does nothing while
+ `MainWindow.IsPreviewPlaying` (bound from the pane's `IsPlaying`): a frame for a position playback has left
+ could never serve. **A simulation edit stops playback at once** (the user's rule: the whole run is stale, playing
+ its frames on or computing more of them serves nothing), so the edit is then handled as after a scrub: the frame
+ for the position playback halted at is recomputed and the re-run follows with the position kept; what may play on
+ is a run that has not reached the position, and the frame for the final position is requested when playback stops.
- **Colour schemes and the scene image** (2026-10-13, decisions provisional, see the list at the end
of this bullet): a `ColorScheme` is a list of `ColorStop`s, each a colour at a position 0..1
(`Stops`; the table interpolates between neighbours and holds the end colours outside), observable
@@ -720,17 +733,19 @@ passing harnesses for comfort, never all of them after an edit.
- `_tools/ui_check/PreviewCheck.ps1` drives the WPF preview pane end to end on the default project
(43 checks: play/pause at the 60 fps clock, speed incl. locale spelling, timeline keys, frame
steps, in/out/loop, split radios, splitter, tab order, focus hand-off, foreground untouched).
- `_tools/ui_check/RunCheck.ps1` runs simulations (36 checks): `tiny.wdproj` (64×48, 200 steps) with
+ `_tools/ui_check/RunCheck.ps1` runs simulations (71 checks): `tiny.wdproj` (64×48, 200 steps) with
F5 to completion, live follow, `,` detaching, the field picture differing between frames (MD5 of
a `PrintWindow` crop), `F` re-attaching, playback of the stored frames, a second run, the source
handle sitting at the source's canvas position on both pictures, staying there when the window
is resized (`SetWindowPos` without activation; the handles once lagged a layout pass behind the
labels, 2026-10-10) and moving with the arrow keys while the tree selection follows, a scratch frame replacing the black canvas after an edit with
no run, and after the run: an edit marks it stale, the shown frame is recomputed from scratch,
- the automatic re-run completes with the position kept and reproduces the original picture; then
+ the automatic re-run completes with the position kept and reproduces the original picture, and an edit during
+ looping playback stales the run, stops playback where it is and the re-run completes with that position kept
+ (2026-10-16); then
`tiny_long.wdproj` (2 000 000 steps, a frame every 1000) for F6 pause/resume and a stop through the Run button turned Stop with the
partial frames kept, then an edit starting an automatic re-run that leaves the position and Live
- alone, and a second edit stopping it at once before a fresh one starts. Both dot-source `Common.ps1` (native helpers, lookups, `Start-App`/`Stop-App`,
+ alone, a second edit stopping it at once before a fresh one starts, and an edit during a run the user started (F5) stopping that one too, the automatic re-run following (2026-10-16). Both dot-source `Common.ps1` (native helpers, lookups, `Start-App`/`Stop-App`,
`CanvasPoint` for the picture's uniform mapping, `TreeItemSelected`). The split orientation is a
persisted user setting, so a harness must set the layout it expects instead of assuming it.
`_tools/ui_check/LanguageCheck.ps1` (11 checks) drives View > Language by keyboard: F10 focuses the
diff --git a/WaveDudeNext.WPF/MainWindow.Live.cs b/WaveDudeNext.WPF/MainWindow.Live.cs
index 902b2da..5922e72 100644
--- a/WaveDudeNext.WPF/MainWindow.Live.cs
+++ b/WaveDudeNext.WPF/MainWindow.Live.cs
@@ -65,6 +65,10 @@ public partial class MainWindow
public static readonly DependencyProperty PreviewPositionProperty = DependencyProperty.Register(
nameof(PreviewPosition), typeof(int), typeof(MainWindow), new PropertyMetadata(0, (d, _) => ((MainWindow)d).OnPreviewPositionChanged()));
+ /// <summary>Identifies the <see cref="IsPreviewPlaying"/> property.</summary>
+ public static readonly DependencyProperty IsPreviewPlayingProperty = DependencyProperty.Register(
+ nameof(IsPreviewPlaying), typeof(bool), typeof(MainWindow), new PropertyMetadata(false, (d, _) => ((MainWindow)d).OnPreviewPlayingChanged()));
+
private ProjectWatcher? _watcher;
private ScratchRunner? _scratch;
private SimulationRun? _pausedForScratch;
@@ -120,6 +124,13 @@ public partial class MainWindow
set => SetValue(PreviewPositionProperty, value);
}
+ /// <summary>Whether the preview is playing, bound from the pane: no scratch frame is requested while it is (see <see cref="ScheduleScratch"/>).</summary>
+ public bool IsPreviewPlaying
+ {
+ get => (bool)GetValue(IsPreviewPlayingProperty);
+ set => SetValue(IsPreviewPlayingProperty, value);
+ }
+
/// <summary>A new project: watch it instead of the old one; nothing is stale and no scratch frame applies.</summary>
private void OnProjectReplaced(Project? project)
{
@@ -187,10 +198,19 @@ public partial class MainWindow
{
IsRunStale = true;
}
- if (Run is { IsAutomatic: true, IsActive: true })
+ if (previewPane.IsPlaying)
{
- // its snapshot is already obsolete: stop it now, the quiet period starts a fresh one
- Logger.Debug("The automatic run is stopped: its snapshot is obsolete");
+ // the whole run is stale: playing its frames on, or computing more of them, serves nothing (the user's
+ // rule, 2026-10-16); stopped first, so the scratch frame below is for the position playback halted at
+ Logger.Debug("Playback stopped: the run is stale");
+ previewPane.IsPlaying = false;
+ }
+ if (Run is { IsActive: true })
+ {
+ // its snapshot is already obsolete: stop it now, the quiet period starts a fresh one. Any run, since
+ // 2026-10-16 (the user's rule: an edit makes the whole run moot, whoever started it); before, a run the
+ // user had started went on and was only marked stale
+ Logger.Debug("The {Kind} is stopped: its snapshot is obsolete", Run.IsAutomatic ? "automatic run" : "run");
Run.Stop();
}
ScheduleScratch();
@@ -236,9 +256,30 @@ public partial class MainWindow
}
}
- /// <summary>After <see cref="ScratchDelay"/> of quiet, asks for the frame at the preview's position.</summary>
+ /// <summary>Playback stopped at a position the store cannot show: now the frame for it is asked for.</summary>
+ private void OnPreviewPlayingChanged()
+ {
+ if (!IsPreviewPlaying && NeedsScratch)
+ {
+ ScheduleScratch();
+ }
+ }
+
+ /// <summary>
+ /// After <see cref="ScratchDelay"/> of quiet, asks for the frame at the preview's position. Not while the preview
+ /// plays (2026-10-16, after an edit during playback kept playing the old frames until playback ended): a scratch
+ /// frame could never serve then, the position has moved on before it arrives, and every playback step restarted
+ /// this timer, so it never fired and the automatic re-run, which waits behind a pending scratch frame, never
+ /// started. A simulation edit stops playback at once (<see cref="OnProjectChanged"/>); what plays on is a run
+ /// that has not reached the position, and the frame for the final position is asked for when playback stops
+ /// (<see cref="OnPreviewPlayingChanged"/>).
+ /// </summary>
private void ScheduleScratch()
{
+ if (IsPreviewPlaying)
+ {
+ return;
+ }
_scratchTimer ??= MakeTimer(ScratchDelay, RequestScratch);
_scratchTimer.Stop();
_scratchTimer.Start();
diff --git a/WaveDudeNext.WPF/MainWindow.xaml b/WaveDudeNext.WPF/MainWindow.xaml
index 36f1623..cd5971c 100644
--- a/WaveDudeNext.WPF/MainWindow.xaml
+++ b/WaveDudeNext.WPF/MainWindow.xaml
@@ -353,7 +353,7 @@ version. It is distributed WITHOUT ANY WARRANTY. See the LICENSE.md file or
<panes:PreviewPane x:Name="previewPane" Project="{Binding Project}" Run="{Binding Run}" SelectedItem="{Binding SelectedItem, Mode=TwoWay}"
IsStale="{Binding IsRunStale}" IsProbesStale="{Binding IsProbesStale}" ScratchFrame="{Binding ScratchFrame}"
IsComputing="{Binding IsComputingFrame}"
- Position="{Binding PreviewPosition, Mode=TwoWay}"
+ Position="{Binding PreviewPosition, Mode=TwoWay}" IsPlaying="{Binding IsPreviewPlaying, Mode=OneWayToSource}"
Orientation="{Binding Source={x:Static p:Settings.Default}, Path=PreviewOrientation, Mode=TwoWay}" />
</Border>
</docking:DockHost.DocumentContent>
diff --git a/WaveDudeNext.WPF/Panes/PreviewPane.xaml.cs b/WaveDudeNext.WPF/Panes/PreviewPane.xaml.cs
index adc7b0c..272d528 100644
--- a/WaveDudeNext.WPF/Panes/PreviewPane.xaml.cs
+++ b/WaveDudeNext.WPF/Panes/PreviewPane.xaml.cs
@@ -620,6 +620,15 @@ public partial class PreviewPane : UserControl
break;
case nameof(SimulationRun.State):
UpdateFrames();
+ if (IsLive && Run?.IsFinished == true)
+ {
+ // the end: follow to the last frame whatever the presenter is doing (the busy gate above skips a
+ // follow while a picture is drawn and relies on the next heartbeat, and none comes after the end;
+ // the live follow stopped at 90 of 99 on a short run, 2026-10-16)
+ _following = true;
+ Position = NewestFrame;
+ _following = false;
+ }
UpdateSource();
if (Run?.IsFinished == true)
{
diff --git a/_tools/ui_check/RunCheck.ps1 b/_tools/ui_check/RunCheck.ps1
index e3778b1..6e709f9 100644
--- a/_tools/ui_check/RunCheck.ps1
+++ b/_tools/ui_check/RunCheck.ps1
@@ -129,6 +129,25 @@ try {
Check "moving it back: stale, then re-run" ((WaitFor { (RunStatus) -like "*edited since*" } 3000) -and (WaitFor { (RunStatus) -like "Completed:*" -and (RunStatus) -notlike "*edited since*" } 20000 200)) (RunStatus)
Start-Sleep -Milliseconds 200
Check "the re-run reproduces the original frame (same picture)" ((FieldHash) -eq $stored) ("{0} vs {1}" -f (FieldHash).Substring(0, 8), $stored.Substring(0, 8))
+ # ------------------------------------------------------------ an edit during playback (2026-10-16)
+ # the whole run is stale, so playback stops at once where it is, the frame there is recomputed and the re-run
+ # follows with the position kept (before, every playback step restarted the scratch timer, behind which the
+ # automatic re-run waits, so the old frames played on until playback ended)
+ FocusOn "PreviewPlay"
+ Key $VK_L 150 # loop, so playback would not end by itself
+ Key $VK_SPACE 100
+ Check "playback runs (looping)" ((WaitFor { $p = Position; $p -gt 0 -and $p -lt 99 } 3000 20) -and (LoopOn)) (Frame)
+ FocusOn "Source:Source 1"
+ Key $VK_RIGHT 100
+ Check "an edit during playback marks the run stale" (WaitFor { (RunStatus) -like "*edited since*" } 3000) (RunStatus)
+ $halted = Position
+ Start-Sleep -Milliseconds 400
+ Check "and stops playback at once" ((Position) -eq $halted) ("{0} then {1}" -f $halted, (Position))
+ Check "the re-run completes with the position kept" ((WaitFor { (RunStatus) -like "Completed:*" -and (RunStatus) -notlike "*edited since*" } 20000 200) -and (Position) -eq $halted) ("{0}, {1}" -f (RunStatus), (Frame))
+ Key $VK_LEFT 100
+ Check "moving it back: stale, then re-run" ((WaitFor { (RunStatus) -like "*edited since*" } 3000) -and (WaitFor { (RunStatus) -like "Completed:*" -and (RunStatus) -notlike "*edited since*" } 20000 200)) (RunStatus)
+ Key $VK_L 150 # loop off again
+ Check "loop off" (WaitFor { -not (LoopOn) } 1000)
FocusOn "PreviewPlay"
Key $VK_SPACE 100
$moving = WaitFor { $p = Position; $p -gt 0 -and $p -lt 99 } 3000 20
@@ -190,7 +209,17 @@ try {
Check "and a fresh automatic run starts after the quiet period" (WaitFor { (RunStatus) -like "Running:*" } 6000 100) (RunStatus)
Check "the position is still frame 30" ((Position) -eq 30) (Frame)
Key $VK_ESCAPE 150
- Key $VK_LEFT 50; Key $VK_LEFT 150
+ # ------------------------------------------------------------ an edit during a run the user started stops it too (2026-10-16)
+ Check "Esc stops the automatic run" (WaitFor { (RunStatus) -like "Stopped:*" } 3000 50) (RunStatus)
+ FocusOn "PreviewPlay"
+ Key $VK_F5 100
+ Check "F5 starts a run" (WaitFor { (RunStatus) -like "Running:*" } 6000 100) (RunStatus)
+ FocusOn "Source:Source 1"
+ Key $VK_RIGHT 100
+ Check "an edit stops the run the user started at once" (WaitFor { (RunStatus) -like "Stopped:*" } 3000 50) (RunStatus)
+ Check "and the automatic re-run follows after the quiet period" (WaitFor { (RunStatus) -like "Running:*" } 6000 100) (RunStatus)
+ Key $VK_ESCAPE 150
+ Key $VK_LEFT 50; Key $VK_LEFT 50; Key $VK_LEFT 150
Key $VK_ESCAPE 100
Check "the app never took the foreground" ((Foreground) -ne "APP") (Foreground)
}