74 lines
2.7 KiB
C#
74 lines
2.7 KiB
C#
using System.Windows;
|
|
using System.Windows.Input;
|
|
using EbbesMemeClipboard.Services;
|
|
using EbbesMemeClipboard.ViewModels;
|
|
using EbbesMemeClipboard.Views;
|
|
using H.NotifyIcon;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace EbbesMemeClipboard;
|
|
|
|
public partial class App : Application
|
|
{
|
|
private static readonly ModifierKeys DefaultHotkeyModifiers = ModifierKeys.Control | ModifierKeys.Alt;
|
|
private const Key DefaultHotkeyKey = Key.M;
|
|
|
|
private IServiceProvider _services = null!;
|
|
private IGlobalHotkeyService _hotkeyService = null!;
|
|
private PickerWindow _pickerWindow = null!;
|
|
private PickerViewModel _pickerViewModel = null!;
|
|
private TaskbarIcon? _trayIcon;
|
|
|
|
protected override void OnStartup(StartupEventArgs e)
|
|
{
|
|
base.OnStartup(e);
|
|
ShutdownMode = ShutdownMode.OnExplicitShutdown;
|
|
|
|
var services = new ServiceCollection();
|
|
services.AddSingleton<ILocalMemeLibraryService, LocalMemeLibraryService>();
|
|
services.AddSingleton<IClipboardService, ClipboardService>();
|
|
services.AddSingleton<IGlobalHotkeyService, GlobalHotkeyService>();
|
|
services.AddSingleton<PickerViewModel>();
|
|
services.AddSingleton<PickerWindow>();
|
|
_services = services.BuildServiceProvider();
|
|
|
|
_pickerViewModel = _services.GetRequiredService<PickerViewModel>();
|
|
_pickerWindow = _services.GetRequiredService<PickerWindow>();
|
|
|
|
_trayIcon = (TaskbarIcon)Resources["TrayIcon"];
|
|
_trayIcon.TrayLeftMouseUp += (_, _) => _pickerWindow.ToggleVisibility();
|
|
|
|
_hotkeyService = _services.GetRequiredService<IGlobalHotkeyService>();
|
|
if (_hotkeyService.Register(DefaultHotkeyModifiers, DefaultHotkeyKey))
|
|
{
|
|
_hotkeyService.HotkeyPressed += (_, _) => Dispatcher.Invoke(() => _pickerWindow.ToggleVisibility());
|
|
}
|
|
else
|
|
{
|
|
MessageBox.Show(
|
|
"Couldn't register the global hotkey (Ctrl+Alt+M) - it may already be in use by another application. " +
|
|
"You can still open the picker from the tray icon.",
|
|
"Ebbe's Meme Clipboard",
|
|
MessageBoxButton.OK,
|
|
MessageBoxImage.Warning);
|
|
}
|
|
}
|
|
|
|
private void TrayOpenPicker_Click(object sender, RoutedEventArgs e) => _pickerWindow.ShowNearCursor();
|
|
|
|
private async void TrayAddMemes_Click(object sender, RoutedEventArgs e)
|
|
{
|
|
_pickerWindow.ShowNearCursor();
|
|
await _pickerViewModel.ImportFilesCommand.ExecuteAsync(null);
|
|
}
|
|
|
|
private void TrayExit_Click(object sender, RoutedEventArgs e) => Shutdown();
|
|
|
|
protected override void OnExit(ExitEventArgs e)
|
|
{
|
|
_hotkeyService?.Dispose();
|
|
_trayIcon?.Dispose();
|
|
base.OnExit(e);
|
|
}
|
|
}
|