added a ban on running a second instance of the application.

This commit is contained in:
2026-08-09 01:29:25 +05:00
parent 4a2ebd9a70
commit 9e0b00dd98
3 changed files with 158 additions and 0 deletions
+34
View File
@@ -13,11 +13,23 @@ namespace CursorLang;
public partial class App : Application
{
private ServiceProvider? _services;
private SingleInstanceGate? _instanceGate;
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
var gate = new SingleInstanceGate();
if (!gate.TryAcquire())
{
gate.Dispose();
Shutdown();
return;
}
_instanceGate = gate;
_instanceGate.ActivationRequested += OnActivationRequested;
var services = new ServiceCollection();
ConfigureServices(services);
_services = services.BuildServiceProvider();
@@ -34,9 +46,31 @@ public partial class App : Application
protected override void OnExit(ExitEventArgs e)
{
_services?.Dispose();
if (_instanceGate is not null)
{
_instanceGate.ActivationRequested -= OnActivationRequested;
_instanceGate.Dispose();
}
base.OnExit(e);
}
private void OnActivationRequested(object? sender, EventArgs e)
{
if (MainWindow is not { IsVisible: true })
{
return;
}
if (MainWindow.WindowState == WindowState.Minimized)
{
MainWindow.WindowState = WindowState.Normal;
}
MainWindow.Activate();
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new KeyboardLayoutOptions());