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