StartLoading.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. using ImageMagick;
  2. using PicView.WPF.ChangeImage;
  3. using PicView.WPF.ConfigureSettings;
  4. using PicView.WPF.ImageHandling;
  5. using PicView.WPF.Properties;
  6. using PicView.WPF.Shortcuts;
  7. using PicView.WPF.SystemIntegration;
  8. using PicView.WPF.UILogic.Sizing;
  9. using PicView.WPF.Views.UserControls.Misc;
  10. using PicView.WPF.Views.Windows;
  11. using System.IO;
  12. using System.Windows;
  13. using System.Windows.Controls;
  14. using System.Windows.Media;
  15. using System.Windows.Media.Imaging;
  16. using static PicView.WPF.ChangeImage.Navigation;
  17. using static PicView.WPF.UILogic.Loading.LoadContextMenus;
  18. using static PicView.WPF.UILogic.Loading.LoadControls;
  19. using static PicView.WPF.UILogic.Sizing.WindowSizing;
  20. using static PicView.WPF.UILogic.TransformImage.ZoomLogic;
  21. using static PicView.WPF.UILogic.UC;
  22. namespace PicView.WPF.UILogic.Loading
  23. {
  24. internal static class StartLoading
  25. {
  26. internal static async Task LoadedEvent(Startup_Window startupWindow)
  27. {
  28. var spinner = GetSpinWaiter;
  29. spinner = new SpinWaiter();
  30. startupWindow.TheGrid.Children.Add(spinner);
  31. Panel.SetZIndex(spinner, 99);
  32. BitmapSource? bitmapSource = null;
  33. var image = new Image
  34. {
  35. Stretch = Stretch.Uniform,
  36. };
  37. // Load sizing properties
  38. MonitorInfo = MonitorSize.GetMonitorSize(startupWindow);
  39. if (Settings.Default.AutoFitWindow == false && Settings.Default.Fullscreen == false)
  40. {
  41. SetLastWindowSize(startupWindow);
  42. }
  43. var args = Environment.GetCommandLineArgs();
  44. if (args.Length > 1)
  45. {
  46. await Task.Run(async () =>
  47. {
  48. using var magickImage = new MagickImage();
  49. magickImage.Ping(args[1]);
  50. var thumb = magickImage.GetExifProfile()?.CreateThumbnail();
  51. var bitmapThumb = thumb?.ToBitmapSource();
  52. bitmapThumb?.Freeze();
  53. await startupWindow.Dispatcher.InvokeAsync(() =>
  54. {
  55. image.Source = bitmapThumb;
  56. startupWindow.TheGrid.Children.Add(image);
  57. });
  58. });
  59. bitmapSource = await Image2BitmapSource.ReturnBitmapSourceAsync(new FileInfo(args[1])).ConfigureAwait(false);
  60. await startupWindow.Dispatcher.InvokeAsync(() =>
  61. {
  62. if (Settings.Default.AutoFitWindow)
  63. {
  64. var size = Core.Calculations.ImageSizeCalculationHelper.GetImageSize(
  65. width: bitmapSource.PixelWidth * MonitorInfo.DpiScaling,
  66. height: bitmapSource.PixelHeight * MonitorInfo.DpiScaling,
  67. monitorWidth: MonitorInfo.WorkArea.Width * MonitorInfo.DpiScaling,
  68. monitorHeight: MonitorInfo.WorkArea.Height * MonitorInfo.DpiScaling,
  69. rotationAngle: 0,
  70. stretch: Settings.Default.FillImage,
  71. padding: 20 * MonitorInfo.DpiScaling,
  72. dpiScaling: MonitorInfo.DpiScaling,
  73. fullscreen: Settings.Default.Fullscreen,
  74. uiTopSize: ConfigureWindows.GetMainWindow.TitleBar.Height * MonitorInfo.DpiScaling,
  75. uiBottomSize: ConfigureWindows.GetMainWindow.LowerBar.Height * MonitorInfo.DpiScaling,
  76. galleryHeight: Settings.Default.IsBottomGalleryShown ? Settings.Default.BottomGalleryItemSize : 0,
  77. autoFit: true,
  78. containerWidth: ConfigureWindows.GetMainWindow.ParentContainer.Width * MonitorInfo.DpiScaling,
  79. containerHeight: ConfigureWindows.GetMainWindow.ParentContainer.Height * MonitorInfo.DpiScaling,
  80. Settings.Default.ScrollEnabled
  81. );
  82. image.Stretch = Stretch.Fill;
  83. image.Width = startupWindow.Width = size.Width;
  84. image.Height = startupWindow.Height = size.Height;
  85. CenterWindowOnScreen(startupWindow);
  86. ScaleImage.AspectRatio = size.AspectRatio;
  87. }
  88. image.Source = bitmapSource;
  89. });
  90. }
  91. await startupWindow.Dispatcher.InvokeAsync(() =>
  92. {
  93. startupWindow.TheGrid.Children.Remove(spinner);
  94. var mainWindow = new MainWindow();
  95. ConfigureWindows.GetMainWindow = mainWindow;
  96. if (Settings.Default.AutoFitWindow == false)
  97. {
  98. SetLastWindowSize(mainWindow);
  99. }
  100. mainWindow.MainImage.Source = image.Source;
  101. GetSpinWaiter = spinner;
  102. mainWindow.ParentContainer.Children.Add(spinner);
  103. });
  104. Pics = new List<string>();
  105. await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
  106. {
  107. // Set min size to DPI scaling
  108. ConfigureWindows.GetMainWindow.MinWidth *= MonitorInfo.DpiScaling;
  109. ConfigureWindows.GetMainWindow.MinHeight *= MonitorInfo.DpiScaling;
  110. SetWindowBehavior();
  111. if (Settings.Default.AutoFitWindow == false)
  112. SetLastWindowSize(ConfigureWindows.GetMainWindow);
  113. ConfigureWindows.GetMainWindow.Scroller.VerticalScrollBarVisibility = Settings.Default.ScrollEnabled
  114. ? ScrollBarVisibility.Auto
  115. : ScrollBarVisibility.Disabled;
  116. if (!Settings.Default.ShowInterface)
  117. {
  118. ConfigureWindows.GetMainWindow.TitleBar.Visibility =
  119. ConfigureWindows.GetMainWindow.LowerBar.Visibility
  120. = Visibility.Collapsed;
  121. }
  122. else if (!Settings.Default.ShowBottomNavBar)
  123. {
  124. ConfigureWindows.GetMainWindow.LowerBar.Visibility
  125. = Visibility.Collapsed;
  126. }
  127. });
  128. if (args.Length > 1)
  129. {
  130. await QuickLoad.QuickLoadAsync(args[1], null, bitmapSource).ConfigureAwait(false);
  131. }
  132. else
  133. {
  134. ErrorHandling.Unload(true);
  135. }
  136. await ConfigureWindows.GetMainWindow.Dispatcher.InvokeAsync(() =>
  137. {
  138. ConfigureWindows.GetMainWindow.Show();
  139. if (Settings.Default.Fullscreen)
  140. {
  141. if (args.Length < 2)
  142. {
  143. Settings.Default.Fullscreen = false;
  144. }
  145. else
  146. {
  147. Fullscreen_Restore(true);
  148. }
  149. }
  150. if (Settings.Default.AutoFitWindow)
  151. {
  152. ScaleImage.TryFitImage();
  153. }
  154. startupWindow.Close();
  155. });
  156. await CustomKeybindings.LoadKeybindings().ConfigureAwait(false);
  157. ConfigColors.UpdateColor();
  158. }
  159. internal static void AddDictionaries()
  160. {
  161. // Add dictionaries
  162. Application.Current.Resources.MergedDictionaries.Add(
  163. new ResourceDictionary
  164. {
  165. Source = new Uri(@"/PicView;component/Themes/Styles/Menu.xaml", UriKind.Relative)
  166. }
  167. );
  168. Application.Current.Resources.MergedDictionaries.Add(
  169. new ResourceDictionary
  170. {
  171. Source = new Uri(@"/PicView;component/Themes/Styles/ToolTip.xaml", UriKind.Relative)
  172. }
  173. );
  174. Application.Current.Resources.MergedDictionaries.Add(
  175. new ResourceDictionary
  176. {
  177. Source = new Uri(@"/PicView;component/Themes/Styles/Slider.xaml", UriKind.Relative)
  178. }
  179. );
  180. Application.Current.Resources.MergedDictionaries.Add(
  181. new ResourceDictionary
  182. {
  183. Source = new Uri(@"/PicView;component/Views/Resources/MainContextMenu.xaml", UriKind.Relative)
  184. }
  185. );
  186. Application.Current.Resources.MergedDictionaries.Add(
  187. new ResourceDictionary
  188. {
  189. Source = new Uri(@"/PicView;component/Views/Resources/WindowContextMenu.xaml", UriKind.Relative)
  190. }
  191. );
  192. Application.Current.Resources.MergedDictionaries.Add(
  193. new ResourceDictionary
  194. {
  195. Source = new Uri(@"/PicView;component/Views/Resources/NavigationContextMenu.xaml", UriKind.Relative)
  196. }
  197. );
  198. Application.Current.Resources.MergedDictionaries.Add(
  199. new ResourceDictionary
  200. {
  201. Source = new Uri(@"/PicView;component/Views/Resources/Icons.xaml", UriKind.Relative)
  202. }
  203. );
  204. Application.Current.Resources.MergedDictionaries.Add(
  205. new ResourceDictionary
  206. {
  207. Source = new Uri(@"/PicView;component/Views/Resources/GalleryContextMenu.xaml", UriKind.Relative)
  208. }
  209. );
  210. }
  211. internal static void AddUiElementsAndUpdateValues()
  212. {
  213. // Update values
  214. ConfigureWindows.GetMainWindow.FolderButton.BackgroundEvents();
  215. ConfigureWindows.GetMainWindow.AllowDrop = true;
  216. ConfigColors.SetColors();
  217. LoadClickArrow(true);
  218. LoadClickArrow(false);
  219. Loadx2();
  220. LoadMinus();
  221. LoadRestoreButton();
  222. LoadGalleryShortcut();
  223. // Update WindowStyle
  224. if (!Settings.Default.ShowInterface)
  225. {
  226. GetClickArrowLeft.Opacity =
  227. GetClickArrowRight.Opacity =
  228. GetX2.Opacity =
  229. GetMinus.Opacity =
  230. GetRestoreButton.Opacity =
  231. GetGalleryShortcut.Opacity =
  232. 0;
  233. GetClickArrowLeft.Visibility =
  234. GetClickArrowRight.Visibility =
  235. GetX2.Visibility =
  236. GetMinus.Visibility =
  237. GetGalleryShortcut.Visibility =
  238. GetRestoreButton.Visibility =
  239. Visibility.Visible;
  240. }
  241. else if (Settings.Default.Fullscreen)
  242. {
  243. GetClickArrowLeft.Opacity =
  244. GetClickArrowRight.Opacity =
  245. GetX2.Opacity =
  246. GetMinus.Opacity =
  247. GetRestoreButton.Opacity =
  248. 1;
  249. GetClickArrowLeft.Visibility =
  250. GetClickArrowRight.Visibility =
  251. GetX2.Visibility =
  252. GetMinus.Visibility =
  253. GetRestoreButton.Visibility =
  254. Visibility.Visible;
  255. if (!Settings.Default.IsBottomGalleryShown)
  256. {
  257. GetGalleryShortcut.Opacity = 1;
  258. GetGalleryShortcut.Visibility = Visibility.Visible;
  259. }
  260. }
  261. // Add UserControls :)
  262. LoadFileMenu();
  263. LoadImageSettingsMenu();
  264. LoadQuickSettingsMenu();
  265. LoadToolsAndEffectsMenu();
  266. LoadTooltipStyle();
  267. // Initialize things!
  268. InitializeZoom();
  269. GetFileHistory ??= new FileHistory();
  270. // Add things!
  271. Timers.AddTimers();
  272. AddContextMenus();
  273. }
  274. }
  275. }