浏览代码

Fix button cursor placement logic for new R3 rendering.

Refactor: simplify navigation commands by consolidating bindings, removing redundant methods, and improving button press handling logic.
Ruben 3 月之前
父节点
当前提交
d00e55c0cf

+ 2 - 10
src/PicView.Avalonia/Navigation/NavigationManager.cs

@@ -388,15 +388,8 @@ public static class NavigationManager
         }
     }
     public static async Task Iterate(bool next) => await Iterate(next, UIHelper.GetMainView.DataContext as MainViewModel).ConfigureAwait(false);
-
-    /// <summary>
-    ///     Navigates and moves the cursor to the corresponding button.
-    /// </summary>
-    /// <param name="next">True to navigate to the next image, false for the previous image.</param>
-    /// <param name="arrow">True to move cursor to the arrow, false for the button.</param>
-    /// <param name="vm">The main view model instance.</param>
-    /// <returns>A task representing the asynchronous operation.</returns>
-    public static async Task NavigateAndPositionCursor(bool next, bool arrow, MainViewModel vm)
+    
+    public static async Task NavigateOrScrollGallery(bool next, MainViewModel vm)
     {
         if (!CanNavigate(vm))
         {
@@ -410,7 +403,6 @@ public static class NavigationManager
         else
         {
             await Navigate(next, vm);
-            UIHelper.MoveCursorOnButtonClick(next, arrow, vm);
         }
     }
 

+ 5 - 62
src/PicView.Avalonia/UI/UIHelper.cs

@@ -1,11 +1,9 @@
 using System.Diagnostics.CodeAnalysis;
-using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Threading;
 using PicView.Avalonia.CustomControls;
 using PicView.Avalonia.Gallery;
-using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.Views;
 using PicView.Avalonia.Views.UC;
@@ -109,77 +107,22 @@ public static class UIHelper
             await Dispatcher.UIThread.InvokeAsync(() => { GetGalleryView.GalleryListBox.ScrollToHome(); });
         }
     }
-
-    /// <summary>
-    ///     Moves the cursor on the navigation button.
-    /// </summary>
-    /// <param name="next">True to move the cursor to the next button, false for the previous button.</param>
-    /// <param name="arrow">True to move the cursor on the arrow, false to move the cursor on the button.</param>
-    /// <param name="vm">The main view model instance.</param>
-    public static void MoveCursorOnButtonClick(bool next, bool arrow, MainViewModel vm) =>
-        Dispatcher.UIThread.Post(() =>
-        {
-            var buttonName = arrow
-                ? next ? "ClickArrowRight" : "ClickArrowLeft"
-                : next
-                    ? "NextButton"
-                    : "PreviousButton";
-            Control control = arrow
-                ? GetMainView.GetControl<UserControl>(buttonName)
-                : GetBottomBar.GetControl<Button>(buttonName);
-            var point = arrow
-                ? next ? new Point(65, 95) : new Point(15, 95)
-                : new Point(50, 10);
-            var p = control.PointToScreen(point);
-            vm.PlatformService?.SetCursorPos(p.X, p.Y);
-        }, DispatcherPriority.ContextIdle);
-
-    #endregion
-
-    #region Navigation buttons
-
-    /// <summary>
-    /// Navigates to the next image using the bottom navigation button
-    /// </summary>
-    public static async Task NextButtonNavigation() =>
-        await SetButtonIntervalAndNavigate(GetBottomBar?.NextButton, true, false, GetMainView.DataContext as MainViewModel);
-
-    /// <summary>
-    /// Navigates to the previous image using the bottom navigation button
-    /// </summary>
-    public static async Task PreviousButtonNavigation() =>
-        await SetButtonIntervalAndNavigate(GetBottomBar?.PreviousButton, false, false, GetMainView.DataContext as MainViewModel);
-
-    /// <summary>
-    /// Navigates to the next image using the arrow button
-    /// </summary>
-    public static async Task NextArrowButtonNavigation() =>
-        await SetButtonIntervalAndNavigate(GetMainView?.ClickArrowRight?.PolyButton, true, true, GetMainView.DataContext as MainViewModel);
-    /// <inheritdoc cref="NextArrowButtonNavigation(MainViewModel vm)"/>
-    public static async Task PreviousArrowButtonNavigation() =>
-        await SetButtonIntervalAndNavigate(GetMainView?.ClickArrowLeft?.PolyButton, false, true, GetMainView.DataContext as MainViewModel);
-
-    private static async Task SetButtonIntervalAndNavigate(RepeatButton? button, bool isNext, bool isArrow,
-        MainViewModel vm)
+    
+    public static void SetButtonInterval(RepeatButton? button)
     {
         if (button != null)
         {
             button.Interval = (int)TimeSpan.FromSeconds(Settings.UIProperties.NavSpeed).TotalMilliseconds;
         }
-
-        await NavigationManager.NavigateAndPositionCursor(isNext, isArrow, vm);
     }
-
-    private static async Task SetButtonIntervalAndNavigate(IconButton? button, bool isNext, bool isArrow,
-        MainViewModel vm)
+    
+    public static void SetButtonInterval(IconButton? button)   
     {
         if (button != null)
         {
             button.Interval = (int)TimeSpan.FromSeconds(Settings.UIProperties.NavSpeed).TotalMilliseconds;
         }
-
-        await NavigationManager.NavigateAndPositionCursor(isNext, isArrow, vm);
     }
-
+    
     #endregion
 }

+ 5 - 0
src/PicView.Avalonia/ViewModels/MainWindowViewModel.cs

@@ -11,6 +11,11 @@ namespace PicView.Avalonia.ViewModels;
 
 public class MainWindowViewModel : IDisposable
 {
+    public bool IsNavigationButtonLeftClicked { get; set;  }
+    public bool IsNavigationButtonRightClicked { get; set;  }
+    public bool IsClickArrowLeftClicked { get; set;  }
+    public bool IsClickArrowRightClicked { get; set;  }
+    
     public BindableReactiveProperty<Brush?> ImageBackground { get; } = new();
 
     public BindableReactiveProperty<Brush?> ConstrainedImageBackground { get; } = new();

+ 0 - 22
src/PicView.Avalonia/ViewModels/NavigationViewModel.cs

@@ -19,16 +19,6 @@ public class NavigationViewModel : IDisposable
         await NavigationManager.Iterate(next: true).ConfigureAwait(false);
     });
     
-    public ReactiveCommand NextButtonCommand { get; } = new(async (_, _) =>
-    {
-        await UIHelper.NextButtonNavigation();
-    });
-    
-    public ReactiveCommand NextArrowButtonCommand { get; } = new(async (_, _) =>
-    {
-        await UIHelper.NextArrowButtonNavigation();
-    });
-    
     public ReactiveCommand NextFolderCommand { get; } = new(async (_, _) =>
     {
         await NavigationManager.NavigateBetweenDirectories(next: true).ConfigureAwait(false);
@@ -39,14 +29,6 @@ public class NavigationViewModel : IDisposable
     {
         await NavigationManager.Iterate(next: false).ConfigureAwait(false);
     });
-    public ReactiveCommand PreviousButtonCommand { get; } = new(async (_, _) =>
-    {
-        await UIHelper.PreviousButtonNavigation();
-    });
-    public ReactiveCommand PreviousArrowButtonCommand { get; } = new(async (_, _) =>
-    {
-        await UIHelper.PreviousArrowButtonNavigation();
-    });
 
     public ReactiveCommand PreviousFolderCommand { get; } = new(async (_, _) =>
     {
@@ -84,12 +66,8 @@ public class NavigationViewModel : IDisposable
     {
         Disposable.Dispose(ReloadCommand,
             NextCommand,
-            NextButtonCommand,
-            NextArrowButtonCommand,
             NextFolderCommand,
             PreviousCommand,
-            PreviousButtonCommand,
-            PreviousArrowButtonCommand,
             PreviousFolderCommand,
             FirstCommand,
             LastCommand,

+ 2 - 2
src/PicView.Avalonia/Views/BottomBar.axaml

@@ -264,7 +264,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="hover"
-                    Command="{CompiledBinding Navigation.PreviousButtonCommand}"
+                    Command="{CompiledBinding Navigation.PreviousCommand}"
                     Data="{StaticResource FilledArrowLeftGeometry}"
                     Focusable="False"
                     Foreground="{DynamicResource MainTextColor}"
@@ -282,7 +282,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="hover"
-                    Command="{CompiledBinding Navigation.NextButtonCommand}"
+                    Command="{CompiledBinding Navigation.NextCommand}"
                     Data="{StaticResource FilledArrowRightGeometry}"
                     Focusable="False"
                     Foreground="{DynamicResource MainTextColor}"

+ 18 - 0
src/PicView.Avalonia/Views/BottomBar.axaml.cs

@@ -3,6 +3,8 @@ using Avalonia.Controls;
 using Avalonia.Input;
 using Avalonia.Media;
 using PicView.Avalonia.DragAndDrop;
+using PicView.Avalonia.UI;
+using PicView.Avalonia.ViewModels;
 using PicView.Avalonia.WindowBehavior;
 
 namespace PicView.Avalonia.Views;
@@ -65,6 +67,22 @@ public partial class BottomBar : UserControl
                 NextButton.Foreground = new SolidColorBrush(color);
                 PreviousButton.Foreground = new SolidColorBrush(color);
             }
+
+            if (DataContext is not MainViewModel vm)
+            {
+                return;
+            }
+
+            PreviousButton.Click += (_, _) =>
+            {
+                vm.MainWindow.IsNavigationButtonLeftClicked = true;
+                UIHelper.SetButtonInterval(PreviousButton);
+            };
+            NextButton.Click += (_, _) =>
+            {
+                vm.MainWindow.IsNavigationButtonRightClicked = true;
+                UIHelper.SetButtonInterval(NextButton);
+            };
         };
     }
 

+ 1 - 1
src/PicView.Avalonia/Views/UC/Buttons/ClickArrowLeft.axaml

@@ -18,7 +18,7 @@
         BorderBrush="{StaticResource MainBorderColor}"
         BorderThickness="0,1,1,1"
         Classes="hover"
-        Command="{CompiledBinding Navigation.PreviousArrowButtonCommand}"
+        Command="{CompiledBinding Navigation.PreviousCommand}"
         CornerRadius="0, 2, 2, 0"
         Height="60"
         HorizontalAlignment="Left"

+ 6 - 0
src/PicView.Avalonia/Views/UC/Buttons/ClickArrowLeft.axaml.cs

@@ -1,4 +1,5 @@
 using Avalonia.Controls;
+using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 
 namespace PicView.Avalonia.Views.UC.Buttons;
@@ -14,6 +15,11 @@ public partial class ClickArrowLeft : UserControl
                 return;
             }
             PointerWheelChanged += async (_, e) => await ImageViewer.PreviewOnPointerWheelChanged(this, e);
+            PolyButton.Click += (_, _) =>
+            {
+                vm.MainWindow.IsClickArrowLeftClicked = true;
+                UIHelper.SetButtonInterval(PolyButton);
+            };
         };
     }
 }

+ 1 - 1
src/PicView.Avalonia/Views/UC/Buttons/ClickArrowRight.axaml

@@ -18,7 +18,7 @@
         BorderBrush="{StaticResource MainBorderColor}"
         BorderThickness="1,1,0,1"
         Classes="hover"
-        Command="{CompiledBinding Navigation.NextArrowButtonCommand}"
+        Command="{CompiledBinding Navigation.NextCommand}"
         CornerRadius="2,0,0,2"
         Height="60"
         HorizontalAlignment="Right"

+ 6 - 0
src/PicView.Avalonia/Views/UC/Buttons/ClickArrowRight.axaml.cs

@@ -1,4 +1,5 @@
 using Avalonia.Controls;
+using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 
 namespace PicView.Avalonia.Views.UC.Buttons;
@@ -14,6 +15,11 @@ public partial class ClickArrowRight : UserControl
                 return;
             }
             PointerWheelChanged += async (_, e) => await ImageViewer.PreviewOnPointerWheelChanged(this, e);
+            PolyButton.Click += (_, _) =>
+            {
+                vm.MainWindow.IsClickArrowRightClicked = true;
+                UIHelper.SetButtonInterval(PolyButton);
+            };
         };
     }
 }

+ 40 - 0
src/PicView.Avalonia/WindowBehavior/WindowResizing.cs

@@ -44,6 +44,46 @@ public static class WindowResizing
         var y = (size.OldValue.Value.Height - size.NewValue.Value.Height) / 2;
 
         window.Position = new PixelPoint(window.Position.X + (int)x, window.Position.Y + (int)y);
+        if (window.DataContext is not MainViewModel vm)
+        {
+            return;
+        }
+
+        if (vm.MainWindow.IsNavigationButtonLeftClicked)
+        {
+            var leftButton = UIHelper.GetBottomBar.GetControl<Button>("PreviousButton");
+            var point = new Point(50, 10);
+            var p = leftButton.PointToScreen(point);
+            vm.PlatformService?.SetCursorPos(p.X, p.Y);
+            vm.MainWindow.IsNavigationButtonLeftClicked = false;
+        }
+
+        if (vm.MainWindow.IsNavigationButtonRightClicked)
+        {
+            var rightButton = UIHelper.GetBottomBar.GetControl<Button>("NextButton");
+            var point = new Point(50, 10);
+            var p = rightButton.PointToScreen(point);
+            vm.PlatformService?.SetCursorPos(p.X, p.Y);
+            vm.MainWindow.IsNavigationButtonRightClicked = false;
+        }
+
+        if (vm.MainWindow.IsClickArrowLeftClicked)
+        {
+            var clickArrowLeft = UIHelper.GetMainView.GetControl<UserControl>("ClickArrowLeft");
+            var point = new Point(15, 95);
+            var p = clickArrowLeft.PointToScreen(point);
+            vm.PlatformService?.SetCursorPos(p.X, p.Y);
+            vm.MainWindow.IsClickArrowLeftClicked = false;
+        }
+
+        if (vm.MainWindow.IsClickArrowRightClicked)
+        {
+            var clickArrowRight = UIHelper.GetMainView.GetControl<UserControl>("ClickArrowRight");
+            var point = new Point(65, 95);
+            var p = clickArrowRight.PointToScreen(point);
+            vm.PlatformService?.SetCursorPos(p.X, p.Y);
+            vm.MainWindow.IsClickArrowRightClicked = false;
+        }
     }
 
     #endregion