-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
193 lines (164 loc) · 6.09 KB
/
MainWindow.xaml.cs
File metadata and controls
193 lines (164 loc) · 6.09 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
using System.Windows;
using LapKeys.Services;
using LapKeys.ViewModels;
using LapKeys.Views;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using Key = System.Windows.Input.Key;
using Keyboard = System.Windows.Input.Keyboard;
using Button = System.Windows.Controls.Button;
namespace LapKeys;
public partial class MainWindow : Window
{
private readonly HotkeyService _hotkeyService;
private MainViewModel ViewModel => (MainViewModel)DataContext;
private bool _isCapturingHotkey;
public MainWindow()
{
InitializeComponent();
// Force the creation of the handle so hotkeys can be registered
// even if the window is never shown (e.g., --minimized)
var helper = new System.Windows.Interop.WindowInteropHelper(this);
helper.EnsureHandle();
DataContext = new MainViewModel();
_hotkeyService = new HotkeyService();
ViewModel.RequestHotkeyCapture += OnRequestHotkeyCapture;
ViewModel.RefreshRateChanged += OnRefreshRateChanged;
ViewModel.BrightnessChanged += OnBrightnessChanged;
ViewModel.RefreshRateHotkeyToggled += RegisterCurrentHotkey;
ViewModel.BrightnessHotkeysToggled += RegisterCurrentHotkey;
_hotkeyService.Initialize(this);
_hotkeyService.HotkeyPressed += OnHotkeyPressed;
RegisterCurrentHotkey();
}
private void RegisterCurrentHotkey()
{
_hotkeyService.UnregisterAllHotkeys();
if (ViewModel.IsRefreshRateHotkeyEnabled)
{
var hotkeys = ViewModel.GetAllRefreshRateHotkeys();
foreach (var hk in hotkeys)
{
if (_hotkeyService.RegisterHotkey(hk))
{
ViewModel.StatusMessage = $"Hotkey registered: {hk}";
}
else
{
ViewModel.StatusMessage = $"Failed to register hotkey (may be in use by another app)";
}
}
}
if (ViewModel.IsBrightnessSupported && ViewModel.IsBrightnessHotkeysEnabled)
{
_hotkeyService.RegisterHotkey(ViewModel.BrightnessUpHotkey);
_hotkeyService.RegisterHotkey(ViewModel.BrightnessDownHotkey);
}
}
private void OnHotkeyPressed(object? sender, Models.HotkeyBinding binding)
{
switch (binding.Action)
{
case "CycleRefreshRate":
ViewModel.ExecuteCycleRefreshRate(binding.DeviceName);
break;
case "BrightnessUp":
ViewModel.ExecuteIncreaseBrightness();
break;
case "BrightnessDown":
ViewModel.ExecuteDecreaseBrightness();
break;
}
}
private void OnRequestHotkeyCapture()
{
_isCapturingHotkey = true;
_hotkeyService.UnregisterAllHotkeys();
Focus();
}
private void OnRefreshRateChanged(int newRate, string? deviceName)
{
RefreshRateOverlay.ShowOverlay(newRate, deviceName);
}
private void OnBrightnessChanged(int brightness)
{
BrightnessOverlay.ShowOverlay(brightness);
}
private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (!_isCapturingHotkey)
return;
var key = e.Key == Key.System ? e.SystemKey : e.Key;
if (key == Key.LeftCtrl || key == Key.RightCtrl ||
key == Key.LeftAlt || key == Key.RightAlt ||
key == Key.LeftShift || key == Key.RightShift ||
key == Key.LWin || key == Key.RWin)
{
e.Handled = true;
return;
}
e.Handled = true;
_isCapturingHotkey = false;
var modifiers = System.Windows.Input.ModifierKeys.None;
if (Keyboard.IsKeyDown(Key.LeftCtrl) || Keyboard.IsKeyDown(Key.RightCtrl))
modifiers |= System.Windows.Input.ModifierKeys.Control;
if (Keyboard.IsKeyDown(Key.LeftAlt) || Keyboard.IsKeyDown(Key.RightAlt))
modifiers |= System.Windows.Input.ModifierKeys.Alt;
if (Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift))
modifiers |= System.Windows.Input.ModifierKeys.Shift;
if (Keyboard.IsKeyDown(Key.LWin) || Keyboard.IsKeyDown(Key.RWin))
modifiers |= System.Windows.Input.ModifierKeys.Windows;
ViewModel.SetNewHotkey(modifiers, key);
RegisterCurrentHotkey();
}
private void RefreshRateButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is LapKeys.Models.RefreshRateOption option && DataContext is MainViewModel vm)
{
vm.SetRefreshRate(option.Rate);
}
}
private void CycleRateButton_Click(object sender, RoutedEventArgs e)
{
if (sender is Button button && button.DataContext is LapKeys.Models.RefreshRateOption option && DataContext is MainViewModel vm)
{
option.IsIncludedInCycle = !option.IsIncludedInCycle;
vm.SaveCycleRates();
}
}
private void Window_StateChanged(object? sender, EventArgs e)
{
if (WindowState == WindowState.Minimized)
{
Hide();
}
}
private void ScrollViewer_PreviewMouseWheel(object sender, System.Windows.Input.MouseWheelEventArgs e)
{
var scrollViewer = sender as System.Windows.Controls.ScrollViewer;
if (scrollViewer != null)
{
double scrollAmount = e.Delta / 3.0;
scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - scrollAmount);
e.Handled = true;
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (ViewModel.MinimizeToTrayOnClose)
{
e.Cancel = true;
Hide();
}
else
{
_hotkeyService.Dispose();
System.Windows.Application.Current.Shutdown();
}
base.OnClosing(e);
}
protected override void OnClosed(EventArgs e)
{
_hotkeyService.Dispose();
base.OnClosed(e);
}
}