Skip to content

Commit

Permalink
Work around for mono/SkiaSharp#2645
Browse files Browse the repository at this point in the history
  • Loading branch information
davidxuang committed Nov 24, 2023
1 parent 75726fd commit d5956ad
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 6 deletions.
11 changes: 11 additions & 0 deletions MusicDecrypto.Avalonia/Helpers/MathHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;

namespace MusicDecrypto.Avalonia.Helpers;

internal static class MathHelper
{
public static double RoundToEven(double v)
{
return Math.Round(v / 2, MidpointRounding.AwayFromZero) * 2;
}
}
37 changes: 31 additions & 6 deletions MusicDecrypto.Avalonia/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Media.Imaging;
using Avalonia.Platform;
using Avalonia.Platform.Storage;
using Avalonia.Threading;
using ByteSizeLib;
using FluentAvalonia.UI.Controls;
using MusicDecrypto.Avalonia.Controls;
using MusicDecrypto.Avalonia.Helpers;
using MusicDecrypto.Library;

namespace MusicDecrypto.Avalonia.ViewModels;
Expand All @@ -27,15 +26,16 @@ public partial class MainViewModel : ViewModelBase
private static readonly Regex _regex = NameRegex();
private static readonly SemaphoreSlim _dialogLock = new(1);

private readonly double _scaling = 0;
private const int _imageWidth = 72;
private readonly int _imageSize;

public ObservableCollection<Item> Items { get; private set; } = [];

public bool IsEmpty => Items.Count == 0;

public MainViewModel(double scaling)
{
_scaling = scaling;
_imageSize = (int)MathHelper.RoundToEven(_imageWidth * 2 * scaling);
Items.CollectionChanged += (s, e) => RaisePropertyChanged(nameof(IsEmpty));
}

Expand All @@ -47,6 +47,10 @@ public void AddFile(IStorageFile file)
Items.Add(item);
Task.Run(async () => await DecryptFileAsync(item));
}
else
{
file.Dispose();
}
}

public async ValueTask DecryptFileAsync(Item item)
Expand Down Expand Up @@ -87,7 +91,21 @@ public async ValueTask DecryptFileAsync(Item item)
if (info.Cover != null)
{
using var stream = new MemoryStream(info.Cover);
item.Cover = Bitmap.DecodeToWidth(stream, (int)(72 * 2 * _scaling));
// https://github.com/mono/SkiaSharp/issues/2645
// item.Cover = Bitmap.DecodeToWidth(stream, (int)(72 * 2 * _scaling));
var bm = new Bitmap(stream);
var size = Math.Max(bm.Size.Width, bm.Size.Height);
if (size > _imageSize)
{
item.Cover = bm.Size.Width > bm.Size.Height
? bm.CreateScaledBitmap(new(_imageSize, (int)Math.Round(_imageSize * bm.Size.Height / bm.Size.Width)))
: bm.CreateScaledBitmap(new((int)Math.Round(_imageSize * bm.Size.Width / bm.Size.Height), _imageSize));
bm.Dispose();
}
else
{
item.Cover = bm;
}
}

await using (var file = await newFile!.OpenWriteAsync())
Expand Down Expand Up @@ -135,10 +153,17 @@ private static async ValueTask<bool> OnRequestMatchAsync(string message, IEnumer
}
}

public class Item(IStorageFile file) : INotifyPropertyChanged
public class Item(IStorageFile file) : INotifyPropertyChanged, IDisposable
{
private static readonly Bitmap _coverFallback = new(AssetLoader.Open(new Uri("avares://musicdecrypto-avalonia/Assets/MusicNote.png")));

public void Dispose()
{
File.Dispose();
_cover?.Dispose();
GC.SuppressFinalize(this);
}

public IStorageFile File { get; init; } = file;

public event PropertyChangedEventHandler? PropertyChanged;
Expand Down

0 comments on commit d5956ad

Please sign in to comment.