Battery

Battery information describes the device’s remaining charge and whether it is charging. An app can use it to display power status or postpone work that would consume substantial energy.

day-part-device returns a snapshot through the platform battery API, without requiring a UI.

Add to your app

Add day-part-device to your app:

cargo add 'day-part-device@0.*'

The implementation is selected by the compilation target; no toolkit feature flags or dependency entry in Day.toml are needed.

Platforms / toolkitsAvailability
macOS, iOS, Android, Windows, Linux (GTK / Qt), HarmonyOSBattery information where hardware and OS reporting are available.
WebUnavailable; status() returns None.

Authoring

if let Some(b) = day_part_device::battery::status() {
    println!("{:?} · {}%", b.state, b.percent().unwrap_or(0));
}

status() -> Option<BatteryStatus> returns None where there’s no battery API (or no battery). BatteryStatus { level: Option<f32>, state: BatteryState }; level is 0.0..=1.0 (percent() gives 0..=100); BatteryState is Charging | Discharging | Full | NotCharging | Unknown.

There are no cargo features; platform selection is purely #[cfg(target_os)], because a battery is an OS concern rather than a toolkit one. crates/day-part-device/examples/battery.rs is a plain main that uses it with no Day framework at all.

Platform implementations

OSAPIdependency
macOSIOKit IOPowerSources (CoreFoundation)core-foundation, IOKit framework
iOSUIDevice.batteryLevel / batteryStateobjc2-ui-kit
WindowsGetSystemPowerStatusraw FFI (kernel32)
Linux/sys/class/power_supplystd only
HarmonyOSnative OH_BatteryInfo_GetCapacity / GetPluggedType (libohbattery_info.so)raw FFI (BasicServicesKit)
AndroidBatteryManager via the inline Java arm in src/android.rsday-toolkit-material + day-bridge (bridge)

iOS reads on the main thread (UIDevice is MainThreadOnly); off it, status() returns None. The simulator has no battery, so you get level: None, state: Unknown (the API path still runs).

HarmonyOS is target_os = "linux" but sandboxes /sys away, so it’s gated on target_env = "ohos" and uses the native BasicServicesKit C API instead. That’s pure FFI: it needs neither a permission nor the Day runtime (unlike Android). The native API exposes capacity + plugged type but no explicit charge state, so the state is inferred from whether external power is connected. The showcase’s Device & sensors page shows a live readout (docs/harmonyos).

Extension integration

This is the first headless external crate: it has no UI Piece and registers nothing into any toolkit’s RENDERERS slice. It demonstrates that the standalone-piece toolkit-contribution mechanism (see extending) already accommodates headless capability crates: day-part-device contributes an Android implementation exactly like the UI pieces, but registers no renderer; the staging is independent of rendering. On Android the crate rides on the Day runtime (it uses day-toolkit-material’s cached JVM + DayBridge.ctx); on every other platform it is fully day-independent.

The Android half is a daybridge arm (bridge): the Java that reads BatteryManager lives inline in crates/day-part-device/src/android.rs, beside the declaration it implements, and day build stages it into the app’s Gradle build. It replaced a checked-in DayBattery.java, a [package.metadata.day.android] java = [...] table, and a packed-i64 wire format that was written twice and kept in agreement by comment. day-part-speech is the fuller demonstration, with six languages in one file, in a separate repository.