瀏覽代碼

Fix closing from fullscreen and fullscreen startup.

Refactor: rename and restructure `Close` to `HandleShouldClosing`, adjust `ExitCommand` implementation, streamline fullscreen initialization, and improve dialog management logic.
Ruben 3 月之前
父節點
當前提交
8d2e71b562

+ 2 - 2
src/PicView.Avalonia/Functions/FunctionsMapper.cs

@@ -407,9 +407,9 @@ public static class FunctionsMapper
         });
     }
     
-    /// <inheritdoc cref="DialogManager.Close(MainViewModel)" />
+    /// <inheritdoc cref="DialogManager.HandleShouldClosing" />
     public static async Task Close() =>
-        await DialogManager.Close(Vm).ConfigureAwait(false);
+        await DialogManager.HandleShouldClosing(Vm).ConfigureAwait(false);
 
     public static async Task Center() =>
         await UIHelper.CenterAsync(Vm).ConfigureAwait(false);

+ 12 - 13
src/PicView.Avalonia/StartUp/StartUpHelper.cs

@@ -135,31 +135,30 @@ public static class StartUpHelper
         
         vm.MainWindow.LayoutButtonSubscription();
         
+        SetWindowEventHandlers(window);
+        MenuManager.AddMenus();
+        
+        if (!Settings.WindowProperties.AutoFit)
+        {
+            // Need to update the screen size after the window is shown,
+            // to avoid rendering error when switching between auto-fit
+            ScreenHelper.UpdateScreenSize(window);
+        }
+        
         // Need to delay setting fullscreen or maximized until after the window is shown to select the correct monitor
         if (Settings.WindowProperties.Maximized && !Settings.WindowProperties.Fullscreen)
         {
             Dispatcher.UIThread.InvokeAsync(() =>
             {
                 vm.PlatformWindowService.Maximize(false);
-            }, DispatcherPriority.Normal).Wait();
+            }, DispatcherPriority.Background).Wait();
         }
         else if (Settings.WindowProperties.Fullscreen)
         {
             Dispatcher.UIThread.InvokeAsync(() =>
             {
                 vm.PlatformWindowService.Fullscreen(false);
-            }, DispatcherPriority.Normal).Wait();
-        }
-        
-
-        SetWindowEventHandlers(window);
-        MenuManager.AddMenus();
-        
-        if (!Settings.WindowProperties.AutoFit)
-        {
-            // Need to update the screen size after the window is shown,
-            // to avoid rendering error when switching between auto-fit
-            ScreenHelper.UpdateScreenSize(window);
+            }, DispatcherPriority.Background).Wait();
         }
 
         Application.Current.Name = "PicView";

+ 18 - 13
src/PicView.Avalonia/UI/DialogManager.cs

@@ -15,7 +15,7 @@ public static class DialogManager
     /// <summary>
     /// Handles close action based on current application state
     /// </summary>
-    public static async Task Close(MainViewModel vm)
+    public static async Task HandleShouldClosing(MainViewModel vm)
     {
         // Handle open menus
         if (MenuManager.IsAnyMenuOpen(vm))
@@ -46,21 +46,26 @@ public static class DialogManager
         }
         
         // Handle window close
-        if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
+        await Dispatcher.UIThread.InvokeAsync(CloseWithOptionalDialog);
+    }
+
+    public static void CloseWithOptionalDialog()
+    {
+        if (Settings.UIProperties.ShowConfirmationOnEsc)
         {
-            return;
+            UIHelper.GetMainView?.MainGrid.Children.Add(new CloseDialog());
         }
+        else
+        {
+            Close();
+        }
+    }
 
-        await Dispatcher.UIThread.InvokeAsync(() =>
+    public static void Close()
+    {
+        if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
         {
-            if (Settings.UIProperties.ShowConfirmationOnEsc)
-            {
-                UIHelper.GetMainView?.MainGrid.Children.Add(new CloseDialog());
-            }
-            else
-            {
-                desktop.MainWindow?.Close();
-            }
-        });
+            desktop.MainWindow?.Close();
+        }
     }
 }

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

@@ -93,11 +93,9 @@ public class MainWindowViewModel : IDisposable
     private static void ToggleToolsMenu(Unit unit) => MenuManager.ToggleToolsMenu(UIHelper.GetMainView.DataContext as MainViewModel);
 
     #endregion Menus
-    
-    public ReactiveCommand ExitCommand { get; } = new(async (_, _) =>
-    {
-        await FunctionsMapper.Close();
-    });
+
+    public ReactiveCommand ExitCommand { get; } = new(Close);
+    private static void Close(Unit unit) => DialogManager.Close();
     
     public ReactiveCommand MaximizeCommand { get; } = new(async (_, _) =>
     {