Browse Source
* Add ShouldRenderOnUIThread//Fps options to the AvaloniaHeadlessPlatformOptions, allowing SleepLoopRenderTimer to be enabled * Don't throw from ForceRenderTimerTick * Run DispatcherFrame on RequestCommitAsync, when HeadlessRenderTimer is not available * This method apparently was publicpull/21537/head
committed by
GitHub
2 changed files with 95 additions and 50 deletions
@ -0,0 +1,40 @@ |
|||
using System; |
|||
using System.Diagnostics; |
|||
using Avalonia.Reactive; |
|||
using Avalonia.Rendering; |
|||
using Avalonia.Threading; |
|||
|
|||
namespace Avalonia.Headless; |
|||
|
|||
/// <summary>
|
|||
/// A render timer implementation for headless environments that uses a DispatcherTimer to schedule ticks on the UI thread.
|
|||
/// Can be controlled with <see cref="ForceTick"/> method.
|
|||
/// </summary>
|
|||
internal class HeadlessRenderTimer(int framesPerSecond) : DefaultRenderTimer(framesPerSecond) |
|||
{ |
|||
private readonly int _framesPerSecond = framesPerSecond; |
|||
private Action? _forceTick; |
|||
protected override IDisposable StartCore(Action<TimeSpan> tick) |
|||
{ |
|||
var st = Stopwatch.StartNew(); |
|||
_forceTick = () => tick(st.Elapsed); |
|||
|
|||
var timer = new DispatcherTimer(DispatcherPriority.UiThreadRender) |
|||
{ |
|||
Interval = TimeSpan.FromSeconds(1.0 / _framesPerSecond), |
|||
Tag = "HeadlessRenderTimer" |
|||
}; |
|||
timer.Tick += (s, e) => tick(st.Elapsed); |
|||
timer.Start(); |
|||
|
|||
return Disposable.Create(() => |
|||
{ |
|||
_forceTick = null; |
|||
timer.Stop(); |
|||
}); |
|||
} |
|||
|
|||
public override bool RunsInBackground => false; |
|||
|
|||
public void ForceTick() => _forceTick?.Invoke(); |
|||
} |
|||
Loading…
Reference in new issue