-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.axaml.cs
More file actions
185 lines (162 loc) · 6.93 KB
/
MainWindow.axaml.cs
File metadata and controls
185 lines (162 loc) · 6.93 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
using System;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Threading;
using AutoClacker.ViewModels;
using AutoClacker.Services;
namespace AutoClacker;
public partial class MainWindow : Window
{
private readonly MainViewModel _vm;
private IGlobalHotkey? _globalHotkey;
private Slider? _slider; private TextBlock? _intLbl, _statusLbl, _kbLbl, _hotkeyLbl; private Border? _statusBorder;
private Button? _toggleBtn, _setKeyBtn; private ComboBox? _mouseCombo;
private RadioButton? _single, _double, _mouseMode, _kbMode; private StackPanel? _mousePanel, _kbPanel;
public MainWindow()
{
Logger.Init();
Logger.Log("MainWindow constructor called");
InitializeComponent();
_vm = new MainViewModel();
DataContext = _vm;
Loaded += OnLoaded;
KeyDown += OnKeyDown;
Closed += OnClosed;
Logger.Log($"MainWindow: ViewModel created, TriggerKey={_vm.TriggerKey}, Mode={(_vm.IsMouseMode ? "Mouse" : "Keyboard")}");
}
void OnLoaded(object? s, RoutedEventArgs e)
{
Logger.Log("MainWindow.OnLoaded called");
_slider = this.FindControl<Slider>("IntervalSlider");
_intLbl = this.FindControl<TextBlock>("IntervalLabel");
_statusLbl = this.FindControl<TextBlock>("StatusLabel");
_kbLbl = this.FindControl<TextBlock>("KeyboardKeyLabel");
_statusBorder = this.FindControl<Border>("StatusBorder");
_toggleBtn = this.FindControl<Button>("ToggleButton");
_setKeyBtn = this.FindControl<Button>("SetKeyButton");
_mouseCombo = this.FindControl<ComboBox>("MouseButtonCombo");
_single = this.FindControl<RadioButton>("SingleClick");
_double = this.FindControl<RadioButton>("DoubleClick");
_mouseMode = this.FindControl<RadioButton>("MouseModeRadio");
_kbMode = this.FindControl<RadioButton>("KeyboardModeRadio");
_mousePanel = this.FindControl<StackPanel>("MouseSettings");
_kbPanel = this.FindControl<StackPanel>("KeyboardSettings");
_hotkeyLbl = this.FindControl<TextBlock>("HotkeyLabel");
if (_kbLbl != null) _kbLbl.Text = _vm.KbKey;
if (_hotkeyLbl != null) _hotkeyLbl.Text = _vm.TriggerKey;
if (_slider != null) { _slider.Value = _vm.Interval; _slider.PropertyChanged += (_, a) => { if (a.Property.Name == "Value") { _intLbl!.Text = $"Interval: {(int)_slider.Value}ms"; _vm.Interval = (int)_slider.Value; } }; }
if (_intLbl != null) _intLbl.Text = $"Interval: {_vm.Interval}ms";
if (_vm.IsMouseMode == false && _kbMode != null) { _kbMode.IsChecked = true; SetMode(false); }
if (_mouseMode != null) _mouseMode.IsCheckedChanged += (_, _) => SetMode(_mouseMode.IsChecked == true);
if (_kbMode != null) _kbMode.IsCheckedChanged += (_, _) => SetMode(_kbMode.IsChecked != true);
// Register global hotkey (works even when app not focused)
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
_globalHotkey = new GlobalHotkey();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
_globalHotkey = new LinuxGlobalHotkey();
else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
_globalHotkey = new MacGlobalHotkey();
if (_globalHotkey != null)
{
_globalHotkey.OnHotkeyPressed += OnGlobalHotkey;
_globalHotkey.Register(_vm.TriggerKey);
Logger.Log($"Global hotkey registered for {_vm.TriggerKey}");
}
Logger.Log($"OnLoaded complete. Press {_vm.TriggerKey} anywhere to start/stop!");
}
void OnGlobalHotkey()
{
Logger.Log("Global hotkey pressed!");
Dispatcher.UIThread.Post(() =>
{
ToggleFromHotkey();
});
}
void OnClosed(object? s, EventArgs e)
{
Logger.Log("MainWindow.OnClosed called");
_vm.Stop();
_globalHotkey?.Dispose();
}
void SetMode(bool mouse)
{
Logger.Log($"SetMode called: mouse={mouse}");
if (_mousePanel != null) _mousePanel.IsVisible = mouse;
if (_kbPanel != null) _kbPanel.IsVisible = !mouse;
_vm.IsMouseMode = mouse;
}
void OnKeyDown(object? s, KeyEventArgs e)
{
Logger.Log($"OnKeyDown: Key={e.Key}, Capturing={_vm.Capturing}");
if (_vm.Capturing)
{
Logger.Log($"Capturing key: {e.Key}");
_vm.CaptureKey(e.Key.ToString());
if (_kbLbl != null && _vm.Target == "kb") _kbLbl.Text = e.Key.ToString();
if (_setKeyBtn != null) _setKeyBtn.Content = "Set Key";
e.Handled = true;
}
}
void SetKeyButton_Click(object? s, RoutedEventArgs e)
{
Logger.Log("SetKeyButton_Click - starting capture");
_vm.StartCapture("kb");
if (_setKeyBtn != null) _setKeyBtn.Content = "Press any key...";
}
async void ToggleButton_Click(object? s, RoutedEventArgs e)
{
Logger.Log("ToggleButton_Click called");
if (_mouseCombo?.SelectedItem is ComboBoxItem i)
{
_vm.MouseBtn = i.Content?.ToString() ?? "Left";
Logger.Log($"MouseBtn set to: {_vm.MouseBtn}");
}
_vm.ClickType = _double?.IsChecked == true ? "Double" : "Single";
Logger.Log($"ClickType set to: {_vm.ClickType}");
if (!_vm.Running)
{
// Starting - add delay so user can move cursor away
UpdateUIState(true, "Starting in 2s...");
Logger.Log("Starting in 2 seconds - move cursor away!");
await Task.Delay(2000);
if (!_vm.Running) // Check if cancelled during delay
{
_vm.Start();
UpdateUIState(true, "Running");
}
}
else
{
_vm.Stop();
UpdateUIState(false, "Stopped");
}
}
void ToggleFromHotkey()
{
Logger.Log($"ToggleFromHotkey: Running={_vm.Running}");
_vm.Toggle();
UpdateUIState(_vm.Running, _vm.Status);
}
void UpdateUIState(bool running, string status)
{
if (_statusLbl != null) _statusLbl.Text = status;
if (_toggleBtn != null) _toggleBtn.Content = running ? "Stop" : "Start";
if (_statusBorder != null) _statusBorder.Background = new SolidColorBrush(running ? Color.Parse("#4CAF50") : Color.Parse("#F44336"));
Logger.Log($"UI updated: Status={status}");
}
void SettingsButton_Click(object? s, RoutedEventArgs e)
{
Logger.Log("Opening SettingsWindow");
new SettingsWindow(_vm, OnTriggerKeyChanged).ShowDialog(this);
}
void OnTriggerKeyChanged(string newKey)
{
Logger.Log($"Trigger key changed to {newKey}, re-registering hotkey");
_globalHotkey?.Register(newKey);
if (_hotkeyLbl != null) _hotkeyLbl.Text = newKey;
}
}