-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainWindow.xaml.cs
More file actions
181 lines (153 loc) · 5.49 KB
/
Copy pathMainWindow.xaml.cs
File metadata and controls
181 lines (153 loc) · 5.49 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
using Microsoft.Win32;
using System.Windows;
using System.ComponentModel;
namespace ToastDesk;
public partial class MainWindow : Window, IDisposable
{
private readonly WindowsNotificationPlatform notificationPlatform;
private readonly WindowsNotificationListener windowsNotificationListener;
private readonly WindowsNotificationPlatformResult platformResult;
private readonly AppSettings settings;
private readonly NotificationStore store;
private readonly NotificationSoundService soundService;
private bool allowClose;
private bool isDisposed;
public MainWindow(
NotificationStore store,
AppSettings settings,
WindowsNotificationPlatform notificationPlatform,
WindowsNotificationPlatformResult platformResult)
{
this.store = store;
this.settings = settings;
this.notificationPlatform = notificationPlatform;
this.platformResult = platformResult;
windowsNotificationListener = new WindowsNotificationListener(store, Dispatcher);
soundService = new NotificationSoundService(store, settings);
InitializeComponent();
DataContext = new MainViewModel(store, settings);
Loaded += MainWindow_Loaded;
settings.PropertyChanged += Settings_PropertyChanged;
}
protected override void OnStateChanged(EventArgs e)
{
base.OnStateChanged(e);
if (WindowState == WindowState.Minimized)
{
Hide();
}
}
protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
{
if (allowClose)
{
Dispose();
return;
}
e.Cancel = true;
Hide();
}
public void AllowCloseForExit()
{
allowClose = true;
}
public void Dispose()
{
if (isDisposed)
{
return;
}
isDisposed = true;
settings.PropertyChanged -= Settings_PropertyChanged;
windowsNotificationListener.Dispose();
soundService.Dispose();
}
private void TestNotificationButton_Click(object sender, RoutedEventArgs e)
{
var result = notificationPlatform.SendTestNotification();
ListenerStatusText.Text = $"{result.Message} Capture status: {windowsNotificationListener.LastStatusMessage}";
}
private async void EnableWindowsListenerButton_Click(object sender, RoutedEventArgs e)
{
if (!settings.EnableWindowsCapture)
{
settings.EnableWindowsCapture = true;
return;
}
await StartWindowsCaptureAsync(forcePrompt: true);
}
private void ClearNotificationsButton_Click(object sender, RoutedEventArgs e)
{
store.Clear();
ListenerStatusText.Text = $"{platformResult.Message} Cleared ToastDesk's visible list. Windows capture baseline is preserved.";
}
private async void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
Loaded -= MainWindow_Loaded;
if (settings.EnableWindowsCapture)
{
await StartWindowsCaptureAsync(forcePrompt: false);
}
else
{
ListenerStatusText.Text = $"{platformResult.Message} Windows capture is disabled in Settings.";
EnableWindowsListenerButton.IsEnabled = true;
}
}
private async Task StartWindowsCaptureAsync(bool forcePrompt)
{
EnableWindowsListenerButton.IsEnabled = false;
ListenerStatusText.Text = $"{platformResult.Message} Starting Windows notification capture...";
var result = await windowsNotificationListener.StartAsync(forcePrompt);
if (isDisposed || !IsLoaded)
{
return;
}
ListenerStatusText.Text = $"{platformResult.Message} {result.Message}";
EnableWindowsListenerButton.IsEnabled = !result.IsEnabled;
}
private void SoundPresetComboBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
{
if (SoundPresetComboBox.SelectedValue is string presetId)
{
settings.SoundPresetId = presetId;
}
}
private void ChooseCustomSoundButton_Click(object sender, RoutedEventArgs e)
{
var dialog = new Microsoft.Win32.OpenFileDialog
{
Title = "Choose notification sound",
Filter = "Audio files (*.wav;*.mp3;*.wma)|*.wav;*.mp3;*.wma|All files (*.*)|*.*"
};
if (dialog.ShowDialog(this) != true)
{
return;
}
settings.CustomSoundPath = dialog.FileName;
settings.SoundPresetId = NotificationSoundCatalog.CustomPresetId;
SoundPresetComboBox.SelectedValue = NotificationSoundCatalog.CustomPresetId;
}
private void PreviewSoundButton_Click(object sender, RoutedEventArgs e)
{
if (!soundService.PlayPreview())
{
ListenerStatusText.Text = "No notification sound is available for the current sound settings.";
}
}
private async void Settings_PropertyChanged(object? sender, PropertyChangedEventArgs e)
{
if (isDisposed || e.PropertyName != nameof(AppSettings.EnableWindowsCapture))
{
return;
}
if (settings.EnableWindowsCapture)
{
await StartWindowsCaptureAsync(forcePrompt: false);
return;
}
windowsNotificationListener.Stop();
ListenerStatusText.Text = $"{platformResult.Message} Windows capture is disabled in Settings.";
EnableWindowsListenerButton.IsEnabled = true;
}
}