added a ban on running a second instance of the application.
This commit is contained in:
@@ -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());
|
||||
|
||||
Reference in New Issue
Block a user