Browse Source

[Avalonia] Multi monitor fix, refactor, misc

Ruben 1 year ago
parent
commit
1df6767098

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

@@ -47,7 +47,6 @@ public partial class WinMainWindow : Window
                 }
             });
         };
-        PointerPressed += (_, e) => MoveWindow(e);
         if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
         {
             return;
@@ -59,15 +58,6 @@ public partial class WinMainWindow : Window
         };
     }
 
-    private void MoveWindow(PointerPressedEventArgs e)
-    {
-        if (VisualRoot is null) { return; }
-        if (!MainKeyboardShortcuts.ShiftDown) { return; }
-
-        var hostWindow = (Window)VisualRoot;
-        hostWindow?.BeginMoveDrag(e);
-    }
-
     protected override async void OnClosing(WindowClosingEventArgs e)
     {
         e.Cancel = true;

+ 1 - 0
src/PicView.Avalonia/Navigation/NavigationHelper.cs

@@ -276,6 +276,7 @@ public static class NavigationHelper
             };
             await client.StartDownloadAsync().ConfigureAwait(false);
             destination = httpDownload.DownloadPath;
+            // TODO add `destination` to be cleared at application exit
         }
         catch (Exception e)
         {

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

@@ -5,21 +5,21 @@ using Avalonia.Platform;
 
 namespace PicView.Avalonia.UI;
 
-public struct ScreenSize(int width, int height, int workingAreaWidth, int workingAreaHeight, double scaling)
+public readonly struct ScreenSize(int width, int height, int workingAreaWidth, int workingAreaHeight, double scaling)
 {
-    public int Width { get; set; } = width;
-    public int Height { get; set; } = height;
-    public int WorkingAreaWidth { get; set; } = workingAreaWidth;
-    public int WorkingAreaHeight { get; set; } = workingAreaHeight;
-    public double Scaling { get; set; } = scaling;
+    public int Width { get; init; } = width;
+    public int Height { get; init; } = height;
+    public int WorkingAreaWidth { get; init; } = workingAreaWidth;
+    public int WorkingAreaHeight { get; init; } = workingAreaHeight;
+    public double Scaling { get; init; } = scaling;
 }
 
 public static class ScreenHelper
 {
     public static ScreenSize ScreenSize { get; set; }
-    public static ScreenSize GetScreenSize(Control control)
+    public static ScreenSize GetScreenSize(Window window)
     {
-        var screen = GetScreen(control);
+        var screen = window.Screens.ScreenFromWindow(window);
         
         return new ScreenSize
         {
@@ -30,10 +30,4 @@ public static class ScreenHelper
             Scaling = screen.Scaling
         };
     }
-    public static Screen? GetScreen(Control control)
-    {
-        var window = control.GetSelfAndLogicalAncestors().OfType<Window>().First();
-        
-        return window.Screens.ScreenFromPoint(new PixelPoint());
-    }
 }

+ 12 - 8
src/PicView.Avalonia/UI/WindowHelper.cs

@@ -26,6 +26,7 @@ public static class WindowHelper
         }
 
         window.BeginMoveDrag(e);
+        ScreenHelper.ScreenSize = ScreenHelper.GetScreenSize(window);
     }
 
     public static void InitializeWindowSizeAndPosition(Window window)
@@ -85,12 +86,12 @@ public static class WindowHelper
             return;
         }
 
-        var screen = ScreenHelper.GetScreen(desktop.MainWindow);
+        var window = desktop.MainWindow;
+        var screen = window.Screens.ScreenFromWindow(window);
         if (screen is null)
         {
             return;
         }
-        var window = desktop.MainWindow;
 
         Dispatcher.UIThread.InvokeAsync(() =>
         {
@@ -295,14 +296,17 @@ public static class WindowHelper
         }
         if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
         {
-            vm.IsTopToolbarShown = true;
-            vm.TitlebarHeight = SizeDefaults.TitlebarHeight;
-            if (SettingsHelper.Settings.UIProperties.ShowBottomNavBar)
+            if (SettingsHelper.Settings.UIProperties.ShowInterface)
             {
-                vm.IsBottomToolbarShown = true;
-                vm.BottombarHeight = SizeDefaults.BottombarHeight;
+                vm.IsTopToolbarShown = true;
+                vm.TitlebarHeight = SizeDefaults.TitlebarHeight;
+                if (SettingsHelper.Settings.UIProperties.ShowBottomNavBar)
+                {
+                    vm.IsBottomToolbarShown = true;
+                    vm.BottombarHeight = SizeDefaults.BottombarHeight;
+                }
+                vm.IsInterfaceShown = true;
             }
-            vm.IsInterfaceShown = true;
         }
         Dispatcher.UIThread.InvokeAsync(() => 
             desktop.MainWindow.WindowState = WindowState.Normal);

+ 13 - 3
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -25,11 +25,23 @@ public partial class MainView : UserControl
         AddHandler(DragDrop.DropEvent, Drop);
         
         GotFocus += CloseTitlebarIfOpen;
-        PointerPressed += CloseTitlebarIfOpen;
+        PointerPressed += PointerPressedBehavior;
         
         MainContextMenu.Opened += OnMainContextMenuOpened;
     }
     
+    private void PointerPressedBehavior(object? sender, PointerPressedEventArgs e)
+    {
+        CloseTitlebarIfOpen(sender, e);
+        if (MainKeyboardShortcuts.ShiftDown)
+        {
+            var hostWindow = (Window)VisualRoot!;
+            WindowHelper.WindowDragAndDoubleClickBehavior(hostWindow, e);
+        }
+        
+        MainKeyboardShortcuts.ClearKeyDownModifiers();
+    }
+    
     private void CloseTitlebarIfOpen(object? sender, EventArgs e)
     {
         if (DataContext is not MainViewModel vm)
@@ -40,8 +52,6 @@ public partial class MainView : UserControl
         {
             vm.IsEditableTitlebarOpen = false;
         }
-
-        MainKeyboardShortcuts.ClearKeyDownModifiers();
     }
 
     private void OnMainContextMenuOpened(object? sender, EventArgs e)

+ 3 - 2
src/PicView.Core/ImageDecoding/ImageFunctionHelper.cs

@@ -17,7 +17,8 @@ public static class ImageFunctionHelper
     {
         try
         {
-            using var magickImageCollection = new MagickImageCollection(file);
+            using var magickImageCollection = new MagickImageCollection();
+            magickImageCollection.Ping(file);
             return magickImageCollection.Count;
         }
         catch (MagickException ex)
@@ -26,7 +27,7 @@ public static class ImageFunctionHelper
             Trace.WriteLine($"{nameof(GetImageFrames)} Exception \n{ex}");
             #endif
             
-            return 1;
+            return 0;
         }
     }
 }