using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
using XFEWarframe.Client;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Asynchronous original texture/cache image; never blocks the dispatcher with IO or JSON.</summary>
public sealed class GameAssetImage : Grid
{
private readonly Image image = new() { Stretch = Stretch.Uniform };
private readonly ProgressRing loading = new() { Width = 20, Height = 20 };
private int generation;
private SoftwareBitmap? pixels;
public string Resource { get => (string)GetValue(ResourceProperty); set => SetValue(ResourceProperty, value); }
public static readonly DependencyProperty ResourceProperty = DependencyProperty.Register(nameof(Resource), typeof(string), typeof(GameAssetImage), new PropertyMetadata("", (s, _) => ((GameAssetImage)s).Refresh()));
public string CacheKey { get => (string)GetValue(CacheKeyProperty); set => SetValue(CacheKeyProperty, value); }
public static readonly DependencyProperty CacheKeyProperty = DependencyProperty.Register(nameof(CacheKey), typeof(string), typeof(GameAssetImage), new PropertyMetadata("", (s, _) => ((GameAssetImage)s).Refresh()));
public bool IsCached { get; private set; }
public int PixelWidth { get; private set; }
public int PixelHeight { get; private set; }
public ImageSource? Source => image.Source;
public event EventHandler? ImageResolved;
public GameAssetImage() { Children.Add(image); Children.Add(loading); Loaded += (_, _) => Refresh(); Unloaded += (_, _) => generation++; }
public async void Refresh()
{
// Dependency properties belong to the UI thread. Background cache work
// must use snapshots, never read the control from a Task.Run callback.
var cacheKey = CacheKey;
var resource = Resource;
var observed = ++generation; image.Source = null; IsCached = false;
pixels?.Dispose(); pixels = null; PixelWidth = PixelHeight = 0;
loading.IsActive = resource.Length > 0 || cacheKey.Length > 0; loading.Visibility = loading.IsActive ? Visibility.Visible : Visibility.Collapsed;
SoftwareBitmap? decoded = null;
try
{
var path = cacheKey.Length > 0 ? await Task.Run(() => VisualAssetCache.Find(cacheKey)) : resource.Length > 0 ? await Task.Run(() => VisualAssetCache.IconAsync(resource)) : null;
if (observed != generation) return;
if (path is not null)
{
// Decode through WIC off the dispatcher. XAML's async image upload
// can wait for a render pass when a window or item is not visible.
decoded = await Task.Run(async () =>
{
using var stream = await Windows.Storage.Streams.FileRandomAccessStream.OpenAsync(path, Windows.Storage.FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
return await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
});
if (observed != generation) return;
var bitmap = new WriteableBitmap(decoded.PixelWidth, decoded.PixelHeight);
decoded.CopyToBuffer(bitmap.PixelBuffer);
bitmap.Invalidate();
image.Source = bitmap;
pixels = decoded; decoded = null;
PixelWidth = pixels.PixelWidth; PixelHeight = pixels.PixelHeight;
IsCached = true;
}
}
catch (OperationCanceledException) when (observed != generation) { }
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Runtime.InteropServices.COMException)
{ if (observed == generation) image.Source = null; }
finally { decoded?.Dispose(); if (observed == generation) { loading.IsActive = false; loading.Visibility = Visibility.Collapsed; ImageResolved?.Invoke(this, EventArgs.Empty); } }
}
public void SetDragPreview(DragUI preview)
{
if (IsCached && pixels is not null) preview.SetContentFromSoftwareBitmap(pixels, new Windows.Foundation.Point(0, 0));
}
}
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Graphics.Imaging;
using XFEWarframe.Client;
namespace XFEWarframe.Desktop.Editing;
/// <summary>Asynchronous original texture/cache image; never blocks the dispatcher with IO or JSON.</summary>
public sealed class GameAssetImage : Grid
{
private readonly Image image = new() { Stretch = Stretch.Uniform };
private readonly ProgressRing loading = new() { Width = 20, Height = 20 };
private int generation;
private SoftwareBitmap? pixels;
public string Resource { get => (string)GetValue(ResourceProperty); set => SetValue(ResourceProperty, value); }
public static readonly DependencyProperty ResourceProperty = DependencyProperty.Register(nameof(Resource), typeof(string), typeof(GameAssetImage), new PropertyMetadata("", (s, _) => ((GameAssetImage)s).Refresh()));
public string CacheKey { get => (string)GetValue(CacheKeyProperty); set => SetValue(CacheKeyProperty, value); }
public static readonly DependencyProperty CacheKeyProperty = DependencyProperty.Register(nameof(CacheKey), typeof(string), typeof(GameAssetImage), new PropertyMetadata("", (s, _) => ((GameAssetImage)s).Refresh()));
public bool IsCached { get; private set; }
public int PixelWidth { get; private set; }
public int PixelHeight { get; private set; }
public ImageSource? Source => image.Source;
public event EventHandler? ImageResolved;
public GameAssetImage() { Children.Add(image); Children.Add(loading); Loaded += (_, _) => Refresh(); Unloaded += (_, _) => generation++; }
public async void Refresh()
{
// Dependency properties belong to the UI thread. Background cache work
// must use snapshots, never read the control from a Task.Run callback.
var cacheKey = CacheKey;
var resource = Resource;
var observed = ++generation; image.Source = null; IsCached = false;
pixels?.Dispose(); pixels = null; PixelWidth = PixelHeight = 0;
loading.IsActive = resource.Length > 0 || cacheKey.Length > 0; loading.Visibility = loading.IsActive ? Visibility.Visible : Visibility.Collapsed;
SoftwareBitmap? decoded = null;
try
{
var path = cacheKey.Length > 0 ? await Task.Run(() => VisualAssetCache.Find(cacheKey)) : resource.Length > 0 ? await Task.Run(() => VisualAssetCache.IconAsync(resource)) : null;
if (observed != generation) return;
if (path is not null)
{
// Decode through WIC off the dispatcher. XAML's async image upload
// can wait for a render pass when a window or item is not visible.
decoded = await Task.Run(async () =>
{
using var stream = await Windows.Storage.Streams.FileRandomAccessStream.OpenAsync(path, Windows.Storage.FileAccessMode.Read);
var decoder = await BitmapDecoder.CreateAsync(stream);
return await decoder.GetSoftwareBitmapAsync(BitmapPixelFormat.Bgra8, BitmapAlphaMode.Premultiplied);
});
if (observed != generation) return;
var bitmap = new WriteableBitmap(decoded.PixelWidth, decoded.PixelHeight);
decoded.CopyToBuffer(bitmap.PixelBuffer);
bitmap.Invalidate();
image.Source = bitmap;
pixels = decoded; decoded = null;
PixelWidth = pixels.PixelWidth; PixelHeight = pixels.PixelHeight;
IsCached = true;
}
}
catch (OperationCanceledException) when (observed != generation) { }
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException or System.Runtime.InteropServices.COMException)
{ if (observed == generation) image.Source = null; }
finally { decoded?.Dispose(); if (observed == generation) { loading.IsActive = false; loading.Visibility = Visibility.Collapsed; ImageResolved?.Invoke(this, EventArgs.Empty); } }
}
public void SetDragPreview(DragUI preview)
{
if (IsCached && pixels is not null) preview.SetContentFromSoftwareBitmap(pixels, new Windows.Foundation.Point(0, 0));
}
}