-
Notifications
You must be signed in to change notification settings - Fork 760
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(svg): [Wasm] Add support for ms-appdata
- Loading branch information
1 parent
69095eb
commit 8dd1e30
Showing
5 changed files
with
250 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...sApp/UITests.Shared/Windows_UI_Xaml_Controls/ImageTests/SvgImageSource_FromMsAppData.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<Page x:Class="UITests.Windows_UI_Xaml_Controls.ImageTests.SvgImageSource_FromMsAppData" | ||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" | ||
xmlns:local="using:UITests.Windows_UI_Xaml_Controls.ImageTests" | ||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008" | ||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" | ||
mc:Ignorable="d" | ||
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> | ||
|
||
<Grid> | ||
<Grid.RowDefinitions> | ||
<RowDefinition Height="Auto" /> | ||
<RowDefinition Height="*" /> | ||
</Grid.RowDefinitions> | ||
|
||
<StackPanel Spacing="4"> | ||
<ComboBox ItemsSource="{x:Bind Sources}" SelectedItem="{x:Bind SelectedSource, Mode=TwoWay}" Header="SVG image"> | ||
<ComboBox.ItemTemplate> | ||
<DataTemplate x:DataType="local:SampleSvgSource"> | ||
<TextBlock Text="{x:Bind Name}" /> | ||
</DataTemplate> | ||
</ComboBox.ItemTemplate> | ||
</ComboBox> | ||
<ComboBox ItemsSource="{x:Bind Stretches}" SelectedItem="{x:Bind SelectedStretch, Mode=TwoWay}" Header="Stretch" /> | ||
<TextBox Text="{x:Bind ImageWidth, Mode=TwoWay}" Header="Image width" /> | ||
<TextBox Text="{x:Bind ImageHeight, Mode=TwoWay}" Header="Image height" /> | ||
<TextBox Text="{x:Bind RasterizedWidth, Mode=TwoWay}" Header="Rasterized width" /> | ||
<TextBox Text="{x:Bind RasterizedHeight, Mode=TwoWay}" Header="Rasterized height" /> | ||
</StackPanel> | ||
|
||
<Grid Grid.Row="1"> | ||
<Border HorizontalAlignment="Center" VerticalAlignment="Center" BorderBrush="Black" BorderThickness="2"> | ||
<Image x:Name="ImageElement" /> | ||
</Border> | ||
</Grid> | ||
</Grid> | ||
</Page> |
180 changes: 180 additions & 0 deletions
180
...p/UITests.Shared/Windows_UI_Xaml_Controls/ImageTests/SvgImageSource_FromMsAppData.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
#pragma warning disable CA1848 // Use LoggerMessage | ||
|
||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Text; | ||
using Microsoft.Extensions.Logging; | ||
using Uno.Extensions; | ||
using Uno.UI.Samples.Controls; | ||
using Windows.Storage; | ||
using Windows.UI.Xaml.Controls; | ||
using Windows.UI.Xaml.Media; | ||
using Windows.UI.Xaml.Media.Imaging; | ||
|
||
namespace UITests.Windows_UI_Xaml_Controls.ImageTests; | ||
|
||
[Sample("Image")] | ||
public sealed partial class SvgImageSource_FromMsAppData : Page | ||
{ | ||
private SampleSvgSource _selectedSource; | ||
private string _imageWidth = "100"; | ||
private string _rasterizedWidth = ""; | ||
private string _imageHeight = "100"; | ||
private string _rasterizedHeight = ""; | ||
private string _selectedStretch = "None"; | ||
|
||
public SvgImageSource_FromMsAppData() | ||
{ | ||
this.InitializeComponent(); | ||
|
||
_selectedSource = Sources[0]; | ||
OnPropertyChanged(); | ||
} | ||
|
||
public SampleSvgSource[] Sources { get; } = new SampleSvgSource[] | ||
{ | ||
new("Temp", new Uri("ms-appx:///Assets/Formats/couch.svg")), | ||
new("Local", new Uri("ms-appx:///Assets/Formats/czcalendar.svg")), | ||
new("Roaming", new Uri("ms-appx:///Assets/Formats/heliocentric.svg")) | ||
}; | ||
|
||
public string[] Stretches { get; } = Enum.GetNames(typeof(Stretch)).ToArray(); | ||
|
||
public SampleSvgSource SelectedSource | ||
{ | ||
get => _selectedSource; | ||
set | ||
{ | ||
_selectedSource = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string SelectedStretch | ||
{ | ||
get => _selectedStretch; | ||
set | ||
{ | ||
_selectedStretch = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string ImageWidth | ||
{ | ||
get => _imageWidth; | ||
set | ||
{ | ||
_imageWidth = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string ImageHeight | ||
{ | ||
get => _imageHeight; | ||
set | ||
{ | ||
_imageHeight = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string RasterizedWidth | ||
{ | ||
get => _rasterizedWidth; | ||
set | ||
{ | ||
_rasterizedWidth = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
public string RasterizedHeight | ||
{ | ||
get => _rasterizedHeight; | ||
set | ||
{ | ||
_rasterizedHeight = value; | ||
OnPropertyChanged(); | ||
} | ||
} | ||
|
||
private async void OnPropertyChanged() | ||
{ | ||
try | ||
{ | ||
if (ImageElement.Source is not SvgImageSource svgImageSource) | ||
{ | ||
svgImageSource = new SvgImageSource(); | ||
ImageElement.Source = svgImageSource; | ||
} | ||
|
||
if (SelectedSource != null) | ||
{ | ||
var file = await StorageFile.GetFileFromApplicationUriAsync(SelectedSource.Uri); | ||
|
||
var targetFolder = SelectedSource.Name switch { | ||
"Local" => ApplicationData.Current.LocalFolder, | ||
"Temp" => ApplicationData.Current.TemporaryFolder, | ||
"Roaming" => ApplicationData.Current.RoamingFolder, | ||
_ => throw new NotSupportedException("Unsupported ms-appx target") | ||
}; | ||
|
||
await file.CopyAsync(targetFolder, file.Name, NameCollisionOption.ReplaceExisting); | ||
|
||
svgImageSource.UriSource = new Uri($"ms-appdata:///local/{file.Name}"); | ||
|
||
var text = await FileIO.ReadTextAsync(file); | ||
using MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(text)); | ||
await svgImageSource.SetSourceAsync(stream.AsRandomAccessStream()); | ||
} | ||
|
||
if (Enum.TryParse(SelectedStretch, out Stretch stretch)) | ||
{ | ||
ImageElement.Stretch = stretch; | ||
} | ||
|
||
if (double.TryParse(ImageWidth, out var width)) | ||
{ | ||
ImageElement.Width = width; | ||
} | ||
else | ||
{ | ||
ImageElement.Width = double.NaN; | ||
} | ||
|
||
if (double.TryParse(ImageHeight, out var height)) | ||
{ | ||
ImageElement.Height = height; | ||
} | ||
else | ||
{ | ||
ImageElement.Height = double.NaN; | ||
} | ||
|
||
if (double.TryParse(RasterizedWidth, out var rasterizedWidth)) | ||
{ | ||
svgImageSource.RasterizePixelWidth = rasterizedWidth; | ||
} | ||
else | ||
{ | ||
//svgImageSource.RasterizePixelWidth = double.PositiveInfinity; | ||
} | ||
|
||
if (double.TryParse(RasterizedHeight, out var rasterizedHeight)) | ||
{ | ||
svgImageSource.RasterizePixelHeight = rasterizedHeight; | ||
} | ||
else | ||
{ | ||
//svgImageSource.RasterizePixelHeight = double.PositiveInfinity; | ||
} | ||
} | ||
catch (Exception ex) | ||
{ | ||
this.Log().LogError(ex, "Error while changing SVG properties"); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters