-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.axaml.cs
323 lines (290 loc) · 14.1 KB
/
MainWindow.axaml.cs
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
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Platform;
using Avalonia.Threading;
using BatteryDischarger.BatteryRelated;
using BatteryDischarger.DataSource;
using BatteryDischarger.Miscellaneous;
using BatteryDischarger.PlatformSpecificActions;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace BatteryDischarger
{
public partial class MainWindow : Window
{
private BatteryInfoManager batteryManager;
private BatteryWaster batteryWaster;
private object StartStopLock = new object();
private static Task ControlledDischargeTask = null;
public MainWindow()
{
// Helper
batteryManager = new BatteryInfoManager();
batteryWaster = new BatteryWaster();
// GUI
InitializeComponent();
StaticHelperCore.TryCatchIgnore(() => { tbVersion.Text = StaticHelperCore.Version; });
// Icon https://github.com/AvaloniaUI/Avalonia/issues/4488
StaticHelperCore.TryCatchIgnore(() =>
{
var assets = AvaloniaLocator.Current.GetService<IAssetLoader>();
var iconStream = assets.Open(new Uri("avares://BatteryDischarger/Assets/BatteryDischarger.ico"));
this.Icon = new WindowIcon(iconStream);
});
// Generell events
bStartControlledDischarge.Click += BStartControlledDischarge_Click;
bStopControlledDischarge.Click += BStopControlledDischarge_Click;
sTargetBatteryChargeInPercent.PropertyChanged += STargetBatteryChargeInPercent_PropertyChanged;
tbTargetBatteryChargeInPercent.PropertyChanged += TbTargetBatteryChargeInPercent_PropertyChanged;
cbAccelerateBatteryDischarge.PropertyChanged += CbAccelerateBatteryDischarge_PropertyChanged;
cbPreventUnwantedSystemSleepMode.PropertyChanged += CbPreventUnwantedSystemSleepMode_PropertyChanged;
bLegalNotice.Click += BLegalNotice_Click;
// EndAction
List<EndActionEnumNamedContainer> useableEndActions = new List<EndActionEnumNamedContainer>();
foreach (EndActionEnum entry in PlatformSpecificActionsManager.PlatformSpecificEndActions.GetSupportedEndActions()) useableEndActions.Add(new EndActionEnumNamedContainer(entry));
cbActionAtTheSelectedBatteryChargeLevel.Items = useableEndActions;
foreach (var entry in cbActionAtTheSelectedBatteryChargeLevel.Items)
{
if (((EndActionEnumNamedContainer)entry).EmbeddedEnum == IniConfiguration.Instance.EndAction)
{
cbActionAtTheSelectedBatteryChargeLevel.SelectedItem = entry;
break;
}
}
cbActionAtTheSelectedBatteryChargeLevel.SelectionChanged += CbActionAtTheSelectedBatteryChargeLevel_SelectionChanged;
// Language
cbLanguage.Items = LanguageHandler.GetLanguages();
foreach (var entry in cbLanguage.Items)
{
if (entry.ToString().Substring(0, 2) == IniConfiguration.Instance.Language)
{
cbLanguage.SelectedItem = entry;
break;
}
}
cbLanguage.SelectionChanged += CbLanguage_SelectionChanged;
// Load config
tbTargetBatteryChargeInPercent.Text = IniConfiguration.Instance.TargetBatteryChargeInPercent.ToString();
cbAccelerateBatteryDischarge.IsChecked = IniConfiguration.Instance.AccelerateBatteryDischarge;
cbPreventUnwantedSystemSleepMode.IsChecked = IniConfiguration.Instance.PreventUnwantedSystemSleepMode;
// Battery info
Task.Run(() =>
{
while (true)
{
try
{
var x = batteryManager.GetEstimatedChargeRemaining().ToString();
Dispatcher.UIThread.Post(() =>
{
try { tbCurrentBatteryChargeInPercent.Text = x; } catch { }
}, DispatcherPriority.MaxValue);
}
catch { }
Thread.Sleep(1000);
}
});
// Autostart?
if (Program.Autostart)
{
BStartControlledDischarge_Click(null, null);
}
}
private void CbPreventUnwantedSystemSleepMode_PropertyChanged(object? sender, AvaloniaPropertyChangedEventArgs e)
{
if (cbPreventUnwantedSystemSleepMode.IsChecked.HasValue) IniConfiguration.Instance.PreventUnwantedSystemSleepMode = cbPreventUnwantedSystemSleepMode.IsChecked.Value;
}
private void CbLanguage_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (cbLanguage.SelectedItem is not null)
{
var languageCode = cbLanguage.SelectedItem.ToString().Substring(0, 2);
if (IniConfiguration.Instance.Language != languageCode)
{
IniConfiguration.Instance.Language = languageCode;
Thread.Sleep(1000);
// Restart
Process.Start(Process.GetCurrentProcess().MainModule.FileName);
Close();
}
}
}
private void CbActionAtTheSelectedBatteryChargeLevel_SelectionChanged(object? sender, SelectionChangedEventArgs e)
{
if (cbActionAtTheSelectedBatteryChargeLevel.SelectedItem is not null)
{
var endAction = ((EndActionEnumNamedContainer)cbActionAtTheSelectedBatteryChargeLevel.SelectedItem).EmbeddedEnum;
IniConfiguration.Instance.EndAction = endAction;
}
}
private void BLegalNotice_Click(object? sender, RoutedEventArgs e)
{
StaticHelperCore.TryCatchShowErrorMessageBox(() =>
{
var window = new LegalNoticeWindow();
window.ShowDialog(this);
});
}
private void CbAccelerateBatteryDischarge_PropertyChanged(object? sender, Avalonia.AvaloniaPropertyChangedEventArgs e)
{
if (cbAccelerateBatteryDischarge.IsChecked.HasValue) IniConfiguration.Instance.AccelerateBatteryDischarge = cbAccelerateBatteryDischarge.IsChecked.Value;
}
private void BStopControlledDischarge_Click(object? sender, RoutedEventArgs e)
{
lock (StartStopLock)
{
if (bStopControlledDischarge.IsVisible)
{
// Do
StaticHelperCore.TryCatchIgnore(() => { ControlledDischargeTask = null; });
StaticHelperCore.TryCatchIgnore(() => { batteryWaster.Stop(); });
// Change button
ToggleStartStop();
// Reset gui
tbNumberOfMinutesUntilTheSelectedBatteryLevelIsReached.Text = "-";
}
}
}
private void ToggleStartStop()
{
lock (StartStopLock)
{
// Visible
bStartControlledDischarge.IsVisible = !bStartControlledDischarge.IsVisible;
bStopControlledDischarge.IsVisible = !bStopControlledDischarge.IsVisible;
// Enabled
tbTargetBatteryChargeInPercent.IsEnabled = bStartControlledDischarge.IsVisible;
sTargetBatteryChargeInPercent.IsEnabled = bStartControlledDischarge.IsVisible;
cbAccelerateBatteryDischarge.IsEnabled = bStartControlledDischarge.IsVisible;
cbPreventUnwantedSystemSleepMode.IsEnabled = bStartControlledDischarge.IsVisible;
// KeepSystemAwakeService
if (IniConfiguration.Instance.PreventUnwantedSystemSleepMode)
{
StaticHelperCore.TryCatchIgnore(() =>
{
KeepSystemAwakeService.KeepSystemAwake = bStopControlledDischarge.IsVisible;
});
}
}
}
private void TbTargetBatteryChargeInPercent_PropertyChanged(object? sender, Avalonia.AvaloniaPropertyChangedEventArgs e)
{
if (int.TryParse(((TextBox)sender).Text, out int result))
{
if (result >= 0 && result <= 100)
{
sTargetBatteryChargeInPercent.Value = result;
IniConfiguration.Instance.TargetBatteryChargeInPercent = result;
}
}
else
{
((TextBox)sender).Text = "0";
}
}
private void BStartControlledDischarge_Click(object? sender, RoutedEventArgs e)
{
lock (StartStopLock)
{
// If Battery is zero
StaticHelperCore.TryCatchIgnore(() =>
{
if (batteryManager.GetEstimatedChargeRemaining() == 0)
{
var messageBox = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow(Properties.Resources.Error, Properties.Resources.NoBatteryWasDetected, icon: MessageBox.Avalonia.Enums.Icon.Error);
messageBox.ShowDialog(this);
return;
}
});
// Do
if (bStartControlledDischarge.IsVisible)
{
// Check IsBatteryDischaring
try
{
if (!batteryManager.IsBatteryDischaring())
{
var messageBox = MessageBox.Avalonia.MessageBoxManager.GetMessageBoxStandardWindow(Properties.Resources.Information, Properties.Resources.TheDeviceMayStillBeChargingOrTheBatteryStatusCouldNotBeDetermined, icon: MessageBox.Avalonia.Enums.Icon.Info);
messageBox.Show();
}
}
catch (Exception ex)
{
StaticHelperCore.GetErrorMessageBox(ex).ShowDialog(this);
return;
}
// Waste
if (cbAccelerateBatteryDischarge.IsChecked == true)
{
StaticHelperCore.TryCatchShowErrorMessageBox(() =>
{
batteryWaster.Reset();
batteryWaster.StartWasting();
});
}
// Start process which shutdown the pc if battery have been discharged enough
ControlledDischargeTask = new Task(() =>
{
List<int> estimatedTimeRingBuffer = new List<int>();
int estimatedTimeRingBufferIndex = 0;
const int estimatedTimeRingBufferMax = 128;
while (ControlledDischargeTask is not null)
{
StaticHelperCore.TryCatchIgnore(() =>
{
if (batteryManager.GetEstimatedChargeRemaining() <= IniConfiguration.Instance.TargetBatteryChargeInPercent)
{
PlatformSpecificActionsManager.TryExecuteEndAction(IniConfiguration.Instance.EndAction);
Thread.Sleep(10000);
ControlledDischargeTask = null;
Dispatcher.UIThread.Post(() =>
{
ToggleStartStop();
Close();
}, DispatcherPriority.MaxValue);
}
Dispatcher.UIThread.Post(() =>
{
try
{
var estimatedTime = batteryManager.GetEstimatedTimeLeftUntilGivenPerctageHasBeenReached(IniConfiguration.Instance.TargetBatteryChargeInPercent);
if (estimatedTimeRingBuffer.Count < estimatedTimeRingBufferIndex + 1)
{
estimatedTimeRingBuffer.Add(estimatedTime);
}
else
{
estimatedTimeRingBuffer[estimatedTimeRingBufferIndex] = estimatedTime;
}
estimatedTimeRingBufferIndex++;
if (estimatedTimeRingBufferIndex >= estimatedTimeRingBufferMax) estimatedTimeRingBufferIndex = 0;
var calculatedTime = (int)(estimatedTimeRingBuffer.ToList().Sum() / estimatedTimeRingBuffer.Count);
tbNumberOfMinutesUntilTheSelectedBatteryLevelIsReached.Text = "≈" + calculatedTime.ToString();
}
catch { }
}, DispatcherPriority.MaxValue);
});
Thread.Sleep(1000);
}
});
ControlledDischargeTask.Start();
// Change button
ToggleStartStop();
}
}
}
private void STargetBatteryChargeInPercent_PropertyChanged(object? sender, Avalonia.AvaloniaPropertyChangedEventArgs e)
{
var value = ((int)((Slider)sender).Value).ToString();
if (value != tbTargetBatteryChargeInPercent.Text)
{
tbTargetBatteryChargeInPercent.Text = value;
}
}
}
}