@ -8,6 +8,9 @@ using Avalonia.Threading;
namespace Avalonia.Headless ;
/// <summary>
/// Set of extension methods to simplify usage of Avalonia.Headless platform.
/// </summary>
public static class HeadlessWindowExtensions
{
/// <summary>
@ -20,7 +23,7 @@ public static class HeadlessWindowExtensions
AvaloniaHeadlessPlatform . ForceRenderTimerTick ( ) ;
return topLevel . GetLastRenderedFrame ( ) ;
}
/// <summary>
/// Reads last rendered frame.
/// Note, in order to trigger rendering timer, call <see cref="AvaloniaHeadlessPlatform.ForceRenderTimerTick"/> method.
@ -30,7 +33,8 @@ public static class HeadlessWindowExtensions
{
if ( AvaloniaLocator . Current . GetService < IPlatformRenderInterface > ( ) is HeadlessPlatformRenderInterface )
{
throw new NotSupportedException ( "To capture a rendered frame, make sure that headless application was initialized with '.UseSkia()' and disabled 'UseHeadlessDrawing' in the 'AvaloniaHeadlessPlatformOptions'." ) ;
throw new NotSupportedException (
"To capture a rendered frame, make sure that headless application was initialized with '.UseSkia()' and disabled 'UseHeadlessDrawing' in the 'AvaloniaHeadlessPlatformOptions'." ) ;
}
return GetImpl ( topLevel ) . GetLastRenderedFrame ( ) ;
@ -40,49 +44,58 @@ public static class HeadlessWindowExtensions
/// Simulates keyboard press on the headless window/toplevel.
/// </summary>
public static void KeyPress ( this TopLevel topLevel , Key key , RawInputModifiers modifiers ) = >
GetImpl ( topLevel ) . KeyPress ( key , modifiers ) ;
RunJobsAnd GetImpl( topLevel ) . KeyPress ( key , modifiers ) ;
/// <summary>
/// Simulates keyboard release on the headless window/toplevel.
/// </summary>
public static void KeyRelease ( this TopLevel topLevel , Key key , RawInputModifiers modifiers ) = >
GetImpl ( topLevel ) . KeyRelease ( key , modifiers ) ;
RunJobsAnd GetImpl( topLevel ) . KeyRelease ( key , modifiers ) ;
/// <summary>
/// Simulates mouse down on the headless window/toplevel.
/// </summary>
public static void MouseDown ( this TopLevel topLevel , Point point , MouseButton button ,
RawInputModifiers modifiers = RawInputModifiers . None ) = > GetImpl ( topLevel ) . MouseDown ( point , button , modifiers ) ;
RawInputModifiers modifiers = RawInputModifiers . None ) = >
RunJobsAndGetImpl ( topLevel ) . MouseDown ( point , button , modifiers ) ;
/// <summary>
/// Simulates mouse move on the headless window/toplevel.
/// </summary>
public static void MouseMove ( this TopLevel topLevel , Point point ,
RawInputModifiers modifiers = RawInputModifiers . None ) = > GetImpl ( topLevel ) . MouseMove ( point , modifiers ) ;
RawInputModifiers modifiers = RawInputModifiers . None ) = >
RunJobsAndGetImpl ( topLevel ) . MouseMove ( point , modifiers ) ;
/// <summary>
/// Simulates mouse up on the headless window/toplevel.
/// </summary>
public static void MouseUp ( this TopLevel topLevel , Point point , MouseButton button ,
RawInputModifiers modifiers = RawInputModifiers . None ) = > GetImpl ( topLevel ) . MouseUp ( point , button , modifiers ) ;
RawInputModifiers modifiers = RawInputModifiers . None ) = >
RunJobsAndGetImpl ( topLevel ) . MouseUp ( point , button , modifiers ) ;
/// <summary>
/// Simulates mouse wheel on the headless window/toplevel.
/// </summary>
public static void MouseWheel ( this TopLevel topLevel , Point point , Vector delta ,
RawInputModifiers modifiers = RawInputModifiers . None ) = > GetImpl ( topLevel ) . MouseWheel ( point , delta , modifiers ) ;
RawInputModifiers modifiers = RawInputModifiers . None ) = >
RunJobsAndGetImpl ( topLevel ) . MouseWheel ( point , delta , modifiers ) ;
/// <summary>
/// Simulates drag'n'drop target on the headless window/toplevel.
/// </summary>
public static void DragDrop ( this TopLevel topLevel , Point point , RawDragEventType type , IDataObject data ,
DragDropEffects effects , RawInputModifiers modifiers = RawInputModifiers . None ) = >
GetImpl ( topLevel ) . DragDrop ( point , type , data , effects , modifiers ) ;
RunJobsAnd GetImpl( topLevel ) . DragDrop ( point , type , data , effects , modifiers ) ;
private static IHeadlessWindow GetImpl ( this TopLevel topLevel )
private static IHeadlessWindow RunJobsAnd GetImpl( this TopLevel topLevel )
{
Dispatcher . UIThread . RunJobs ( ) ;
return GetImpl ( topLevel ) ;
}
private static IHeadlessWindow GetImpl ( this TopLevel topLevel )
{
return topLevel . PlatformImpl as IHeadlessWindow ? ?
throw new InvalidOperationException ( "TopLevel must be a headless window." ) ;
throw new InvalidOperationException ( "TopLevel must be a headless window." ) ;
}
}