CropFunctions.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. using Avalonia;
  2. using Avalonia.Media.Imaging;
  3. using Avalonia.Threading;
  4. using PicView.Avalonia.Functions;
  5. using PicView.Avalonia.UI;
  6. using PicView.Avalonia.ViewModels;
  7. using PicView.Avalonia.Views.UC;
  8. using PicView.Avalonia.WindowBehavior;
  9. using PicView.Core.Gallery;
  10. using PicView.Core.ImageDecoding;
  11. using PicView.Core.Localization;
  12. namespace PicView.Avalonia.Crop;
  13. public static class CropFunctions
  14. {
  15. public static bool IsCropping { get; private set; }
  16. /// <summary>
  17. /// Starts the cropping functionality by setting up the ImageCropperViewModel
  18. /// and adding the CropControl to the main view.
  19. /// </summary>
  20. /// <param name="vm">The main view model instance containing image properties and state.</param>
  21. /// <remarks>
  22. /// This method checks if cropping can be enabled and if the image source is valid.
  23. /// If conditions are met, it configures the crop control with the appropriate dimensions
  24. /// and updates the view model's title and tooltip to reflect the cropping state.
  25. /// </remarks>
  26. public static async Task StartCropControlAsync(MainViewModel vm)
  27. {
  28. if (!DetermineIfShouldBeEnabled(vm))
  29. {
  30. return;
  31. }
  32. if (vm?.PicViewer.ImageSource.CurrentValue is not Bitmap bitmap)
  33. {
  34. return;
  35. }
  36. var isBottomGalleryShown = Settings.Gallery.IsBottomGalleryShown;
  37. // Hide bottom gallery when entering crop mode
  38. if (isBottomGalleryShown)
  39. {
  40. vm.Gallery.GalleryMode.Value = GalleryMode.Closed;
  41. // Reset setting before resizing
  42. Settings.Gallery.IsBottomGalleryShown = false;
  43. await WindowResizing.SetSizeAsync(vm);
  44. }
  45. var size = new Size(vm.PicViewer.ImageWidth.CurrentValue, vm.PicViewer.ImageHeight.CurrentValue);
  46. await Dispatcher.UIThread.InvokeAsync(() =>
  47. {
  48. vm.Crop = new ImageCropperViewModel(bitmap);
  49. vm.Crop.ImageWidth.Value = size.Width;
  50. vm.Crop.ImageHeight.Value = size.Height;
  51. vm.Crop.AspectRatio.Value = vm.PicViewer.AspectRatio.CurrentValue;
  52. var cropControl = new CropControl
  53. {
  54. DataContext = vm,
  55. Width = size.Width,
  56. Height = size.Height,
  57. Margin = new Thickness(0)
  58. };
  59. vm.MainWindow.CurrentView.Value = cropControl;
  60. });
  61. IsCropping = true;
  62. vm.PicViewer.Title.Value = TranslationManager.Translation.CropMessage!;
  63. vm.PicViewer.TitleTooltip.Value = TranslationManager.Translation.CropMessage!;
  64. await FunctionsMapper.CloseMenus();
  65. if (isBottomGalleryShown)
  66. {
  67. Settings.Gallery.IsBottomGalleryShown = true;
  68. }
  69. }
  70. public static void CloseCropControl(MainViewModel vm)
  71. {
  72. if (Settings.Gallery.IsBottomGalleryShown)
  73. {
  74. if (vm.Gallery is {} gallery)
  75. {
  76. gallery.GalleryMode.Value = GalleryMode.ClosedToBottom;
  77. }
  78. WindowResizing.SetSize(vm);
  79. }
  80. vm.MainWindow.CurrentView.Value = vm.ImageViewer;
  81. IsCropping = false;
  82. TitleManager.SetTitle(vm);
  83. // Reset image type to fix issue with animated images
  84. switch (vm.PicViewer.ImageType.CurrentValue)
  85. {
  86. case ImageType.AnimatedWebp:
  87. vm.PicViewer.ImageType.Value = ImageType.Bitmap;
  88. vm.PicViewer.ImageType.Value = ImageType.AnimatedWebp;
  89. break;
  90. case ImageType.AnimatedGif:
  91. vm.PicViewer.ImageType.Value = ImageType.Bitmap;
  92. vm.PicViewer.ImageType.Value = ImageType.AnimatedGif;
  93. break;
  94. }
  95. vm.Crop = null;
  96. }
  97. public static bool DetermineIfShouldBeEnabled(MainViewModel vm)
  98. {
  99. if (IsCropping)
  100. {
  101. return false;
  102. }
  103. if (vm?.PicViewer.ImageSource.CurrentValue is not Bitmap || Settings.ImageScaling.ShowImageSideBySide)
  104. {
  105. vm.PicViewer.ShouldCropBeEnabled.Value = false;
  106. return false;
  107. }
  108. if (DialogManager.IsDialogOpen)
  109. {
  110. return false;
  111. }
  112. if (vm.MainWindow.IsEditableTitlebarOpen.CurrentValue)
  113. {
  114. return false;
  115. }
  116. if (vm.GlobalSettings.RotationAngle.CurrentValue is 0 && vm.PicViewer.ScaleX.CurrentValue is 1)
  117. {
  118. vm.PicViewer.ShouldCropBeEnabled.Value = true;
  119. return true;
  120. }
  121. vm.PicViewer.ShouldCropBeEnabled.Value = false;
  122. return false;
  123. }
  124. }