ai-battery 0.1.19 → 0.1.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,135 +1,135 @@
1
- $nativeCode = @"
2
- using System;
3
- using System.Runtime.InteropServices;
4
- [StructLayout(LayoutKind.Sequential)]
5
- public struct AiBatteryRect {
6
- public int Left;
7
- public int Top;
8
- public int Right;
9
- public int Bottom;
10
- }
11
- [StructLayout(LayoutKind.Sequential)]
12
- public struct AiBatteryMonitorInfo {
13
- public int CbSize;
14
- public AiBatteryRect Monitor;
15
- public AiBatteryRect Work;
16
- public int Flags;
17
- }
18
- [StructLayout(LayoutKind.Sequential)]
19
- public struct AiBatteryPowerThrottlingState {
20
- public uint Version;
21
- public uint ControlMask;
22
- public uint StateMask;
23
- }
1
+ $nativeCode = @"
2
+ using System;
3
+ using System.Runtime.InteropServices;
4
+ [StructLayout(LayoutKind.Sequential)]
5
+ public struct AiBatteryRect {
6
+ public int Left;
7
+ public int Top;
8
+ public int Right;
9
+ public int Bottom;
10
+ }
11
+ [StructLayout(LayoutKind.Sequential)]
12
+ public struct AiBatteryMonitorInfo {
13
+ public int CbSize;
14
+ public AiBatteryRect Monitor;
15
+ public AiBatteryRect Work;
16
+ public int Flags;
17
+ }
18
+ [StructLayout(LayoutKind.Sequential)]
19
+ public struct AiBatteryPowerThrottlingState {
20
+ public uint Version;
21
+ public uint ControlMask;
22
+ public uint StateMask;
23
+ }
24
24
  public delegate void AiBatteryWinEventDelegate(IntPtr hWinEventHook, uint eventType, IntPtr hwnd, int idObject, int idChild, uint dwEventThread, uint dwmsEventTime);
25
25
  public delegate bool AiBatteryEnumWindowsDelegate(IntPtr hwnd, IntPtr lParam);
26
- public static class AiBatteryNative {
27
- public const int ProcessPowerThrottling = 4;
28
- public const uint PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1;
29
- public const uint PROCESS_POWER_THROTTLING_EXECUTION_SPEED = 0x1;
30
- public const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
31
- public const int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4;
32
- public const int OBJID_WINDOW = 0;
33
- public const int GWL_EXSTYLE = -20;
34
- public const int WS_EX_TRANSPARENT = 0x20;
35
- public const int WS_EX_TOOLWINDOW = 0x80;
36
- public const int WS_EX_NOACTIVATE = 0x08000000;
37
- public const int SW_HIDE = 0;
38
- public const int SW_MAXIMIZE = 3;
39
- public const int SW_SHOWNOACTIVATE = 4;
40
- public const int SW_RESTORE = 9;
41
- public const UInt32 MONITOR_DEFAULTTONEAREST = 2;
42
- public const UInt32 GW_HWNDNEXT = 2;
43
- public const int DWMWA_CLOAKED = 14;
44
- public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
45
- public const int DWMWA_NCRENDERING_POLICY = 2;
46
- public const int DWMWA_WINDOW_CORNER_PREFERENCE = 33;
47
- public const int DWMWA_BORDER_COLOR = 34;
48
- public const int DWMWA_COLOR_DEFAULT = -1;
49
- public const int DWMWA_COLOR_NONE = -2;
50
- public const int DWMWCP_DEFAULT = 0;
51
- public const int DWMWCP_DONOTROUND = 1;
52
- public const int DWMWCP_ROUND = 2;
53
- public const int DWMNCRP_USEWINDOWSTYLE = 0;
54
- public const int DWMNCRP_DISABLED = 1;
55
- public const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
56
- public const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;
57
- public const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;
58
- public const uint EVENT_OBJECT_SHOW = 0x8002;
59
- public const uint EVENT_OBJECT_HIDE = 0x8003;
60
- public const uint EVENT_OBJECT_REORDER = 0x8004;
61
- public const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;
62
- public const uint EVENT_OBJECT_CLOAKED = 0x8017;
63
- public const uint EVENT_OBJECT_UNCLOAKED = 0x8018;
64
- public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
65
- public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
66
- public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
67
- public static readonly IntPtr HWND_TOP = IntPtr.Zero;
68
- public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
69
- public const UInt32 GW_HWNDPREV = 3;
70
- public const UInt32 SWP_NOSIZE = 0x0001;
71
- public const UInt32 SWP_NOMOVE = 0x0002;
72
- public const UInt32 SWP_NOZORDER = 0x0004;
73
- public const UInt32 SWP_NOACTIVATE = 0x0010;
74
- public const UInt32 SWP_SHOWWINDOW = 0x0040;
75
- [DllImport("user32.dll", SetLastError=true)]
76
- public static extern bool SetProcessDPIAware();
77
- [DllImport("shcore.dll")]
78
- public static extern int SetProcessDpiAwareness(int value);
26
+ public static class AiBatteryNative {
27
+ public const int ProcessPowerThrottling = 4;
28
+ public const uint PROCESS_POWER_THROTTLING_CURRENT_VERSION = 1;
29
+ public const uint PROCESS_POWER_THROTTLING_EXECUTION_SPEED = 0x1;
30
+ public const int PROCESS_PER_MONITOR_DPI_AWARE = 2;
31
+ public const int DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = -4;
32
+ public const int OBJID_WINDOW = 0;
33
+ public const int GWL_EXSTYLE = -20;
34
+ public const int WS_EX_TRANSPARENT = 0x20;
35
+ public const int WS_EX_TOOLWINDOW = 0x80;
36
+ public const int WS_EX_NOACTIVATE = 0x08000000;
37
+ public const int SW_HIDE = 0;
38
+ public const int SW_MAXIMIZE = 3;
39
+ public const int SW_SHOWNOACTIVATE = 4;
40
+ public const int SW_RESTORE = 9;
41
+ public const UInt32 MONITOR_DEFAULTTONEAREST = 2;
42
+ public const UInt32 GW_HWNDNEXT = 2;
43
+ public const int DWMWA_CLOAKED = 14;
44
+ public const int DWMWA_EXTENDED_FRAME_BOUNDS = 9;
45
+ public const int DWMWA_NCRENDERING_POLICY = 2;
46
+ public const int DWMWA_WINDOW_CORNER_PREFERENCE = 33;
47
+ public const int DWMWA_BORDER_COLOR = 34;
48
+ public const int DWMWA_COLOR_DEFAULT = -1;
49
+ public const int DWMWA_COLOR_NONE = -2;
50
+ public const int DWMWCP_DEFAULT = 0;
51
+ public const int DWMWCP_DONOTROUND = 1;
52
+ public const int DWMWCP_ROUND = 2;
53
+ public const int DWMNCRP_USEWINDOWSTYLE = 0;
54
+ public const int DWMNCRP_DISABLED = 1;
55
+ public const uint EVENT_SYSTEM_FOREGROUND = 0x0003;
56
+ public const uint EVENT_SYSTEM_MOVESIZESTART = 0x000A;
57
+ public const uint EVENT_SYSTEM_MOVESIZEEND = 0x000B;
58
+ public const uint EVENT_OBJECT_SHOW = 0x8002;
59
+ public const uint EVENT_OBJECT_HIDE = 0x8003;
60
+ public const uint EVENT_OBJECT_REORDER = 0x8004;
61
+ public const uint EVENT_OBJECT_LOCATIONCHANGE = 0x800B;
62
+ public const uint EVENT_OBJECT_CLOAKED = 0x8017;
63
+ public const uint EVENT_OBJECT_UNCLOAKED = 0x8018;
64
+ public const uint WINEVENT_OUTOFCONTEXT = 0x0000;
65
+ public const uint WINEVENT_SKIPOWNPROCESS = 0x0002;
66
+ public static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
67
+ public static readonly IntPtr HWND_TOP = IntPtr.Zero;
68
+ public static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
69
+ public const UInt32 GW_HWNDPREV = 3;
70
+ public const UInt32 SWP_NOSIZE = 0x0001;
71
+ public const UInt32 SWP_NOMOVE = 0x0002;
72
+ public const UInt32 SWP_NOZORDER = 0x0004;
73
+ public const UInt32 SWP_NOACTIVATE = 0x0010;
74
+ public const UInt32 SWP_SHOWWINDOW = 0x0040;
75
+ [DllImport("user32.dll", SetLastError=true)]
76
+ public static extern bool SetProcessDPIAware();
77
+ [DllImport("shcore.dll")]
78
+ public static extern int SetProcessDpiAwareness(int value);
79
79
  [DllImport("user32.dll", SetLastError=true)]
80
80
  public static extern bool SetProcessDpiAwarenessContext(IntPtr dpiContext);
81
81
  [DllImport("user32.dll", SetLastError=true)]
82
82
  public static extern IntPtr SetThreadDpiAwarenessContext(IntPtr dpiContext);
83
- [DllImport("user32.dll")]
84
- public static extern uint GetDpiForWindow(IntPtr hWnd);
85
- [DllImport("user32.dll")]
86
- public static extern IntPtr GetForegroundWindow();
87
- [DllImport("user32.dll")]
88
- public static extern short GetAsyncKeyState(int vKey);
89
- [DllImport("user32.dll")]
90
- public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
91
- [DllImport("user32.dll")]
92
- public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
93
- [DllImport("user32.dll")]
94
- public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags);
95
- [DllImport("user32.dll")]
96
- public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
97
- [DllImport("user32.dll")]
98
- public static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
99
- [DllImport("user32.dll")]
100
- public static extern IntPtr GetTopWindow(IntPtr hWnd);
101
- [DllImport("user32.dll")]
102
- public static extern bool IsWindowVisible(IntPtr hWnd);
83
+ [DllImport("user32.dll")]
84
+ public static extern uint GetDpiForWindow(IntPtr hWnd);
85
+ [DllImport("user32.dll")]
86
+ public static extern IntPtr GetForegroundWindow();
87
+ [DllImport("user32.dll")]
88
+ public static extern short GetAsyncKeyState(int vKey);
89
+ [DllImport("user32.dll")]
90
+ public static extern int GetWindowLong(IntPtr hWnd, int nIndex);
91
+ [DllImport("user32.dll")]
92
+ public static extern int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong);
93
+ [DllImport("user32.dll")]
94
+ public static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, UInt32 uFlags);
95
+ [DllImport("user32.dll")]
96
+ public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
97
+ [DllImport("user32.dll")]
98
+ public static extern IntPtr GetWindow(IntPtr hWnd, UInt32 uCmd);
99
+ [DllImport("user32.dll")]
100
+ public static extern IntPtr GetTopWindow(IntPtr hWnd);
101
+ [DllImport("user32.dll")]
102
+ public static extern bool IsWindowVisible(IntPtr hWnd);
103
103
  [DllImport("user32.dll", CharSet=CharSet.Auto)]
104
104
  public static extern int GetClassName(IntPtr hWnd, System.Text.StringBuilder lpClassName, int nMaxCount);
105
105
  [DllImport("user32.dll")]
106
106
  private static extern bool EnumWindows(AiBatteryEnumWindowsDelegate callback, IntPtr lParam);
107
- [DllImport("dwmapi.dll")]
108
- public static extern int DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, out int pvAttribute, int cbAttribute);
109
- [DllImport("dwmapi.dll")]
110
- public static extern int DwmSetWindowAttribute(IntPtr hWnd, int dwAttribute, ref int pvAttribute, int cbAttribute);
111
- [DllImport("dwmapi.dll", EntryPoint="DwmGetWindowAttribute")]
112
- public static extern int DwmGetWindowAttributeRect(IntPtr hWnd, int dwAttribute, out AiBatteryRect pvAttribute, int cbAttribute);
113
- [DllImport("dwmapi.dll")]
114
- public static extern int DwmFlush();
115
- [DllImport("user32.dll")]
116
- public static extern bool IsWindow(IntPtr hWnd);
117
- [DllImport("user32.dll")]
118
- public static extern bool IsIconic(IntPtr hWnd);
119
- [DllImport("user32.dll")]
120
- public static extern bool IsZoomed(IntPtr hWnd);
121
- [DllImport("kernel32.dll")]
122
- public static extern IntPtr GetCurrentProcess();
123
- [DllImport("kernel32.dll", SetLastError=true)]
124
- public static extern bool SetProcessInformation(IntPtr hProcess, int ProcessInformationClass, ref AiBatteryPowerThrottlingState ProcessInformation, int ProcessInformationSize);
125
- [DllImport("user32.dll")]
126
- public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, AiBatteryWinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
127
- [DllImport("user32.dll")]
128
- public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
129
- [DllImport("user32.dll", CharSet=CharSet.Auto)]
130
- public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
131
- [DllImport("user32.dll", CharSet=CharSet.Auto)]
132
- public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
107
+ [DllImport("dwmapi.dll")]
108
+ public static extern int DwmGetWindowAttribute(IntPtr hWnd, int dwAttribute, out int pvAttribute, int cbAttribute);
109
+ [DllImport("dwmapi.dll")]
110
+ public static extern int DwmSetWindowAttribute(IntPtr hWnd, int dwAttribute, ref int pvAttribute, int cbAttribute);
111
+ [DllImport("dwmapi.dll", EntryPoint="DwmGetWindowAttribute")]
112
+ public static extern int DwmGetWindowAttributeRect(IntPtr hWnd, int dwAttribute, out AiBatteryRect pvAttribute, int cbAttribute);
113
+ [DllImport("dwmapi.dll")]
114
+ public static extern int DwmFlush();
115
+ [DllImport("user32.dll")]
116
+ public static extern bool IsWindow(IntPtr hWnd);
117
+ [DllImport("user32.dll")]
118
+ public static extern bool IsIconic(IntPtr hWnd);
119
+ [DllImport("user32.dll")]
120
+ public static extern bool IsZoomed(IntPtr hWnd);
121
+ [DllImport("kernel32.dll")]
122
+ public static extern IntPtr GetCurrentProcess();
123
+ [DllImport("kernel32.dll", SetLastError=true)]
124
+ public static extern bool SetProcessInformation(IntPtr hProcess, int ProcessInformationClass, ref AiBatteryPowerThrottlingState ProcessInformation, int ProcessInformationSize);
125
+ [DllImport("user32.dll")]
126
+ public static extern IntPtr SetWinEventHook(uint eventMin, uint eventMax, IntPtr hmodWinEventProc, AiBatteryWinEventDelegate lpfnWinEventProc, uint idProcess, uint idThread, uint dwFlags);
127
+ [DllImport("user32.dll")]
128
+ public static extern bool UnhookWinEvent(IntPtr hWinEventHook);
129
+ [DllImport("user32.dll", CharSet=CharSet.Auto)]
130
+ public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
131
+ [DllImport("user32.dll", CharSet=CharSet.Auto)]
132
+ public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
133
133
  [DllImport("user32.dll")]
134
134
  public static extern bool GetWindowRect(IntPtr hWnd, out AiBatteryRect rect);
135
135
  public static bool HasVisibleTopLevelWindowClass(string className) {
@@ -148,16 +148,16 @@ public static class AiBatteryNative {
148
148
  GC.KeepAlive(callback);
149
149
  return found;
150
150
  }
151
- [DllImport("user32.dll")]
152
- public static extern IntPtr MonitorFromWindow(IntPtr hWnd, UInt32 dwFlags);
153
- [DllImport("user32.dll")]
154
- public static extern bool GetMonitorInfo(IntPtr hMonitor, ref AiBatteryMonitorInfo lpmi);
155
- [DllImport("user32.dll")]
156
- public static extern bool DestroyIcon(IntPtr hIcon);
157
- }
158
- "@
159
- Add-Type -TypeDefinition $nativeCode
160
-
151
+ [DllImport("user32.dll")]
152
+ public static extern IntPtr MonitorFromWindow(IntPtr hWnd, UInt32 dwFlags);
153
+ [DllImport("user32.dll")]
154
+ public static extern bool GetMonitorInfo(IntPtr hMonitor, ref AiBatteryMonitorInfo lpmi);
155
+ [DllImport("user32.dll")]
156
+ public static extern bool DestroyIcon(IntPtr hIcon);
157
+ }
158
+ "@
159
+ Add-Type -TypeDefinition $nativeCode
160
+
161
161
  function Enable-HudDpiAwareness {
162
162
  $context = [IntPtr]::new([AiBatteryNative]::DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2)
163
163
  try {
@@ -173,40 +173,40 @@ function Enable-HudDpiAwareness {
173
173
  } catch {
174
174
  # Fall through to older process-level APIs.
175
175
  }
176
-
177
- try {
178
- if ([AiBatteryNative]::SetProcessDpiAwareness([AiBatteryNative]::PROCESS_PER_MONITOR_DPI_AWARE) -eq 0) { return "per-monitor" }
179
- } catch {
180
- # Fall through to system DPI awareness.
181
- }
182
-
183
- try {
184
- if ([AiBatteryNative]::SetProcessDPIAware()) { return "system" }
185
- } catch {
186
- # DPI awareness is a quality improvement; the HUD can still run without it.
187
- }
188
-
189
- return "default"
190
- }
191
- $script:hudDpiAwareness = Enable-HudDpiAwareness
192
-
193
- function Disable-ProcessPowerThrottling {
194
- # When a fullscreen app is in the foreground, Windows applies EcoQoS power
195
- # throttling to background processes like this HUD, which stretches the
196
- # WinForms timer from 250ms out to several seconds. That made the HUD linger
197
- # over a fullscreen window for 3-5s before hiding (while unhide, which happens
198
- # after throttling lifts, stayed instant). Opt the process out so the
199
- # fullscreen check keeps firing on time even while backgrounded.
200
- try {
201
- $state = New-Object AiBatteryPowerThrottlingState
202
- $state.Version = [AiBatteryNative]::PROCESS_POWER_THROTTLING_CURRENT_VERSION
203
- $state.ControlMask = [AiBatteryNative]::PROCESS_POWER_THROTTLING_EXECUTION_SPEED
204
- $state.StateMask = 0
205
- $size = [System.Runtime.InteropServices.Marshal]::SizeOf([type][AiBatteryPowerThrottlingState])
206
- return [AiBatteryNative]::SetProcessInformation([AiBatteryNative]::GetCurrentProcess(), [AiBatteryNative]::ProcessPowerThrottling, [ref]$state, $size)
207
- } catch {
208
- # Older Windows builds lack ProcessPowerThrottling; the HUD still works.
209
- return $false
210
- }
211
- }
212
- $script:powerThrottlingDisabled = Disable-ProcessPowerThrottling
176
+
177
+ try {
178
+ if ([AiBatteryNative]::SetProcessDpiAwareness([AiBatteryNative]::PROCESS_PER_MONITOR_DPI_AWARE) -eq 0) { return "per-monitor" }
179
+ } catch {
180
+ # Fall through to system DPI awareness.
181
+ }
182
+
183
+ try {
184
+ if ([AiBatteryNative]::SetProcessDPIAware()) { return "system" }
185
+ } catch {
186
+ # DPI awareness is a quality improvement; the HUD can still run without it.
187
+ }
188
+
189
+ return "default"
190
+ }
191
+ $script:hudDpiAwareness = Enable-HudDpiAwareness
192
+
193
+ function Disable-ProcessPowerThrottling {
194
+ # When a fullscreen app is in the foreground, Windows applies EcoQoS power
195
+ # throttling to background processes like this HUD, which stretches the
196
+ # WinForms timer from 250ms out to several seconds. That made the HUD linger
197
+ # over a fullscreen window for 3-5s before hiding (while unhide, which happens
198
+ # after throttling lifts, stayed instant). Opt the process out so the
199
+ # fullscreen check keeps firing on time even while backgrounded.
200
+ try {
201
+ $state = New-Object AiBatteryPowerThrottlingState
202
+ $state.Version = [AiBatteryNative]::PROCESS_POWER_THROTTLING_CURRENT_VERSION
203
+ $state.ControlMask = [AiBatteryNative]::PROCESS_POWER_THROTTLING_EXECUTION_SPEED
204
+ $state.StateMask = 0
205
+ $size = [System.Runtime.InteropServices.Marshal]::SizeOf([type][AiBatteryPowerThrottlingState])
206
+ return [AiBatteryNative]::SetProcessInformation([AiBatteryNative]::GetCurrentProcess(), [AiBatteryNative]::ProcessPowerThrottling, [ref]$state, $size)
207
+ } catch {
208
+ # Older Windows builds lack ProcessPowerThrottling; the HUD still works.
209
+ return $false
210
+ }
211
+ }
212
+ $script:powerThrottlingDisabled = Disable-ProcessPowerThrottling