Skip to content

Commit

Permalink
Fix css for Source property
Browse files Browse the repository at this point in the history
  • Loading branch information
wieslawsoltes committed Nov 24, 2024
1 parent 83c684a commit 62b1613
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 18 deletions.
11 changes: 9 additions & 2 deletions samples/AvaloniaSvgSkiaSample/MainWindow.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,9 +140,16 @@
<ComboBoxItem>UniformToFill</ComboBoxItem>
</ComboBox>
<Grid ColumnDefinitions="50*,8,50*">
<Svg Grid.Column="0" Name="svgString" Source="{Binding #stringTextBox.Text}" />
<Svg Grid.Column="0" Name="svgString"
Source="{Binding #stringTextBox.Text}"
Css="{Binding #cssTextBox.Text}" />
<GridSplitter Grid.Column="1" />
<TextBox Grid.Column="2" Name="stringTextBox" AcceptsReturn="True" AcceptsTab="True" />
<Grid Grid.Column="2" RowDefinitions="Auto,50*,Auto,30*">
<TextBlock Grid.Row="0" Text="Svg" />
<TextBox Grid.Row="1" Name="stringTextBox" AcceptsReturn="True" AcceptsTab="True" />
<TextBlock Grid.Row="2" Text="Svg" />
<TextBox Grid.Row="3" Name="cssTextBox" AcceptsReturn="True" AcceptsTab="True" />
</Grid>
</Grid>
</DockPanel>
</TabItem>
Expand Down
43 changes: 35 additions & 8 deletions src/Avalonia.Svg.Skia/Svg.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Avalonia.Controls;
using Avalonia.Logging;
using Avalonia.Media;
Expand Down Expand Up @@ -246,38 +248,61 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

if (change.Property == PathProperty)
{
var path = change.GetNewValue<string?>();
var css = GetCss(this);
var currentCss = GetCurrentCss(this);
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
var path = change.GetNewValue<string?>();
LoadFromPath(path, parameters);
InvalidateVisual();
}

if (change.Property == CssProperty)
{
var path = Path;
var css = change.GetNewValue<string?>();
var currentCss = GetCurrentCss(this);
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
LoadFromPath(path, parameters);
var path = Path;
var source = Source;

if (path is { })
{
LoadFromPath(path, parameters);
}
else if (source is { })
{
LoadFromSource(source, parameters);
}

InvalidateVisual();
}

if (change.Property == CurrentCssProperty)
{
var path = Path;
var css = GetCss(this);
var currentCss = change.GetNewValue<string?>();
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
LoadFromPath(path, parameters);
var path = Path;
var source = Source;

if (path is { })
{
LoadFromPath(path, parameters);
}
else if (source is { })
{
LoadFromSource(source, parameters);
}

InvalidateVisual();
}

if (change.Property == SourceProperty)
{
var source = change.GetNewValue<string?>();
LoadFromSource(source);
var css = GetCss(this);
var currentCss = GetCurrentCss(this);
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
LoadFromSource(source, parameters);
InvalidateVisual();
}

Expand Down Expand Up @@ -333,7 +358,7 @@ private void LoadFromPath(string? path, SvgParameters? parameters = null)
}
}

private void LoadFromSource(string? source)
private void LoadFromSource(string? source, SvgParameters? parameters = null)
{
if (source is null)
{
Expand All @@ -345,7 +370,9 @@ private void LoadFromSource(string? source)

try
{
_svg = SvgSource.LoadFromSvg(source);
var bytes = Encoding.UTF8.GetBytes(source);
using var ms = new MemoryStream(bytes);
_svg = SvgSource.LoadFromStream(ms, parameters);
}
catch (Exception e)
{
Expand Down
41 changes: 34 additions & 7 deletions src/Avalonia.Svg/Svg.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Avalonia.Controls;
using Avalonia.Media;
using Avalonia.Metadata;
Expand Down Expand Up @@ -239,28 +241,51 @@ protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs chang

if (change.Property == CssProperty)
{
var path = Path;
var css = change.GetNewValue<string?>();
var currentCss = GetCurrentCss(this);
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
LoadFromPath(path, parameters);
var path = Path;
var source = Source;

if (path is { })
{
LoadFromPath(path, parameters);
}
else if (source is { })
{
LoadFromSource(source, parameters);
}

InvalidateVisual();
}

if (change.Property == CurrentCssProperty)
{
var path = Path;
var css = GetCss(this);
var currentCss = change.GetNewValue<string?>();
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
LoadFromPath(path, parameters);
var path = Path;
var source = Source;

if (path is { })
{
LoadFromPath(path, parameters);
}
else if (source is { })
{
LoadFromSource(source, parameters);
}

InvalidateVisual();
}

if (change.Property == SourceProperty)
{
var css = GetCss(this);
var currentCss = GetCurrentCss(this);
var parameters = new SvgParameters(null, string.Concat(css, ' ', currentCss));
var source = change.GetNewValue<string?>();
LoadFromSource(source);
LoadFromSource(source, parameters);
InvalidateVisual();
}
}
Expand All @@ -280,14 +305,16 @@ private void LoadFromPath(string? path, SvgParameters? parameters = null)
}
}

private void LoadFromSource(string? source)
private void LoadFromSource(string? source, SvgParameters? parameters = null)
{
_picture = default;
_avaloniaPicture?.Dispose();

if (source is not null)
{
_picture = SvgSource.LoadPictureFromSvg(source);
var bytes = Encoding.UTF8.GetBytes(source);
using var ms = new MemoryStream(bytes);
_picture = SvgSource.LoadPicture(ms, parameters);
if (_picture is { })
{
_avaloniaPicture = AvaloniaPicture.Record(_picture);
Expand Down
2 changes: 1 addition & 1 deletion src/Avalonia.Svg/SvgSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ public static SvgSource Load(Stream stream, SvgParameters? parameters = null)
/// </summary>
/// <param name="source">The svg source.</param>
/// <returns>The svg picture.</returns>
public static SKPicture? LoadPictureFromSvg(string source)
public static SKPicture? LoadPictureFromSvg(string source, SvgParameters? parameters = null)
{
var document = SM.SvgExtensions.FromSvg(source);
return document is { } ? SM.SvgExtensions.ToModel(document, s_assetLoader, out _, out _) : default;
Expand Down

0 comments on commit 62b1613

Please sign in to comment.