-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
499 lines (444 loc) · 17.8 KB
/
MainWindow.xaml.cs
File metadata and controls
499 lines (444 loc) · 17.8 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
using AutoClick.Models;
using AutoClick.Utils;
using Microsoft.Win32;
using Newtonsoft.Json;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Windows;
namespace AutoClick
{
public partial class MainWindow : Window
{
private bool _isWatching = false;
private long _previousActionTimestamp = 0;
public ObservableCollection<Models.Action> Actions;
private readonly IniFile _iniFile;
private const string Section = "MainWindowPosition";
private const string IniFileName = "setting.ini";
private MouseKeyHookHandler hookHandler;
private CancellationTokenSource? cancellationTokenSource;
public MainWindow()
{
InitializeComponent();
Actions = new ObservableCollection<Models.Action>();
DgActions.ItemsSource = Actions;
DeleteTempImage();
string iniFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, IniFileName);
_iniFile = new IniFile(iniFilePath);
hookHandler = new MouseKeyHookHandler(Actions);
}
private void MainWindow_Closing(object sender, CancelEventArgs e)
{
hookHandler.Unsubscribe();
SaveWindowPosition();
}
private void SaveWindowPosition()
{
_iniFile.WriteValue(Section, "Left", Left.ToString());
_iniFile.WriteValue(Section, "Top", Top.ToString());
_iniFile.WriteValue(Section, "Width", Width.ToString());
_iniFile.WriteValue(Section, "Height", Height.ToString());
}
public static void DeleteTempImage()
{
var imgFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "_img");
try
{
if (Directory.Exists(imgFolder))
{
string[] files = Directory.GetFiles(imgFolder);
foreach (string file in files)
{
File.Delete(file);
}
}
}
catch (Exception e)
{
MessageBox.Show($"Delete temporary image error: {e.Message}");
}
}
private void BtnOpenScript_Click(object sender, RoutedEventArgs e)
{
var recordFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "record");
OpenFileDialog openFileDialog = new()
{
Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*",
InitialDirectory = recordFolder,
Title = "Select a JSON File"
};
// Show OpenFileDialog
if (openFileDialog.ShowDialog() == true)
{
try
{
string json = File.ReadAllText(openFileDialog.FileName);
var actions = JsonConvert.DeserializeObject<ObservableCollection<Models.Action>>(json);
Actions.Clear();
if (actions != null)
{
foreach (var action in actions)
{
Actions.Add(action);
}
}
}
catch (Exception ex)
{
MessageBox.Show($"Error loading JSON file: {ex.Message}", "Error", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}
private void BtnSaveScript_Click(object sender, RoutedEventArgs e)
{
StopRecording();
var recordFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "record");
var imgFolder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "img");
if (!Directory.Exists(recordFolder))
{
Directory.CreateDirectory(recordFolder);
}
if (!Directory.Exists(imgFolder))
{
Directory.CreateDirectory(imgFolder);
}
foreach (Models.Action action in Actions)
{
if (action.Type == "mouse_template" && action.Data != null)
{
string img = action.Data.ToString().Replace("_img", "img");
File.Copy(action.Data, img, true);
action.Data = img;
}
action.ExecuteSucceeded = null;
}
string json = JsonConvert.SerializeObject(Actions, Formatting.Indented);
// Open the save file dialog
SaveFileDialog saveFileDialog = new()
{
Filter = "JSON files (*.json)|*.json|All files (*.*)|*.*",
InitialDirectory = recordFolder,
FileName = "temp.json"
};
if (saveFileDialog.ShowDialog() == true)
{
File.WriteAllText(saveFileDialog.FileName, json);
}
}
private void BtnResetScript_Click(object sender, RoutedEventArgs e)
{
Actions.Clear();
_previousActionTimestamp = 0;
}
private void DgActions_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.Delete)
{
foreach (Models.Action action in DgActions.SelectedItems)
{
Actions.Remove(action);
}
}
}
public void AddAction(string type, string? button, Position? position = null, string? data = null, bool ignoreWait = false)
{
long timeNow = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
long waitTime = 1000;
if (_previousActionTimestamp != 0 && !ignoreWait)
{
waitTime = timeNow - _previousActionTimestamp;
}
var waitAction = new Models.Action("wait", null, null, waitTime.ToString());
Actions.Add(waitAction);
_previousActionTimestamp = timeNow;
var mouseAction = new Models.Action(type, button, position, data);
Actions.Add(mouseAction);
DgActions.ScrollIntoView(mouseAction);
}
public void ResetActionsExecuteSucceeded()
{
foreach (Models.Action action in Actions)
{
action.ExecuteSucceeded = null;
}
DgActions.Items.Refresh();
DgActions.UpdateLayout();
}
private void ToolBar_DragDelta(object sender, System.Windows.Controls.Primitives.DragDeltaEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
Left += e.HorizontalChange;
Top += e.VerticalChange;
}
private void BtnToggleRecord_Click(object sender, RoutedEventArgs e)
{
if (BtnToggleRecord.IsChecked == true)
{
StartRecording();
}
else
{
StopRecording();
}
}
private void StartRecording()
{
hookHandler.SubscribeGlobal();
_isWatching = true;
BtnToggleRecord.IsChecked = true;
}
private void StopRecording()
{
hookHandler.Unsubscribe();
_isWatching = false;
BtnToggleRecord.IsChecked = false;
}
private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadWindowPosition();
}
private void LoadWindowPosition()
{
var left = _iniFile.ReadValue(Section, "Left", "0");
var top = _iniFile.ReadValue(Section, "Top", "0");
var width = _iniFile.ReadValue(Section, "Width", "340");
var height = _iniFile.ReadValue(Section, "Height", "56");
Left = double.Parse(left);
Top = double.Parse(top);
Width = double.Parse(width);
Height = double.Parse(height);
}
private void ToolBarTray_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
DragMove();
}
private void BtnToggleScreenShot_Click(object sender, RoutedEventArgs e)
{
if (_isWatching)
{
StopRecording();
CaptureWindow captureWindow = new();
captureWindow.ShowDialog();
string? savedFilePath = captureWindow.SavedFilePath;
Position? point = captureWindow.StartPoint;
if (!string.IsNullOrEmpty(savedFilePath))
{
Execute.Mouse.Click.Template(savedFilePath);
AddAction("mouse_template", null, point, savedFilePath);
}
StartRecording();
}
BtnToggleScreenShot.IsChecked = false;
}
//private void BtnToggleScreenCheck_Click(object sender, RoutedEventArgs e)
//{
// if (_isWatching)
// {
// hookHandler.Unsubscribe();
// CaptureWindow captureWindow = new();
// captureWindow.ShowDialog();
// string? savedFilePath = captureWindow.SavedFilePath;
// Position? point = captureWindow.StartPoint;
// if (!string.IsNullOrEmpty(savedFilePath) && point != null)
// {
// AddAction("screen_check", null, point, savedFilePath);
// }
// }
// BtnToggleScreenCheck.IsChecked = false;
//}
private async void BtnTogglePlay_Click(object sender, RoutedEventArgs e)
{
if (BtnTogglePlay.IsChecked == true)
{
if (Actions.Count == 0)
{
BtnTogglePlay.IsChecked = false;
return;
}
if (!int.TryParse(CbExecuteTimes.Text, out int executeTimes))
{
MessageBox.Show("Recheck the execute times");
BtnTogglePlay.IsChecked = false;
return;
}
if (executeTimes == 0)
{
executeTimes = 1;
}
StopRecording();
ResetActionsExecuteSucceeded();
// Initialize cancellation token source
cancellationTokenSource = new CancellationTokenSource();
int waitTimeDrop = 0;
bool isDrag = false;
for (int i = executeTimes - 1; i >= 0; i--)
{
foreach (Models.Action action in Actions)
{
if (cancellationTokenSource.Token.IsCancellationRequested)
{
return;
}
bool isExecuteSucceeded = true;
action.ExecuteSucceeded = isExecuteSucceeded;
DgActions.Items.Refresh();
DgActions.UpdateLayout();
DgActions.ScrollIntoView(action);
switch (action.Type)
{
case "mouse_click":
if (action.Position == null)
{
isExecuteSucceeded = false;
break;
}
switch (action.Button)
{
case "Left":
await Task.Run(() => Execute.Mouse.Click.Left(action.Position));
break;
case "Middle":
await Task.Run(() => Execute.Mouse.Click.Middle(action.Position));
break;
case "Right":
await Task.Run(() => Execute.Mouse.Click.Right(action.Position));
break;
}
break;
case "mouse_double":
if (action.Position == null)
{
isExecuteSucceeded = false;
break;
}
await Task.Run(() => Execute.Mouse.Click.Double(action.Position));
break;
case "mouse_drag":
if (action.Position == null)
{
isExecuteSucceeded = false;
break;
}
await Task.Run(() =>
{
Execute.Mouse.Drag.Start(action.Position.X, action.Position.Y);
});
isDrag = true;
break;
case "mouse_drop":
if (action.Position == null)
{
isExecuteSucceeded = false;
break;
}
if (isDrag)
{
await Task.Run(() => Execute.Mouse.Drag.End(action.Position.X, action.Position.Y, waitTimeDrop));
isDrag = false;
}
break;
case "mouse_scroll":
if (action.Data == null)
{
isExecuteSucceeded = false;
break;
}
await Task.Run(() => Execute.Mouse.Scroll(int.Parse(action.Data)));
break;
case "mouse_template":
if (action.Data == null)
{
isExecuteSucceeded = false;
break;
}
await Task.Run(() => Execute.Mouse.Click.Template(action.Data));
break;
case "key_press":
if (action.Button == null)
{
isExecuteSucceeded = false;
break;
}
await Task.Run(() => Execute.Keyboard.Type(action.Button));
break;
case "wait":
if (action.Data == null)
{
isExecuteSucceeded = false;
break;
}
if (isDrag)
{
waitTimeDrop = int.Parse(action.Data);
}
else
{
await Task.Run(() => System.Threading.Thread.Sleep(int.Parse(action.Data)));
}
break;
default:
return;
}
if (!isExecuteSucceeded)
{
action.ExecuteSucceeded = isExecuteSucceeded;
DgActions.Items.Refresh();
DgActions.UpdateLayout();
}
}
CbExecuteTimes.Text = i.ToString();
ResetActionsExecuteSucceeded();
}
}
else
{
cancellationTokenSource?.Cancel();
}
BtnTogglePlay.IsChecked = false;
}
private void BtnToggleExpand_Click(object sender, RoutedEventArgs e)
{
if (BtnToggleExpand.IsChecked == true)
{
Width = 510;
Height = 600;
}
else
{
Width = 510;
Height = 56;
}
}
private void CloseApp_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
Close();
}
private void MinimizeApp_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
WindowState = WindowState.Minimized;
}
private void MaximizeApp_Click(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (WindowState == WindowState.Maximized)
{
WindowState = WindowState.Normal;
}
else
{
WindowState = WindowState.Maximized;
}
}
private void PackIcon_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
{
System.Windows.Input.Mouse.OverrideCursor = System.Windows.Input.Cursors.Hand;
}
private void PackIcon_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
{
System.Windows.Input.Mouse.OverrideCursor = null;
}
}
}