WindowFunctions.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Input;
  5. using Avalonia.Layout;
  6. using Avalonia.Threading;
  7. using PicView.Avalonia.Input;
  8. using PicView.Avalonia.Interfaces;
  9. using PicView.Avalonia.Navigation;
  10. using PicView.Avalonia.UI;
  11. using PicView.Avalonia.ViewModels;
  12. using PicView.Core.ArchiveHandling;
  13. using PicView.Core.FileHandling;
  14. using PicView.Core.FileHistory;
  15. // ReSharper disable CompareOfFloatsByEqualityOperator
  16. namespace PicView.Avalonia.WindowBehavior;
  17. public static class WindowFunctions
  18. {
  19. public static async Task WindowClosingBehavior()
  20. {
  21. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  22. {
  23. return;
  24. }
  25. await WindowClosingBehavior(desktop.MainWindow);
  26. }
  27. public static async Task WindowClosingBehavior(Window window)
  28. {
  29. WindowResizing.SaveSize(window);
  30. if (Dispatcher.UIThread.CheckAccess())
  31. {
  32. window.Hide();
  33. }
  34. else
  35. {
  36. await Dispatcher.UIThread.InvokeAsync(window.Hide);
  37. }
  38. var vm = window.DataContext as MainViewModel;
  39. string? lastFile;
  40. if (NavigationManager.CanNavigate(vm))
  41. {
  42. if (!string.IsNullOrEmpty(ArchiveExtraction.LastOpenedArchive))
  43. {
  44. lastFile = ArchiveExtraction.LastOpenedArchive;
  45. }
  46. else
  47. {
  48. lastFile = vm?.PicViewer.FileInfo?.CurrentValue.FullName ?? FileHistoryManager.GetLastEntry();
  49. }
  50. }
  51. else
  52. {
  53. var url = vm?.PicViewer.Title.CurrentValue.GetURL();
  54. lastFile = !string.IsNullOrWhiteSpace(url) ? url : FileHistoryManager.GetLastEntry();
  55. }
  56. Settings.StartUp.LastFile = lastFile ?? "";
  57. await SaveSettingsAsync();
  58. await KeybindingManager.UpdateKeyBindingsFile(); // Save keybindings
  59. TempFileHelper.DeleteTempFiles();
  60. await FileHistoryManager.SaveToFileAsync();
  61. ArchiveExtraction.Cleanup();
  62. Environment.Exit(0);
  63. }
  64. #region Window State
  65. public static async Task ResizeAndFixRenderingError(MainViewModel vm)
  66. {
  67. await Dispatcher.UIThread.InvokeAsync(() =>
  68. {
  69. if (Settings.WindowProperties.AutoFit)
  70. {
  71. if (vm.PicViewer.PixelWidth.Value > UIHelper.GetMainView.Bounds.Width || vm.PicViewer.PixelHeight.Value > UIHelper.GetMainView.Bounds.Height)
  72. {
  73. vm.ImageViewer.MainBorder.Height = double.NaN;
  74. vm.ImageViewer.MainBorder.Width = double.NaN;
  75. WindowResizing.SetSize(1, 1, 0, 0, 0, vm);
  76. }
  77. else
  78. {
  79. WindowResizing.SetSize(vm);
  80. }
  81. CenterWindowOnScreen(false);
  82. }
  83. else
  84. {
  85. WindowResizing.SetSize(vm);
  86. }
  87. if (Settings.WindowProperties.AutoFit)
  88. {
  89. if (Settings.ImageScaling.StretchImage)
  90. {
  91. // Setting horizontal and vertical alignment fixes the rendering error
  92. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  93. {
  94. return;
  95. }
  96. Dispatcher.UIThread.Post(() => WindowResizing.SetSize(vm), DispatcherPriority.Render);
  97. desktop.MainWindow.HorizontalAlignment = HorizontalAlignment.Center;
  98. desktop.MainWindow.VerticalAlignment = VerticalAlignment.Center;
  99. }
  100. else
  101. {
  102. if (vm.PicViewer.PixelWidth.CurrentValue > UIHelper.GetMainView.Bounds.Width || vm.PicViewer.PixelHeight.CurrentValue > UIHelper.GetMainView.Bounds.Height)
  103. {
  104. Dispatcher.UIThread.Post(() => WindowResizing.SetSize(vm), DispatcherPriority.Render);
  105. }
  106. }
  107. }
  108. }, DispatcherPriority.Send);
  109. if (Settings.ImageScaling.StretchImage)
  110. {
  111. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  112. {
  113. return;
  114. }
  115. Dispatcher.UIThread.Post(() =>
  116. {
  117. WindowResizing.SetSize(vm);
  118. // Reset the horizontal and vertical alignment after fixing the rendering error
  119. desktop.MainWindow.HorizontalAlignment = HorizontalAlignment.Stretch;
  120. desktop.MainWindow.VerticalAlignment = VerticalAlignment.Stretch;
  121. }, DispatcherPriority.Render);
  122. }
  123. }
  124. public static void ShowMinimizedWindow(Window window)
  125. {
  126. window.BringIntoView();
  127. window.WindowState = WindowState.Normal;
  128. window.Activate();
  129. window.Focus();
  130. }
  131. public static async Task ToggleTopMost(MainViewModel vm)
  132. {
  133. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  134. {
  135. return;
  136. }
  137. if (Settings.WindowProperties.TopMost)
  138. {
  139. vm.GlobalSettings.IsTopMost.Value = false;
  140. desktop.MainWindow.Topmost = false;
  141. Settings.WindowProperties.TopMost = false;
  142. }
  143. else
  144. {
  145. vm.GlobalSettings.IsTopMost.Value = true;
  146. desktop.MainWindow.Topmost = true;
  147. Settings.WindowProperties.TopMost = true;
  148. }
  149. await SaveSettingsAsync().ConfigureAwait(false);
  150. }
  151. public static async Task ToggleAutoFit(MainViewModel vm)
  152. {
  153. if (Settings.WindowProperties.AutoFit)
  154. {
  155. vm.MainWindow.SizeToContent.Value = SizeToContent.Manual;
  156. vm.MainWindow.CanResize.Value = true;
  157. Settings.WindowProperties.AutoFit = false;
  158. vm.GlobalSettings.IsAutoFit.Value = false;
  159. }
  160. else
  161. {
  162. vm.MainWindow.SizeToContent.Value = SizeToContent.WidthAndHeight;
  163. vm.MainWindow.CanResize.Value = false;
  164. Settings.WindowProperties.AutoFit = true;
  165. vm.GlobalSettings.IsAutoFit.Value = true;
  166. }
  167. await ResizeAndFixRenderingError(vm);
  168. await SaveSettingsAsync().ConfigureAwait(false);
  169. }
  170. public static async Task AutoFitAndStretch(MainViewModel vm)
  171. {
  172. if (Settings.WindowProperties.AutoFit)
  173. {
  174. vm.MainWindow.SizeToContent.Value = SizeToContent.Manual;
  175. vm.MainWindow.CanResize.Value = true;
  176. Settings.WindowProperties.AutoFit = false;
  177. Settings.ImageScaling.StretchImage = false;
  178. vm.GlobalSettings.IsStretched.Value = false;
  179. vm.GlobalSettings.IsAutoFit.Value = false;
  180. }
  181. else
  182. {
  183. vm.MainWindow.SizeToContent.Value = SizeToContent.WidthAndHeight;
  184. vm.MainWindow.CanResize.Value = false;
  185. Settings.WindowProperties.AutoFit = true;
  186. Settings.ImageScaling.StretchImage = true;
  187. vm.GlobalSettings.IsAutoFit.Value = true;
  188. vm.GlobalSettings.IsStretched.Value = true;
  189. }
  190. await ResizeAndFixRenderingError(vm);
  191. await SaveSettingsAsync().ConfigureAwait(false);
  192. }
  193. public static async Task NormalWindow(MainViewModel vm)
  194. {
  195. vm.MainWindow.SizeToContent.Value = SizeToContent.Manual;
  196. vm.MainWindow.CanResize.Value = true;
  197. Settings.WindowProperties.AutoFit = false;
  198. await WindowResizing.SetSizeAsync(vm);
  199. vm.ImageViewer.MainImage.InvalidateVisual();
  200. await SaveSettingsAsync().ConfigureAwait(false);
  201. }
  202. public static async Task NormalWindowStretch(MainViewModel vm)
  203. {
  204. vm.MainWindow.SizeToContent.Value = SizeToContent.Manual;
  205. vm.MainWindow.CanResize.Value = true;
  206. Settings.WindowProperties.AutoFit = false;
  207. Settings.ImageScaling.StretchImage = true;
  208. vm.GlobalSettings.IsStretched.Value = true;
  209. await WindowResizing.SetSizeAsync(vm);
  210. vm.ImageViewer.MainImage.InvalidateVisual();
  211. await SaveSettingsAsync().ConfigureAwait(false);
  212. }
  213. public static async Task Stretch(MainViewModel vm)
  214. {
  215. Settings.ImageScaling.StretchImage = true;
  216. vm.GlobalSettings.IsStretched.Value = true;
  217. await WindowResizing.SetSizeAsync(vm);
  218. vm.ImageViewer.MainImage.InvalidateVisual();
  219. await SaveSettingsAsync().ConfigureAwait(false);
  220. }
  221. public static async Task Minimize()
  222. {
  223. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  224. {
  225. return;
  226. }
  227. await Dispatcher.UIThread.InvokeAsync(() =>
  228. desktop.MainWindow.WindowState = WindowState.Minimized);
  229. }
  230. public static async Task Close()
  231. {
  232. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  233. {
  234. return;
  235. }
  236. await Dispatcher.UIThread.InvokeAsync(() =>
  237. desktop.MainWindow.Close());
  238. }
  239. #endregion
  240. #region Window Size and Position
  241. public static void CenterWindowOnScreen(bool horizontal = true, bool top = false)
  242. {
  243. if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
  244. {
  245. return;
  246. }
  247. Dispatcher.UIThread.Post(() =>
  248. {
  249. var window = desktop.MainWindow;
  250. if (window.WindowState is WindowState.Maximized or WindowState.FullScreen)
  251. {
  252. return;
  253. }
  254. ScreenHelper.UpdateScreenSize(window);
  255. var screen = ScreenHelper.ScreenSize;
  256. // Get the size of the window
  257. var windowSize = window.ClientSize;
  258. var x = screen.X;
  259. var y = screen.Y;
  260. // Calculate the position to center the window on the screen
  261. var centeredX = x + (screen.WorkingAreaWidth - windowSize.Width) / 2;
  262. var centeredY = y + (top ? 0 : (screen.WorkingAreaHeight - windowSize.Height) / 2);
  263. // Set the window's new position
  264. window.Position = horizontal
  265. ? new PixelPoint((int)centeredX, (int)centeredY)
  266. : new PixelPoint(window.Position.X, (int)centeredY);
  267. });
  268. }
  269. public static void InitializeWindowSizeAndPosition(Window window)
  270. {
  271. if (Dispatcher.UIThread.CheckAccess())
  272. {
  273. window.Position = new PixelPoint((int)Settings.WindowProperties.Left,
  274. (int)Settings.WindowProperties.Top);
  275. window.Width = Settings.WindowProperties.Width;
  276. window.Height = Settings.WindowProperties.Height;
  277. }
  278. else
  279. {
  280. Dispatcher.UIThread.InvokeAsync(() =>
  281. {
  282. window.Position = new PixelPoint((int)Settings.WindowProperties.Left,
  283. (int)Settings.WindowProperties.Top);
  284. window.Width = Settings.WindowProperties.Width;
  285. window.Height = Settings.WindowProperties.Height;
  286. });
  287. }
  288. }
  289. #endregion
  290. #region Window Drag and Behavior
  291. public static void WindowDragAndDoubleClickBehavior(Window window, PointerPressedEventArgs e, IPlatformWindowService platformWindowService)
  292. {
  293. var currentScreen = ScreenHelper.ScreenSize;
  294. var screen = window.Screens.ScreenFromVisual(window);
  295. if (screen == null)
  296. {
  297. return;
  298. }
  299. if (e.ClickCount == 2 && e.GetCurrentPoint(window).Properties.IsLeftButtonPressed)
  300. {
  301. platformWindowService.MaximizeRestore();
  302. return;
  303. }
  304. window.BeginMoveDrag(e);
  305. if (screen.WorkingArea.Width == currentScreen.WorkingAreaWidth &&
  306. screen.WorkingArea.Height == currentScreen.WorkingAreaHeight && screen.Scaling == currentScreen.Scaling)
  307. {
  308. return;
  309. }
  310. ScreenHelper.UpdateScreenSize(window);
  311. WindowResizing.SetSize(window.DataContext as MainViewModel);
  312. }
  313. public static void WindowDragBehavior(Window window, PointerPressedEventArgs e)
  314. {
  315. var currentScreen = ScreenHelper.ScreenSize;
  316. window.BeginMoveDrag(e);
  317. var screen = window.Screens.ScreenFromVisual(window);
  318. if (screen == null)
  319. {
  320. return;
  321. }
  322. if (screen.WorkingArea.Width == currentScreen.WorkingAreaWidth &&
  323. screen.WorkingArea.Height == currentScreen.WorkingAreaHeight && screen.Scaling == currentScreen.Scaling)
  324. {
  325. return;
  326. }
  327. ScreenHelper.UpdateScreenSize(window);
  328. WindowResizing.SetSize(window.DataContext as MainViewModel);
  329. }
  330. #endregion
  331. }