StartUpHelper.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Controls.Primitives;
  4. using PicView.Avalonia.Keybindings;
  5. using PicView.Avalonia.Navigation;
  6. using PicView.Avalonia.ViewModels;
  7. using PicView.Avalonia.Views;
  8. using PicView.Core.Config;
  9. using PicView.Core.Gallery;
  10. using PicView.Core.Localization;
  11. using StartUpMenu = PicView.Avalonia.Views.StartUpMenu;
  12. namespace PicView.Avalonia.UI;
  13. public static class StartUpHelper
  14. {
  15. public static void Start(MainViewModel vm, bool settingsExists, IClassicDesktopStyleApplicationLifetime desktop, Window w)
  16. {
  17. if (!settingsExists)
  18. {
  19. // Fixes incorrect window
  20. w.Height = w.MinHeight;
  21. w.Width = w.MinWidth;
  22. WindowHelper.CenterWindowOnScreen();
  23. vm.CanResize = true;
  24. vm.IsAutoFit = false;
  25. }
  26. else
  27. {
  28. if (SettingsHelper.Settings.WindowProperties.Fullscreen)
  29. {
  30. WindowHelper.Fullscreen(vm, desktop);
  31. }
  32. else if (SettingsHelper.Settings.WindowProperties.Maximized)
  33. {
  34. WindowHelper.Maximize();
  35. }
  36. else if (SettingsHelper.Settings.WindowProperties.AutoFit)
  37. {
  38. desktop.MainWindow.WindowStartupLocation = WindowStartupLocation.CenterScreen;
  39. vm.SizeToContent = SizeToContent.WidthAndHeight;
  40. vm.CanResize = false;
  41. vm.IsAutoFit = true;
  42. if (SettingsHelper.Settings.UIProperties.ShowInterface)
  43. {
  44. vm.IsTopToolbarShown = true;
  45. vm.IsBottomToolbarShown = SettingsHelper.Settings.UIProperties.ShowBottomNavBar;
  46. }
  47. }
  48. else
  49. {
  50. vm.CanResize = true;
  51. vm.IsAutoFit = false;
  52. WindowHelper.InitializeWindowSizeAndPosition(w);
  53. if (SettingsHelper.Settings.UIProperties.ShowInterface)
  54. {
  55. vm.IsTopToolbarShown = true;
  56. vm.IsBottomToolbarShown = SettingsHelper.Settings.UIProperties.ShowBottomNavBar;
  57. }
  58. }
  59. }
  60. w.Show();
  61. ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(w);
  62. UIHelper.SetControls(desktop);
  63. vm.IsLoading = true;
  64. vm.UpdateLanguage();
  65. vm.GetFlipped = vm.Flip;
  66. if (SettingsHelper.Settings.Zoom.ScrollEnabled)
  67. {
  68. vm.ToggleScrollBarVisibility = ScrollBarVisibility.Visible;
  69. vm.GetScrolling = TranslationHelper.Translation.ScrollingEnabled;
  70. vm.IsScrollingEnabled = true;
  71. }
  72. else
  73. {
  74. vm.ToggleScrollBarVisibility = ScrollBarVisibility.Disabled;
  75. vm.GetScrolling = TranslationHelper.Translation.ScrollingDisabled;
  76. vm.IsScrollingEnabled = false;
  77. }
  78. vm.GetBottomGallery = vm.IsGalleryShown ?
  79. TranslationHelper.Translation.HideBottomGallery :
  80. TranslationHelper.Translation.ShowBottomGallery;
  81. if (SettingsHelper.Settings.WindowProperties.TopMost)
  82. {
  83. desktop.MainWindow.Topmost = true;
  84. }
  85. vm.ImageViewer = new ImageViewer();
  86. var args = Environment.GetCommandLineArgs();
  87. if (args.Length > 1)
  88. {
  89. Task.Run(() => QuickLoad.QuickLoadAsync(vm, args[1]));
  90. }
  91. else if (SettingsHelper.Settings.StartUp.OpenLastFile)
  92. {
  93. if (string.IsNullOrWhiteSpace(SettingsHelper.Settings.StartUp.LastFile))
  94. {
  95. vm.CurrentView = new StartUpMenu();
  96. vm.IsLoading = false;
  97. }
  98. else
  99. {
  100. Task.Run(() => QuickLoad.QuickLoadAsync(vm, SettingsHelper.Settings.StartUp.LastFile));
  101. }
  102. }
  103. else
  104. {
  105. vm.CurrentView = new StartUpMenu();
  106. vm.IsLoading = false;
  107. }
  108. if (!settingsExists)
  109. {
  110. vm.GetBottomGalleryItemHeight = GalleryDefaults.DefaultBottomGalleryHeight;
  111. vm.GetFullGalleryItemHeight = GalleryDefaults.DefaultFullGalleryHeight;
  112. }
  113. // Set default gallery sizes if they are out of range or upgrading from an old version
  114. if (vm.GetBottomGalleryItemHeight < vm.MinBottomGalleryItemHeight ||
  115. vm.GetBottomGalleryItemHeight > vm.MaxBottomGalleryItemHeight)
  116. {
  117. vm.GetBottomGalleryItemHeight = GalleryDefaults.DefaultBottomGalleryHeight;
  118. }
  119. if (vm.GetFullGalleryItemHeight < vm.MinFullGalleryItemHeight ||
  120. vm.GetFullGalleryItemHeight > vm.MaxFullGalleryItemHeight)
  121. {
  122. vm.GetFullGalleryItemHeight = GalleryDefaults.DefaultFullGalleryHeight;
  123. }
  124. if (!settingsExists)
  125. {
  126. if (string.IsNullOrWhiteSpace(SettingsHelper.Settings.Gallery.BottomGalleryStretchMode))
  127. {
  128. SettingsHelper.Settings.Gallery.BottomGalleryStretchMode = "UniformToFill";
  129. }
  130. if (string.IsNullOrWhiteSpace(SettingsHelper.Settings.Gallery.FullGalleryStretchMode))
  131. {
  132. SettingsHelper.Settings.Gallery.FullGalleryStretchMode = "UniformToFill";
  133. }
  134. }
  135. ThemeHelper.SetBackground(vm);
  136. Task.Run(KeybindingsHelper.LoadKeybindings);
  137. vm.GetLooping = SettingsHelper.Settings.UIProperties.Looping
  138. ? TranslationHelper.Translation.LoopingEnabled
  139. : TranslationHelper.Translation.LoopingDisabled;
  140. vm.GetScrolling = SettingsHelper.Settings.Zoom.ScrollEnabled
  141. ? TranslationHelper.Translation.ScrollingEnabled
  142. : TranslationHelper.Translation.ScrollingDisabled;
  143. vm.GetCtrlZoom = SettingsHelper.Settings.Zoom.CtrlZoom
  144. ? TranslationHelper.Translation.CtrlToZoom
  145. : TranslationHelper.Translation.ScrollToZoom;
  146. UIHelper.AddMenus();
  147. UIHelper.AddToolTipMessage();
  148. w.KeyDown += async (_, e) => await MainKeyboardShortcuts.MainWindow_KeysDownAsync(e).ConfigureAwait(false);
  149. w.KeyUp += (_, e) => MainKeyboardShortcuts.MainWindow_KeysUp(e);
  150. w.PointerPressed += async (_, e) => await MouseShortcuts.MainWindow_PointerPressed(e).ConfigureAwait(false);
  151. }
  152. }