-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRandomNoteSettingsWindow.axaml.cs
More file actions
581 lines (504 loc) · 22.4 KB
/
RandomNoteSettingsWindow.axaml.cs
File metadata and controls
581 lines (504 loc) · 22.4 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
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Platform.Storage;
using Microsoft.Toolkit.Uwp.Notifications;
using NAudio.CoreAudioApi;
using NAudio.Wave;
using OpenCvSharp;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using AvaloniaWindow = Avalonia.Controls.Window;
namespace ShowWrite
{
public partial class RandomNoteSettingsWindow : AvaloniaWindow
{
private readonly CameraService _cameraService;
private string _currentShortcut;
private string _currentSavePath;
private ToggleSwitch? _enableToggleSwitch;
private ComboBox? _cameraComboBox;
private ComboBox? _microphoneComboBox;
private TextBox? _shortcutTextBox;
private TextBox? _savePathTextBox;
private NumericUpDown? _durationNumericUpDown;
private Button? _setShortcutButton;
private Button? _browseButton;
private Button? _testMicrophoneButton;
private StackPanel? _microphoneTestPanel;
private ProgressBar? _microphoneLevelBar;
private List<string> _microphoneNames = new List<string>();
private WaveInEvent? _waveIn;
private bool _isTestingMicrophone;
// 托盘相关:从 App 中获取的静态引用
private static TrayIcon? _trayIcon;
private static NativeMenuItem? _startMenuItem;
private static NativeMenuItem? _stopMenuItem;
// 录制控制
private static CancellationTokenSource? _currentRecordingCts;
private static Task? _currentRecordingTask;
static RandomNoteSettingsWindow()
{
if (Application.Current?.ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
{
desktop.Exit += OnApplicationExit;
}
}
public RandomNoteSettingsWindow(CameraService cameraService)
{
_cameraService = cameraService ?? throw new ArgumentNullException(nameof(cameraService));
InitializeComponent();
// 查找控件
_enableToggleSwitch = this.FindControl<ToggleSwitch>("EnableToggleSwitch");
_cameraComboBox = this.FindControl<ComboBox>("CameraComboBox");
_microphoneComboBox = this.FindControl<ComboBox>("MicrophoneComboBox");
_shortcutTextBox = this.FindControl<TextBox>("ShortcutTextBox");
_savePathTextBox = this.FindControl<TextBox>("SavePathTextBox");
_durationNumericUpDown = this.FindControl<NumericUpDown>("DurationNumericUpDown");
_setShortcutButton = this.FindControl<Button>("SetShortcutButton");
_browseButton = this.FindControl<Button>("BrowseButton");
_testMicrophoneButton = this.FindControl<Button>("TestMicrophoneButton");
_microphoneTestPanel = this.FindControl<StackPanel>("MicrophoneTestPanel");
_microphoneLevelBar = this.FindControl<ProgressBar>("MicrophoneLevelBar");
// 绑定摄像头列表
if (_cameraComboBox != null)
_cameraComboBox.ItemsSource = _cameraService.GetAvailableCameraNames();
// 绑定麦克风列表
InitializeMicrophoneList();
// 加载设置
var config = Config.Load();
var settings = config.RandomNote;
if (_enableToggleSwitch != null)
_enableToggleSwitch.IsChecked = settings.Enabled;
if (_cameraComboBox != null)
{
var cameraNames = _cameraService.GetAvailableCameraNames();
if (settings.DefaultCameraIndex >= 0 && settings.DefaultCameraIndex < cameraNames.Count)
_cameraComboBox.SelectedIndex = settings.DefaultCameraIndex;
else if (cameraNames.Any())
_cameraComboBox.SelectedIndex = 0;
}
// 初始化保存路径
_currentSavePath = settings.SavePath;
if (string.IsNullOrEmpty(_currentSavePath))
_currentSavePath = GetDefaultSavePath();
if (_savePathTextBox != null)
_savePathTextBox.Text = _currentSavePath;
// 初始化麦克风选择
if (_microphoneComboBox != null && settings.MicrophoneIndex >= 0 && settings.MicrophoneIndex < _microphoneNames.Count)
_microphoneComboBox.SelectedIndex = settings.MicrophoneIndex;
_currentShortcut = settings.ShortcutKey;
if (_shortcutTextBox != null)
_shortcutTextBox.Text = settings.ShortcutKey;
if (_durationNumericUpDown != null)
_durationNumericUpDown.Value = settings.RecordingDurationMinutes;
if (_setShortcutButton != null)
_setShortcutButton.Click += SetShortcutButton_Click;
if (_browseButton != null)
_browseButton.Click += BrowseButton_Click;
if (_testMicrophoneButton != null)
_testMicrophoneButton.Click += TestMicrophoneButton_Click;
// 初始化托盘引用
InitializeTrayIcon();
// 根据设置更新托盘状态
RefreshTrayIconState();
}
private void InitializeMicrophoneList()
{
_microphoneNames.Clear();
for (int i = 0; i < WaveInEvent.DeviceCount; i++)
{
var capabilities = WaveInEvent.GetCapabilities(i);
_microphoneNames.Add(capabilities.ProductName);
}
if (_microphoneComboBox != null)
_microphoneComboBox.ItemsSource = _microphoneNames.ToList();
}
private static string GetDefaultSavePath()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), "ShowWrite", "RandomNote");
}
private async void BrowseButton_Click(object? sender, RoutedEventArgs e)
{
var folders = await StorageProvider.OpenFolderPickerAsync(new FolderPickerOpenOptions
{
Title = "选择保存路径",
AllowMultiple = false
});
if (folders.Count > 0)
{
_currentSavePath = folders[0].Path.LocalPath;
if (_savePathTextBox != null)
_savePathTextBox.Text = _currentSavePath;
}
}
private void TestMicrophoneButton_Click(object? sender, RoutedEventArgs e)
{
if (_isTestingMicrophone)
{
StopMicrophoneTest();
}
else
{
StartMicrophoneTest();
}
}
private void StartMicrophoneTest()
{
if (_microphoneComboBox == null || _microphoneComboBox.SelectedIndex < 0)
return;
int deviceIndex = _microphoneComboBox.SelectedIndex;
try
{
_waveIn = new WaveInEvent
{
DeviceNumber = deviceIndex,
WaveFormat = new WaveFormat(16000, 1)
};
_waveIn.DataAvailable += WaveIn_DataAvailable;
_waveIn.StartRecording();
_isTestingMicrophone = true;
if (_testMicrophoneButton != null)
_testMicrophoneButton.Content = "停止";
if (_microphoneTestPanel != null)
_microphoneTestPanel.IsVisible = true;
}
catch (Exception ex)
{
Debug.WriteLine($"麦克风测试失败: {ex.Message}");
ShowNotification("闪记", "无法启动麦克风测试");
}
}
private void StopMicrophoneTest()
{
if (_waveIn != null)
{
_waveIn.StopRecording();
_waveIn.DataAvailable -= WaveIn_DataAvailable;
_waveIn.Dispose();
_waveIn = null;
}
_isTestingMicrophone = false;
if (_testMicrophoneButton != null)
_testMicrophoneButton.Content = "测试";
if (_microphoneTestPanel != null)
_microphoneTestPanel.IsVisible = false;
if (_microphoneLevelBar != null)
_microphoneLevelBar.Value = 0;
}
private void WaveIn_DataAvailable(object? sender, WaveInEventArgs e)
{
float max = 0;
for (int i = 0; i < e.BytesRecorded; i += 2)
{
short sample = (short)(e.Buffer[i + 1] << 8 | e.Buffer[i]);
float sample32 = sample / 32768f;
if (sample32 > max) max = sample32;
}
Avalonia.Threading.Dispatcher.UIThread.Post(() =>
{
if (_microphoneLevelBar != null)
_microphoneLevelBar.Value = max * 100;
});
}
private static void InitializeTrayIcon()
{
if (_trayIcon != null) return;
_trayIcon = App.RandomNoteTrayIcon;
_startMenuItem = App.StartMenuItem;
_stopMenuItem = App.StopMenuItem;
if (_trayIcon != null)
{
if (_startMenuItem != null)
_startMenuItem.Click += (s, e) => StartRecordingFromTray();
if (_stopMenuItem != null)
_stopMenuItem.Click += (s, e) => StopRecording();
if (_trayIcon.Menu is NativeMenu menu)
{
var settingsItem = menu.Items.OfType<NativeMenuItem>().FirstOrDefault(item => item.Header?.ToString() == "设置");
if (settingsItem != null)
settingsItem.Click += (s, e) => OpenSettingsWindow();
}
_trayIcon.Clicked += (s, e) => OpenSettingsWindow();
}
}
private static void RefreshTrayIconState()
{
var settings = Config.Load().RandomNote;
if (_trayIcon != null)
{
_trayIcon.IsVisible = settings.Enabled;
UpdateTrayMenuState();
}
}
private static void UpdateTrayMenuState()
{
bool isRecording = IsRecording();
if (_startMenuItem != null)
_startMenuItem.IsEnabled = !isRecording;
if (_stopMenuItem != null)
_stopMenuItem.IsEnabled = isRecording;
}
public static bool IsRecording() => _currentRecordingCts != null && !_currentRecordingCts.IsCancellationRequested;
public static async void StartRecordingFromTray(CameraService? cameraService = null, Action<string>? showNotification = null)
{
var settings = Config.Load().RandomNote;
if (!settings.Enabled) return;
cameraService ??= Application.Current?.Resources["CameraService"] as CameraService;
if (cameraService == null)
{
if (showNotification != null)
showNotification("摄像头服务不可用");
else
ShowNotification("闪记", "摄像头服务不可用");
return;
}
var cameraNames = cameraService.GetAvailableCameraNames();
if (cameraNames.Count == 0)
{
if (showNotification != null)
showNotification("未检测到可用摄像头");
else
ShowNotification("闪记", "未检测到可用摄像头");
return;
}
if (settings.DefaultCameraIndex < 0 || settings.DefaultCameraIndex >= cameraNames.Count)
{
if (showNotification != null)
showNotification("默认摄像头配置无效,请在设置中重新选择");
else
ShowNotification("闪记", "默认摄像头配置无效,请在设置中重新选择");
return;
}
var cameraName = cameraNames[settings.DefaultCameraIndex];
var cameraIndex = cameraService.GetCameraIndexByName(cameraName);
if (cameraIndex < 0)
{
if (showNotification != null)
showNotification($"无法找到摄像头 \"{cameraName}\"");
else
ShowNotification("闪记", $"无法找到摄像头 \"{cameraName}\"");
return;
}
StopRecording();
_currentRecordingCts = new CancellationTokenSource();
var duration = TimeSpan.FromMinutes(settings.RecordingDurationMinutes);
_currentRecordingTask = Task.Run(() =>
{
var recorder = new RandomNoteRecorder();
recorder.StartRecording(cameraIndex, duration, _currentRecordingCts.Token);
});
if (showNotification != null)
showNotification($"开始录制,时长 {settings.RecordingDurationMinutes} 分钟");
else
ShowNotification("闪记", $"开始录制,时长 {settings.RecordingDurationMinutes} 分钟");
UpdateTrayMenuState();
}
public static void StopRecording()
{
if (_currentRecordingCts != null)
{
_currentRecordingCts.Cancel();
try
{
_currentRecordingTask?.Wait(3000);
}
catch (AggregateException) { }
_currentRecordingCts.Dispose();
_currentRecordingCts = null;
_currentRecordingTask = null;
ShowNotification("闪记", "录制已停止");
UpdateTrayMenuState();
}
}
private static void OpenSettingsWindow()
{
var cameraService = Application.Current?.Resources["CameraService"] as CameraService;
if (cameraService == null) return;
var window = new RandomNoteSettingsWindow(cameraService);
window.Show();
}
public static void ShowNotification(string title, string message)
{
new ToastContentBuilder()
.AddText(title)
.AddText(message)
.Show();
}
private async void SetShortcutButton_Click(object? sender, RoutedEventArgs e)
{
var dialog = new ShortcutInputDialog();
var result = await dialog.ShowDialog<string?>(this);
if (!string.IsNullOrEmpty(result))
{
_currentShortcut = result;
if (_shortcutTextBox != null)
_shortcutTextBox.Text = result;
}
}
private void OkButton_Click(object? sender, RoutedEventArgs e)
{
StopMicrophoneTest();
var config = Config.Load();
config.RandomNote.Enabled = _enableToggleSwitch?.IsChecked == true;
config.RandomNote.DefaultCameraIndex = _cameraComboBox?.SelectedIndex ?? -1;
config.RandomNote.MicrophoneIndex = _microphoneComboBox?.SelectedIndex ?? -1;
config.RandomNote.SavePath = _currentSavePath;
config.RandomNote.ShortcutKey = _currentShortcut;
config.RandomNote.RecordingDurationMinutes = (int)(_durationNumericUpDown?.Value ?? 5);
config.Save();
RefreshTrayIconState();
Close();
}
private void CancelButton_Click(object? sender, RoutedEventArgs e)
{
StopMicrophoneTest();
Close();
}
private static void OnApplicationExit(object? sender, ControlledApplicationLifetimeExitEventArgs e)
{
StopRecording();
}
public static bool TryTriggerShortcut(string pressedKeyString, CameraService cameraService, Action<string> showNotification)
{
var settings = Config.Load().RandomNote;
if (!settings.Enabled) return false;
if (string.IsNullOrEmpty(settings.ShortcutKey)) return false;
if (pressedKeyString != settings.ShortcutKey) return false;
var cameraNames = cameraService.GetAvailableCameraNames();
if (cameraNames.Count == 0)
{
showNotification("未检测到可用摄像头");
return false;
}
if (settings.DefaultCameraIndex < 0 || settings.DefaultCameraIndex >= cameraNames.Count)
{
showNotification("默认摄像头配置无效,请在设置中重新选择");
return false;
}
var cameraName = cameraNames[settings.DefaultCameraIndex];
var cameraIndex = cameraService.GetCameraIndexByName(cameraName);
if (cameraIndex < 0)
{
showNotification($"无法找到摄像头 \"{cameraName}\"");
return false;
}
StopRecording();
_currentRecordingCts = new CancellationTokenSource();
var duration = TimeSpan.FromMinutes(settings.RecordingDurationMinutes);
_currentRecordingTask = Task.Run(() =>
{
var recorder = new RandomNoteRecorder();
recorder.StartRecording(cameraIndex, duration, _currentRecordingCts.Token);
});
showNotification($"开始录制,时长 {settings.RecordingDurationMinutes} 分钟");
UpdateTrayMenuState();
return true;
}
}
// 快捷键设置对话框
public class ShortcutInputDialog : AvaloniaWindow
{
private TextBlock _infoText;
private TextBlock _keyText;
private string? _result;
public ShortcutInputDialog()
{
Title = "设置快捷键";
Width = 300;
Height = 150;
WindowStartupLocation = WindowStartupLocation.CenterOwner;
CanResize = false;
var stack = new StackPanel { Margin = new Thickness(20), Spacing = 10 };
_infoText = new TextBlock { Text = "请按下快捷键组合(例如 Alt+Z)" };
_keyText = new TextBlock { FontSize = 16, Text = "等待按键..." };
var btnCancel = new Button { Content = "取消", HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center, Width = 80 };
btnCancel.Click += (s, e) => { _result = null; Close(); };
stack.Children.Add(_infoText);
stack.Children.Add(_keyText);
stack.Children.Add(btnCancel);
Content = stack;
KeyDown += OnKeyDown;
}
private void OnKeyDown(object? sender, KeyEventArgs e)
{
var modifiers = e.KeyModifiers;
var key = e.Key;
if (key == Key.Escape)
{
_result = null;
Close();
return;
}
// 排除仅修饰键
if (key == Key.LeftAlt || key == Key.RightAlt || key == Key.LeftCtrl || key == Key.RightCtrl ||
key == Key.LeftShift || key == Key.RightShift || key == Key.LWin || key == Key.RWin)
return;
var parts = new List<string>();
if (modifiers.HasFlag(KeyModifiers.Control)) parts.Add("Ctrl");
if (modifiers.HasFlag(KeyModifiers.Alt)) parts.Add("Alt");
if (modifiers.HasFlag(KeyModifiers.Shift)) parts.Add("Shift");
parts.Add(key.ToString());
_result = string.Join("+", parts);
_keyText.Text = _result;
Close();
}
public async Task<string?> ShowDialog(AvaloniaWindow parent)
{
await ShowDialog(parent);
return _result;
}
}
// 录制器类(支持取消)
public class RandomNoteRecorder
{
public void StartRecording(int cameraIndex, TimeSpan duration, CancellationToken cancellationToken = default)
{
Task.Run(() => DoRecording(cameraIndex, duration, cancellationToken));
}
private void DoRecording(int cameraIndex, TimeSpan duration, CancellationToken cancellationToken)
{
try
{
using var capture = new VideoCapture(cameraIndex, VideoCaptureAPIs.MSMF);
if (!capture.IsOpened()) return;
capture.Set(VideoCaptureProperties.FrameWidth, 1280);
capture.Set(VideoCaptureProperties.FrameHeight, 720);
int width = (int)capture.Get(VideoCaptureProperties.FrameWidth);
int height = (int)capture.Get(VideoCaptureProperties.FrameHeight);
double fps = capture.Get(VideoCaptureProperties.Fps);
if (fps <= 0) fps = 30;
string dir = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyVideos), "ShowWrite", "RandomNote");
Directory.CreateDirectory(dir);
string fileName = $"闪记_{DateTime.Now:yyyyMMdd_HHmmss}.mp4";
string filePath = Path.Combine(dir, fileName);
using var writer = new VideoWriter(filePath, FourCC.MP4V, fps, new OpenCvSharp.Size(width, height));
if (!writer.IsOpened()) return;
var frame = new Mat();
DateTime start = DateTime.Now;
int frameIntervalMs = (int)(1000.0 / fps);
while ((DateTime.Now - start) < duration)
{
if (cancellationToken.IsCancellationRequested)
break;
if (!capture.Read(frame) || frame.Empty())
break;
writer.Write(frame);
Thread.Sleep(frameIntervalMs);
}
}
catch (Exception ex)
{
Debug.WriteLine($"录制失败: {ex.Message}");
}
}
}
}