-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.xaml.cs
970 lines (802 loc) · 35.4 KB
/
App.xaml.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
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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
using System.Windows;
using System.Runtime.InteropServices;
using System.Drawing.Text;
using Windows.System.Power;
using Microsoft.Win32;
using Microsoft.Win32.SafeHandles;
using System.Drawing;
using System.Windows.Controls;
using Wpf.Ui.Controls;
using Wpf.Ui.Appearance;
using Hardcodet.Wpf.TaskbarNotification;
using Wpf.Ui.Input;
using System.Windows.Input;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Windows.Media.Imaging;
using System.Windows.Interop;
using LiveCharts;
using LiveCharts.Defaults;
using LibreHardwareMonitor.Hardware;
using System.Security.Principal;
using LibreHardwareMonitor.Hardware.Cpu;
using Microsoft.Win32.TaskScheduler;
using System.Threading.Tasks;
using System.Windows.Media.Animation;
using System.Xml.Linq;
/// MAIN TODO:
/// SUFFERING:
// graphs weirdly lag other powertray windows and use way too much CPU
// card expanders have annoyingly small click area
// sub-context menu (power plan) broked if not first in the list
// scroll is too sensitive
// figure out how to use win32 API to make it give weird information (and use same battery as kernel)
// make option for multiple batteries besides the auto-selected one
namespace PowerTray
{
public class PowerPlan
{
public readonly string Name;
public Guid Guid;
public PowerPlan(string name, Guid guid)
{
Name = name;
Guid = guid;
}
}
public partial class App : System.Windows.Application
{
// import dll for destroy icon
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern bool DestroyIcon(IntPtr handle);
public enum DisplayedInfo { Percentage, ReportedChargeRate, CalculatedChargeRate, PowerPlan};
// PowerPlans
public static Wpf.Ui.Controls.MenuItem pwrPlans;
public static Guid active_plan;
// Params ---
public static List<PowerPlan> plans = new List<PowerPlan>();
public static bool startup = false;
public static bool adminstartup = false;
public static BatteryStatus? charging = null;
public static string acplanName = "Balanced";
public static string batteryPlanName = "Balanced";
public static bool notifs = true;
public static DisplayedInfo tray_display = DisplayedInfo.Percentage; // (CHANGEABLE)
static float trayFontSize = 11f; // (CHANGEABLE)
public static String trayFontType = "Segoe UI";
static float trayFontQualityMultiplier = 2.0f;
public static int maxChargeHistoryLength = 60; // in seconds (CHANGEABLE)
public static int graphsHistoryLength = 120; // in seconds (CHANGEABLE)
public static int trayRefreshRate = 1000; // in milliseconds (CHANGEABLE)
public static int batInfoRefreshRate = 1000; // in milliseconds (CHANGEABLE)
public static int graphRefreshRate = 1000; // in milliseconds (CHANGEABLE)
static Color chargingColor = Color.Green;
static Color highColor = Color.Black;
static Color highDarkColor = Color.White;
static Color mediumColor = Color.FromArgb(255, 220, 100, 20);
static Color lowColor = Color.FromArgb(255, 232, 17, 35);
static Color boostColor = Color.Blue;
static Color boostDarkColor = Color.Cyan;
public static int highAmount = 40; // (CHANGEABLE)
public static int mediumAmount = 25; // (CHANGEABLE)
public static int lowAmount = 0;
// ---
public static uint batteryTag = 0;
public static SafeFileHandle batteryHandle = null;
public static bool auto_switch = false;
public static bool firstTime = true;
public static bool graphFirstTime = true;
public static List<int> remainChargeHistory = new List<int>();
public static List<long> chargeHistoryTime = new List<long>();
public static long graphCreatedTimeStamp = -1;
public static ChartValues<ObservablePoint> calcChargeRateGraph = new ChartValues<ObservablePoint>();
public static ChartValues<ObservablePoint> chargeRateGraph = new ChartValues<ObservablePoint>();
public static ChartValues<ObservablePoint> cpuWattageGraph = new ChartValues<ObservablePoint>();
// use open hardware to get info about computertron
static Computer c = new Computer()
{
IsCpuEnabled = true,
};
public static long calcChargeRateMw = 0;
public static long calcTimeDelta = 0;
public static bool windowDarkMode = true;
public static bool taskbarDarkMode = true;
public static BatInfo batteryInfoWindow;
public static Graph graphWindow;
public static Settings settingsWindow;
public static bool aot = true; // always on top
static System.Windows.Threading.DispatcherTimer tray_timer = new System.Windows.Threading.DispatcherTimer();
static System.Windows.Threading.DispatcherTimer info_timer = new System.Windows.Threading.DispatcherTimer();
static System.Windows.Threading.DispatcherTimer graph_timer = new System.Windows.Threading.DispatcherTimer();
static TaskbarIcon trayIcon;
static ToolTip toolTip;
static Font trayFont = new Font(trayFontType, trayFontSize * trayFontQualityMultiplier, System.Drawing.FontStyle.Bold);
private static ICommand BatInfoOpen = new RelayCommand<dynamic>(action => CreateInfoWindow(), canExecute => true);
private static ICommand SettingsOpen = new RelayCommand<dynamic>(action => CreateSettingsWindow(), canExecute => true);
private static ICommand QuitProgram = new RelayCommand<dynamic>(action => Quit(), canExecute => true);
private static ICommand ClearBuffer = new RelayCommand<dynamic>(action => ResetBuffer(), canExecute => true);
private static ICommand TraySwitch = new RelayCommand<dynamic>(action => SwitchTrayInfo(), canExecute => true);
private static ICommand GraphsOpen = new RelayCommand<dynamic>(action => CreateGraphWindow(), canExecute => true);
private static string GetExecutableLocation()
{
return Process.GetCurrentProcess().MainModule.FileName;
}
public static void ManageAdminStartup(bool delete)
{
if (IsAdministrator())
{
// Get the service on the local machine
using (TaskService ts = new TaskService())
{
if (delete)
{
if (ts.RootFolder.Tasks.Exists("PowerTray"))
{
ts.RootFolder.DeleteTask("PowerTray");
}
}
else
{
ManageAdminStartup(true);
// Create a new task definition and assign properties
TaskDefinition td = ts.NewTask();
td.RegistrationInfo.Description = "Run PowerTray on Startup";
td.Triggers.Add(new LogonTrigger { });
td.Actions.Add(new ExecAction(GetExecutableLocation()));
td.Principal.RunLevel = TaskRunLevel.Highest;
td.Settings.DisallowStartIfOnBatteries = false;
td.Settings.StopIfGoingOnBatteries = false;
td.Settings.ExecutionTimeLimit = TimeSpan.Zero;
var registeredTask = ts.RootFolder.RegisterTaskDefinition(@"PowerTray", td);
registeredTask.Enabled = true;
}
}
}
}
public static void ManageStartup(bool delete)
{
var keyPath = @"Software\Microsoft\Windows\CurrentVersion\Run";
if (delete)
{
var key = Registry.CurrentUser.OpenSubKey(keyPath, true);
if (key != null && key.GetValue("PowerTray") != null)
{
key.DeleteValue("PowerTray");
}
}
else
{
ManageStartup(true);
Registry.SetValue(@"HKEY_CURRENT_USER\" + keyPath, "PowerTray", GetExecutableLocation(), RegistryValueKind.String);
}
}
public static float GetHardwareInfo()
{
float cpuWattage = 0;
foreach (var hardware in c.Hardware)
{
if (hardware.HardwareType == HardwareType.Cpu)
{
// only fire the update when found
hardware.Update();
// loop through the data
foreach (var sensor in hardware.Sensors)
{
if (sensor.SensorType == SensorType.Power && sensor.Name.ToLower().Contains("pack"))
{
cpuWattage = sensor.Value.GetValueOrDefault();
}
}
}
}
return cpuWattage;
}
public static void ResetGraphs()
{
calcChargeRateGraph.Clear();
chargeRateGraph.Clear();
cpuWattageGraph.Clear();
graphCreatedTimeStamp = -1;
}
public static void ResetBuffer()
{
firstTime = true;
remainChargeHistory = new List<int>();
chargeHistoryTime = new List<long>();
calcChargeRateMw = 0;
calcTimeDelta = 0;
}
// ERROR HANDLING
void ShowUnhandledException(Exception e, string unhandledExceptionType, bool promptUserForShutdown)
{
var messageBoxTitle = $"Unexpected Error Occurred: {unhandledExceptionType}";
var messageBoxMessage = $"The following exception occurred:\n\n{e}";
var messageBoxButtons = System.Windows.MessageBoxButton.OK;
if (promptUserForShutdown)
{
messageBoxMessage += "\n\nNormally the program would DIE now. Should we let it die painlessly, or let it suffer?";
messageBoxButtons = System.Windows.MessageBoxButton.YesNo;
}
// Let the user decide if the app should die or not (if applicable).
if (System.Windows.MessageBox.Show(messageBoxMessage, messageBoxTitle, messageBoxButtons) == System.Windows.MessageBoxResult.Yes)
{
Application.Current.Shutdown();
}
}
private void SetupUnhandledExceptionHandling()
{
// Catch exceptions from all threads in the AppDomain.
AppDomain.CurrentDomain.UnhandledException += (sender, args) =>
ShowUnhandledException(args.ExceptionObject as Exception, "AppDomain.CurrentDomain.UnhandledException", false);
// Catch exceptions from each AppDomain that uses a task scheduler for async operations.
TaskScheduler.UnobservedTaskException += (sender, args) =>
ShowUnhandledException(args.Exception, "TaskScheduler.UnobservedTaskException", false);
// Catch exceptions from a single specific UI dispatcher thread.
Dispatcher.UnhandledException += (sender, args) =>
{
// If we are debugging, let Visual Studio handle the exception and take us to the code that threw it.
if (!Debugger.IsAttached)
{
args.Handled = true;
ShowUnhandledException(args.Exception, "Dispatcher.UnhandledException", true);
}
};
}
public static void LoadSettings()
{
var settings = (Options)settingsWindow.AppConfig.Sections["Options"];
aot = settings.AlwaysOnTop;
batteryInfoWindow.Topmost = aot;
trayFontSize = settings.FontSize;
maxChargeHistoryLength = settings.BufferSize;
graphsHistoryLength = settings.HistoryLength;
startup = settings.Startup;
adminstartup = settings.AdminStartup;
// prevent overlap
if (startup && adminstartup)
{
startup = false;
}
auto_switch = settings.AutoSwitch;
notifs = settings.Notifs;
acplanName = settings.ACPlan;
batteryPlanName = settings.BatteryPlan;
trayRefreshRate = settings.TrayRefreshRate;
tray_timer.Interval = new TimeSpan(0, 0, 0, 0, trayRefreshRate);
batInfoRefreshRate = settings.BatInfoRefreshRate;
info_timer.Interval = new TimeSpan(0, 0, 0, 0, batInfoRefreshRate);
graphRefreshRate = settings.GraphRefreshRate;
graph_timer.Interval = new TimeSpan(0, 0, 0, 0, graphRefreshRate);
highAmount = settings.MediumCharge;
mediumAmount = settings.LowCharge;
tray_display = settings.TrayText;
// reset trayfont
trayFont = new Font(trayFontType, trayFontSize * trayFontQualityMultiplier, settings.FontStyle);
// add startup
if (startup)
{
ManageStartup(false);
}
else
{
ManageStartup(true);
}
if (adminstartup)
{
ManageAdminStartup(false);
}
else
{
ManageAdminStartup(true);
}
}
private void RebuildContextMenu()
{
var batteryInfo = new Wpf.Ui.Controls.MenuItem()
{
Header = "Battery Info",
Icon = new SymbolIcon(SymbolRegular.Info20, 14, false),
Command = BatInfoOpen,
};
var graphs = new Wpf.Ui.Controls.MenuItem()
{
Header = "Graphs",
Icon = new SymbolIcon(SymbolRegular.DataUsage20, 14, false),
Command = GraphsOpen,
};
var switchInfo = new Wpf.Ui.Controls.MenuItem()
{
Header = "Switch Tray Data",
Icon = new SymbolIcon(SymbolRegular.ArrowRepeatAll20, 14, false),
ToolTip = "Cycle the information displayed on the tray icon",
Command = TraySwitch,
};
var clearBuffer = new Wpf.Ui.Controls.MenuItem()
{
Header = "Clear Buffer",
Icon = new SymbolIcon(SymbolRegular.Delete20, 14, false),
Command = ClearBuffer
};
pwrPlans = new Wpf.Ui.Controls.MenuItem()
{
Header = "Power Plans",
Icon = new SymbolIcon(SymbolRegular.BatterySaver20, 14, false),
};
PowerPlans.RefreshPowerPlans();
var settings = new Wpf.Ui.Controls.MenuItem()
{
Header = "Settings",
Icon = new SymbolIcon(SymbolRegular.Settings20, 14, false),
Command = SettingsOpen,
};
var exit = new Wpf.Ui.Controls.MenuItem()
{
Header = "Quit",
Icon = new SymbolIcon(SymbolRegular.Power20, 14, false),
Command = QuitProgram
};
var contextMenu = new ContextMenu()
{
Items = { pwrPlans, batteryInfo, graphs, switchInfo, clearBuffer, settings, exit }
};
trayIcon.ContextMenu = contextMenu;
}
private void App_Startup(object sender, StartupEventArgs e)
{
// repair startup
if (startup)
{
ManageStartup(false);
}
if (startup)
{
ManageAdminStartup(false);
}
// debug
SetupUnhandledExceptionHandling();
c.Open(); // open hardware for inspection
batteryInfoWindow = new BatInfo();
batteryInfoWindow.Topmost = aot;
graphWindow = new Graph();
graphWindow.Topmost = aot;
settingsWindow = new Settings();
updateWindowIcons();
LoadSettings();
// apply theme
ApplicationThemeManager.Apply(
(checkDarkMode()[1] ? ApplicationTheme.Dark : ApplicationTheme.Light), // Theme type
WindowBackdropType.Mica, // Background type
true // Whether to change accents automatically
);
Wpf.Ui.Appearance.SystemThemeWatcher.Watch(batteryInfoWindow);
// get battery tag from info
var info = BatteryManagement.GetBatteryTag();
batteryHandle = info[0];
batteryTag = info[1];
toolTip = new ToolTip();
trayIcon = new TaskbarIcon()
{
TrayToolTip = toolTip,
LeftClickCommand = BatInfoOpen,
DoubleClickCommand = SettingsOpen,
};
RebuildContextMenu();
// Create Update Timer for tray icon
tray_timer.Interval = new TimeSpan(0, 0, 0, 0, trayRefreshRate);
tray_timer.Tick += new EventHandler(UpdateTray);
tray_timer.Start();
info_timer.Interval = new TimeSpan(0, 0, 0, 0, batInfoRefreshRate);
info_timer.Tick += new EventHandler(BatInfo.UpdateData);
info_timer.Start();
graph_timer.Interval = new TimeSpan(0, 0, 0, 0, graphRefreshRate);
graph_timer.Tick += new EventHandler(UpdateGraphs);
graph_timer.Start();
BatInfo.UpdateData(null, null);
UpdateTray(null, null);
UpdateGraphs(null, null);
taskbarDarkMode = checkDarkMode()[0];
}
private void UpdateGraphs(object sender, EventArgs e)
{
long timeStamp = DateTime.Now.Ticks;
var bat_info = BatteryManagement.GetBatteryInfo(batteryTag, batteryHandle);
int fullChargeCapMwh = (int)bat_info["Battery Capacity mWh"];
int remainChargeCapMwh = (int)bat_info["Remaining Charge mWh"];
int chargeRateMw = (int)bat_info["Reported Charge Rate mW"];
if (graphCreatedTimeStamp == -1)
{
graphCreatedTimeStamp = timeStamp;
}
long timeDelta = (long)((timeStamp - graphCreatedTimeStamp) / 10000000);
var keys = chargeRateGraph.Select(p => p.X).ToArray();
while (keys.Length > 0 && timeDelta - keys[0] > graphsHistoryLength)
{
calcChargeRateGraph.RemoveAt(0);
chargeRateGraph.RemoveAt(0);
cpuWattageGraph.RemoveAt(0);
keys = chargeRateGraph.Select(p => p.X).ToArray();
}
int timetemp = (int)timeDelta;
calcChargeRateGraph.Add(new ObservablePoint(timetemp, -calcChargeRateMw / 1000d));
chargeRateGraph.Add(new ObservablePoint(timetemp, -chargeRateMw / 1000d));
var hwinfo = GetHardwareInfo();
cpuWattageGraph.Add(new ObservablePoint(timetemp, hwinfo));
if (graphFirstTime && keys.Length == 1)
{
graphFirstTime = false;
calcChargeRateGraph.RemoveAt(0);
chargeRateGraph.RemoveAt(0);
cpuWattageGraph.RemoveAt(0);
}
}
private static void updateWindowIcons()
{
// switch between dark and light icons (for taskbar)
if (taskbarDarkMode)
{
batteryInfoWindow.Icon = ToImageSource(PowerTray.Resources.DarkIcon);
settingsWindow.Icon = ToImageSource(PowerTray.Resources.DarkIcon);
graphWindow.Icon = ToImageSource(PowerTray.Resources.DarkIcon);
}
else
{
batteryInfoWindow.Icon = ToImageSource(PowerTray.Resources.LightIcon);
settingsWindow.Icon = ToImageSource(PowerTray.Resources.LightIcon);
graphWindow.Icon = ToImageSource(PowerTray.Resources.LightIcon);
}
}
private void UpdateTray(object sender, EventArgs e)
{
if (active_plan != PowerPlans.GetActivePlanGuid())
{
PowerPlans.RefreshPowerPlans();
}
var info = BatteryManagement.GetBatteryTag(); // prevent data from expiring
batteryHandle = info[0];
batteryTag = info[1];
// check if dark mode is enabled ---
bool darkModeEnabled = checkDarkMode()[0];
bool appdarkMode = checkDarkMode()[1];
if (appdarkMode != windowDarkMode)
{
windowDarkMode = appdarkMode;
ApplicationThemeManager.Apply(
(appdarkMode ? ApplicationTheme.Dark : ApplicationTheme.Light));
//Style style = (Style)Resources["Style"];
RebuildContextMenu();
}
if (darkModeEnabled != taskbarDarkMode)
{
taskbarDarkMode = darkModeEnabled;
updateWindowIcons();
}
// ---
var bat_info = BatteryManagement.GetBatteryInfo(batteryTag, batteryHandle);
int remainChargeCapMwh = (int)bat_info["Remaining Charge mWh"];
int chargeRateMw = (int)bat_info["Reported Charge Rate mW"];
// plans
if (auto_switch && charging != (BatteryStatus)bat_info["Status"])
{
charging = (BatteryStatus)bat_info["Status"];
if ((charging == BatteryStatus.Idle && (BatteryStatus)bat_info["Status"] == BatteryStatus.Charging) ||
(charging == BatteryStatus.Charging && (BatteryStatus)bat_info["Status"] == BatteryStatus.Idle))
{
// since it's still plugged in, don't do anything (idle is when it's not charging but stil connected to AC)
}
else // switch plan
{
var ac = charging != BatteryStatus.Discharging;
string name = batteryPlanName;
if (ac)
{
name = acplanName;
}
Guid guid = plans[0].Guid;
foreach (PowerPlan plan in plans)
{
if (plan.Name == name)
{
guid = plan.Guid;
}
}
PowerPlans.SetPlan(guid, name);
}
}
// update remainChargeHistory ---
var historyLength = remainChargeHistory.Count;
if (historyLength == 0 || remainChargeHistory[historyLength - 1] != remainChargeCapMwh)
{
long timeStamp = DateTime.Now.Ticks;
remainChargeHistory.Add(remainChargeCapMwh);
chargeHistoryTime.Add(timeStamp);
// cleanup yucky slurpy paste (misleading data point)
if (firstTime && historyLength == 1)
{
firstTime = false;
remainChargeHistory.RemoveAt(0);
chargeHistoryTime.RemoveAt(0);
}
// calculate charge rate ---
long charge_delta_Mws = (long)(remainChargeCapMwh - remainChargeHistory[0]) * 3600;
calcTimeDelta = (timeStamp - chargeHistoryTime[0]) / 10000; // milliseconds
if (calcTimeDelta != 0)
{
calcChargeRateMw = charge_delta_Mws * 1000 / calcTimeDelta;
while (calcTimeDelta > maxChargeHistoryLength * 1000)
{
remainChargeHistory.RemoveAt(0);
chargeHistoryTime.RemoveAt(0);
calcTimeDelta = (timeStamp - chargeHistoryTime[0]) / 10000; // milliseconds
}
}
}
// ---
double batteryPercent = (double)bat_info["Percent Remaining"];
int roundPercent = (int)Math.Round(batteryPercent, 0);
Color statusColor = highColor;
if (batteryHandle != null) {
if (chargeRateMw > 0)
{
statusColor = chargingColor;
}
else if (roundPercent >= highAmount)
{
statusColor = highColor;
}
else if (roundPercent >= mediumAmount)
{
statusColor = mediumColor;
}
else if (roundPercent >= lowAmount)
{
statusColor = lowColor;
}
}
if (PowerPlans.ReadFriendlyName(active_plan) == "BatteryBoost")
{
statusColor = boostColor;
}
// Lighter text for darkmode
if (darkModeEnabled)
{
if (statusColor == highColor)
{
statusColor = highDarkColor;
}
else if (statusColor == boostColor)
{
statusColor = boostDarkColor;
}
else
{
statusColor = LightenColor(statusColor);
}
}
var toolTipText = CreateTooltipText(bat_info);
// Tray Icon ---
String trayIconText = "!!";
dynamic decimalMode = null;
if (tray_display == DisplayedInfo.Percentage)
{
trayIconText = roundPercent == 100 ? ":)" : roundPercent.ToString();
}
else if (tray_display == DisplayedInfo.ReportedChargeRate)
{
decimal rate = Math.Round((decimal)Math.Abs(chargeRateMw / 1000f), 1);
if (rate >= 10)
{
trayIconText = Math.Round(rate).ToString();
}
else
{
trayIconText = rate.ToString();
}
decimalMode = rate;
}
else if (tray_display == DisplayedInfo.CalculatedChargeRate)
{
decimal rate = Math.Round((decimal)Math.Abs(calcChargeRateMw / 1000f), 1);
if (rate >= 10)
{
trayIconText = Math.Round(rate).ToString();
}
else
{
trayIconText = rate.ToString();
}
decimalMode = rate;
}
else if (tray_display == DisplayedInfo.PowerPlan)
{
trayIconText = PowerPlans.ReadFriendlyName(active_plan).Substring(0, 2);
//decimalMode = 1.1;
}
if (trayFontSize > 8.5 && decimalMode != null)
{
var font_size = trayFontSize;
if (decimalMode < 10)
{
font_size = 8.5f;
}
var settings = (Options)settingsWindow.AppConfig.Sections["Options"];
trayFont = new Font(trayFontType, font_size * trayFontQualityMultiplier, settings.FontStyle);
}
SolidBrush trayFontColor = new SolidBrush(statusColor);
float dpi;
int textWidth, textHeight;
// Measure the rendered size of tray icon text under the current system DPI setting.
using (var bitmap = new Bitmap(1, 1))
{
SizeF size;
using (var graphics = Graphics.FromImage(bitmap))
{
// Measure the rendering size of the tray icon text using this font.
size = graphics.MeasureString(trayIconText, trayFont);
dpi = graphics.DpiX * trayFontQualityMultiplier;
}
// Round the size to integer.
textWidth = (int)Math.Round(size.Width);
textHeight = (int)Math.Round(size.Height);
}
var iconDimension = (int)Math.Round(16 * (dpi / 96));
// Draw the tray icon
using (var bitmap = new Bitmap(iconDimension, iconDimension))
{
using (var graphics = Graphics.FromImage(bitmap))
{
if (!darkModeEnabled)
{
// Anti-Aliasing looks the best when the taskbar is in light mode
graphics.TextRenderingHint = TextRenderingHint.AntiAlias;
}
// Draw the text, centering it, and padding it with 1%
graphics.DrawString(trayIconText, trayFont, trayFontColor,
(iconDimension - textWidth) / 2f,
(iconDimension - textHeight) / 2f);
// The above scaling and start position alignments aim to remove the
// padding of the font so that the text fills the tray icon edge to edge.
}
// Set tray icon from the drawn bitmap image.
System.IntPtr intPtr = bitmap.GetHicon();
try
{
trayIcon.Icon = Icon.FromHandle(intPtr);
toolTip.Content = toolTipText;
}
finally
{
// Destroy icon hand to release it from memory as soon as it's set to the tray.
DestroyIcon(intPtr);
// This should be the very last call when updating the tray icon.
}
}
//---
}
public static void SwitchTrayInfo()
{
if ((int)tray_display + 1 > Enum.GetValues(typeof(DisplayedInfo)).Length - 1)
{
tray_display = (DisplayedInfo)0;
}
else
{
tray_display = (DisplayedInfo)((int)tray_display + 1);
}
}
private static Color LightenColor(Color color)
{
var amount = 30;
Color lightColor = Color.FromArgb(color.A,
Math.Min((int)(color.R + amount), 255), Math.Min((int)(color.G + amount), 255), Math.Min((int)(color.B + amount), 255));
return lightColor;
}
// Tray Icon Helper Functions ---
private static void Quit() // check if the exit button was pressed
{
trayIcon.Dispose();
Current.Shutdown();
}
public static string GetCalculatedTimeLeft(int remainChargeCapMwh, int fullChargeCapMwh)
{
double ctimeLeft = 0;
if (calcChargeRateMw < 0)
{
ctimeLeft = (remainChargeCapMwh / -(double)calcChargeRateMw) * 60;
}
if (calcChargeRateMw > 0)
{
ctimeLeft = ((fullChargeCapMwh - remainChargeCapMwh) / (double)calcChargeRateMw) * 60;
}
return EasySecondsToTime((int)ctimeLeft);
}
private string CreateTooltipText(OrderedDictionary bat_info)
{
// use battery info
bool isCharging = (BatteryStatus)bat_info["Status"] == BatteryStatus.Charging;
int fullChargeCapMwh = (int)bat_info["Battery Capacity mWh"];
int remainChargeCapMwh = (int)bat_info["Remaining Charge mWh"];
int chargeRateMw = (int)bat_info["Reported Charge Rate mW"];
double batteryPercent = (double)bat_info["Percent Remaining"];
string rtimeLeft = (string)bat_info["Reported Time Left"];
string reported_charge_time_text = (chargeRateMw > 0 ? (isCharging ? "Charging: " + rtimeLeft
+ " until fully charged" : "Not charging") + "" : rtimeLeft + " remaining");
string ctimeLeft = GetCalculatedTimeLeft(remainChargeCapMwh, fullChargeCapMwh);
string calculated_charge_time_text = (chargeRateMw > 0 ? (isCharging ? "Charging: " + ctimeLeft
+ " until fully charged" : "Not charging") + "" : ctimeLeft + " remaining");
String toolTipText =
Math.Round(batteryPercent, 3).ToString() + "% " +
(isCharging ? "connected to AC\n" : "on battery\n") +
"Current Charge: " + remainChargeCapMwh.ToString() + " mWh" +
"\n\nReported Data:\n" +
reported_charge_time_text +
"\n" + (chargeRateMw > 0 ? "Charge Rate: " : "Discharge Rate: ") + Math.Abs(chargeRateMw).ToString() + " mW" +
"\n\nCalulated Data:\n" +
calculated_charge_time_text +
"\n" + (calcChargeRateMw > 0 ? "Charge Rate: " : "Discharge Rate: ") + Math.Abs(calcChargeRateMw).ToString() + " mW" +
"\n" + "Buffer Size: " + ((int)(calcTimeDelta / 1000)).ToString() + " sec" +
"\n\nCurrent Power Plan: " + PowerPlans.ReadFriendlyName(active_plan).ToString() +
"\n(The tray is currently\ndisplaying " + tray_display.ToString() + ")";
return toolTipText;
}
private static void CreateInfoWindow()
{
batteryInfoWindow = new BatInfo();
batteryInfoWindow.Topmost = aot;
batteryInfoWindow.Show();
updateWindowIcons();
}
public static void CreateGraphWindow()
{
graphWindow = new Graph();
graphWindow.Topmost = aot;
graphWindow.Show();
updateWindowIcons();
}
public static void CreateSettingsWindow()
{
settingsWindow = new Settings();
settingsWindow.Show();
updateWindowIcons();
}
public static bool[] checkDarkMode()
{
string RegistryKey = @"HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize\";
int appLightKey = (int)Registry.GetValue(RegistryKey, "AppsUseLightTheme", -1);
int trayLightKey = (int)Registry.GetValue(RegistryKey, "SystemUsesLightTheme", -1);
bool appdark = !Convert.ToBoolean(appLightKey == -1 ? true : appLightKey);
bool traydark = !Convert.ToBoolean(trayLightKey == -1 ? false : trayLightKey);
// zero is taskbar, one is system
return [traydark, appdark];
}
public static System.Windows.Media.ImageSource ToImageSource(Icon icon)
{
System.Windows.Media.ImageSource imageSource = Imaging.CreateBitmapSourceFromHIcon(
icon.Handle,
Int32Rect.Empty,
BitmapSizeOptions.FromEmptyOptions());
return imageSource;
}
public static String EasySecondsToTime(int seconds) // Convert seconds to a readable format
{
String time = "_";
if (seconds < 60)
{
time = seconds.ToString() + " mins";
}
else
{
time = (seconds / 60).ToString() + " hr" + (seconds / 60 == 1 ? "" : "s") + " and " + (seconds % 60).ToString() + " mins";
}
if (seconds == -1 || seconds == 0)
{
time = "Unknown";
}
return time;
}
public static bool IsAdministrator()
{
return (new WindowsPrincipal(WindowsIdentity.GetCurrent()))
.IsInRole(WindowsBuiltInRole.Administrator);
}
}
}