Files
cursor-lang/CursorLang/App.xaml.cs
T
2026-08-09 01:55:45 +05:00

104 lines
3.2 KiB
C#

using System.Windows;
using CursorLang.Models;
using CursorLang.Services;
using CursorLang.ViewModels;
using CursorLang.Views;
using Microsoft.Extensions.DependencyInjection;
namespace CursorLang;
/// <summary>
/// Композиционный корень: собирает контейнер и запускает окно настроек.
/// </summary>
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<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();
}
private static void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(new KeyboardLayoutOptions());
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<IKeyboardLayoutService, KeyboardLayoutService>();
services.AddSingleton<ILayoutPopupService, LayoutPopupService>();
services.AddSingleton<ICapsLockHotkeyService, CapsLockHotkeyService>();
services.AddSingleton<LayoutNotificationCoordinator>();
services.AddSingleton<CapsLockSwitchCoordinator>();
services.AddSingleton<LayoutPopupViewModel>();
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<LayoutPopupWindow>();
services.AddSingleton<MainWindow>();
}
}