Browse Source

Close bottom gallery when entering crop mode, and reopen it when exiting crop mode. Fixes incorrect layout.

Ruben 8 months ago
parent
commit
d0aa0b36ea

+ 22 - 4
src/PicView.Avalonia/Crop/CropFunctions.cs

@@ -3,6 +3,8 @@ using Avalonia.Media.Imaging;
 using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.Views.UC;
+using PicView.Avalonia.WindowBehavior;
+using PicView.Core.Gallery;
 using PicView.Core.Localization;
 
 namespace PicView.Avalonia.Crop;
@@ -12,7 +14,7 @@ public static class CropFunctions
     public static bool IsCropping {get; private set;} 
     
     /// <summary>
-    /// Initializes the cropping functionality by setting up the ImageCropperViewModel 
+    /// Starts the cropping functionality by setting up the ImageCropperViewModel 
     /// and adding the CropControl to the main view.
     /// </summary>
     /// <param name="vm">The main view model instance containing image properties and state.</param>
@@ -21,7 +23,7 @@ public static class CropFunctions
     /// If conditions are met, it configures the crop control with the appropriate dimensions
     /// and updates the view model's title and tooltip to reflect the cropping state.
     /// </remarks>
-    public static void Init(MainViewModel vm)
+    public static void StartCropControl(MainViewModel vm)
     {
         if (!DetermineIfShouldBeEnabled(vm))
         {
@@ -31,6 +33,15 @@ public static class CropFunctions
         {
             return;
         }
+        // Hide bottom gallery when entering crop mode
+        if (Settings.Gallery.IsBottomGalleryShown)
+        {
+            vm.GalleryMode = GalleryMode.Closed;
+            // Reset setting before resizing
+            Settings.Gallery.IsBottomGalleryShown = false;
+            WindowResizing.SetSize(vm);
+            Settings.Gallery.IsBottomGalleryShown = true;
+        }
         var size = new Size(vm.ImageWidth, vm.ImageHeight);
         var cropperViewModel = new ImageCropperViewModel(bitmap)
         {
@@ -43,8 +54,9 @@ public static class CropFunctions
             DataContext = cropperViewModel,
             Width = size.Width,
             Height = size.Height,
+            Margin = new Thickness(0)
         };
-        UIHelper.GetMainView.MainGrid.Children.Add(cropControl);
+        vm.CurrentView = cropControl;
         
         IsCropping = true;
         vm.Title = TranslationHelper.Translation.CropMessage;
@@ -53,7 +65,13 @@ public static class CropFunctions
     
     public static void CloseCropControl(MainViewModel vm)
     {
-        UIHelper.GetMainView.MainGrid.Children.Remove(UIHelper.GetMainView.MainGrid.Children.OfType<CropControl>().First());
+        if (Settings.Gallery.IsBottomGalleryShown)
+        {
+            vm.GalleryMode = GalleryMode.ClosedToBottom;
+            WindowResizing.SetSize(vm);
+        }
+
+        vm.CurrentView = vm.ImageViewer;
         IsCropping = false;
         SetTitleHelper.RefreshTitle(vm);
     }

+ 5 - 1
src/PicView.Avalonia/Input/MainKeyboardShortcuts.cs

@@ -4,6 +4,7 @@ using Avalonia.Input;
 using PicView.Avalonia.Crop;
 using PicView.Avalonia.CustomControls;
 using PicView.Avalonia.UI;
+using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.Views.UC;
 
 namespace PicView.Avalonia.Input;
@@ -123,7 +124,10 @@ public static class MainKeyboardShortcuts
         
         if (CropFunctions.IsCropping)
         {
-            await UIHelper.GetMainView.MainGrid.Children.OfType<CropControl>().FirstOrDefault().KeyDownHandler(null,e);
+            if (UIHelper.GetMainView.MainGrid.DataContext is MainViewModel { CurrentView: CropControl cropControl })
+            {
+                await cropControl.KeyDownHandler(null, e);
+            }
             return;
         }
 

+ 1 - 1
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -740,7 +740,7 @@ public static class FunctionsHelper
 
     public static async Task Crop()
     {
-        await Dispatcher.UIThread.InvokeAsync(() => CropFunctions.Init(Vm));
+        await Dispatcher.UIThread.InvokeAsync(() => CropFunctions.StartCropControl(Vm));
     }
 
     public static Task Flip()