QuickLoad.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using PicView.Core.FileHandling;
  2. using PicView.WPF.ImageHandling;
  3. using PicView.WPF.PicGallery;
  4. using PicView.WPF.Properties;
  5. using PicView.WPF.SystemIntegration;
  6. using PicView.WPF.UILogic;
  7. using System.Diagnostics;
  8. using System.IO;
  9. using System.Windows;
  10. using System.Windows.Input;
  11. using System.Windows.Media.Imaging;
  12. using System.Windows.Threading;
  13. using XamlAnimatedGif;
  14. using static PicView.WPF.ChangeImage.LoadPic;
  15. using static PicView.WPF.ChangeImage.Navigation;
  16. using static PicView.WPF.ChangeImage.PreLoader;
  17. using static PicView.WPF.ChangeTitlebar.SetTitle;
  18. using static PicView.WPF.FileHandling.FileLists;
  19. using static PicView.WPF.UILogic.Sizing.ScaleImage;
  20. namespace PicView.WPF.ChangeImage
  21. {
  22. internal static class QuickLoad
  23. {
  24. /// <summary>
  25. /// Load Image from blank values and show loading preview
  26. /// </summary>
  27. /// <param name="file"></param>
  28. internal static async Task QuickLoadAsync(string file, FileInfo? fileInfo = null, BitmapSource? preparedSource = null) => await Task.Run(async () =>
  29. {
  30. var mainWindow = ConfigureWindows.GetMainWindow;
  31. fileInfo ??= new FileInfo(file);
  32. if (preparedSource is not null)
  33. {
  34. if (!fileInfo.Exists) // If not file, try to load if URL, base64 or directory
  35. {
  36. await LoadPicFromStringAsync(file, fileInfo).ConfigureAwait(false);
  37. return;
  38. }
  39. if (file.IsArchive()) // Handle if file exist and is archive
  40. {
  41. await LoadPicFromArchiveAsync(file).ConfigureAwait(false);
  42. return;
  43. }
  44. }
  45. var bitmapSource = preparedSource ?? await Image2BitmapSource.ReturnBitmapSourceAsync(fileInfo).ConfigureAwait(false);
  46. await mainWindow.MainImage.Dispatcher.InvokeAsync(() =>
  47. {
  48. mainWindow.MainImage.Source = bitmapSource;
  49. }, DispatcherPriority.Send);
  50. if (fileInfo.Extension.Equals(".gif", StringComparison.OrdinalIgnoreCase))
  51. {
  52. var frames = ImageFunctions.GetImageFrames(fileInfo.FullName);
  53. if (frames > 1)
  54. {
  55. var uri = new Uri(fileInfo.FullName);
  56. await mainWindow.Dispatcher.InvokeAsync(
  57. () => { AnimationBehavior.SetSourceUri(mainWindow.MainImage, uri); },
  58. DispatcherPriority.Normal);
  59. }
  60. }
  61. Pics = await Task.FromResult(FileList(fileInfo)).ConfigureAwait(false);
  62. FolderIndex = Pics.IndexOf(fileInfo.FullName);
  63. var shouldLoadBottomGallery = Settings.Default.IsBottomGalleryShown;
  64. if (Settings.Default.ShowInterface == false)
  65. {
  66. shouldLoadBottomGallery = Settings.Default.ShowAltInterfaceBottomGallery;
  67. }
  68. await mainWindow.Dispatcher.InvokeAsync(() =>
  69. {
  70. SetTitleString(bitmapSource.PixelWidth, bitmapSource.PixelHeight, FolderIndex, fileInfo);
  71. UC.GetSpinWaiter.Visibility = Visibility.Collapsed;
  72. mainWindow.MainImage.Cursor = Cursors.Arrow;
  73. if (shouldLoadBottomGallery)
  74. {
  75. GalleryToggle.ShowBottomGallery();
  76. }
  77. if (preparedSource is not null)
  78. {
  79. FitImage(bitmapSource.Width, bitmapSource.Height);
  80. }
  81. }, DispatcherPriority.Normal);
  82. if (FolderIndex > 0)
  83. {
  84. Taskbar.Progress((double)FolderIndex / Pics.Count);
  85. _ = PreLoadAsync(FolderIndex, Pics.Count).ConfigureAwait(false);
  86. }
  87. _ = AddAsync(FolderIndex, fileInfo, bitmapSource).ConfigureAwait(false);
  88. if (shouldLoadBottomGallery)
  89. {
  90. _ = Task.Run(async () =>
  91. {
  92. try
  93. {
  94. await GalleryLoad.LoadAsync().ConfigureAwait(false);
  95. // Update gallery selections
  96. await UC.GetPicGallery.Dispatcher.InvokeAsync(() =>
  97. {
  98. // Select current item
  99. GalleryNavigation.SetSelected(FolderIndex, true);
  100. GalleryNavigation.SelectedGalleryItem = FolderIndex;
  101. GalleryNavigation.ScrollToGalleryCenter();
  102. });
  103. }
  104. catch (TaskCanceledException exception)
  105. {
  106. #if DEBUG
  107. Trace.WriteLine($"{nameof(QuickLoadAsync)} exception:\n{exception.Message}");
  108. #endif
  109. if (ConfigureWindows.GetMainWindow.Visibility == Visibility.Hidden)
  110. {
  111. Environment.Exit(0);
  112. }
  113. }
  114. catch (Exception)
  115. {
  116. //
  117. }
  118. });
  119. }
  120. // Add recent files, except when browsing archive
  121. if (string.IsNullOrWhiteSpace(ArchiveExtraction.TempZipFile) && Pics.Count > FolderIndex)
  122. {
  123. GetFileHistory ??= new FileHistory();
  124. GetFileHistory.Add(Pics[FolderIndex]);
  125. }
  126. });
  127. }
  128. }