Bläddra i källkod

Update window centering logic and fullscreen logic for multi monitor scenarios #198

Ruben 8 månader sedan
förälder
incheckning
a3f7f7a649

+ 5 - 2
src/PicView.Avalonia/UI/ScreenHelper.cs

@@ -35,14 +35,15 @@ public static class ScreenHelper
         {
             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
+                // TODO: Get macOS dock size
             }
             var screen = window.Screens.ScreenFromVisual(window);
         
             var monitorWidth = screen.WorkingArea.Width / screen.Scaling;
             var monitorHeight = screen.WorkingArea.Height / screen.Scaling;
             
-            var width = window.Width / screen.Scaling;
-            var height = window.Height / screen.Scaling;
+            var width = screen.Bounds.Width / screen.Scaling;
+            var height = screen.Bounds.Height / screen.Scaling;
         
             ScreenSize = new ScreenSize           
             {
@@ -51,6 +52,8 @@ public static class ScreenHelper
                 Width = width,
                 Height = height,
                 Scaling = screen.Scaling,
+                X = screen.Bounds.X,
+                Y = screen.Bounds.Y
             };
         }
     }

+ 23 - 25
src/PicView.Avalonia/WindowBehavior/WindowFunctions.cs

@@ -250,16 +250,25 @@ public static class WindowFunctions
         if (Settings.WindowProperties.Fullscreen)
         {
             vm.IsFullscreen = false;
-            await Dispatcher.UIThread.InvokeAsync(() =>
-                desktop.MainWindow.WindowState = WindowState.Normal);
+
             if (saveSettings)
             {
                 Settings.WindowProperties.Fullscreen = false;
             }
 
-            WindowResizing.RestoreSize(desktop.MainWindow);
+            if (Settings.WindowProperties.AutoFit)
+            {
+                await Dispatcher.UIThread.InvokeAsync(() =>
+                    desktop.MainWindow.WindowState = WindowState.Normal);
+                CenterWindowOnScreen();
+            }
+            else
+            {
+                WindowResizing.RestoreSize(desktop.MainWindow);
+            }
+
             Restore(vm, desktop);
-            
+
             if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
             {
                 vm.BottomCornerRadius = new CornerRadius(0, 0, 8, 8);
@@ -347,18 +356,17 @@ public static class WindowFunctions
         Dispatcher.UIThread.InvokeAsync(() =>
             desktop.MainWindow.WindowState = WindowState.Normal);
         vm.IsUIShown = Settings.UIProperties.ShowInterface;
-        InitializeWindowSizeAndPosition(desktop.MainWindow);
         
         if (Settings.WindowProperties.AutoFit)
         {
             vm.SizeToContent = SizeToContent.WidthAndHeight;
             vm.CanResize = false;
-            CenterWindowOnScreen();
         }
         else
         {
             vm.SizeToContent = SizeToContent.Manual;
             vm.CanResize = true;
+            InitializeWindowSizeAndPosition(desktop.MainWindow);
         }
         WindowResizing.SetSize(vm);
     }
@@ -540,32 +548,22 @@ public static class WindowFunctions
         Dispatcher.UIThread.Post(() =>
         {
             var window = desktop.MainWindow;
-
-            // Get the screen that the window is currently on
-            var screens = window.Screens;
-            var screen = screens.ScreenFromVisual(window);
-
-            if (screen == null)
+            if (window.WindowState is WindowState.Maximized or WindowState.FullScreen)
             {
-                return; // No screen found (edge case)
+                return;
             }
-
-            // Get the scaling factor of the screen (DPI scaling)
-            var scalingFactor = screen.Scaling;
-
-            // Get the current screen's bounds (in physical pixels, not adjusted for scaling)
-            var screenBounds = screen.WorkingArea;
-
-            // Calculate the actual bounds in logical units (adjusting for scaling)
-            var screenWidth = screenBounds.Width / scalingFactor;
-            var screenHeight = screenBounds.Height / scalingFactor;
+            ScreenHelper.UpdateScreenSize(window);
+            var screen = ScreenHelper.ScreenSize;
 
             // Get the size of the window
             var windowSize = window.ClientSize;
+            
+            var x = screen.X;
+            var y = screen.Y;
 
             // Calculate the position to center the window on the screen
-            var centeredX = screenBounds.X + (screenWidth - windowSize.Width) / 2;
-            var centeredY = screenBounds.Y + (screenHeight - windowSize.Height) / 2;
+            var centeredX = x + (screen.WorkingAreaWidth - windowSize.Width) / 2;
+            var centeredY = y + (screen.WorkingAreaHeight - windowSize.Height) / 2;
 
             // Set the window's new position
             window.Position = horizontal

+ 3 - 0
src/PicView.Core/Sizing/ScreenSize.cs

@@ -18,6 +18,9 @@ public readonly record struct ScreenSize
     public double Width { get; init; }
     public double Height { get; init; }
     
+    public double X { get; init; }
+    public double Y { get; init; }
+    
     /// <summary>
     /// Gets the DPI scaling factor of the screen.
     /// </summary>