131 lines
4.3 KiB
C#
131 lines
4.3 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 MainWindowPresenter? _presenter;
|
|
private ITrayIcon? _tray;
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
|
|
bool automatic = StartupLaunch.IsAutomatic(e.Args);
|
|
|
|
var gate = new SingleInstanceGate();
|
|
if (!gate.TryAcquire(showRunningInstance: !automatic))
|
|
{
|
|
gate.Dispose();
|
|
Shutdown();
|
|
return;
|
|
}
|
|
|
|
_instanceGate = gate;
|
|
_instanceGate.ActivationRequested += OnActivationRequested;
|
|
|
|
var services = new ServiceCollection();
|
|
ConfigureServices(services);
|
|
_services = services.BuildServiceProvider();
|
|
|
|
_services.GetRequiredService<ThemeService>();
|
|
|
|
_presenter = _services.GetRequiredService<MainWindowPresenter>();
|
|
|
|
MainWindow = _services.GetRequiredService<MainWindow>();
|
|
|
|
_tray = _services.GetRequiredService<ITrayIcon>();
|
|
_tray.OpenRequested += OnOpenRequested;
|
|
_tray.ExitRequested += OnExitRequested;
|
|
|
|
bool hasTray = _tray.Install();
|
|
|
|
if (!automatic || !hasTray)
|
|
{
|
|
_presenter.Show();
|
|
}
|
|
|
|
if (!hasTray)
|
|
{
|
|
_presenter.Detach();
|
|
ShutdownMode = ShutdownMode.OnMainWindowClose;
|
|
}
|
|
|
|
_ = _services.GetRequiredService<SettingsViewModel>().InitializeAsync();
|
|
_ = _services.GetRequiredService<UpdateViewModel>().StartAsync();
|
|
_services.GetRequiredService<LayoutNotificationCoordinator>().Start();
|
|
_services.GetRequiredService<CapsLockSwitchCoordinator>().Start();
|
|
}
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
if (_tray is not null)
|
|
{
|
|
_tray.OpenRequested -= OnOpenRequested;
|
|
_tray.ExitRequested -= OnExitRequested;
|
|
}
|
|
|
|
_services?.Dispose();
|
|
|
|
if (_instanceGate is not null)
|
|
{
|
|
_instanceGate.ActivationRequested -= OnActivationRequested;
|
|
_instanceGate.Dispose();
|
|
}
|
|
|
|
base.OnExit(e);
|
|
}
|
|
|
|
private void OnActivationRequested(object? sender, EventArgs e) => _presenter?.Show();
|
|
|
|
private void OnOpenRequested(object? sender, EventArgs e) => _presenter?.Show();
|
|
|
|
private void OnExitRequested(object? sender, EventArgs e)
|
|
{
|
|
_presenter?.AllowClose();
|
|
Shutdown();
|
|
}
|
|
|
|
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<MainWindowPresenter>();
|
|
|
|
services.AddSingleton<TrayIcon>();
|
|
services.AddSingleton<ITrayIcon>(provider => provider.GetRequiredService<TrayIcon>());
|
|
|
|
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>();
|
|
}
|
|
}
|