Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/ModernOverlay.Win32/Win32OverlayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,12 @@ public void RunFrameLoop(Func<TimeSpan> resolveInterval, Action renderFrame, Can
ownerThread.RunFrameLoop(resolveInterval, renderFrame, cancellationToken);
}

public void RunFrameLoop(Func<TimeSpan> resolveInterval, Func<bool> renderFrame, CancellationToken cancellationToken)
{
ThrowIfDisposed();
ownerThread.RunFrameLoop(resolveInterval, renderFrame, cancellationToken);
}

public void Dispose()
{
if (disposed)
Expand Down
32 changes: 28 additions & 4 deletions src/ModernOverlay.Win32/Win32OwnerThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,16 @@ public void RunFrameLoop(TimeSpan interval, Action renderFrame, CancellationToke
=> RunFrameLoop(() => interval, renderFrame, cancellationToken);

public void RunFrameLoop(Func<TimeSpan> resolveInterval, Action renderFrame, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(renderFrame);
RunFrameLoop(resolveInterval, () =>
{
renderFrame();
return true;
}, cancellationToken);
}

public void RunFrameLoop(Func<TimeSpan> resolveInterval, Func<bool> renderFrame, CancellationToken cancellationToken)
{
ArgumentNullException.ThrowIfNull(resolveInterval);
ArgumentNullException.ThrowIfNull(renderFrame);
Expand Down Expand Up @@ -169,7 +179,7 @@ private void RunMessageLoop()
DrainMessages();
}

private void RunFrameLoopCore(Func<TimeSpan> resolveInterval, Action renderFrame, CancellationToken cancellationToken)
private void RunFrameLoopCore(Func<TimeSpan> resolveInterval, Func<bool> renderFrame, CancellationToken cancellationToken)
{
TimeSpan interval = NormalizeFrameInterval(resolveInterval());
if (interval == TimeSpan.Zero)
Expand Down Expand Up @@ -226,7 +236,7 @@ private void RunFrameLoopCore(Func<TimeSpan> resolveInterval, Action renderFrame
SetFrameTimer(timer, interval);
}

renderFrame();
_ = renderFrame();
}
}
}
Expand Down Expand Up @@ -254,8 +264,14 @@ private static TimeSpan NormalizeFrameInterval(TimeSpan interval)
: throw new ArgumentOutOfRangeException(nameof(interval), "Frame interval cannot be negative.");
}

private void RunUnlimitedFrameLoop(Action renderFrame, CancellationToken cancellationToken)
private void RunUnlimitedFrameLoop(Func<bool> renderFrame, CancellationToken cancellationToken)
{
nint[] handles =
[
workAvailable.SafeWaitHandle.DangerousGetHandle(),
cancellationToken.WaitHandle.SafeWaitHandle.DangerousGetHandle(),
];

while (!stopRequested && !cancellationToken.IsCancellationRequested)
{
DrainWorkItems();
Expand All @@ -266,7 +282,15 @@ private void RunUnlimitedFrameLoop(Action renderFrame, CancellationToken cancell
break;
}

renderFrame();
if (!renderFrame())
{
_ = NativeMethods.MsgWaitForMultipleObjectsEx(
(uint)handles.Length,
handles,
1,
NativeMethods.QsAllInput,
NativeMethods.MwmoInputAvailable);
}
}
}

Expand Down
11 changes: 6 additions & 5 deletions src/ModernOverlay/OverlayWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -406,22 +406,22 @@ public ValueTask DisposeAsync()
return ValueTask.CompletedTask;
}

private void RenderOneFrame()
private bool RenderOneFrame()
{
if (paused)
{
return;
return false;
}

if (!desiredVisible && Options.HiddenRenderPolicy == HiddenRenderPolicy.Pause)
{
return;
return false;
}

SyncTargetBoundsIfDue(start: DateTimeOffset.UtcNow);
if (targetRenderPaused)
{
return;
return false;
}

DateTimeOffset start = DateTimeOffset.UtcNow;
Expand All @@ -430,10 +430,11 @@ private void RenderOneFrame()
try
{
nativeWindow.InvokeOnOwnerThread(() => RenderOneFrameOnOwnerThread(start));
return true;
}
catch when (Options.ExceptionPolicy == RenderExceptionPolicy.Continue)
{
return;
return false;
}
catch when (Options.ExceptionPolicy == RenderExceptionPolicy.PauseOverlay)
{
Expand Down
21 changes: 21 additions & 0 deletions tests/ModernOverlay.Tests/OverlayWindowThreadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,27 @@ public async Task HiddenOverlayPausesRenderingByDefault()
Assert.AreEqual(0, overlay.FrameStats.FrameCount);
}

[TestMethod]
[TestCategory("WindowsIntegration")]
public async Task HiddenUnlimitedOverlayPausesWithoutRendering()
{
using var runCancellation = new CancellationTokenSource(TimeSpan.FromMilliseconds(250));
int renderAttempts = 0;

await using OverlayWindow overlay = await OverlayWindow.CreateAsync(new OverlayWindowOptions
{
IsVisible = false,
FrameRateLimit = FrameRateLimit.Unlimited,
});

overlay.Render += _ => renderAttempts++;

await overlay.RunAsync(runCancellation.Token);

Assert.AreEqual(0, renderAttempts);
Assert.AreEqual(0, overlay.FrameStats.FrameCount);
}

[TestMethod]
[TestCategory("WindowsIntegration")]
public async Task PauseSuppressesRenderingEvenWhenHiddenRenderingContinues()
Expand Down
Loading