[CmdPal][PerfMon] Add battery widget (charge, status, time remaining)#47870
[CmdPal][PerfMon] Add battery widget (charge, status, time remaining)#47870Knyrps wants to merge 2 commits into
Conversation
|
@microsoft-github-policy-service agree |
|
@Knyrps would you mind adding a screenshot to the PR description? |
|
Edit: Mention @niels9001 Sure: The battery percentage is polled every second on the shared DataManager timer (the same 1 Hz tick that already drives the CPU, memory, network and GPU widgets), so the dock band icon and the widget body stay in sync without any extra threads or timers. Each tick calls GetSystemPowerStatus once via CsWin32, maps the result through Icons.BatteryGlyph(percent, isCharging, hasBattery) against the pre built 23 entry IconInfo cache, and assigns the resulting IconInfo to _batteryItem.Icon, so the only allocation per tick is the assignment itself. IsCharging is taken straight from the BatteryFlag charging bit (0x08); the ACLineStatus value is surfaced separately as IsOnAcPower and is used only for the status text, not for icon selection. That keeps the icon honest: at 100% on AC the system reports "not actively charging" and you see the full icon; while the battery is being topped up you see the charging variant; on battery you see the plain discharge glyph at the matching level; and when the OS reports unknown / no battery (BatteryFlag == 0xFF or 0x80, or BatteryLifePercent == 255) the widget falls back to MobBatteryUnknown (\uEC02) instead of guessing. Apologies for not being able to provide screenshots at lower charge levels, my battery is currently full ^^ but the icon resolves to the correct glyph in both charging and discharging states, rounded to the nearest 10% (so 0%, 10%, … 100%). |


Summary
Adds a Battery widget to the CmdPal Performance Monitor dock, exposing live charge percentage, charging/AC status, and estimated time remaining. The dock-band icon updates each tick to reflect the current charge level and charging state, matching the existing CPU/RAM/GPU/Network widget pattern.
Closes #47218
Detail
SystemBatteryUsageWidgetPageinPerformanceWidgetsPage.cs, mirroring the existing widget pattern.GetSystemPowerStatusvia CsWin32 (AOT-safe), wrapped inBatteryStatsand surfaced throughSystemData/DataManageron the shared 1 s timer.Icons.cscaches 23IconInfoinstances (BatteryIcons[0..10],BatteryChargingIcons[0..10],BatteryUnknownIcon); alloc-freeBatteryGlyph()selects the glyph per tick. Codepoints0xEBA0–0xEBB5/0xEC02(Segoe Fluent Icons MobBattery family).SystemBatteryTemplate.jsonregistered as<None Update … PreserveNewest>, consistent with the other PerfMon templates.Battery_*strings underStrings\en-US\Resources.resw.GetSystemPowerStatussentinels:BatteryLifePercent == 255,BatteryLifeTime == 0xFFFFFFFF,BatteryFlag == 0xFF,0x80(no battery),0x08(charging).Screenshots
When the battery is at 100%, the widget will always show the "full" icon (


MobBatteryCharging10).Otherwise and when on AC, it will show the "charging" variant of the icon:
The icon also always reflects the current charging state: It resolves to the correct glyph in both charging and discharging states, rounded to the nearest 10% (so 0%, 10%, … 100%).
How tested
MSBuild CommandPalette.slnf /p:Configuration=Release /p:Platform=x64 /m /restore— green, 0 errors, 0 warnings.Microsoft.CmdPal.UI.exe, opened PerfMon dock, confirmed Battery widget renders charge %, status, time remaining, and that the dock-band glyph matches the current charge level (verified on AC, on battery, and while charging).