Browse Source

Merge branch 'master' into move-first-last-skip-disabled

pull/8514/head
Luis v.d.Eltz 4 years ago
committed by GitHub
parent
commit
e343de6637
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      Avalonia.sln
  2. 4
      samples/ControlCatalog.Android/ControlCatalog.Android.csproj
  3. 3
      samples/ControlCatalog.NetCore/NativeControls/Gtk/EmbedSample.Gtk.cs
  4. 9
      samples/ControlCatalog.NetCore/NativeControls/Gtk/GtkHelper.cs
  5. 1
      samples/ControlCatalog/App.xaml
  6. 34
      src/Avalonia.Base/Rendering/Composition/CompositingRenderer.cs
  7. 13
      src/Avalonia.Base/Rendering/Composition/CompositionDrawListVisual.cs
  8. 44
      src/Avalonia.Base/Rendering/Composition/CompositionTarget.cs
  9. 6
      src/Avalonia.Base/Rendering/Composition/ICompositionTargetDebugEvents.cs
  10. 3
      src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs
  11. 35
      src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual.cs
  12. 6
      src/Avalonia.Base/Rendering/Composition/Transport/BatchStream.cs
  13. 10
      src/Avalonia.Base/Rendering/Composition/Transport/BatchStreamArrayPool.cs
  14. 2
      src/Avalonia.Base/Rendering/Composition/Visual.cs
  15. 8
      src/Avalonia.Controls.DataGrid/Collections/DataGridSortDescription.cs
  16. 2
      src/Avalonia.Controls.DataGrid/DataGridColumn.cs
  17. 5
      src/Avalonia.Controls/Flyouts/MenuFlyoutPresenter.cs
  18. 4
      src/Avalonia.Controls/MenuBase.cs
  19. 8
      src/Avalonia.Controls/TrayIcon.cs
  20. 3
      src/Avalonia.FreeDesktop/DBusHelper.cs
  21. 33
      src/Avalonia.FreeDesktop/DBusSystemDialog.cs
  22. 1
      src/Avalonia.Themes.Default/Controls/NativeMenuBar.xaml
  23. 1
      src/Avalonia.Themes.Fluent/Controls/NativeMenuBar.xaml
  24. 2
      src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml
  25. 15
      src/Avalonia.X11/Interop/GtkInteropHelper.cs
  26. 64
      src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs
  27. 25
      src/Avalonia.X11/NativeDialogs/Gtk.cs
  28. 30
      src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs
  29. 8
      src/Avalonia.X11/X11Platform.cs
  30. 3
      src/Avalonia.X11/X11Structs.cs
  31. 9
      src/Avalonia.X11/X11Window.cs
  32. 20
      src/Windows/Avalonia.Win32/IconImpl.cs
  33. 4
      src/Windows/Avalonia.Win32/TrayIconImpl.cs
  34. 29
      src/Windows/Avalonia.Win32/Win32NativeToManagedMenuExporter.cs
  35. 8
      src/Windows/Avalonia.Win32/Win32Platform.cs
  36. 417
      tests/Avalonia.Base.UnitTests/Rendering/CompositorHitTestingTests.cs
  37. 109
      tests/Avalonia.Base.UnitTests/Rendering/CompositorInvalidationTests.cs
  38. 208
      tests/Avalonia.Base.UnitTests/Rendering/CompositorTestsBase.cs
  39. 27
      tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs

1
Avalonia.sln

@ -559,6 +559,7 @@ Global
{2B390431-288C-435C-BB6B-A374033BD8D1} = {4ED8B739-6F4E-4CD4-B993-545E6B5CE637} {2B390431-288C-435C-BB6B-A374033BD8D1} = {4ED8B739-6F4E-4CD4-B993-545E6B5CE637}
{EABE2161-989B-42BF-BD8D-1E34B20C21F1} = {C5A00AC3-B34C-4564-9BDD-2DA473EF4D8B} {EABE2161-989B-42BF-BD8D-1E34B20C21F1} = {C5A00AC3-B34C-4564-9BDD-2DA473EF4D8B}
{1BBFAD42-B99E-47E0-B00A-A4BC6B6BB4BB} = {4ED8B739-6F4E-4CD4-B993-545E6B5CE637} {1BBFAD42-B99E-47E0-B00A-A4BC6B6BB4BB} = {4ED8B739-6F4E-4CD4-B993-545E6B5CE637}
{4D36CEC8-53F2-40A5-9A37-79AAE356E2DA} = {86C53C40-57AA-45B8-AD42-FAE0EFDF0F2B}
EndGlobalSection EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {87366D66-1391-4D90-8999-95A620AD786A} SolutionGuid = {87366D66-1391-4D90-8999-95A620AD786A}

4
samples/ControlCatalog.Android/ControlCatalog.Android.csproj

@ -21,12 +21,12 @@
<RunAOTCompilation>True</RunAOTCompilation> <RunAOTCompilation>True</RunAOTCompilation>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(RunAOTCompilation)'=='True'"> <!-- PropertyGroup Condition="'$(RunAOTCompilation)'=='True'">
<EnableLLVM>True</EnableLLVM> <EnableLLVM>True</EnableLLVM>
<AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments> <AndroidAotAdditionalArguments>no-write-symbols,nodebug</AndroidAotAdditionalArguments>
<AndroidAotMode>Hybrid</AndroidAotMode> <AndroidAotMode>Hybrid</AndroidAotMode>
<AndroidGenerateJniMarshalMethods>True</AndroidGenerateJniMarshalMethods> <AndroidGenerateJniMarshalMethods>True</AndroidGenerateJniMarshalMethods>
</PropertyGroup> </PropertyGroup -->
<PropertyGroup Condition="'$(AndroidEnableProfiler)'=='True'"> <PropertyGroup Condition="'$(AndroidEnableProfiler)'=='True'">
<IsEmulator Condition="'$(IsEmulator)' == ''">True</IsEmulator> <IsEmulator Condition="'$(IsEmulator)' == ''">True</IsEmulator>

3
samples/ControlCatalog.NetCore/NativeControls/Gtk/EmbedSample.Gtk.cs

@ -22,8 +22,7 @@ public class EmbedSampleGtk : INativeDemoControl
var control = createDefault(); var control = createDefault();
var nodes = Path.GetFullPath(Path.Combine(typeof(EmbedSample).Assembly.GetModules()[0].FullyQualifiedName, var nodes = Path.GetFullPath(Path.Combine(typeof(EmbedSample).Assembly.GetModules()[0].FullyQualifiedName,
"..", "..", "NativeControls", "Gtk", "nodes.mp4"));
"nodes.mp4"));
_mplayer = Process.Start(new ProcessStartInfo("mplayer", _mplayer = Process.Start(new ProcessStartInfo("mplayer",
$"-vo x11 -zoom -loop 0 -wid {control.Handle.ToInt64()} \"{nodes}\"") $"-vo x11 -zoom -loop 0 -wid {control.Handle.ToInt64()} \"{nodes}\"")
{ {

9
samples/ControlCatalog.NetCore/NativeControls/Gtk/GtkHelper.cs

@ -2,6 +2,7 @@ using System;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Controls.Platform; using Avalonia.Controls.Platform;
using Avalonia.Platform.Interop; using Avalonia.Platform.Interop;
using Avalonia.X11.Interop;
using Avalonia.X11.NativeDialogs; using Avalonia.X11.NativeDialogs;
using static Avalonia.X11.NativeDialogs.Gtk; using static Avalonia.X11.NativeDialogs.Gtk;
using static Avalonia.X11.NativeDialogs.Glib; using static Avalonia.X11.NativeDialogs.Glib;
@ -10,8 +11,6 @@ namespace ControlCatalog.NetCore;
internal class GtkHelper internal class GtkHelper
{ {
private static Task<bool> s_gtkTask;
class FileChooser : INativeControlHostDestroyableControlHandle class FileChooser : INativeControlHostDestroyableControlHandle
{ {
private readonly IntPtr _widget; private readonly IntPtr _widget;
@ -38,11 +37,7 @@ internal class GtkHelper
public static INativeControlHostDestroyableControlHandle CreateGtkFileChooser(IntPtr parentXid) public static INativeControlHostDestroyableControlHandle CreateGtkFileChooser(IntPtr parentXid)
{ {
if (s_gtkTask == null) return GtkInteropHelper.RunOnGlibThread(() =>
s_gtkTask = StartGtk();
if (!s_gtkTask.Result)
return null;
return RunOnGlibThread(() =>
{ {
using (var title = new Utf8Buffer("Embedded")) using (var title = new Utf8Buffer("Embedded"))
{ {

1
samples/ControlCatalog/App.xaml

@ -43,6 +43,7 @@
<NativeMenuItemSeparator /> <NativeMenuItemSeparator />
<NativeMenuItem Header="Option 3" ToggleType="CheckBox" IsChecked="True" Command="{Binding ToggleCommand}" /> <NativeMenuItem Header="Option 3" ToggleType="CheckBox" IsChecked="True" Command="{Binding ToggleCommand}" />
<NativeMenuItem Icon="/Assets/test_icon.ico" Header="Restore Defaults" Command="{Binding ToggleCommand}" /> <NativeMenuItem Icon="/Assets/test_icon.ico" Header="Restore Defaults" Command="{Binding ToggleCommand}" />
<NativeMenuItem Header="Disabled option" IsEnabled="False" />
</NativeMenu> </NativeMenu>
</NativeMenuItem> </NativeMenuItem>
<NativeMenuItem Header="Exit" Command="{Binding ExitCommand}" /> <NativeMenuItem Header="Exit" Command="{Binding ExitCommand}" />

34
src/Avalonia.Base/Rendering/Composition/CompositingRenderer.cs

@ -24,11 +24,12 @@ public class CompositingRenderer : IRendererWithCompositor
DrawingContext _recordingContext; DrawingContext _recordingContext;
private HashSet<Visual> _dirty = new(); private HashSet<Visual> _dirty = new();
private HashSet<Visual> _recalculateChildren = new(); private HashSet<Visual> _recalculateChildren = new();
private readonly CompositionTarget _target;
private bool _queuedUpdate; private bool _queuedUpdate;
private Action _update; private Action _update;
private Action _invalidateScene; private Action _invalidateScene;
internal CompositionTarget CompositionTarget;
/// <summary> /// <summary>
/// Asks the renderer to only draw frames on the render thread. Makes Paint to wait until frame is rendered. /// Asks the renderer to only draw frames on the render thread. Makes Paint to wait until frame is rendered.
/// </summary> /// </summary>
@ -40,8 +41,8 @@ public class CompositingRenderer : IRendererWithCompositor
_root = root; _root = root;
_compositor = compositor; _compositor = compositor;
_recordingContext = new DrawingContext(_recorder); _recordingContext = new DrawingContext(_recorder);
_target = compositor.CreateCompositionTarget(root.CreateRenderTarget); CompositionTarget = compositor.CreateCompositionTarget(root.CreateRenderTarget);
_target.Root = ((Visual)root!.VisualRoot!).AttachToCompositor(compositor); CompositionTarget.Root = ((Visual)root!.VisualRoot!).AttachToCompositor(compositor);
_update = Update; _update = Update;
_invalidateScene = InvalidateScene; _invalidateScene = InvalidateScene;
} }
@ -49,15 +50,15 @@ public class CompositingRenderer : IRendererWithCompositor
/// <inheritdoc/> /// <inheritdoc/>
public bool DrawFps public bool DrawFps
{ {
get => _target.DrawFps; get => CompositionTarget.DrawFps;
set => _target.DrawFps = value; set => CompositionTarget.DrawFps = value;
} }
/// <inheritdoc/> /// <inheritdoc/>
public bool DrawDirtyRects public bool DrawDirtyRects
{ {
get => _target.DrawDirtyRects; get => CompositionTarget.DrawDirtyRects;
set => _target.DrawDirtyRects = value; set => CompositionTarget.DrawDirtyRects = value;
} }
/// <inheritdoc/> /// <inheritdoc/>
@ -81,12 +82,11 @@ public class CompositingRenderer : IRendererWithCompositor
/// <inheritdoc/> /// <inheritdoc/>
public IEnumerable<IVisual> HitTest(Point p, IVisual root, Func<IVisual, bool>? filter) public IEnumerable<IVisual> HitTest(Point p, IVisual root, Func<IVisual, bool>? filter)
{ {
var res = _target.TryHitTest(p, filter); var res = CompositionTarget.TryHitTest(p, filter);
if(res == null) if(res == null)
yield break; yield break;
for (var index = res.Count - 1; index >= 0; index--) foreach(var v in res)
{ {
var v = res[index];
if (v is CompositionDrawListVisual dv) if (v is CompositionDrawListVisual dv)
{ {
if (filter == null || filter(dv.Visual)) if (filter == null || filter(dv.Visual))
@ -234,8 +234,8 @@ public class CompositingRenderer : IRendererWithCompositor
SyncChildren(v); SyncChildren(v);
_dirty.Clear(); _dirty.Clear();
_recalculateChildren.Clear(); _recalculateChildren.Clear();
_target.Size = _root.ClientSize; CompositionTarget.Size = _root.ClientSize;
_target.Scaling = _root.RenderScaling; CompositionTarget.Scaling = _root.RenderScaling;
Compositor.InvokeOnNextCommit(_invalidateScene); Compositor.InvokeOnNextCommit(_invalidateScene);
} }
@ -246,24 +246,24 @@ public class CompositingRenderer : IRendererWithCompositor
public void Paint(Rect rect) public void Paint(Rect rect)
{ {
Update(); Update();
_target.RequestRedraw(); CompositionTarget.RequestRedraw();
if(RenderOnlyOnRenderThread && Compositor.Loop.RunsInBackground) if(RenderOnlyOnRenderThread && Compositor.Loop.RunsInBackground)
Compositor.RequestCommitAsync().Wait(); Compositor.RequestCommitAsync().Wait();
else else
_target.ImmediateUIThreadRender(); CompositionTarget.ImmediateUIThreadRender();
} }
public void Start() => _target.IsEnabled = true; public void Start() => CompositionTarget.IsEnabled = true;
public void Stop() public void Stop()
{ {
_target.IsEnabled = false; CompositionTarget.IsEnabled = false;
} }
public void Dispose() public void Dispose()
{ {
Stop(); Stop();
_target.Dispose(); CompositionTarget.Dispose();
// Wait for the composition batch to be applied and rendered to guarantee that // Wait for the composition batch to be applied and rendered to guarantee that
// render target is not used anymore and can be safely disposed // render target is not used anymore and can be safely disposed

13
src/Avalonia.Base/Rendering/Composition/CompositionDrawListVisual.cs

@ -54,13 +54,20 @@ internal class CompositionDrawListVisual : CompositionContainerVisual
internal override bool HitTest(Point pt, Func<IVisual, bool>? filter) internal override bool HitTest(Point pt, Func<IVisual, bool>? filter)
{ {
if (DrawList == null) var custom = Visual as ICustomHitTest;
if (DrawList == null && custom == null)
return false; return false;
if (filter != null && !filter(Visual)) if (filter != null && !filter(Visual))
return false; return false;
if (Visual is ICustomHitTest custom) if (custom != null)
{
// Simulate the old behavior
// TODO: Change behavior once legacy renderers are removed
pt += new Point(Offset.X, Offset.Y);
return custom.HitTest(pt); return custom.HitTest(pt);
foreach (var op in DrawList) }
foreach (var op in DrawList!)
if (op.Item.HitTest(pt)) if (op.Item.HitTest(pt))
return true; return true;
return false; return false;

44
src/Avalonia.Base/Rendering/Composition/CompositionTarget.cs

@ -62,7 +62,7 @@ namespace Avalonia.Rendering.Composition
bool TryGetInvertedTransform(CompositionVisual visual, out Matrix matrix) bool TryGetInvertedTransform(CompositionVisual visual, out Matrix matrix)
{ {
var m = visual.TryGetServerTransform(); var m = visual.TryGetServerGlobalTransform();
if (m == null) if (m == null)
{ {
matrix = default; matrix = default;
@ -73,52 +73,44 @@ namespace Avalonia.Rendering.Composition
return m33.TryInvert(out matrix); return m33.TryInvert(out matrix);
} }
bool TryTransformTo(CompositionVisual visual, ref Point v) bool TryTransformTo(CompositionVisual visual, Point globalPoint, out Point v)
{ {
v = default;
if (TryGetInvertedTransform(visual, out var m)) if (TryGetInvertedTransform(visual, out var m))
{ {
v = v * m; v = globalPoint * m;
return true; return true;
} }
return false; return false;
} }
bool HitTestCore(CompositionVisual visual, Point point, PooledList<CompositionVisual> result, void HitTestCore(CompositionVisual visual, Point globalPoint, PooledList<CompositionVisual> result,
Func<IVisual, bool>? filter) Func<IVisual, bool>? filter)
{ {
//TODO: Check readback too
if (visual.Visible == false) if (visual.Visible == false)
return false; return;
if (!TryTransformTo(visual, ref point)) if (!TryTransformTo(visual, globalPoint, out var point))
return false; return;
if (visual.ClipToBounds if (visual.ClipToBounds
&& (point.X < 0 || point.Y < 0 || point.X > visual.Size.X || point.Y > visual.Size.Y)) && (point.X < 0 || point.Y < 0 || point.X > visual.Size.X || point.Y > visual.Size.Y))
return false; return;
if (visual.Clip?.FillContains(point) == false)
return false;
bool success = false; if (visual.Clip?.FillContains(point) == false)
// Hit-test the current node return;
if (visual.HitTest(point, filter))
{ // Inspect children
result.Add(visual);
success = true;
}
// Inspect children too
if (visual is CompositionContainerVisual cv) if (visual is CompositionContainerVisual cv)
for (var c = cv.Children.Count - 1; c >= 0; c--) for (var c = cv.Children.Count - 1; c >= 0; c--)
{ {
var ch = cv.Children[c]; var ch = cv.Children[c];
var hit = HitTestCore(ch, point, result, filter); HitTestCore(ch, globalPoint, result, filter);
if (hit)
return true;
} }
return success; // Hit-test the current node
if (visual.HitTest(point, filter))
result.Add(visual);
} }
/// <summary> /// <summary>

6
src/Avalonia.Base/Rendering/Composition/ICompositionTargetDebugEvents.cs

@ -0,0 +1,6 @@
namespace Avalonia.Rendering.Composition;
internal interface ICompositionTargetDebugEvents
{
void RectInvalidated(Rect rc);
}

3
src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionTarget.cs

@ -33,7 +33,7 @@ namespace Avalonia.Rendering.Composition.Server
private HashSet<ServerCompositionVisual> _attachedVisuals = new(); private HashSet<ServerCompositionVisual> _attachedVisuals = new();
private Queue<ServerCompositionVisual> _adornerUpdateQueue = new(); private Queue<ServerCompositionVisual> _adornerUpdateQueue = new();
public ICompositionTargetDebugEvents? DebugEvents { get; set; }
public ReadbackIndices Readback { get; } = new(); public ReadbackIndices Readback { get; } = new();
public int RenderedVisuals { get; set; } public int RenderedVisuals { get; set; }
@ -173,6 +173,7 @@ namespace Avalonia.Rendering.Composition.Server
if(rect.IsEmpty) if(rect.IsEmpty)
return; return;
var snapped = SnapToDevicePixels(rect, Scaling); var snapped = SnapToDevicePixels(rect, Scaling);
DebugEvents?.RectInvalidated(rect);
_dirtyRect = _dirtyRect.Union(snapped); _dirtyRect = _dirtyRect.Union(snapped);
_redrawRequested = true; _redrawRequested = true;
} }

35
src/Avalonia.Base/Rendering/Composition/Server/ServerCompositionVisual.cs

@ -119,14 +119,15 @@ namespace Avalonia.Rendering.Composition.Server
var oldTransformedContentBounds = TransformedOwnContentBounds; var oldTransformedContentBounds = TransformedOwnContentBounds;
var oldCombinedTransformedClipBounds = _combinedTransformedClipBounds; var oldCombinedTransformedClipBounds = _combinedTransformedClipBounds;
var dirtyOldBounds = false;
if (_parent?.IsDirtyComposition == true) if (_parent?.IsDirtyComposition == true)
{ {
IsDirtyComposition = true; IsDirtyComposition = true;
_isDirtyForUpdate = true; _isDirtyForUpdate = true;
dirtyOldBounds = true;
} }
var invalidateOldBounds = _isDirtyForUpdate;
var invalidateNewBounds = _isDirtyForUpdate;
GlobalTransformMatrix = newTransform; GlobalTransformMatrix = newTransform;
@ -157,30 +158,20 @@ namespace Avalonia.Rendering.Composition.Server
EffectiveOpacity = Opacity * (Parent?.EffectiveOpacity ?? 1); EffectiveOpacity = Opacity * (Parent?.EffectiveOpacity ?? 1);
IsVisibleInFrame = Visible && EffectiveOpacity > 0.04 && !_isBackface && IsVisibleInFrame = _parent?.IsVisibleInFrame != false && Visible && EffectiveOpacity > 0.04 && !_isBackface &&
!_combinedTransformedClipBounds.IsEmpty; !_combinedTransformedClipBounds.IsEmpty;
if (wasVisible != IsVisibleInFrame)
_isDirtyForUpdate = true;
// Invalidate previous rect and queue new rect based on visibility
if (positionChanged)
{
if (wasVisible)
dirtyOldBounds = true;
if (IsVisibleInFrame) if (wasVisible != IsVisibleInFrame || positionChanged)
_isDirtyForUpdate = true; {
invalidateOldBounds |= wasVisible;
invalidateNewBounds |= IsVisibleInFrame;
} }
// Invalidate new bounds // Invalidate new bounds
if (IsVisibleInFrame && _isDirtyForUpdate) if (invalidateNewBounds)
{
dirtyOldBounds = true;
AddDirtyRect(TransformedOwnContentBounds.Intersect(_combinedTransformedClipBounds)); AddDirtyRect(TransformedOwnContentBounds.Intersect(_combinedTransformedClipBounds));
}
if (dirtyOldBounds && wasVisible) if (invalidateOldBounds)
AddDirtyRect(oldTransformedContentBounds.Intersect(oldCombinedTransformedClipBounds)); AddDirtyRect(oldTransformedContentBounds.Intersect(oldCombinedTransformedClipBounds));
@ -190,7 +181,7 @@ namespace Avalonia.Rendering.Composition.Server
var i = Root!.Readback; var i = Root!.Readback;
ref var readback = ref GetReadback(i.WriteIndex); ref var readback = ref GetReadback(i.WriteIndex);
readback.Revision = root.Revision; readback.Revision = root.Revision;
readback.Matrix = CombinedTransformMatrix; readback.Matrix = GlobalTransformMatrix;
readback.TargetId = Root.Id; readback.TargetId = Root.Id;
readback.Visible = IsVisibleInFrame; readback.Visible = IsVisibleInFrame;
} }

6
src/Avalonia.Base/Rendering/Composition/Transport/BatchStream.cs

@ -72,7 +72,7 @@ internal class BatchStreamWriter : IDisposable
var size = Unsafe.SizeOf<T>(); var size = Unsafe.SizeOf<T>();
if (_currentDataSegment.Data == IntPtr.Zero || _currentDataSegment.ElementCount + size > _memoryPool.BufferSize) if (_currentDataSegment.Data == IntPtr.Zero || _currentDataSegment.ElementCount + size > _memoryPool.BufferSize)
NextDataSegment(); NextDataSegment();
*(T*)((byte*)_currentDataSegment.Data + _currentDataSegment.ElementCount) = item; Unsafe.WriteUnaligned<T>((byte*)_currentDataSegment.Data + _currentDataSegment.ElementCount, item);
_currentDataSegment.ElementCount += size; _currentDataSegment.ElementCount += size;
} }
@ -123,7 +123,7 @@ internal class BatchStreamReader : IDisposable
if (_memoryOffset + size > _currentDataSegment.ElementCount) if (_memoryOffset + size > _currentDataSegment.ElementCount)
throw new InvalidOperationException("Attempted to read more memory then left in the current segment"); throw new InvalidOperationException("Attempted to read more memory then left in the current segment");
var rv = *(T*)((byte*)_currentDataSegment.Data + _memoryOffset); var rv = Unsafe.ReadUnaligned<T>((byte*)_currentDataSegment.Data + _memoryOffset);
_memoryOffset += size; _memoryOffset += size;
if (_memoryOffset == _currentDataSegment.ElementCount) if (_memoryOffset == _currentDataSegment.ElementCount)
{ {
@ -181,4 +181,4 @@ internal class BatchStreamReader : IDisposable
while (_input.Objects.Count > 0) while (_input.Objects.Count > 0)
_objectPool.Return(_input.Objects.Dequeue().Data); _objectPool.Return(_input.Objects.Dequeue().Data);
} }
} }

10
src/Avalonia.Base/Rendering/Composition/Transport/BatchStreamArrayPool.cs

@ -1,8 +1,8 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using Avalonia.Platform;
using Avalonia.Threading; using Avalonia.Threading;
namespace Avalonia.Rendering.Composition.Transport; namespace Avalonia.Rendering.Composition.Transport;
@ -17,6 +17,7 @@ internal abstract class BatchStreamPoolBase<T> : IDisposable
int _usage; int _usage;
readonly int[] _usageStatistics = new int[10]; readonly int[] _usageStatistics = new int[10];
int _usageStatisticsSlot; int _usageStatisticsSlot;
bool _reclaimImmediately;
public int CurrentUsage => _usage; public int CurrentUsage => _usage;
public int CurrentPool => _pool.Count; public int CurrentPool => _pool.Count;
@ -27,7 +28,10 @@ internal abstract class BatchStreamPoolBase<T> : IDisposable
GC.SuppressFinalize(needsFinalize); GC.SuppressFinalize(needsFinalize);
var updateRef = new WeakReference<BatchStreamPoolBase<T>>(this); var updateRef = new WeakReference<BatchStreamPoolBase<T>>(this);
StartUpdateTimer(startTimer, updateRef); if (AvaloniaLocator.Current.GetService<IPlatformThreadingInterface>() == null)
_reclaimImmediately = true;
else
StartUpdateTimer(startTimer, updateRef);
} }
static void StartUpdateTimer(Action<Func<bool>>? startTimer, WeakReference<BatchStreamPoolBase<T>> updateRef) static void StartUpdateTimer(Action<Func<bool>>? startTimer, WeakReference<BatchStreamPoolBase<T>> updateRef)
@ -90,7 +94,7 @@ internal abstract class BatchStreamPoolBase<T> : IDisposable
lock (_pool) lock (_pool)
{ {
_usage--; _usage--;
if (!_disposed) if (!_disposed && !_reclaimImmediately)
{ {
_pool.Push(item); _pool.Push(item);
return; return;

2
src/Avalonia.Base/Rendering/Composition/Visual.cs

@ -31,7 +31,7 @@ namespace Avalonia.Rendering.Composition
} }
} }
internal Matrix4x4? TryGetServerTransform() internal Matrix4x4? TryGetServerGlobalTransform()
{ {
if (Root == null) if (Root == null)
return null; return null;

8
src/Avalonia.Controls.DataGrid/Collections/DataGridSortDescription.cs

@ -268,11 +268,11 @@ namespace Avalonia.Collections
public static DataGridSortDescription FromComparer(IComparer comparer, ListSortDirection direction = ListSortDirection.Ascending) public static DataGridSortDescription FromComparer(IComparer comparer, ListSortDirection direction = ListSortDirection.Ascending)
{ {
return new DataGridComparerSortDesctiption(comparer, direction); return new DataGridComparerSortDescription(comparer, direction);
} }
} }
public class DataGridComparerSortDesctiption : DataGridSortDescription public class DataGridComparerSortDescription : DataGridSortDescription
{ {
private readonly IComparer _innerComparer; private readonly IComparer _innerComparer;
private readonly ListSortDirection _direction; private readonly ListSortDirection _direction;
@ -281,7 +281,7 @@ namespace Avalonia.Collections
public IComparer SourceComparer => _innerComparer; public IComparer SourceComparer => _innerComparer;
public override IComparer<object> Comparer => _comparer; public override IComparer<object> Comparer => _comparer;
public override ListSortDirection Direction => _direction; public override ListSortDirection Direction => _direction;
public DataGridComparerSortDesctiption(IComparer comparer, ListSortDirection direction) public DataGridComparerSortDescription(IComparer comparer, ListSortDirection direction)
{ {
_innerComparer = comparer; _innerComparer = comparer;
_direction = direction; _direction = direction;
@ -300,7 +300,7 @@ namespace Avalonia.Collections
public override DataGridSortDescription SwitchSortDirection() public override DataGridSortDescription SwitchSortDirection()
{ {
var newDirection = _direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending; var newDirection = _direction == ListSortDirection.Ascending ? ListSortDirection.Descending : ListSortDirection.Ascending;
return new DataGridComparerSortDesctiption(_innerComparer, newDirection); return new DataGridComparerSortDescription(_innerComparer, newDirection);
} }
} }

2
src/Avalonia.Controls.DataGrid/DataGridColumn.cs

@ -1091,7 +1091,7 @@ namespace Avalonia.Controls
{ {
return return
OwningGrid.DataConnection.SortDescriptions OwningGrid.DataConnection.SortDescriptions
.OfType<DataGridComparerSortDesctiption>() .OfType<DataGridComparerSortDescription>()
.FirstOrDefault(s => s.SourceComparer == CustomSortComparer); .FirstOrDefault(s => s.SourceComparer == CustomSortComparer);
} }

5
src/Avalonia.Controls/Flyouts/MenuFlyoutPresenter.cs

@ -11,9 +11,14 @@ namespace Avalonia.Controls
public MenuFlyoutPresenter() public MenuFlyoutPresenter()
:base(new DefaultMenuInteractionHandler(true)) :base(new DefaultMenuInteractionHandler(true))
{ {
}
public MenuFlyoutPresenter(IMenuInteractionHandler menuInteractionHandler)
: base(menuInteractionHandler)
{
} }
public override void Close() public override void Close()
{ {
// DefaultMenuInteractionHandler calls this // DefaultMenuInteractionHandler calls this

4
src/Avalonia.Controls/MenuBase.cs

@ -40,7 +40,7 @@ namespace Avalonia.Controls
/// <summary> /// <summary>
/// Initializes a new instance of the <see cref="MenuBase"/> class. /// Initializes a new instance of the <see cref="MenuBase"/> class.
/// </summary> /// </summary>
public MenuBase() protected MenuBase()
{ {
InteractionHandler = new DefaultMenuInteractionHandler(false); InteractionHandler = new DefaultMenuInteractionHandler(false);
} }
@ -49,7 +49,7 @@ namespace Avalonia.Controls
/// Initializes a new instance of the <see cref="MenuBase"/> class. /// Initializes a new instance of the <see cref="MenuBase"/> class.
/// </summary> /// </summary>
/// <param name="interactionHandler">The menu interaction handler.</param> /// <param name="interactionHandler">The menu interaction handler.</param>
public MenuBase(IMenuInteractionHandler interactionHandler) protected MenuBase(IMenuInteractionHandler interactionHandler)
{ {
InteractionHandler = interactionHandler ?? throw new ArgumentNullException(nameof(interactionHandler)); InteractionHandler = interactionHandler ?? throw new ArgumentNullException(nameof(interactionHandler));
} }

8
src/Avalonia.Controls/TrayIcon.cs

@ -61,6 +61,10 @@ namespace Avalonia.Controls
args.NewValue.Value.CollectionChanged += Icons_CollectionChanged; args.NewValue.Value.CollectionChanged += Icons_CollectionChanged;
} }
} }
else
{
throw new InvalidOperationException("TrayIcon.Icons must be set on the Application.");
}
}); });
var app = Application.Current ?? throw new InvalidOperationException("Application not yet initialized."); var app = Application.Current ?? throw new InvalidOperationException("Application not yet initialized.");
@ -123,9 +127,9 @@ namespace Avalonia.Controls
public static readonly StyledProperty<bool> IsVisibleProperty = public static readonly StyledProperty<bool> IsVisibleProperty =
Visual.IsVisibleProperty.AddOwner<TrayIcon>(); Visual.IsVisibleProperty.AddOwner<TrayIcon>();
public static void SetIcons(AvaloniaObject o, TrayIcons trayIcons) => o.SetValue(IconsProperty, trayIcons); public static void SetIcons(Application o, TrayIcons trayIcons) => o.SetValue(IconsProperty, trayIcons);
public static TrayIcons GetIcons(AvaloniaObject o) => o.GetValue(IconsProperty); public static TrayIcons GetIcons(Application o) => o.GetValue(IconsProperty);
/// <summary> /// <summary>
/// Gets or sets the <see cref="Command"/> property of a TrayIcon. /// Gets or sets the <see cref="Command"/> property of a TrayIcon.

3
src/Avalonia.FreeDesktop/DBusHelper.cs

@ -24,8 +24,7 @@ namespace Avalonia.FreeDesktop
if (_ctx is not null) if (_ctx is not null)
_ctx?.Post(d, state); _ctx?.Post(d, state);
else else
lock (_lock) d(state);
d(state);
} }
} }

33
src/Avalonia.FreeDesktop/DBusSystemDialog.cs

@ -15,27 +15,26 @@ namespace Avalonia.FreeDesktop
{ {
internal class DBusSystemDialog : BclStorageProvider internal class DBusSystemDialog : BclStorageProvider
{ {
private static readonly Lazy<IFileChooser?> s_fileChooser = new(() => private static readonly Lazy<IFileChooser?> s_fileChooser = new(() => DBusHelper.Connection?
.CreateProxy<IFileChooser>("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"));
internal static async Task<IStorageProvider?> TryCreate(IPlatformHandle handle)
{ {
var fileChooser = DBusHelper.Connection?.CreateProxy<IFileChooser>("org.freedesktop.portal.Desktop", "/org/freedesktop/portal/desktop"); if (handle.HandleDescriptor == "XID" && s_fileChooser.Value is { } fileChooser)
if (fileChooser is null)
return null;
try
{ {
_ = fileChooser.GetVersionAsync(); try
return fileChooser; {
} await fileChooser.GetVersionAsync();
catch (Exception e) return new DBusSystemDialog(fileChooser, handle);
{ }
Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(null, $"Unable to connect to org.freedesktop.portal.Desktop: {e.Message}"); catch (Exception e)
return null; {
Logger.TryGet(LogEventLevel.Error, LogArea.X11Platform)?.Log(null, $"Unable to connect to org.freedesktop.portal.Desktop: {e.Message}");
return null;
}
} }
});
internal static DBusSystemDialog? TryCreate(IPlatformHandle handle) return null;
{
return handle.HandleDescriptor == "XID" && s_fileChooser.Value is { } fileChooser
? new DBusSystemDialog(fileChooser, handle) : null;
} }
private readonly IFileChooser _fileChooser; private readonly IFileChooser _fileChooser;

1
src/Avalonia.Themes.Default/Controls/NativeMenuBar.xaml

@ -14,6 +14,7 @@
<Menu.Styles> <Menu.Styles>
<Style Selector="MenuItem"> <Style Selector="MenuItem">
<Setter Property="Header" Value="{Binding Header}"/> <Setter Property="Header" Value="{Binding Header}"/>
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
<Setter Property="InputGesture" Value="{Binding Gesture}"/> <Setter Property="InputGesture" Value="{Binding Gesture}"/>
<Setter Property="Items" Value="{Binding Menu.Items}"/> <Setter Property="Items" Value="{Binding Menu.Items}"/>
<Setter Property="Command" Value="{Binding Command}"/> <Setter Property="Command" Value="{Binding Command}"/>

1
src/Avalonia.Themes.Fluent/Controls/NativeMenuBar.xaml

@ -15,6 +15,7 @@
<Menu.Styles> <Menu.Styles>
<Style x:CompileBindings="False" Selector="MenuItem"> <Style x:CompileBindings="False" Selector="MenuItem">
<Setter Property="Header" Value="{Binding Header}"/> <Setter Property="Header" Value="{Binding Header}"/>
<Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
<Setter Property="InputGesture" Value="{Binding Gesture}"/> <Setter Property="InputGesture" Value="{Binding Gesture}"/>
<Setter Property="Items" Value="{Binding Menu.Items}"/> <Setter Property="Items" Value="{Binding Menu.Items}"/>
<Setter Property="Command" Value="{Binding Command}"/> <Setter Property="Command" Value="{Binding Command}"/>

2
src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml

@ -15,7 +15,7 @@
<Thickness x:Key="DatePickerHostPadding">0,1,0,2</Thickness> <Thickness x:Key="DatePickerHostPadding">0,1,0,2</Thickness>
<Thickness x:Key="DatePickerHostMonthPadding">9,0,0,1</Thickness> <Thickness x:Key="DatePickerHostMonthPadding">9,0,0,1</Thickness>
<Thickness x:Key="ComboBoxEditableTextPadding">10,0,30,0</Thickness> <Thickness x:Key="ComboBoxEditableTextPadding">10,0,30,0</Thickness>
<Thickness x:Key="ComboBoxMinHeight">24</Thickness> <x:Double x:Key="ComboBoxMinHeight">24</x:Double>
<Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness> <Thickness x:Key="ComboBoxPadding">12,1,0,3</Thickness>
<x:Double x:Key="NavigationViewItemOnLeftMinHeight">32</x:Double> <x:Double x:Key="NavigationViewItemOnLeftMinHeight">32</x:Double>
</Style.Resources> </Style.Resources>

15
src/Avalonia.X11/Interop/GtkInteropHelper.cs

@ -0,0 +1,15 @@
using System;
using System.ComponentModel;
using System.Threading.Tasks;
namespace Avalonia.X11.Interop;
public class GtkInteropHelper
{
public static async Task<T> RunOnGlibThread<T>(Func<T> cb)
{
if (!await NativeDialogs.Gtk.StartGtk().ConfigureAwait(false))
throw new Win32Exception("Unable to initialize GTK");
return await NativeDialogs.Glib.RunOnGlibThread(cb).ConfigureAwait(false);
}
}

64
src/Avalonia.X11/NativeDialogs/CompositeStorageProvider.cs

@ -0,0 +1,64 @@
#nullable enable
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Avalonia.Platform.Storage;
namespace Avalonia.X11.NativeDialogs;
internal class CompositeStorageProvider : IStorageProvider
{
private readonly IEnumerable<Func<Task<IStorageProvider?>>> _factories;
public CompositeStorageProvider(IEnumerable<Func<Task<IStorageProvider?>>> factories)
{
_factories = factories;
}
public bool CanOpen => true;
public bool CanSave => true;
public bool CanPickFolder => true;
private async Task<IStorageProvider> EnsureStorageProvider()
{
foreach (var factory in _factories)
{
var provider = await factory();
if (provider is not null)
{
return provider;
}
}
throw new InvalidOperationException("Neither DBus nor GTK are available on the system");
}
public async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options)
{
var provider = await EnsureStorageProvider().ConfigureAwait(false);
return await provider.OpenFilePickerAsync(options).ConfigureAwait(false);
}
public async Task<IStorageFile?> SaveFilePickerAsync(FilePickerSaveOptions options)
{
var provider = await EnsureStorageProvider().ConfigureAwait(false);
return await provider.SaveFilePickerAsync(options).ConfigureAwait(false);
}
public async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
{
var provider = await EnsureStorageProvider().ConfigureAwait(false);
return await provider.OpenFolderPickerAsync(options).ConfigureAwait(false);
}
public async Task<IStorageBookmarkFile?> OpenFileBookmarkAsync(string bookmark)
{
var provider = await EnsureStorageProvider().ConfigureAwait(false);
return await provider.OpenFileBookmarkAsync(bookmark).ConfigureAwait(false);
}
public async Task<IStorageBookmarkFolder?> OpenFolderBookmarkAsync(string bookmark)
{
var provider = await EnsureStorageProvider().ConfigureAwait(false);
return await provider.OpenFolderBookmarkAsync(bookmark).ConfigureAwait(false);
}
}

25
src/Avalonia.X11/NativeDialogs/Gtk.cs

@ -3,6 +3,8 @@ using System.Runtime.InteropServices;
using System.Threading; using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Avalonia.Platform.Interop; using Avalonia.Platform.Interop;
using JetBrains.Annotations;
// ReSharper disable IdentifierTypo // ReSharper disable IdentifierTypo
namespace Avalonia.X11.NativeDialogs namespace Avalonia.X11.NativeDialogs
{ {
@ -256,10 +258,17 @@ namespace Avalonia.X11.NativeDialogs
public static IntPtr GetForeignWindow(IntPtr xid) => gdk_x11_window_foreign_new_for_display(s_display, xid); public static IntPtr GetForeignWindow(IntPtr xid) => gdk_x11_window_foreign_new_for_display(s_display, xid);
static object s_startGtkLock = new();
static Task<bool> s_startGtkTask;
public static Task<bool> StartGtk() public static Task<bool> StartGtk()
{ {
var tcs = new TaskCompletionSource<bool>(); return StartGtkCore();
new Thread(() => }
private static void GtkThread(TaskCompletionSource<bool> tcs)
{
try
{ {
try try
{ {
@ -293,7 +302,17 @@ namespace Avalonia.X11.NativeDialogs
tcs.SetResult(true); tcs.SetResult(true);
while (true) while (true)
gtk_main_iteration(); gtk_main_iteration();
}) {Name = "GTK3THREAD", IsBackground = true}.Start(); }
catch
{
tcs.SetResult(false);
}
}
private static Task<bool> StartGtkCore()
{
var tcs = new TaskCompletionSource<bool>();
new Thread(() => GtkThread(tcs)) {Name = "GTK3THREAD", IsBackground = true}.Start();
return tcs.Task; return tcs.Task;
} }
} }

30
src/Avalonia.X11/NativeDialogs/GtkNativeFileDialogs.cs

@ -17,10 +17,10 @@ namespace Avalonia.X11.NativeDialogs
{ {
internal class GtkSystemDialog : BclStorageProvider internal class GtkSystemDialog : BclStorageProvider
{ {
private Task<bool>? _initialized; private static Task<bool>? _initialized;
private readonly X11Window _window; private readonly X11Window _window;
public GtkSystemDialog(X11Window window) private GtkSystemDialog(X11Window window)
{ {
_window = window; _window = window;
} }
@ -31,10 +31,15 @@ namespace Avalonia.X11.NativeDialogs
public override bool CanPickFolder => true; public override bool CanPickFolder => true;
public override async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options) internal static async Task<IStorageProvider?> TryCreate(X11Window window)
{ {
await EnsureInitialized(); _initialized ??= StartGtk();
return await _initialized ? new GtkSystemDialog(window) : null;
}
public override async Task<IReadOnlyList<IStorageFile>> OpenFilePickerAsync(FilePickerOpenOptions options)
{
return await await RunOnGlibThread(async () => return await await RunOnGlibThread(async () =>
{ {
var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.Open, var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.Open,
@ -46,8 +51,6 @@ namespace Avalonia.X11.NativeDialogs
public override async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options) public override async Task<IReadOnlyList<IStorageFolder>> OpenFolderPickerAsync(FolderPickerOpenOptions options)
{ {
await EnsureInitialized();
return await await RunOnGlibThread(async () => return await await RunOnGlibThread(async () =>
{ {
var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.SelectFolder, var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.SelectFolder,
@ -59,8 +62,6 @@ namespace Avalonia.X11.NativeDialogs
public override async Task<IStorageFile?> SaveFilePickerAsync(FilePickerSaveOptions options) public override async Task<IStorageFile?> SaveFilePickerAsync(FilePickerSaveOptions options)
{ {
await EnsureInitialized();
return await await RunOnGlibThread(async () => return await await RunOnGlibThread(async () =>
{ {
var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.Save, var res = await ShowDialog(options.Title, _window, GtkFileChooserAction.Save,
@ -225,19 +226,6 @@ namespace Avalonia.X11.NativeDialogs
return tcs.Task; return tcs.Task;
} }
private async Task EnsureInitialized()
{
if (_initialized == null)
{
_initialized = StartGtk();
}
if (!(await _initialized))
{
throw new Exception("Unable to initialize GTK on separate thread");
}
}
private static void UpdateParent(IntPtr chooser, IWindowImpl parentWindow) private static void UpdateParent(IntPtr chooser, IWindowImpl parentWindow)
{ {
var xid = parentWindow.Handle.Handle; var xid = parentWindow.Handle.Handle;

8
src/Avalonia.X11/X11Platform.cs

@ -216,16 +216,16 @@ namespace Avalonia
public bool OverlayPopups { get; set; } public bool OverlayPopups { get; set; }
/// <summary> /// <summary>
/// Enables native file dialogs as well as global menu support on Linux desktop environments where it's supported (e. g. XFCE and MATE with plugin, KDE, etc). /// Enables global menu support on Linux desktop environments where it's supported (e. g. XFCE and MATE with plugin, KDE, etc).
/// The default value is true. /// The default value is true.
/// </summary> /// </summary>
public bool UseDBusMenu { get; set; } = true; public bool UseDBusMenu { get; set; } = true;
/// <summary> /// <summary>
/// Enables GTK file picker instead of default FreeDesktop. /// Enables DBus file picker instead of GTK.
/// The default value is true. And FreeDesktop file picker is used instead if available. /// The default value is true.
/// </summary> /// </summary>
public bool UseGtkFilePicker { get; set; } = false; public bool UseDBusFilePicker { get; set; } = true;
/// <summary> /// <summary>
/// Deferred renderer would be used when set to true. Immediate renderer when set to false. The default value is true. /// Deferred renderer would be used when set to true. Immediate renderer when set to false. The default value is true.

3
src/Avalonia.X11/X11Structs.cs

@ -32,6 +32,7 @@ using System.Collections;
using System.Drawing; using System.Drawing;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
// ReSharper disable FieldCanBeMadeReadOnly.Global // ReSharper disable FieldCanBeMadeReadOnly.Global
// ReSharper disable IdentifierTypo // ReSharper disable IdentifierTypo
@ -545,7 +546,7 @@ namespace Avalonia.X11 {
{ {
if (data == null) if (data == null)
throw new InvalidOperationException(); throw new InvalidOperationException();
return *(T*)data; return Unsafe.ReadUnaligned<T>(data);
} }
} }

9
src/Avalonia.X11/X11Window.cs

@ -22,6 +22,7 @@ using Avalonia.Rendering;
using Avalonia.Rendering.Composition; using Avalonia.Rendering.Composition;
using Avalonia.Threading; using Avalonia.Threading;
using Avalonia.X11.Glx; using Avalonia.X11.Glx;
using Avalonia.X11.NativeDialogs;
using static Avalonia.X11.XLib; using static Avalonia.X11.XLib;
// ReSharper disable IdentifierTypo // ReSharper disable IdentifierTypo
// ReSharper disable StringLiteralTypo // ReSharper disable StringLiteralTypo
@ -215,9 +216,11 @@ namespace Avalonia.X11
_x11.Atoms.XA_CARDINAL, 32, PropertyMode.Replace, ref _xSyncCounter, 1); _x11.Atoms.XA_CARDINAL, 32, PropertyMode.Replace, ref _xSyncCounter, 1);
} }
var canUseFreeDekstopPicker = !platform.Options.UseGtkFilePicker && platform.Options.UseDBusMenu; StorageProvider = new CompositeStorageProvider(new Func<Task<IStorageProvider>>[]
StorageProvider = canUseFreeDekstopPicker && DBusSystemDialog.TryCreate(Handle) is {} dBusStorage {
? dBusStorage : new NativeDialogs.GtkSystemDialog(this); () => _platform.Options.UseDBusFilePicker ? DBusSystemDialog.TryCreate(Handle) : Task.FromResult<IStorageProvider>(null),
() => GtkSystemDialog.TryCreate(this),
});
} }
class SurfaceInfo : EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo class SurfaceInfo : EglGlPlatformSurface.IEglWindowGlPlatformSurfaceInfo

20
src/Windows/Avalonia.Win32/IconImpl.cs

@ -1,6 +1,5 @@
using System; using System;
using System.Drawing; using System.Drawing;
using System.Drawing.Imaging;
using System.IO; using System.IO;
using Avalonia.Platform; using Avalonia.Platform;
@ -8,31 +7,18 @@ namespace Avalonia.Win32
{ {
class IconImpl : IWindowIconImpl class IconImpl : IWindowIconImpl
{ {
private Bitmap bitmap; private readonly Icon icon;
private Icon icon;
public IconImpl(Bitmap bitmap)
{
this.bitmap = bitmap;
}
public IconImpl(Icon icon) public IconImpl(Icon icon)
{ {
this.icon = icon; this.icon = icon;
} }
public IntPtr HIcon => icon?.Handle ?? bitmap.GetHicon(); public IntPtr HIcon => icon.Handle;
public void Save(Stream outputStream) public void Save(Stream outputStream)
{ {
if (icon != null) icon.Save(outputStream);
{
icon.Save(outputStream);
}
else
{
bitmap.Save(outputStream, ImageFormat.Png);
}
} }
} }
} }

4
src/Windows/Avalonia.Win32/TrayIconImpl.cs

@ -18,6 +18,8 @@ namespace Avalonia.Win32
[Unstable] [Unstable]
public class TrayIconImpl : ITrayIconImpl public class TrayIconImpl : ITrayIconImpl
{ {
private static readonly IntPtr s_emptyIcon = new System.Drawing.Bitmap(32, 32).GetHicon();
private readonly int _uniqueId; private readonly int _uniqueId;
private static int s_nextUniqueId; private static int s_nextUniqueId;
private bool _iconAdded; private bool _iconAdded;
@ -86,7 +88,7 @@ namespace Avalonia.Win32
uID = _uniqueId, uID = _uniqueId,
uFlags = NIF.TIP | NIF.MESSAGE, uFlags = NIF.TIP | NIF.MESSAGE,
uCallbackMessage = (int)CustomWindowsMessage.WM_TRAYMOUSE, uCallbackMessage = (int)CustomWindowsMessage.WM_TRAYMOUSE,
hIcon = _icon?.HIcon ?? new IconImpl(new System.Drawing.Bitmap(32, 32)).HIcon, hIcon = _icon?.HIcon ?? s_emptyIcon,
szTip = _tooltipText ?? "" szTip = _tooltipText ?? ""
}; };

29
src/Windows/Avalonia.Win32/Win32NativeToManagedMenuExporter.cs

@ -1,6 +1,10 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Reactive.Linq;
using Avalonia.Collections;
using Avalonia.Controls; using Avalonia.Controls;
using Avalonia.Controls.Platform; using Avalonia.Controls.Platform;
using Avalonia.Media.Imaging;
using Avalonia.Utilities;
#nullable enable #nullable enable
@ -15,17 +19,28 @@ namespace Avalonia.Win32
_nativeMenu = nativeMenu; _nativeMenu = nativeMenu;
} }
private IEnumerable<MenuItem> Populate(NativeMenu nativeMenu) private AvaloniaList<MenuItem> Populate(NativeMenu nativeMenu)
{ {
var result = new AvaloniaList<MenuItem>();
foreach (var menuItem in nativeMenu.Items) foreach (var menuItem in nativeMenu.Items)
{ {
if (menuItem is NativeMenuItemSeparator) if (menuItem is NativeMenuItemSeparator)
{ {
yield return new MenuItem { Header = "-" }; result.Add(new MenuItem { Header = "-" });
} }
else if (menuItem is NativeMenuItem item) else if (menuItem is NativeMenuItem item)
{ {
var newItem = new MenuItem { Header = item.Header, Icon = item.Icon, Command = item.Command, CommandParameter = item.CommandParameter }; var newItem = new MenuItem
{
[!MenuItem.HeaderProperty] = item.GetObservable(NativeMenuItem.HeaderProperty).ToBinding(),
[!MenuItem.IconProperty] = item.GetObservable(NativeMenuItem.IconProperty)
.Select(i => i is {} bitmap ? new Image { Source = bitmap } : null).ToBinding(),
[!MenuItem.IsEnabledProperty] = item.GetObservable(NativeMenuItem.IsEnabledProperty).ToBinding(),
[!MenuItem.CommandProperty] = item.GetObservable(NativeMenuItem.CommandProperty).ToBinding(),
[!MenuItem.CommandParameterProperty] = item.GetObservable(NativeMenuItem.CommandParameterProperty).ToBinding(),
[!MenuItem.InputGestureProperty] = item.GetObservable(NativeMenuItem.GestureProperty).ToBinding()
};
if (item.Menu != null) if (item.Menu != null)
{ {
@ -33,15 +48,17 @@ namespace Avalonia.Win32
} }
else if (item.HasClickHandlers && item is INativeMenuItemExporterEventsImplBridge bridge) else if (item.HasClickHandlers && item is INativeMenuItemExporterEventsImplBridge bridge)
{ {
newItem.Click += (_, __) => bridge.RaiseClicked(); newItem.Click += (_, _) => bridge.RaiseClicked();
} }
yield return newItem; result.Add(newItem);
} }
} }
return result;
} }
public IEnumerable<MenuItem>? GetMenu() public AvaloniaList<MenuItem>? GetMenu()
{ {
if (_nativeMenu != null) if (_nativeMenu != null)
{ {

8
src/Windows/Avalonia.Win32/Win32Platform.cs

@ -359,7 +359,7 @@ namespace Avalonia.Win32
using (var memoryStream = new MemoryStream()) using (var memoryStream = new MemoryStream())
{ {
bitmap.Save(memoryStream); bitmap.Save(memoryStream);
return new IconImpl(new System.Drawing.Bitmap(memoryStream)); return CreateIconImpl(memoryStream);
} }
} }
@ -367,11 +367,15 @@ namespace Avalonia.Win32
{ {
try try
{ {
// new Icon() will work only if stream is an "ico" file.
return new IconImpl(new System.Drawing.Icon(stream)); return new IconImpl(new System.Drawing.Icon(stream));
} }
catch (ArgumentException) catch (ArgumentException)
{ {
return new IconImpl(new System.Drawing.Bitmap(stream)); // Fallback to Bitmap creation and converting into a windows icon.
using var icon = new System.Drawing.Bitmap(stream);
var hIcon = icon.GetHicon();
return new IconImpl(System.Drawing.Icon.FromHandle(hIcon));
} }
} }

417
tests/Avalonia.Base.UnitTests/Rendering/CompositorHitTestingTests.cs

@ -0,0 +1,417 @@
using System;
using System.Linq;
using Avalonia.Controls;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Shapes;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using Moq;
using Xunit;
namespace Avalonia.Base.UnitTests.Rendering;
public class CompositorHitTestingTests : CompositorTestsBase
{
[Fact]
public void HitTest_Should_Find_Controls_At_Point()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
var border = new Border
{
Width = 100,
Height = 100,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
s.TopLevel.Content = border;
s.AssertHitTest(new Point(100, 100), null, border);
}
}
[Fact]
public void HitTest_Should_Not_Find_Empty_Controls_At_Point()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
var border = new Border
{
Width = 100,
Height = 100,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
s.TopLevel.Content = border;
s.AssertHitTest(new Point(100, 100), null);
}
}
[Fact]
public void HitTest_Should_Not_Find_Invisible_Controls_At_Point()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Border visible, border;
s.TopLevel.Content = border = new Border
{
Width = 100,
Height = 100,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center,
IsVisible = false,
Child = visible = new Border
{
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Stretch,
VerticalAlignment = VerticalAlignment.Stretch,
}
};
s.AssertHitTest(new Point(100, 100), null);
}
}
[Fact]
public void HitTest_Should_Not_Find_Control_Outside_Point()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
var border = new Border
{
Width = 100,
Height = 100,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
s.TopLevel.Content = border;
s.AssertHitTest(new Point(10, 10), null);
}
}
[Fact]
public void HitTest_Should_Return_Top_Controls_First()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Panel container = new Panel
{
Width = 200,
Height = 200,
Children =
{
new Border
{
Width = 100,
Height = 100,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
},
new Border
{
Width = 50,
Height = 50,
Background = Brushes.Blue,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
}
}
};
s.TopLevel.Content = container;
s.AssertHitTest(new Point(100, 100), null, container.Children[1], container.Children[0]);
}
}
[Fact]
public void HitTest_Should_Return_Top_Controls_First_With_ZIndex()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Panel container = new Panel
{
Width = 200,
Height = 200,
Children =
{
new Border
{
Width = 100,
Height = 100,
ZIndex = 1,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
},
new Border
{
Width = 50,
Height = 50,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
},
new Border
{
Width = 75,
Height = 75,
ZIndex = 2,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
}
}
};
s.TopLevel.Content = container;
s.AssertHitTest(new Point(100, 100), null, new[] { container.Children[2], container.Children[0], container.Children[1] });
}
}
[Fact]
public void HitTest_Should_Find_Control_Translated_Outside_Parent_Bounds()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Border target;
Panel container = new Panel
{
Width = 200,
Height = 200,
Background = Brushes.Red,
ClipToBounds = false,
Children =
{
new Border
{
Width = 100,
Height = 100,
ZIndex = 1,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
Child = target = new Border
{
Width = 50,
Height = 50,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Left,
VerticalAlignment = VerticalAlignment.Top,
RenderTransform = new TranslateTransform(110, 110),
}
},
}
};
s.TopLevel.Content = container;
s.AssertHitTest(new Point(120, 120), null, target, container);
}
}
[Fact]
public void HitTest_Should_Not_Find_Control_Outside_Parent_Bounds_When_Clipped()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Border target;
Panel container = new Panel
{
Width = 100,
Height = 200,
Background = Brushes.Red,
Children =
{
new Panel()
{
Width = 100,
Height = 100,
Background = Brushes.Red,
Margin = new Thickness(0, 100, 0, 0),
ClipToBounds = true,
Children =
{
(target = new Border()
{
Width = 100,
Height = 100,
Background = Brushes.Red,
Margin = new Thickness(0, -100, 0, 0)
})
}
}
}
};
s.TopLevel.Content = container;
s.AssertHitTest(new Point(50, 50), null, container);
}
}
[Fact]
public void HitTest_Should_Not_Find_Control_Outside_Scroll_Viewport()
{
using (var s = new CompositorServices(new Size(100, 200)))
{
Border target;
Border item1;
Border item2;
ScrollContentPresenter scroll;
Panel container = new Panel
{
Width = 100,
Height = 200,
Background = Brushes.Red,
Children =
{
(target = new Border()
{
Name = "b1",
Width = 100,
Height = 100,
Background = Brushes.Red,
}),
new Border()
{
Name = "b2",
Width = 100,
Height = 100,
Background = Brushes.Red,
Margin = new Thickness(0, 100, 0, 0),
Child = scroll = new ScrollContentPresenter()
{
CanHorizontallyScroll = true,
CanVerticallyScroll = true,
Content = new StackPanel()
{
Children =
{
(item1 = new Border()
{
Name = "b3",
Width = 100,
Height = 100,
Background = Brushes.Red,
}),
(item2 = new Border()
{
Name = "b4",
Width = 100,
Height = 100,
Background = Brushes.Red,
}),
}
}
}
}
}
};
s.TopLevel.Content = container;
scroll.UpdateChild();
s.AssertHitTestFirst(new Point(50, 150), null, item1);
s.AssertHitTestFirst(new Point(50,50), null, target);
scroll.Offset = new Vector(0, 100);
s.AssertHitTestFirst(new Point(50, 150), null, item2);
s.AssertHitTestFirst(new Point(50,50), null, target);
}
}
[Fact]
public void HitTest_Should_Not_Find_Path_When_Outside_Fill()
{
using (var s = new CompositorServices(new Size(200, 200)))
{
Path path = new Path
{
Width = 200,
Height = 200,
Fill = Brushes.Red,
Data = StreamGeometry.Parse("M100,0 L0,100 100,100")
};
s.TopLevel.Content = path;
s.AssertHitTest(new Point(100, 100), null, path);
s.AssertHitTest(new Point(10, 10), null);
}
}
[Fact]
public void HitTest_Should_Respect_Geometry_Clip()
{
using (var s = new CompositorServices(new Size(400, 400)))
{
Canvas canvas;
Border border = new Border
{
Background = Brushes.Red,
Clip = StreamGeometry.Parse("M100,0 L0,100 100,100"),
Width = 200,
Height = 200,
Child = canvas = new Canvas
{
Background = Brushes.Yellow,
Margin = new Thickness(10),
}
};
s.TopLevel.Content = border;
s.RunJobs();
Assert.Equal(new Rect(100, 100, 200, 200), border.Bounds);
s.AssertHitTest(new Point(200,200), null, canvas, border);
s.AssertHitTest(new Point(110, 110), null);
}
}
[Fact]
public void HitTest_Should_Accommodate_ICustomHitTest()
{
using (var s = new CompositorServices(new Size(300, 200)))
{
Border border = new CustomHitTestBorder
{
ClipToBounds = false,
Width = 100,
Height = 100,
Background = Brushes.Red,
HorizontalAlignment = HorizontalAlignment.Center,
VerticalAlignment = VerticalAlignment.Center
};
s.TopLevel.Content = border;
s.AssertHitTest(75, 100, null, border);
s.AssertHitTest(125, 100, null, border);
s.AssertHitTest(175, 100, null);
}
}
private IDisposable TestApplication()
{
return UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
}
}

109
tests/Avalonia.Base.UnitTests/Rendering/CompositorInvalidationTests.cs

@ -0,0 +1,109 @@
using Avalonia.Controls;
using Avalonia.Media;
using Xunit;
namespace Avalonia.Base.UnitTests.Rendering;
public class CompositorInvalidationTests : CompositorTestsBase
{
[Fact]
public void Control_Should_Invalidate_Own_Rect_When_Added()
{
using (var s = new CompositorCanvas())
{
var control = new Border()
{
Background = Brushes.Red, Width = 20, Height = 10,
[Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50
};
s.Canvas.Children.Add(control);
s.AssertRects(new Rect(30, 50, 20, 10));
}
}
[Fact]
public void Control_Should_Invalidate_Own_Rect_When_Removed()
{
using (var s = new CompositorCanvas())
{
var control = new Border()
{
Background = Brushes.Red, Width = 20, Height = 10,
[Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50
};
s.Canvas.Children.Add(control);
s.RunJobs();
s.Events.Rects.Clear();
s.Canvas.Children.Remove(control);
s.AssertRects(new Rect(30, 50, 20, 10));
}
}
[Fact]
public void Control_Should_Invalidate_Both_Own_Rects_When_Moved()
{
using (var s = new CompositorCanvas())
{
var control = new Border()
{
Background = Brushes.Red, Width = 20, Height = 10,
[Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50
};
s.Canvas.Children.Add(control);
s.RunJobs();
s.Events.Rects.Clear();
control[Canvas.LeftProperty] = 55;
s.AssertRects(new Rect(30, 50, 20, 10),
new Rect(55, 50, 20, 10)
);
}
}
[Fact]
public void Control_Should_Invalidate_Child_Rects_When_Moved()
{
using (var s = new CompositorCanvas())
{
var control = new Decorator()
{
[Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50,
Padding = new Thickness(10),
Child = new Border()
{
Width = 20, Height = 10,
Background = Brushes.Red
}
};
s.Canvas.Children.Add(control);
s.RunJobs();
s.Events.Rects.Clear();
control[Canvas.LeftProperty] = 55;
s.AssertRects(new Rect(40, 60, 20, 10),
new Rect(65, 60, 20, 10)
);
}
}
[Fact]
public void Control_Should_Invalidate_Child_Rects_When_Becomes_Invisible()
{
using (var s = new CompositorCanvas())
{
var control = new Decorator()
{
[Canvas.LeftProperty] = 30, [Canvas.TopProperty] = 50,
Padding = new Thickness(10),
Child = new Border()
{
Width = 20, Height = 10,
Background = Brushes.Red
}
};
s.Canvas.Children.Add(control);
s.RunJobs();
s.Events.Rects.Clear();
control.IsVisible = false;
s.AssertRects(new Rect(40, 60, 20, 10));
}
}
}

208
tests/Avalonia.Base.UnitTests/Rendering/CompositorTestsBase.cs

@ -0,0 +1,208 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Reactive.Disposables;
using System.Threading;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Controls.Embedding;
using Avalonia.Controls.Presenters;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Input.Raw;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Rendering.Composition;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using JetBrains.Annotations;
using Xunit;
namespace Avalonia.Base.UnitTests.Rendering;
public class CompositorTestsBase
{
public class DebugEvents : ICompositionTargetDebugEvents
{
public List<Rect> Rects = new();
public void RectInvalidated(Rect rc)
{
Rects.Add(rc);
}
public void Reset()
{
Rects.Clear();
}
}
public class ManualRenderTimer : IRenderTimer
{
public event Action<TimeSpan> Tick;
public bool RunsInBackground => false;
public void TriggerTick() => Tick?.Invoke(TimeSpan.Zero);
public Task TriggerBackgroundTick() => Task.Run(TriggerTick);
}
class TopLevelImpl : ITopLevelImpl
{
private readonly Compositor _compositor;
public CompositingRenderer Renderer { get; private set; }
public TopLevelImpl(Compositor compositor, Size clientSize)
{
ClientSize = clientSize;
_compositor = compositor;
}
public void Dispose()
{
}
public Size ClientSize { get; }
public Size? FrameSize { get; }
public double RenderScaling => 1;
public IEnumerable<object> Surfaces { get; } = Array.Empty<object>();
public Action<RawInputEventArgs> Input { get; set; }
public Action<Rect> Paint { get; set; }
public Action<Size, PlatformResizeReason> Resized { get; set; }
public Action<double> ScalingChanged { get; set; }
public Action<WindowTransparencyLevel> TransparencyLevelChanged { get; set; }
public IRenderer CreateRenderer(IRenderRoot root)
{
return Renderer = new CompositingRenderer(root, _compositor);
}
public void Invalidate(Rect rect)
{
}
public void SetInputRoot(IInputRoot inputRoot)
{
}
public Point PointToClient(PixelPoint point) => default;
public PixelPoint PointToScreen(Point point) => new();
public void SetCursor(ICursorImpl cursor)
{
}
public Action Closed { get; set; }
public Action LostFocus { get; set; }
public IMouseDevice MouseDevice { get; } = new MouseDevice();
public IPopupImpl CreatePopup() => throw new NotImplementedException();
public void SetTransparencyLevelHint(WindowTransparencyLevel transparencyLevel)
{
}
public WindowTransparencyLevel TransparencyLevel { get; }
public AcrylicPlatformCompensationLevels AcrylicCompensationLevels { get; }
}
protected class CompositorServices : IDisposable
{
private readonly IDisposable _app;
public Compositor Compositor { get; }
public ManualRenderTimer Timer { get; } = new();
public EmbeddableControlRoot TopLevel { get; }
public CompositingRenderer Renderer { get; } = null!;
public DebugEvents Events { get; } = new();
public void Dispose()
{
TopLevel.Renderer.Stop();
TopLevel.Dispose();
_app.Dispose();
}
public CompositorServices(Size? size = null)
{
_app = UnitTestApplication.Start(TestServices.MockPlatformRenderInterface);
try
{
AvaloniaLocator.CurrentMutable.Bind<IRenderTimer>().ToConstant(Timer);
AvaloniaLocator.CurrentMutable.Bind<IRenderLoop>()
.ToConstant(new RenderLoop(Timer, Dispatcher.UIThread));
Compositor = new Compositor(AvaloniaLocator.Current.GetRequiredService<IRenderLoop>(), null);
var impl = new TopLevelImpl(Compositor, size ?? new Size(1000, 1000));
TopLevel = new EmbeddableControlRoot(impl)
{
Template = new FuncControlTemplate((parent, scope) =>
{
var presenter = new ContentPresenter
{
[~ContentPresenter.ContentProperty] = new TemplateBinding(ContentControl.ContentProperty)
};
scope.Register("PART_ContentPresenter", presenter);
return presenter;
})
};
Renderer = impl.Renderer;
TopLevel.Prepare();
TopLevel.Renderer.Start();
RunJobs();
Renderer.CompositionTarget.Server.DebugEvents = Events;
}
catch
{
_app.Dispose();
throw;
}
}
public void RunJobs()
{
Dispatcher.UIThread.RunJobs();
Timer.TriggerTick();
Dispatcher.UIThread.RunJobs();
}
public void AssertRects(params Rect[] rects)
{
RunJobs();
var toAssert = rects.Select(x => x.ToString()).Distinct().OrderBy(x => x);
var invalidated = Events.Rects.Select(x => x.ToString()).Distinct().OrderBy(x => x);
Assert.Equal(toAssert, invalidated);
Events.Rects.Clear();
}
public void AssertHitTest(double x, double y, Func<IVisual, bool> filter, params object[] expected)
=> AssertHitTest(new Point(x, y), filter, expected);
public void AssertHitTest(Point pt, Func<IVisual, bool> filter, params object[] expected)
{
RunJobs();
var tested = Renderer.HitTest(pt, TopLevel, filter);
Assert.Equal(expected, tested);
}
public void AssertHitTestFirst(Point pt, Func<IVisual, bool> filter, object expected)
{
RunJobs();
var tested = Renderer.HitTest(pt, TopLevel, filter).First();
Assert.Equal(expected, tested);
}
}
protected class CompositorCanvas : CompositorServices
{
public Canvas Canvas { get; } = new();
public CompositorCanvas()
{
TopLevel.Content = Canvas;
RunJobs();
Events.Reset();
}
}
}

27
tests/Avalonia.UnitTests/MockPlatformRenderInterface.cs

@ -4,6 +4,7 @@ using System.IO;
using Avalonia.Media; using Avalonia.Media;
using Avalonia.Platform; using Avalonia.Platform;
using Avalonia.Media.Imaging; using Avalonia.Media.Imaging;
using Avalonia.Rendering;
using Moq; using Moq;
namespace Avalonia.UnitTests namespace Avalonia.UnitTests
@ -25,9 +26,33 @@ namespace Avalonia.UnitTests
return Mock.Of<IGeometryImpl>(x => x.Bounds == rect); return Mock.Of<IGeometryImpl>(x => x.Bounds == rect);
} }
class MockRenderTarget : IRenderTarget
{
public void Dispose()
{
}
public IDrawingContextImpl CreateDrawingContext(IVisualBrushRenderer visualBrushRenderer)
{
var m = new Mock<IDrawingContextImpl>();
m.Setup(c => c.CreateLayer(It.IsAny<Size>()))
.Returns(() =>
{
var r = new Mock<IDrawingContextLayerImpl>();
r.Setup(r => r.CreateDrawingContext(It.IsAny<IVisualBrushRenderer>()))
.Returns(CreateDrawingContext(null));
return r.Object;
}
);
return m.Object;
}
}
public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces) public IRenderTarget CreateRenderTarget(IEnumerable<object> surfaces)
{ {
return Mock.Of<IRenderTarget>(); return new MockRenderTarget();
} }
public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi) public IRenderTargetBitmapImpl CreateRenderTargetBitmap(PixelSize size, Vector dpi)

Loading…
Cancel
Save