Skip to content
This repository has been archived by the owner on Jul 10, 2024. It is now read-only.

Commit

Permalink
try different window icon load strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
HendrikMennen committed Mar 8, 2024
1 parent 28a905b commit 5aadf57
Showing 1 changed file with 20 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/OneWare.Essentials/Converters/PathToWindowIconConverter.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
using System.Globalization;
using System.Reflection;
using Avalonia.Controls;
using Avalonia.Data.Converters;
using Avalonia.Media.Imaging;
using Avalonia.Platform;

namespace OneWare.Essentials.Converters;

public class PathToWindowIconConverter : IValueConverter
{
public object? Convert(object? value, Type targetType, object? parameter, CultureInfo culture)
{
if (value is string st)
if (value == null)
return null;

if (value is not string rawUri || !targetType.IsAssignableFrom(typeof(WindowIcon)))
throw new NotSupportedException();
Uri uri;

// Allow for assembly overrides
if (rawUri.StartsWith("avares://"))
{
uri = new Uri(rawUri);
}
else
{
if (st.StartsWith("avares://"))
{
st = st.Remove(0, "avares://".Length);
var nextSlash = st.IndexOf("/", StringComparison.Ordinal);
if (nextSlash > -1)
{
st = st.Remove(0, nextSlash + 1);
}
return new WindowIcon(st);
}
var assemblyName = Assembly.GetEntryAssembly()?.GetName().Name;
uri = new Uri($"avares://{assemblyName}{rawUri}");
}
return null;

return new WindowIcon(new Bitmap(AssetLoader.Open(uri)));;
}

public object? ConvertBack(object? value, Type targetType, object? parameter, CultureInfo culture)
Expand Down

0 comments on commit 5aadf57

Please sign in to comment.