mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 22:56:29 +00:00
78776d37bd
reuse throttle mechanism for catching global keys
77 lines
2.7 KiB
C#
77 lines
2.7 KiB
C#
using System.Reflection;
|
|
using System.Windows;
|
|
using System.Windows.Input;
|
|
using System.Windows.Threading;
|
|
using Azaion.Annotator.DTO;
|
|
using LibVLCSharp.Shared;
|
|
using MediatR;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using Microsoft.Extensions.Logging;
|
|
using Serilog;
|
|
using Azaion.Annotator.Extensions;
|
|
|
|
namespace Azaion.Annotator;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private readonly IHost _host;
|
|
private readonly ILogger<App> _logger;
|
|
private readonly IMediator _mediator;
|
|
|
|
public App()
|
|
{
|
|
Log.Logger = new LoggerConfiguration()
|
|
.Enrich.FromLogContext()
|
|
.MinimumLevel.Information()
|
|
.WriteTo.Console()
|
|
.WriteTo.File(
|
|
path: "Logs/log.txt",
|
|
rollingInterval: RollingInterval.Day)
|
|
.CreateLogger();
|
|
|
|
_host = Host.CreateDefaultBuilder()
|
|
.ConfigureServices((context, services) =>
|
|
{
|
|
services.AddSingleton<MainWindow>();
|
|
services.AddSingleton<HelpWindow>();
|
|
services.AddMediatR(c => c.RegisterServicesFromAssembly(Assembly.GetExecutingAssembly()));
|
|
services.AddSingleton<LibVLC>(_ => new LibVLC());
|
|
services.AddSingleton<FormState>();
|
|
services.AddSingleton<MediaPlayer>(sp =>
|
|
{
|
|
var libVLC = sp.GetRequiredService<LibVLC>();
|
|
return new MediaPlayer(libVLC);
|
|
});
|
|
services.AddSingleton<IConfigRepository, FileConfigRepository>();
|
|
services.AddSingleton<Config>(sp => sp.GetRequiredService<IConfigRepository>().Get());
|
|
services.AddSingleton<PlayerControlHandler>();
|
|
})
|
|
.UseSerilog()
|
|
.Build();
|
|
_mediator = _host.Services.GetRequiredService<IMediator>();
|
|
_logger = _host.Services.GetRequiredService<ILogger<App>>();
|
|
DispatcherUnhandledException += OnDispatcherUnhandledException;
|
|
}
|
|
|
|
private void OnDispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
|
|
{
|
|
_logger.LogError(e.Exception, e.Exception.Message);
|
|
e.Handled = true;
|
|
}
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
EventManager.RegisterClassHandler(typeof(UIElement), UIElement.KeyDownEvent, new RoutedEventHandler(GlobalClick));
|
|
_host.Start();
|
|
_host.Services.GetRequiredService<MainWindow>().Show();
|
|
|
|
base.OnStartup(e);
|
|
}
|
|
|
|
private void GlobalClick(object sender, RoutedEventArgs e)
|
|
{
|
|
var args = (KeyEventArgs)e;
|
|
_ = ThrottleExt.Throttle(() => _mediator.Publish(new KeyEvent(sender, args)), TimeSpan.FromMilliseconds(50));
|
|
}
|
|
} |