108 lines
3.5 KiB
C#
108 lines
3.5 KiB
C#
using System.Windows;
|
|
using CursorLang.Services;
|
|
using CursorLang.ViewModels;
|
|
using CursorLang.Views;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace CursorLang;
|
|
|
|
// ReSharper disable once RedundantExtendsListEntry
|
|
public partial class App : Application
|
|
{
|
|
private ServiceProvider? _services;
|
|
private SingleInstanceGate? _instanceGate;
|
|
private MainWindowPlacement? _placement;
|
|
|
|
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();
|
|
|
|
_services.GetRequiredService<ThemeService>();
|
|
|
|
_placement = _services.GetRequiredService<MainWindowPlacement>();
|
|
MainWindow = _services.GetRequiredService<MainWindow>();
|
|
MainWindow.Show();
|
|
|
|
_ = _services.GetRequiredService<SettingsViewModel>().InitializeAsync();
|
|
_ = _services.GetRequiredService<UpdateViewModel>().StartAsync();
|
|
_services.GetRequiredService<LayoutNotificationCoordinator>().Start();
|
|
_services.GetRequiredService<CapsLockSwitchCoordinator>().Start();
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
_placement?.Apply(MainWindow);
|
|
|
|
MainWindow.Activate();
|
|
}
|
|
|
|
internal static void ConfigureServices(IServiceCollection services)
|
|
{
|
|
services.AddSingleton(new KeyboardLayoutOptions());
|
|
services.AddSingleton(new UpdateOptions());
|
|
|
|
services.AddSingleton<SettingsService>();
|
|
services.AddSingleton(provider => provider.GetRequiredService<SettingsService>().Load());
|
|
|
|
services.AddSingleton<ThemeService>();
|
|
services.AddSingleton<IThemeService>(provider => provider.GetRequiredService<ThemeService>());
|
|
|
|
services.AddSingleton<MainWindowPlacement>();
|
|
|
|
services.AddSingleton<ILocalizationService, LocalizationService>();
|
|
services.AddSingleton<IStartupService, StartupService>();
|
|
services.AddSingleton<IUpdateService, UpdateService>();
|
|
services.AddSingleton<IKeyboardLayoutService, KeyboardLayoutService>();
|
|
services.AddSingleton<ILayoutPopupService, LayoutPopupService>();
|
|
services.AddSingleton<ICapsLockHotkeyService, CapsLockHotkeyService>();
|
|
services.AddSingleton<LayoutNotificationCoordinator>();
|
|
services.AddSingleton<CapsLockSwitchCoordinator>();
|
|
|
|
services.AddSingleton<LayoutPopupViewModel>();
|
|
services.AddSingleton<UpdateViewModel>();
|
|
services.AddSingleton<SettingsViewModel>();
|
|
|
|
services.AddSingleton<LayoutPopupWindow>();
|
|
services.AddSingleton<ILayoutPopupWindow>(provider => provider.GetRequiredService<LayoutPopupWindow>());
|
|
services.AddSingleton<MainWindow>();
|
|
}
|
|
}
|