Skip to content

Commit

Permalink
[Avalonia] Image loading updates, disposing updates, trimming updates…
Browse files Browse the repository at this point in the history
…, misc
  • Loading branch information
Ruben2776 committed May 31, 2024
1 parent 4af639c commit bb687d3
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 55 deletions.
1 change: 1 addition & 0 deletions src/PicView.Avalonia.Win32/PicView.Avalonia.Win32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<TrimmerRemoveSymbols>true</TrimmerRemoveSymbols>
<OptimizationPreference>Speed</OptimizationPreference>
<EnableUnsafeUTF7Encoding>false</EnableUnsafeUTF7Encoding>
<HttpActivityPropagationSupport>false</HttpActivityPropagationSupport>
<PlatformTarget>x64</PlatformTarget>
<ImplicitUsings>enable</ImplicitUsings>
<Product>PicView</Product>
Expand Down
1 change: 1 addition & 0 deletions src/PicView.Avalonia.Win32/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

namespace PicView.Avalonia.Win32;

// ReSharper disable once ClassNeverInstantiated.Global
internal class Program
{
// Initialization code. Don't use any Avalonia, third-party APIs or any
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,10 @@ await Dispatcher.UIThread.InvokeAsync(() =>
await Dispatcher.UIThread.InvokeAsync(() =>
{
Opacity = to;
Height = double.NaN;
GalleryNavigation.CenterScrollToSelectedItem(vm);
});
vm.GalleryVerticalAlignment = VerticalAlignment.Stretch;


_isAnimating = false;
}

Expand Down Expand Up @@ -205,7 +203,7 @@ private async Task BottomToFullAnimation()
await heightAnimation.RunAsync(this);
await Dispatcher.UIThread.InvokeAsync(() =>
{
Height = double.NaN;
Height = to;
GalleryNavigation.CenterScrollToSelectedItem(vm);
});
UIHelper.SetStretchMode(vm);
Expand Down
11 changes: 6 additions & 5 deletions src/PicView.Avalonia/Helpers/ImageHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using PicView.Avalonia.Navigation;
using PicView.Core.FileHandling;
using PicView.Core.ImageDecoding;
using SkiaSharp;

namespace PicView.Avalonia.Helpers;

Expand Down Expand Up @@ -101,10 +102,10 @@ await Task.Run(() =>

void Add(Stream stream)
{
var bmp = new Bitmap(stream);
imageModel.Image = bmp;
imageModel.PixelWidth = bmp?.PixelSize.Width ?? 0;
imageModel.PixelHeight = bmp?.PixelSize.Height ?? 0;
var writeableBitmap = WriteableBitmap.Decode(stream);
imageModel.Image = writeableBitmap;
imageModel.PixelWidth = writeableBitmap?.PixelSize.Width ?? 0;
imageModel.PixelHeight = writeableBitmap?.PixelSize.Height ?? 0;
imageModel.ImageType = ImageType.Bitmap;
}

Expand Down Expand Up @@ -179,7 +180,7 @@ async Task<Bitmap> CreateThumb(IMagickImage magick)
await using var memoryStream = new MemoryStream();
await magick.WriteAsync(memoryStream);
memoryStream.Position = 0;
return new Bitmap(memoryStream);
return WriteableBitmap.Decode(memoryStream);
}
}

Expand Down
39 changes: 1 addition & 38 deletions src/PicView.Avalonia/Models/ImageModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@

namespace PicView.Avalonia.Models;

public class ImageModel : IDisposable
public class ImageModel
{
private bool _disposedValue;

public object? Image { get; set; }
public FileInfo? FileInfo { get; set; }
public int PixelWidth { get; set; }
Expand Down Expand Up @@ -36,39 +34,4 @@ EXIFHelper.EXIFOrientation.None or EXIFHelper.EXIFOrientation.Normal
};
}
}

protected virtual void Dispose(bool disposing)
{
if (_disposedValue)
{
return;
}

if (disposing)
{
if (Image is Bitmap bmp)
{
bmp.Dispose();
}
FileInfo = null;
}

// TODO: free unmanaged resources (unmanaged objects) and override finalizer
// TODO: set large fields to null
_disposedValue = true;
}

// TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
~ImageModel()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: false);
}

public void Dispose()
{
// Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
Dispose(disposing: true);
GC.SuppressFinalize(this);
}
}
10 changes: 8 additions & 2 deletions src/PicView.Avalonia/Navigation/PreloaderService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using PicView.Core.ImageDecoding;
using System.Collections.Concurrent;
using System.Diagnostics;
using Avalonia.Media.Imaging;
using PicView.Avalonia.Helpers;

namespace PicView.Avalonia.Navigation;
Expand Down Expand Up @@ -193,8 +194,13 @@ public bool Remove(int key, List<string> list)

try
{
_preLoadList[key].ImageModel?.Dispose();
_ = _preLoadList[key];
var item = _preLoadList[key];
if (item.ImageModel.Image is Bitmap img)
{
img.Dispose();
}
item.ImageModel.Image = null;
item.ImageModel.FileInfo = null;
var remove = _preLoadList.TryRemove(key, out _);
#if DEBUG
if (remove && ShowAddRemove)
Expand Down
2 changes: 2 additions & 0 deletions src/PicView.Avalonia/Views/UC/ToolTipMessage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
CornerRadius="6">
<TextBlock
Classes="txt"
FontFamily="/Assets/Fonts/Roboto-Bold.ttf#Roboto"
FontSize="14"
Padding="12"
Text="{CompiledBinding ToolTipUIText}"
x:Name="ToolTipMessageText" />
Expand Down
15 changes: 8 additions & 7 deletions src/PicView.Avalonia/Views/UC/ToolTipMessage.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,18 @@ private async Task DoAnimation()

_isRunning = true;

// Start opacity animation from 0 to 1
var fadeInAnimation = AnimationsHelper.OpacityAnimation(from: 0, to: 1, 1.5);
await fadeInAnimation.RunAsync(this);
// ReSharper disable once CompareOfFloatsByEqualityOperator
if (Opacity != 1)
{
var fadeInAnimation = AnimationsHelper.OpacityAnimation(from: 0, to: 1, 1.5);
await fadeInAnimation.RunAsync(this);
}

// Wait for the duration before fading out
await Task.Delay(TimeSpan.FromSeconds(1.5));

// Start opacity animation from 1 to 0
_isRunning = false;

var fadeOutAnimation = AnimationsHelper.OpacityAnimation(from: 1, to: 0, 1.5);
await fadeOutAnimation.RunAsync(this);

_isRunning = false;
}
}

0 comments on commit bb687d3

Please sign in to comment.