Browse Source

Refactor and adjustments for dragging window between multiple monitor of different sizes and scaling #26

Ruben 1 year ago
parent
commit
f3384fcf19

+ 1 - 1
src/PicView.Avalonia.Win32/Views/WinMainWindow.axaml.cs

@@ -46,7 +46,7 @@ public partial class WinMainWindow : Window
             });
             ScalingChanged += (_, _) =>
             {
-                ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(this);
+                ScreenHelper.UpdateScreenSize(this);
                 WindowHelper.SetSize(DataContext as MainViewModel);
             };
             PointerExited += (_, _) =>

+ 17 - 14
src/PicView.Avalonia/UI/ScreenHelper.cs

@@ -1,8 +1,7 @@
 using Avalonia.Controls;
 
 namespace PicView.Avalonia.UI;
-
-public class ScreenSize(double workingAreaWidth, double workingAreaHeight, double scaling)
+public readonly struct ScreenSize(double workingAreaWidth, double workingAreaHeight, double scaling)
 {
     public double WorkingAreaWidth { get; init; } = workingAreaWidth;
     public double WorkingAreaHeight { get; init; } = workingAreaHeight;
@@ -11,20 +10,24 @@ public class ScreenSize(double workingAreaWidth, double workingAreaHeight, doubl
 
 public static class ScreenHelper
 {
-    public static ScreenSize? ScreenSize { get; set; }
-    public static ScreenSize? GetScreenSize(Window window)
+    private static readonly Lock _lock = new();
+    public static ScreenSize ScreenSize { get; private set; }
+    public static void UpdateScreenSize(Window window)
     {
-        var screen = window.Screens.ScreenFromVisual(window);
+        // Need to lock it to prevent multiple calls
+        lock (_lock)
+        {
+            var screen = window.Screens.ScreenFromVisual(window);
         
-        var monitorWidth = screen.Bounds.Width / screen.Scaling;
-        var monitorHeight = screen.Bounds.Height / screen.Scaling;
-
+            var monitorWidth = screen.Bounds.Width / screen.Scaling;
+            var monitorHeight = screen.Bounds.Height / screen.Scaling;
         
-        return new ScreenSize(monitorWidth, monitorHeight, screen.Scaling)
-        {
-            WorkingAreaWidth = monitorWidth,
-            WorkingAreaHeight = monitorHeight,
-            Scaling = screen.Scaling,
-        };
+            ScreenSize = new ScreenSize(monitorWidth, monitorHeight, screen.Scaling)
+            {
+                WorkingAreaWidth = monitorWidth,
+                WorkingAreaHeight = monitorHeight,
+                Scaling = screen.Scaling,
+            };
+        }
     }
 }

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

@@ -64,7 +64,7 @@ public static class StartUpHelper
 
         w.Show();
         vm.IsLoading = true;
-        ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(w);
+        ScreenHelper.UpdateScreenSize(w);
         UIHelper.SetControls(desktop);
         vm.UpdateLanguage();
         vm.GetFlipped = vm.Flip;

+ 23 - 3
src/PicView.Avalonia/UI/WindowHelper.cs

@@ -28,15 +28,35 @@ public static class WindowHelper
             _ = MaximizeRestore();
             return;
         }
-
+        
+        var currentScreen = ScreenHelper.ScreenSize;
         window.BeginMoveDrag(e);
-        ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(window);
+        var screen = window.Screens.ScreenFromVisual(window);
+        if (screen != null)
+        {
+            if (screen.WorkingArea.Width != currentScreen.WorkingAreaWidth ||
+                screen.WorkingArea.Height != currentScreen.WorkingAreaHeight || screen.Scaling != currentScreen.Scaling)
+            {
+                ScreenHelper.UpdateScreenSize(window);
+                SetSize(window.DataContext as MainViewModel);
+            }
+        }
     }
     
     public static void WindowDragBehavior(Window window, PointerPressedEventArgs e)
     {
+        var currentScreen = ScreenHelper.ScreenSize;
         window.BeginMoveDrag(e);
-        ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(window);
+        var screen = window.Screens.ScreenFromVisual(window);
+        if (screen != null)
+        {
+            if (screen.WorkingArea.Width != currentScreen.WorkingAreaWidth ||
+                screen.WorkingArea.Height != currentScreen.WorkingAreaHeight || screen.Scaling != currentScreen.Scaling)
+            {
+                ScreenHelper.UpdateScreenSize(window);
+                SetSize(window.DataContext as MainViewModel);
+            }
+        }
     }
 
     public static void InitializeWindowSizeAndPosition(Window window)

+ 1 - 1
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -56,7 +56,7 @@ public partial class MainView : UserControl
         if (MainKeyboardShortcuts.ShiftDown)
         {
             var hostWindow = (Window)VisualRoot!;
-            WindowHelper.WindowDragAndDoubleClickBehavior(hostWindow, e);
+            WindowHelper.WindowDragBehavior(hostWindow, e);
         }
         
         MainKeyboardShortcuts.ClearKeyDownModifiers();