Explorar o código

Fix the setting to open in a new window, or the same, to not require restart to take effect. The setting can now be safely turned on and off.

Ruben hai 5 meses
pai
achega
b6c132c5a7

+ 3 - 0
src/PicView.Avalonia/Functions/FunctionsMapper.cs

@@ -886,6 +886,9 @@ public static class FunctionsMapper
     /// <inheritdoc cref="SettingsUpdater.ToggleUsingTouchpad(MainViewModel)" />
     public static async Task ToggleUsingTouchpad() =>
         await SettingsUpdater.ToggleUsingTouchpad(Vm).ConfigureAwait(false);
+    
+    public static async Task ToggleOpeningInSameWindow() =>
+        await SettingsUpdater.ToggleOpeningInSameWindow(Vm).ConfigureAwait(false);
 
     #endregion
     

+ 26 - 8
src/PicView.Avalonia/Navigation/IPC.cs

@@ -5,6 +5,7 @@ using Avalonia.Controls.ApplicationLifetimes;
 using Avalonia.Threading;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.DebugTools;
+using PicView.Core.ProcessHandling;
 
 namespace PicView.Avalonia.Navigation;
 
@@ -21,6 +22,8 @@ internal static class IPC
     /// This pipe is used to facilitate communication between instances of the application.
     /// </summary>
     private const string PipeName = "PicViewPipe";
+    
+    private static bool? _isRunning;
 
     /// <summary>
     /// Sends an argument to a running instance of the application via the specified named pipe.
@@ -74,12 +77,19 @@ internal static class IPC
     /// </remarks>
     internal static async Task StartListeningForArguments(MainViewModel vm)
     {
-        while (true) // Continuously listen for incoming connections
+        if (_isRunning.HasValue && !_isRunning.Value)
+        {
+            _isRunning = true;
+            return;
+        }
+        
+        _isRunning = true;
+        do
         {
-            await using var pipeServer = new NamedPipeServerStream(PipeName);
-
             try
             {
+                await using var pipeServer = new NamedPipeServerStream(PipeName);
+                
                 // Wait for a connection from another instance
                 await pipeServer.WaitForConnectionAsync();
 
@@ -88,6 +98,12 @@ internal static class IPC
                 // Read and process incoming arguments
                 while (await reader.ReadLineAsync() is { } line)
                 {
+                    if (!_isRunning.Value)
+                    {
+                        // Setting to open in same window turned off, start new process instead
+                        ProcessHelper.StartNewProcess(line);
+                        return;
+                    }
                     // Log the received argument if in debug mode
 #if DEBUG
                     Trace.WriteLine("Received argument: " + line);
@@ -110,12 +126,14 @@ internal static class IPC
             }
             catch (Exception ex)
             {
-                // Log any exceptions encountered while processing arguments
-#if DEBUG
-                Trace.WriteLine($"{nameof(StartListeningForArguments)} exception: \n{ex}");
-#endif
+                DebugHelper.LogDebug(nameof(IPC), nameof(StartListeningForArguments), ex);
             }
         }
-        // ReSharper disable once FunctionNeverReturns
+        while (true); // Continuously listen for incoming connections
+    }
+
+    public static void StopListening()
+    {
+        _isRunning = false;
     }
 }

+ 16 - 0
src/PicView.Avalonia/SettingsManagement/SettingsUpdater.cs

@@ -189,6 +189,22 @@ public static class SettingsUpdater
         });
         await SaveSettingsAsync();
     }
+
+    public static async Task ToggleOpeningInSameWindow(MainViewModel vm)
+    {
+        if (Settings.UIProperties.OpenInSameWindow)
+        {
+            _ = IPC.StartListeningForArguments(vm);
+            Settings.UIProperties.OpenInSameWindow = true;
+        }
+        else
+        {
+            IPC.StopListening();
+            Settings.UIProperties.OpenInSameWindow = false;
+        }
+
+        await SaveSettingsAsync();
+    }
     
     #region Image settings
 

+ 3 - 0
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -290,6 +290,8 @@ public class MainViewModel : ReactiveObject
 
         ToggleUsingTouchpadCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleUsingTouchpad);
         
+        ToggleOpeningInSameWindowCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleOpeningInSameWindow);
+        
         ShowSettingsFileCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ShowSettingsFile);
         
         ShowKeybindingsFileCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ShowKeybindingsFile);
@@ -614,6 +616,7 @@ public class MainViewModel : ReactiveObject
     public ReactiveCommand<Unit, Unit>? ChangeCtrlZoomCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleUsingTouchpadCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleUICommand { get; }
+    public ReactiveCommand<Unit, Unit>? ToggleOpeningInSameWindowCommand { get; }
     public ReactiveCommand<Unit, Unit>? ChangeBackgroundCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleBottomNavBarCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleBottomGalleryShownInHiddenUICommand { get; }

+ 1 - 0
src/PicView.Avalonia/Views/WindowSettingsView.axaml

@@ -121,6 +121,7 @@
             Background="Transparent"
             BorderThickness="0"
             Classes="altHover changeColor"
+            Command="{CompiledBinding ToggleOpeningInSameWindowCommand}"
             IsChecked="{CompiledBinding IsOpeningInSameWindow}"
             Margin="0,0,0,10"
             Width="300">

+ 0 - 4
src/PicView.Core/DebugTools/DebugHelper.cs

@@ -23,10 +23,6 @@ public static class DebugHelper
     /// <param name="exception">The exception that was thrown.</param>
     /// <example>
     ///     <code>
-    /// try
-    /// {
-    ///     // Code that might throw
-    /// }
     /// catch (Exception ex)
     /// {
     ///     DebugHelper.LogDebug(nameof(MyClass), nameof(MyMethod), ex);