Skip to content

Commit

Permalink
Improve preloader & and fix being able to change image when resizing
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Mar 20, 2023
1 parent fe9fae0 commit 2d7de92
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
16 changes: 10 additions & 6 deletions PicView/ChangeImage/FastPic.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using PicView.SystemIntegration;
using System;
using System.IO;
using System.Windows.Media.Imaging;
using static PicView.ChangeImage.Navigation;
Expand Down Expand Up @@ -53,7 +54,8 @@ internal static async Task Run(int index)
{
fileInfo = new FileInfo(Pics[index]);
LoadPic.LoadingPreview(fileInfo);
preloadValue = await Preloader.AddAsync(index, fileInfo).ConfigureAwait(false);
await Preloader.AddAsync(index, fileInfo).ConfigureAwait(false);
preloadValue = Preloader.Get(Pics[index]);
if (preloadValue is null)
{
await ErrorHandling.ReloadAsync().ConfigureAwait(false);
Expand All @@ -77,13 +79,15 @@ internal static async Task FastPicUpdateAsync()

_timer = null;
BitmapSource? pic = null;
Preloader.PreloadValue? preloadValue = null;

preloadValue = await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
var preloadValue = Preloader.Get(Pics[FolderIndex]);
if (preloadValue is null)
{
await ErrorHandling.ReloadAsync().ConfigureAwait(false);
return;
await Preloader.AddAsync(FolderIndex).ConfigureAwait(false);
if (preloadValue is null)
{
await ErrorHandling.ReloadAsync().ConfigureAwait(false);
return;
}
}
while (preloadValue.IsLoading)
{
Expand Down
5 changes: 4 additions & 1 deletion PicView/ChangeImage/LoadPic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,8 @@ await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
/// <param name="fileInfo">The file information for the image. If not specified, the file information will be obtained from the image list using the specified index.</param>
internal static async Task LoadPicAtIndexAsync(int index, FileInfo? fileInfo = null)
{
if (GetQuickResize is not null && GetQuickResize.Opacity > 0) return;

FolderIndex = index;
var preloadValue = Preloader.Get(Pics[index]);
fileInfo ??= new FileInfo(Pics[index]);
Expand Down Expand Up @@ -268,7 +270,8 @@ internal static async Task LoadPicAtIndexAsync(int index, FileInfo? fileInfo = n

if (preloadValue is null)
{
preloadValue = await Preloader.AddAsync(index, fileInfo).ConfigureAwait(false);
await Preloader.AddAsync(index, fileInfo).ConfigureAwait(false);
preloadValue = Preloader.Get(Pics[index]);
if (preloadValue is null)
{
await ErrorHandling.ReloadAsync().ConfigureAwait(false);
Expand Down
42 changes: 22 additions & 20 deletions PicView/ChangeImage/Preloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,20 +62,22 @@ internal PreloadValue(BitmapSource? bitmap, bool loading, FileInfo? fileInfo)
/// <param name="fileInfo">The file info of the image</param>
/// <param name="bitmapSource">The bitmap source of the image</param>
/// <returns>Preloadvalue that can be null</returns>
internal static async Task<PreloadValue?> AddAsync(int index, FileInfo? fileInfo = null, BitmapSource? bitmapSource = null)
internal static async Task AddAsync(int index, FileInfo? fileInfo = null, BitmapSource? bitmapSource = null)
{
if (index < 0 || index >= Pics.Count) return null;

if (_preloadList.ContainsKey(Pics[index]))
return Preloader.Get(Pics[index]);
if (index < 0 || index >= Pics.Count) return;

try
{
_keys.Enqueue(Pics[index]);
if (_keys.Count > MaxCount)
{
var oldestKey = _keys.Dequeue();
_preloadList.TryRemove(oldestKey, out _);
var remove = _preloadList.TryRemove(oldestKey, out _);
#if DEBUG
if (remove)
Trace.WriteLine($"{oldestKey} removed at {Pics.IndexOf(Pics[index])}");
#endif
return;
}

var preloadValue = new PreloadValue(null, true, null);
Expand All @@ -89,19 +91,17 @@ await ImageDecoder.ReturnBitmapSourceAsync(fileInfo).ConfigureAwait(false) ??
preloadValue.BitmapSource = bitmapSource;
preloadValue.IsLoading = false;
preloadValue.FileInfo = fileInfo;
return preloadValue;
}
else
{
return Preloader.Get(Pics[index]);

#if DEBUG
Trace.WriteLine($"{fileInfo.Name} added at {index}");
#endif
}
}
catch (Exception ex)
{
#if DEBUG
Trace.WriteLine($"{nameof(AddAsync)} exception: \n {ex}");
#endif
return null;
}
}

Expand Down Expand Up @@ -153,18 +153,21 @@ internal static void Clear()
/// <param name="currentIndex">The starting point for the iteration.</param>
internal static Task PreLoadAsync(int currentIndex) => Task.Run(() =>
{
int nextStartingIndex, prevStartingIndex;
int positiveIterations = 6;
int negativeIterations = 3;
int prevStartingIndex;
int positiveIterations = 4;
int negativeIterations = 2;

#if DEBUG
Trace.WriteLine($"\nPreloading started at {currentIndex} \n");
#endif

if (!Reverse)
{
nextStartingIndex = currentIndex;
prevStartingIndex = currentIndex - 1;

Parallel.For(0, positiveIterations, i =>
{
int index = (nextStartingIndex + i) % Pics.Count;
int index = (currentIndex + i) % Pics.Count;
_= AddAsync(index).ConfigureAwait(false);
});
Parallel.For(0, negativeIterations, i =>
Expand All @@ -175,17 +178,16 @@ internal static Task PreLoadAsync(int currentIndex) => Task.Run(() =>
}
else
{
nextStartingIndex = currentIndex;
prevStartingIndex = currentIndex + 1;

Parallel.For(0, positiveIterations, i =>
{
int index = (nextStartingIndex - i + Pics.Count) % Pics.Count;
int index = (currentIndex - i + Pics.Count) % Pics.Count;
_ = AddAsync(index).ConfigureAwait(false);
});
Parallel.For(0, negativeIterations, i =>
{
int index = (nextStartingIndex - i + Pics.Count) % Pics.Count;
int index = (prevStartingIndex + i + Pics.Count) % Pics.Count;
_ = AddAsync(index).ConfigureAwait(false);
});
}
Expand Down
5 changes: 2 additions & 3 deletions PicView/Views/UserControls/Misc/QuickResize.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@
x:Class="PicView.Views.UserControls.Misc.QuickResize"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:UserControls="clr-namespace:PicView.Views.UserControls"
xmlns:buttons="clr-namespace:PicView.Views.UserControls.Buttons"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:buttons="clr-namespace:PicView.Views.UserControls.Buttons"
d:DesignHeight="300"
BorderThickness="1"
FontFamily="/PicView;component/Themes/Resources/fonts/#Roboto"
Expand Down Expand Up @@ -104,7 +103,7 @@

<Label
x:Name="ApplyButton"
Margin="5,0,5,0"
Padding="9,9,9,9"
VerticalAlignment="Center"
Background="{DynamicResource BorderBrushAlt}"
Content="{StaticResource Apply}">
Expand Down
9 changes: 6 additions & 3 deletions PicView/Views/UserControls/Misc/QuickResize.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,18 @@ private void PercentageBox_SelectionChanged(object sender, SelectionChangedEvent

public void Show()
{
grid.Width = UILogic.Sizing.ScaleImage.XWidth;
grid.Height = UILogic.Sizing.ScaleImage.XHeight;
Visibility = Visibility.Visible;
AnimationHelper.Fade(this, TimeSpan.FromSeconds(.4), TimeSpan.Zero, 0, 1);
PercentageBox.SelectedIndex = 0;
WidthBox.Text = ConfigureWindows.GetMainWindow.MainImage.Source?.Width.ToString();
HeightBox.Text = ConfigureWindows.GetMainWindow.MainImage?.Source?.Height.ToString();

var timer = new System.Timers.Timer(401) { AutoReset = false, Enabled = true };
timer.Elapsed += delegate
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(() =>
{
Keyboard.Focus(WidthBox);
});
Expand All @@ -115,15 +118,15 @@ public void Show()

public void Hide()
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(() =>
{
AnimationHelper.Fade(this, TimeSpan.FromSeconds(.6), TimeSpan.Zero, 1, 0);
});

var timer = new System.Timers.Timer(601) { AutoReset = false, Enabled = true };
timer.Elapsed += delegate
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Render, () =>
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(() =>
{
Visibility = Visibility.Collapsed;
});
Expand Down

0 comments on commit 2d7de92

Please sign in to comment.