Skip to content

Commit

Permalink
Fix incorrect size at startup when loading fullscreen gallery and the…
Browse files Browse the repository at this point in the history
… image is set to be stretched. Fixed fullscreen gallery loading inconsistencies, fixed a problem where the number of image gallery items exceeded the actual number of images
  • Loading branch information
Ruben2776 committed May 21, 2023
1 parent 71e535c commit 37d5ed6
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 68 deletions.
44 changes: 22 additions & 22 deletions PicView/ImageHandling/Thumbnails.cs
Expand Up @@ -18,37 +18,37 @@ internal static class Thumbnails
internal static BitmapSource? GetThumb(int x, FileInfo? fileInfo = null)
{
BitmapSource? pic;

if (GetPicGallery != null && GetPicGallery.Container.Children.Count > 0 && x < GetPicGallery.Container.Children.Count)
{
var y = GetPicGallery.Container.Children[x] as PicGalleryItem;
pic = (BitmapSource)y.ThumbImage.Source;
}
else
try
{
if (fileInfo is null)
if (GetPicGallery != null && GetPicGallery.Container.Children.Count > 0 && x < GetPicGallery.Container.Children.Count)
{
var preLoadValue = PreLoader.Get(x);
if (preLoadValue is null)
{
fileInfo = new FileInfo(Pics[x]);
}
else
{
return preLoadValue.BitmapSource;
}
var y = GetPicGallery.Container.Children[x] as PicGalleryItem;
pic = (BitmapSource)y.ThumbImage.Source;
}
try
else
{
if (fileInfo is null)
{
var preLoadValue = PreLoader.Get(x);
if (preLoadValue is null)
{
fileInfo = new FileInfo(Pics[x]);
}
else
{
return preLoadValue.BitmapSource;
}
}

using var image = new MagickImage();
image.Ping(fileInfo);
var thumb = image.GetExifProfile()?.CreateThumbnail();
pic = thumb?.ToBitmapSource();
}
catch (Exception)
{
return null;
}
}
catch (Exception)
{
return null;
}

if (pic is { IsFrozen: false })
Expand Down
2 changes: 1 addition & 1 deletion PicView/PicGallery/GalleryClick.cs
Expand Up @@ -25,7 +25,7 @@ internal static class GalleryClick
{
internal static async Task ClickAsync(int id)
{
if (GalleryFunctions.IsGalleryOpen == false)
if (Settings.Default.FullscreenGallery)
{
await ItemClickAsync(id).ConfigureAwait(false);
return;
Expand Down
40 changes: 19 additions & 21 deletions PicView/PicGallery/GalleryLoad.cs
Expand Up @@ -117,21 +117,21 @@ internal static async Task LoadAsync()
{
IsLoading = true;
var source = new CancellationTokenSource();
var task = Task.Run(() => LoopAsync(source.Token), source.Token);
try
{
await task.ConfigureAwait(false);
IsLoading = false;
await LoopAsync(source.Token).ConfigureAwait(false);
}
catch (TaskCanceledException)
{
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Background, new Action(async () =>
{
UC.GetPicGallery.Container.Children.Clear();
await LoadAsync().ConfigureAwait(false); // restart when changing directory
}));
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(DispatcherPriority.Background,
new Action(() => { UC.GetPicGallery.Container.Children.Clear(); }));
await LoadAsync().ConfigureAwait(false);
}
finally
{
IsLoading = false;
source.Dispose();
}
finally { source.Dispose(); }
}

private static async Task LoopAsync(CancellationToken cancellationToken)
Expand All @@ -141,7 +141,7 @@ private static async Task LoopAsync(CancellationToken cancellationToken)
var count = Navigation.Pics.Count;
var index = Navigation.FolderIndex;

for (var i = 0; i < count; i++)
for (int i = 0; i < count; i++)
{
if (count != Navigation.Pics.Count)
{
Expand All @@ -151,19 +151,17 @@ private static async Task LoopAsync(CancellationToken cancellationToken)
Add(i, index);
}

for (var i = 0; i < count; i++)
await Parallel.ForEachAsync(Navigation.Pics, cancellationToken, async (item, token) =>
{
if (count != Navigation.Pics.Count)
{
throw new TaskCanceledException();
}

Add(i, index);
}

Parallel.For(0, count, i =>
{
_ = UpdatePic(i).ConfigureAwait(false);
if (cancellationToken.IsCancellationRequested)
{
cancellationToken.ThrowIfCancellationRequested();
}
await UpdatePic(item).ConfigureAwait(false);
});
}

Expand All @@ -186,7 +184,7 @@ internal static void Add(int i, int index)
}));
}

private static async Task UpdatePic(int i)
private static async Task UpdatePic(string file)
{
if (Navigation.Pics?.Count < Navigation.FolderIndex || Navigation.Pics?.Count < 1)
{
Expand All @@ -195,8 +193,8 @@ private static async Task UpdatePic(int i)
return;
}

var source = await Task.FromResult(Thumbnails.GetBitmapSourceThumb(new FileInfo(Navigation.Pics[i]), (int)GalleryNavigation.PicGalleryItemSize));
UpdatePic(i, source);
var source = await Task.FromResult(Thumbnails.GetBitmapSourceThumb(new FileInfo(file), (int)GalleryNavigation.PicGalleryItemSize));
UpdatePic(Navigation.Pics.IndexOf(file), source);
}

internal static void UpdatePic(int i, BitmapSource? pic)
Expand Down
10 changes: 5 additions & 5 deletions PicView/PicGallery/GalleryNavigation.cs
Expand Up @@ -216,11 +216,11 @@ internal static void HorizontalNavigation(Direction direction)

internal static void FullscreenGalleryNavigation()
{
SetSelected(FolderIndex, true);
SelectedGalleryItem = FolderIndex;

ConfigureWindows.GetMainWindow.Dispatcher.Invoke(() =>
{
SetSelected(FolderIndex, true);
SelectedGalleryItem = FolderIndex;
if (Settings.Default.FullscreenGallery)
{
GetPicGallery.Scroller.ScrollToHorizontalOffset(CenterScrollPosition);
Expand All @@ -229,9 +229,9 @@ internal static void FullscreenGalleryNavigation()
{
GetPicGallery.Scroller.ScrollToVerticalOffset(CenterScrollPosition);
}
Tooltip.CloseToolTipMessage();
});

Tooltip.CloseToolTipMessage();
}

#endregion Horizontal Gallery Navigation
Expand Down
13 changes: 2 additions & 11 deletions PicView/PicGallery/GalleryToggle.cs
Expand Up @@ -49,13 +49,8 @@ internal static async Task OpenHorizontalGalleryAsync()
await LoadAndScrollToAsync().ConfigureAwait(false);
}

internal static async Task OpenFullscreenGalleryAsync(bool startup)
internal static async Task OpenFullscreenGalleryAsync()
{
if (Pics?.Count < 1 && !startup)
{
return;
}

await GetMainWindow.Dispatcher.BeginInvoke(DispatcherPriority.Normal, () =>
{
GalleryLoad.LoadLayout();
Expand Down Expand Up @@ -93,11 +88,7 @@ internal static async Task OpenFullscreenGalleryAsync(bool startup)
GetPicGallery.Opacity = GetPicGallery.Container.Opacity = 1;
});

if (startup == false)
{
ScaleImage.TryFitImage();
}

ScaleImage.TryFitImage();
await LoadAndScrollToAsync().ConfigureAwait(false);
}

Expand Down
2 changes: 1 addition & 1 deletion PicView/UILogic/Sizing/ScaleImage.cs
Expand Up @@ -76,7 +76,7 @@ internal static void FitImage(double width, double height)
double maxWidth, maxHeight;
var margin = 0d;
var padding = MonitorInfo.DpiScaling <= 1 ? 20 * MonitorInfo.DpiScaling : 0; // Padding to make it feel more comfortable
var isFullScreenSize = Settings.Default is { Fullscreen: true, FullscreenGallery: true };
var isFullScreenSize = Settings.Default.Fullscreen || Settings.Default.FullscreenGallery;

var borderSpaceHeight = isFullScreenSize ? 0 : GetMainWindow.LowerBar.Height + GetMainWindow.TitleBar.Height;
var borderSpaceWidth = Settings.Default.Fullscreen ? 0 : padding;
Expand Down
4 changes: 3 additions & 1 deletion PicView/Views/UserControls/Gallery/PicGallery.xaml
Expand Up @@ -21,9 +21,11 @@
Panel.ZIndex="1" />
<ScrollViewer
x:Name="Scroller"
CanContentScroll="False"
FocusVisualStyle="{x:Null}"
Focusable="False"
HorizontalScrollBarVisibility="Visible"
HorizontalScrollBarVisibility="Visible"
IsDeferredScrollingEnabled="False"
VerticalScrollBarVisibility="Disabled">
<ScrollViewer.Style>
<Style TargetType="{x:Type ScrollViewer}">
Expand Down
2 changes: 1 addition & 1 deletion PicView/Views/UserControls/Gallery/PicGallery.xaml.cs
Expand Up @@ -6,7 +6,7 @@ namespace PicView.Views.UserControls.Gallery;
/// <summary>
/// Interaction logic for PicGallery.xaml
/// </summary>
public partial class PicGallery : UserControl
public partial class PicGallery
{
public PicGallery()
{
Expand Down
4 changes: 2 additions & 2 deletions PicView/Views/UserControls/Menus/ImageSettings.xaml.cs
Expand Up @@ -77,7 +77,7 @@ public ImageSettings()
Settings.Default.FullscreenGallery = true;
}

await GalleryToggle.OpenFullscreenGalleryAsync(false).ConfigureAwait(false);
await GalleryToggle.OpenFullscreenGalleryAsync().ConfigureAwait(false);
};
FullScreenGalleryButton.Click += async delegate
{
Expand All @@ -88,7 +88,7 @@ public ImageSettings()
Settings.Default.FullscreenGallery = true;
}

await GalleryToggle.OpenFullscreenGalleryAsync(false).ConfigureAwait(false);
await GalleryToggle.OpenFullscreenGalleryAsync().ConfigureAwait(false);
};

// ContainedGalleryBorder
Expand Down
7 changes: 4 additions & 3 deletions PicView/Views/Windows/MainWindow.xaml.cs
Expand Up @@ -70,7 +70,7 @@ public MainWindow()
// w.TitleBar.Background = (ImageBrush) Application.Current.Resources["NoisyBg"];
// w.LowerBar.Background = (ImageBrush) Application.Current.Resources["NoisyBg"];
// }
Task.Run(() =>
Task.Run(async() =>
{
var args = Environment.GetCommandLineArgs();
Expand All @@ -83,7 +83,8 @@ public MainWindow()
}
else
{
_ = GalleryToggle.OpenFullscreenGalleryAsync(true).ConfigureAwait(false);
ConfigureWindows.GetMainWindow.Dispatcher.Invoke(GalleryLoad.LoadLayout, DispatcherPriority.Send);
await GalleryToggle.OpenFullscreenGalleryAsync().ConfigureAwait(false);
}
}
else if (Settings.Default.Fullscreen)
Expand All @@ -105,7 +106,7 @@ public MainWindow()
}
else
{
_ = QuickLoad.QuickLoadAsync(args[1]).ConfigureAwait(false);
await QuickLoad.QuickLoadAsync(args[1]).ConfigureAwait(false);
// TODO maybe load extra images if multiple arguments
}
});
Expand Down

0 comments on commit 37d5ed6

Please sign in to comment.