Slideshow.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. using Avalonia;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using PicView.Avalonia.Gallery;
  4. using PicView.Avalonia.Input;
  5. using PicView.Avalonia.UI;
  6. using PicView.Avalonia.ViewModels;
  7. using PicView.Avalonia.WindowBehavior;
  8. using PicView.Core.Config;
  9. using PicView.Core.Gallery;
  10. using Timer = System.Timers.Timer;
  11. namespace PicView.Avalonia.Navigation;
  12. public static class Slideshow
  13. {
  14. public static bool IsRunning => _timer is not null && _timer.Enabled;
  15. private static Timer? _timer;
  16. public static async Task StartSlideshow(MainViewModel vm)
  17. {
  18. if (!InitiateAndStart(vm))
  19. {
  20. return;
  21. }
  22. await Start(vm, TimeSpan.FromSeconds(SettingsHelper.Settings.UIProperties.SlideShowTimer).TotalMilliseconds);
  23. }
  24. public static async Task StartSlideshow(MainViewModel vm, int milliseconds)
  25. {
  26. if (!InitiateAndStart(vm))
  27. {
  28. return;
  29. }
  30. await Start(vm, milliseconds);
  31. }
  32. public static void StopSlideshow(MainViewModel vm)
  33. {
  34. if (_timer is null)
  35. {
  36. return;
  37. }
  38. if (!SettingsHelper.Settings.WindowProperties.Fullscreen)
  39. {
  40. WindowFunctions.Restore(vm, Application.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime);
  41. if (SettingsHelper.Settings.WindowProperties.AutoFit)
  42. {
  43. WindowFunctions.CenterWindowOnScreen();
  44. }
  45. }
  46. if (SettingsHelper.Settings.Gallery.IsBottomGalleryShown)
  47. {
  48. vm.GalleryMode = GalleryMode.ClosedToBottom;
  49. }
  50. _timer.Stop();
  51. _timer = null;
  52. vm.PlatformService.EnableScreensaver();
  53. }
  54. private static bool InitiateAndStart(MainViewModel vm)
  55. {
  56. if (!NavigationHelper.CanNavigate(vm))
  57. {
  58. return false;
  59. }
  60. if (_timer is null)
  61. {
  62. _timer = new Timer
  63. {
  64. Enabled = true,
  65. };
  66. _timer.Elapsed += async (_, _) =>
  67. {
  68. // TODO: add animation
  69. // E.g. https://codepen.io/arrive/pen/EOGyzK
  70. // https://docs.avaloniaui.net/docs/guides/graphics-and-animation/page-transitions/how-to-create-a-custom-page-transition
  71. // https://docs.avaloniaui.net/docs/guides/graphics-and-animation/page-transitions/page-slide-transition
  72. // https://docs.avaloniaui.net/docs/reference/controls/transitioningcontentcontrol
  73. await NavigationHelper.Navigate(true, vm).ConfigureAwait(false);
  74. };
  75. }
  76. else if (_timer.Enabled)
  77. {
  78. if (!MainKeyboardShortcuts.IsKeyHeldDown)
  79. {
  80. _timer = null;
  81. }
  82. return false;
  83. }
  84. return true;
  85. }
  86. private static async Task Start(MainViewModel vm, double seconds)
  87. {
  88. _timer.Interval = seconds;
  89. _timer.Start();
  90. vm.PlatformService.DisableScreensaver();
  91. UIHelper.CloseMenus(vm);
  92. if (!SettingsHelper.Settings.WindowProperties.Fullscreen)
  93. {
  94. await WindowFunctions.ToggleFullscreen(vm, false);
  95. }
  96. if (GalleryFunctions.IsFullGalleryOpen || SettingsHelper.Settings.Gallery.IsBottomGalleryShown)
  97. {
  98. vm.GalleryMode = GalleryMode.BottomToClosed;
  99. }
  100. }
  101. }