Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Mar 14, 2024
1 parent 0449bcb commit 4d77050
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 173 deletions.
7 changes: 7 additions & 0 deletions Svg.Skia.sln
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Avalonia.Svg", "src\Avaloni
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AvaloniaSvgSkiaStylingSample", "samples\AvaloniaSvgSkiaStylingSample\AvaloniaSvgSkiaStylingSample.csproj", "{8A938DC2-1634-4387-BAB3-69F871D54FB5}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Avalonia.Svg.Skia.UiTests", "tests\Avalonia.Svg.Skia.UiTests\Avalonia.Svg.Skia.UiTests.csproj", "{CDAEEF97-F162-468B-9424-6496253C2BFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -207,6 +209,10 @@ Global
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8A938DC2-1634-4387-BAB3-69F871D54FB5}.Release|Any CPU.Build.0 = Release|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{CDAEEF97-F162-468B-9424-6496253C2BFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -238,6 +244,7 @@ Global
{3049C672-8A3F-4FE4-9973-515B8323B546} = {4C42912C-9F8C-43D9-A4B5-4427F7EC8F18}
{B742F260-0EC6-4805-AE9F-987818CE3CF4} = {4C42912C-9F8C-43D9-A4B5-4427F7EC8F18}
{8A938DC2-1634-4387-BAB3-69F871D54FB5} = {B65D5B3A-77BE-4AFF-B502-A136B9C932F8}
{CDAEEF97-F162-468B-9424-6496253C2BFE} = {7863AE7D-FF68-45BF-BA68-6FA0E5604CB7}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {12D5E557-A27B-4FB2-83A3-4AC75B04B22C}
Expand Down
12 changes: 9 additions & 3 deletions src/Avalonia.Svg.Skia/SvgCustomDrawOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,32 +22,38 @@ public void Dispose()

public Rect Bounds { get; }

public bool HitTest(Point p) => false;
public bool HitTest(Point p) => true;

public bool Equals(ICustomDrawOperation? other) => false;

public void Render(ImmediateDrawingContext context)
{
if (_svg == null)
{
return;

}

var leaseFeature = context.TryGetFeature<ISkiaSharpApiLeaseFeature>();
if (leaseFeature is null)
{
return;
}

using var lease = leaseFeature.Lease();
var canvas = lease?.SkCanvas;
if (canvas is null)
{
return;
}
lock (_svg.Locker)

lock (_svg.Sync)
{
var picture = _svg.Picture;
if (picture is null)
{
return;
}

canvas.Save();
canvas.DrawPicture(picture);
canvas.Restore();
Expand Down
13 changes: 7 additions & 6 deletions src/Svg.Skia/SKSvg.Model.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ public static void Draw(SkiaSharp.SKCanvas skCanvas, string path, SkiaModel skia
private string? _originalPath;
private System.IO.Stream? _originalStream;

public object Sync { get; } = new ();

public SKSvgSettings Settings { get; }

public IAssetLoader AssetLoader { get; }
Expand All @@ -95,8 +97,6 @@ public static void Draw(SkiaSharp.SKCanvas skCanvas, string path, SkiaModel skia

public virtual SkiaSharp.SKPicture? Picture { get; protected set; }

public object Locker { get; } = new object();

public SvgParameters? Parameters => _originalParameters;

public SKSvg()
Expand Down Expand Up @@ -183,7 +183,7 @@ public SKSvg()

public SkiaSharp.SKPicture? ReLoad(SvgParameters? parameters)
{
lock (Locker)
lock (Sync)
{
if (!CacheOriginalStream)
{
Expand Down Expand Up @@ -251,10 +251,11 @@ public bool Save(string path, SkiaSharp.SKColor background, SkiaSharp.SKEncodedI

private void Reset()
{
Model = null;
Drawable = null;
lock (Locker)
lock (Sync)
{
Model = null;
Drawable = null;

Picture?.Dispose();
Picture = null;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/Avalonia.Svg.Skia.UiTests/App.axaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<Application xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="UITests.App" RequestedThemeVariant="Light">
x:Class="Avalonia.Svg.Skia.UiTests.App"
RequestedThemeVariant="Light">
<Application.Styles>
<FluentTheme />
</Application.Styles>
Expand Down
38 changes: 18 additions & 20 deletions tests/Avalonia.Svg.Skia.UiTests/App.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Controls.Primitives;
using Avalonia.Markup.Xaml;

namespace UITests
namespace Avalonia.Svg.Skia.UiTests;

public partial class App : Application
{
public partial class App : Application
{
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}
public override void Initialize()
{
AvaloniaXamlLoader.Load(this);
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new Window();
}

public override void OnFrameworkInitializationCompleted()
{
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.MainWindow = new Window();
}
base.OnFrameworkInitializationCompleted();
}
}
}
base.OnFrameworkInitializationCompleted();
}
}
50 changes: 28 additions & 22 deletions tests/Avalonia.Svg.Skia.UiTests/AvaloniaApp.cs
Original file line number Diff line number Diff line change
@@ -1,33 +1,39 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;

namespace UITests
namespace Avalonia.Svg.Skia.UiTests;

public static class AvaloniaApp
{
public static class AvaloniaApp
{
public static void Stop()
{
var app = GetApp();
if (app is IDisposable disposable)
{
Dispatcher.UIThread.Post(disposable.Dispose);
}
public static void Stop()
{
var app = GetApp();

if (app is IDisposable disposable)
{
Dispatcher.UIThread.Post(disposable.Dispose);
}

if (app != null)
Dispatcher.UIThread.Post(() => app.Shutdown());
}
if (app != null)
{
Dispatcher.UIThread.Post(() => app.Shutdown());
}
}

public static Window? GetMainWindow() => GetApp()?.MainWindow;
public static Window? GetMainWindow()
{
return GetApp()?.MainWindow;
}

public static IClassicDesktopStyleApplicationLifetime? GetApp() =>
(IClassicDesktopStyleApplicationLifetime?)Application.Current?.ApplicationLifetime;
private static IClassicDesktopStyleApplicationLifetime? GetApp()
{
return (IClassicDesktopStyleApplicationLifetime?)Application.Current?.ApplicationLifetime;
}

public static AppBuilder BuildAvaloniaApp() =>
AppBuilder
.Configure<App>()
.UsePlatformDetect();
}
public static AppBuilder BuildAvaloniaApp() =>
AppBuilder
.Configure<App>()
.UsePlatformDetect();
}
Loading

0 comments on commit 4d77050

Please sign in to comment.