Repository navigation
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
111 lines (92 loc) · 3.15 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
111 lines (92 loc) · 3.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
using System.Runtime.InteropServices;
using H.NotifyIcon;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Lexorio.Services;
using Lexorio.ViewModels;
using Windows.Graphics;
using WinRT.Interop;
namespace Lexorio;
public sealed partial class MainWindow : Window
{
private readonly MainViewModel _viewModel;
private readonly HotkeyService _hotkeyService;
private bool _forceClose;
private bool _hotkeyAttached;
public MainWindow(MainViewModel viewModel, HotkeyService hotkeyService)
{
_viewModel = viewModel;
_hotkeyService = hotkeyService;
InitializeComponent();
ExtendsContentIntoTitleBar = true;
SetTitleBar(AppTitleBar);
AppWindow.SetIcon("Assets/AppIcon.ico");
var settings = viewModel.CurrentSettings;
AppWindow.Resize(settings.RememberWindowSize && settings.WindowWidth > 0 && settings.WindowHeight > 0
? new SizeInt32(settings.WindowWidth, settings.WindowHeight)
: new SizeInt32(980, 740));
RootFrame.Navigate(typeof(MainPage), viewModel);
TrayIcon.DataContext = viewModel;
viewModel.RequestShowWindow += (_, _) =>
{
DispatcherQueue.TryEnqueue(ShowFromTray);
};
viewModel.RequestExit += (_, _) =>
{
DispatcherQueue.TryEnqueue(() =>
{
_forceClose = true;
Close();
});
};
AppWindow.Closing += AppWindow_Closing;
Activated += MainWindow_Activated;
}
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)
{
if (_hotkeyAttached)
{
return;
}
var hwnd = WindowNative.GetWindowHandle(this);
_hotkeyService.Attach(hwnd);
_hotkeyService.HotkeyPressed += HotkeyService_HotkeyPressed;
_viewModel.RegisterHotkey();
_hotkeyAttached = true;
}
private void HotkeyService_HotkeyPressed(object? sender, EventArgs e)
{
DispatcherQueue.TryEnqueue(async () => await _viewModel.HandleHotkeyAsync());
}
private void AppWindow_Closing(AppWindow sender, AppWindowClosingEventArgs args)
{
if ((AppWindow.Presenter as OverlappedPresenter)?.State == OverlappedPresenterState.Restored)
{
_viewModel.Settings.SaveWindowSize(AppWindow.Size.Width, AppWindow.Size.Height);
}
if (_forceClose)
{
TrayIcon.Dispose();
_hotkeyService.Dispose();
return;
}
args.Cancel = true;
HideToTray();
}
public void ShowFromTray()
{
AppWindow.Show();
Activate();
var hwnd = WindowNative.GetWindowHandle(this);
ShowWindow(hwnd, 9); // SW_RESTORE
SetForegroundWindow(hwnd);
}
public void HideToTray()
{
AppWindow.Hide();
}
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
}