Skip to content

Commit

Permalink
Window startup improvements and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Oct 17, 2024
1 parent cd9ce51 commit e8bdf78
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 124 deletions.
1 change: 1 addition & 0 deletions src/PicView.Avalonia.MacOS/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
using PicView.Avalonia.Interfaces;
using PicView.Avalonia.MacOS.Views;
using PicView.Avalonia.Navigation;
using PicView.Avalonia.StartUp;
using PicView.Avalonia.UI;
using PicView.Avalonia.ViewModels;
using PicView.Core.Config;
Expand Down
10 changes: 5 additions & 5 deletions src/PicView.Avalonia.MacOS/Views/MacMainWindow.axaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System;
using Avalonia.Controls;
using PicView.Avalonia.ViewModels;
using System;
using PicView.Avalonia.UI;
using PicView.Avalonia.WindowBehavior;

namespace PicView.Avalonia.MacOS.Views;

Expand All @@ -16,7 +16,7 @@ public MacMainWindow()
// Keep window position when resizing
ClientSizeProperty.Changed.Subscribe(size =>
{
WindowHelper.HandleWindowResize(this, size);
WindowResizing.HandleWindowResize(this, size);
});
};
}
Expand All @@ -33,13 +33,13 @@ private void Control_OnSizeChanged(object? sender, SizeChangedEventArgs e)
return;
}
var vm = (MainViewModel)DataContext;
WindowHelper.SetSize(vm);
WindowResizing.SetSize(vm);
}

protected override async void OnClosing(WindowClosingEventArgs e)
{
e.Cancel = true;
await WindowHelper.WindowClosingBehavior(this);
await WindowFunctions.WindowClosingBehavior(this);
base.OnClosing(e);
}
}
4 changes: 2 additions & 2 deletions src/PicView.Avalonia.MacOS/Views/MacOSTitlebar.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Media;
using PicView.Avalonia.UI;
using PicView.Avalonia.Views.UC;
using PicView.Avalonia.WindowBehavior;
using PicView.Core.Config;

namespace PicView.Avalonia.MacOS.Views;
Expand Down Expand Up @@ -68,6 +68,6 @@ private void MoveWindow(PointerPressedEventArgs e)
if (VisualRoot is null) { return; }

var hostWindow = (Window)VisualRoot;
WindowHelper.WindowDragAndDoubleClickBehavior(hostWindow, e);
WindowFunctions.WindowDragAndDoubleClickBehavior(hostWindow, e);
}
}
1 change: 1 addition & 0 deletions src/PicView.Avalonia.Win32/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using Avalonia.Styling;
using PicView.Avalonia.Interfaces;
using PicView.Avalonia.Navigation;
using PicView.Avalonia.StartUp;
using PicView.Avalonia.UI;
using PicView.Avalonia.ViewModels;
using PicView.Avalonia.Win32.Views;
Expand Down
24 changes: 14 additions & 10 deletions src/PicView.Avalonia/Navigation/IPC.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Diagnostics;
using System.IO.Pipes;
using Avalonia.Controls;
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Threading;
using PicView.Avalonia.ViewModels;

Expand All @@ -24,16 +25,15 @@ internal static class IPC
/// If a running instance is detected, the argument (e.g., a file path) is passed to it for processing.
/// </summary>
/// <param name="arg">The argument to send to the running instance, such as a file path.</param>
/// <param name="pipeName">The name of the named pipe to connect to. Defaults to <see cref="PipeName"/>.</param>
/// <returns>A task that represents the asynchronous operation. The task result is <c>true</c> if the argument is sent successfully.</returns>
/// <remarks>
/// This method tries to connect to an existing instance of the application using a named pipe. If successful,
/// it sends the argument. In case of a timeout or other exceptions, these errors are caught and logged in debug mode.
/// This can be used to pass new command-line arguments to a running instance instead of starting a new instance.
/// </remarks>
internal static async Task<bool> SendArgumentToRunningInstance(string arg, string pipeName)
internal static async Task<bool> SendArgumentToRunningInstance(string arg)
{
await using var pipeClient = new NamedPipeClientStream(pipeName);
await using var pipeClient = new NamedPipeClientStream(PipeName);
try
{
// Try to connect to the running instance
Expand Down Expand Up @@ -66,20 +66,18 @@ internal static async Task<bool> SendArgumentToRunningInstance(string arg, strin
/// Starts a named pipe server to listen for incoming arguments from other instances of the application.
/// Processes incoming arguments (e.g., file paths) by instructing the main view model to open the specified picture.
/// </summary>
/// <param name="pipeName">The name of the pipe to listen on. Defaults to <see cref="PipeName"/>.</param>
/// <param name="w">The main window of the application, which will be activated upon receiving an argument.</param>
/// <param name="vm">The main view model that processes the received argument, typically loading a picture.</param>
/// <returns>A task that represents the asynchronous operation. The method runs indefinitely to handle multiple connections.</returns>
/// <remarks>
/// This method runs continuously, waiting for incoming connections on the specified named pipe. When a connection is made,
/// it reads the incoming arguments and processes them. The arguments can include file paths or commands,
/// and they are passed to the main view model to update the UI accordingly.
/// </remarks>
internal static async Task StartListeningForArguments(string pipeName, Window w, MainViewModel vm)
internal static async Task StartListeningForArguments(MainViewModel vm)
{
while (true) // Continuously listen for incoming connections
{
await using var pipeServer = new NamedPipeServerStream(pipeName);
await using var pipeServer = new NamedPipeServerStream(PipeName);

try
{
Expand All @@ -95,9 +93,15 @@ internal static async Task StartListeningForArguments(string pipeName, Window w,
#if DEBUG
Trace.WriteLine("Received argument: " + line);
#endif
// Activate the window and load the picture
await Dispatcher.UIThread.InvokeAsync(w.Activate);
await NavigationHelper.LoadPicFromStringAsync(line, vm).ConfigureAwait(false);
await Dispatcher.UIThread.InvokeAsync(() =>
{
if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
{
return;
}
desktop.MainWindow.Activate();
});
}
}
catch (Exception ex)
Expand Down
Loading

0 comments on commit e8bdf78

Please sign in to comment.