Skip to content

[CmdPal][PerfMon] Add battery widget (charge, status, time remaining)#47870

Open
Knyrps wants to merge 2 commits into
microsoft:mainfrom
Knyrps:feat/cmdpal-dock-battery-47218
Open

[CmdPal][PerfMon] Add battery widget (charge, status, time remaining)#47870
Knyrps wants to merge 2 commits into
microsoft:mainfrom
Knyrps:feat/cmdpal-dock-battery-47218

Conversation

@Knyrps
Copy link
Copy Markdown
Contributor

@Knyrps Knyrps commented May 14, 2026

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

  • New SystemBatteryUsageWidgetPage in PerformanceWidgetsPage.cs, mirroring the existing widget pattern.
  • Data source: GetSystemPowerStatus via CsWin32 (AOT-safe), wrapped in BatteryStats and surfaced through SystemData / DataManager on the shared 1 s timer.
  • Live dock-band icon: Icons.cs caches 23 IconInfo instances (BatteryIcons[0..10], BatteryChargingIcons[0..10], BatteryUnknownIcon); alloc-free BatteryGlyph() selects the glyph per tick. Codepoints 0xEBA00xEBB5 / 0xEC02 (Segoe Fluent Icons MobBattery family).
  • Adaptive card template SystemBatteryTemplate.json registered as <None Update … PreserveNewest>, consistent with the other PerfMon templates.
  • New Battery_* strings under Strings\en-US\Resources.resw.
  • Handles GetSystemPowerStatus sentinels: BatteryLifePercent == 255, BatteryLifeTime == 0xFFFFFFFF, BatteryFlag == 0xFF, 0x80 (no battery), 0x08 (charging).

Screenshots

image

When the battery is at 100%, the widget will always show the "full" icon (MobBatteryCharging10).
image
Otherwise and when on AC, it will show the "charging" variant of the icon:
image
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.
  • Launched dev 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).

@Knyrps
Copy link
Copy Markdown
Contributor Author

Knyrps commented May 14, 2026

@microsoft-github-policy-service agree

@niels9001
Copy link
Copy Markdown
Collaborator

@Knyrps would you mind adding a screenshot to the PR description?

@Knyrps
Copy link
Copy Markdown
Contributor Author

Knyrps commented May 14, 2026

Edit: Mention @niels9001

Sure:
When the battery is at 100%, the widget will always show the "full" icon (MobBatteryCharging10).
image
Otherwise and when on AC, it will show the "charging" variant of the icon:
image
Edit 2: Oh, and the widget:
image

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%).

@niels9001 niels9001 added CmdPal - Dock Issues related to the Command Palette Dock Product-Command Palette Refers to the Command Palette utility labels May 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CmdPal - Dock Issues related to the Command Palette Dock Product-Command Palette Refers to the Command Palette utility

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add battery time remaining on command palette dock

2 participants