Skip to content

Commit

Permalink
feat: 传递启动参数给当前运行的实例
Browse files Browse the repository at this point in the history
  • Loading branch information
LiuYunPlayer committed Feb 7, 2025
1 parent 3ecc669 commit 8d74a1b
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 7 deletions.
51 changes: 46 additions & 5 deletions TuneLab/App.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
using TuneLab.Utils;
using TuneLab.I18N;
using System.Linq;
using System.Diagnostics.CodeAnalysis;
using System.Threading.Tasks;
using System.Threading;
using System.IO.Pipes;

namespace TuneLab;

Expand Down Expand Up @@ -60,16 +64,46 @@ public override void OnFrameworkInitializationCompleted()
Settings.AudioDevice.Modified.Subscribe(() => { AudioEngine.CurrentDevice.Value = Settings.AudioDevice; });

ExtensionManager.LoadExtensions();
var mainWindow = new MainWindow();
desktop.MainWindow = mainWindow;
mMainWindow = new MainWindow();
desktop.MainWindow = mMainWindow;

// 检测启动参数
var args = Environment.GetCommandLineArgs();
if (args.Length > 1)
Log.Info($"Command line args:");
for (int i = 1; i < args.Length; i++)
{
var filePath = args[1];
mainWindow.Editor.OpenProjectByPath(filePath);
Log.Info(args[i]);
HandleArg(args[i]);
}

// 获取主线程SynchronizationContext
var context = SynchronizationContext.Current;
if (context == null)
{
Log.Error("SynchronizationContext.Current is null");
return;
}

// 监听其他实例的启动参数
Task.Run(() =>
{
while (true)
{
var pipeServer = new NamedPipeServerStream("TuneLab", PipeDirection.In);
pipeServer.WaitForConnection();

using var reader = new StreamReader(pipeServer);
while (pipeServer.IsConnected)
{
var arg = reader.ReadLine();
if (arg == null)
continue;

Log.Info($"Received from another instance: {arg}");
context.Post(_ => HandleArg(arg), null);
}
}
});
}
catch (Exception ex)
{
Expand Down Expand Up @@ -100,4 +134,11 @@ public override void OnFrameworkInitializationCompleted()

base.OnFrameworkInitializationCompleted();
}

public void HandleArg(string arg)
{
mMainWindow?.Editor.OpenProjectByPath(arg);
}

MainWindow? mMainWindow = null;
}
23 changes: 22 additions & 1 deletion TuneLab/Program.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Avalonia;
using Avalonia.Media;
using Avalonia.ReactiveUI;
Expand All @@ -28,7 +32,24 @@ public static void Main(string[] args)
var lockFile = LockFile.Create(PathManager.LockFilePath);
if (lockFile == null)
{
// TODO: 传递启动参数给当前运行的app
try
{
using var pipeClient = new NamedPipeClientStream(".", "TuneLab", PipeDirection.Out);
pipeClient.Connect(1000);

using var writer = new StreamWriter(pipeClient);
foreach (var arg in args)
{
writer.WriteLine(arg);
Log.Info($"Sent arguments to running instance: {arg}");
}
writer.Flush();
}
catch (Exception ex)
{
Log.Error($"Failed to send arguments to running instance: {ex}");
}
Log.Info("Another instance is running, exiting.");
Process.GetCurrentProcess().Kill();
Process.GetCurrentProcess().WaitForExit();
return;
Expand Down
2 changes: 1 addition & 1 deletion TuneLab/UI/MainWindow/MainWindow.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ void UpdateTitle()
Title = "TuneLab - " + mEditor.Document.Name + (mEditor.Document.IsSaved ? string.Empty : "*");
}

void OnKeyDown(object sender, KeyEventArgs args)
void OnKeyDown(object? sender, KeyEventArgs args)
{
if (args.KeyModifiers == KeyModifiers.None)
{
Expand Down

0 comments on commit 8d74a1b

Please sign in to comment.