fix: battery icon

This commit is contained in:
Ashley Wulber 2026-03-16 14:02:28 -04:00 committed by Jacob Kauffmann
parent 9dda03ca3d
commit 0272e189f4
6 changed files with 108 additions and 35 deletions

View file

@ -2,11 +2,12 @@ use cosmic::iced::{
Subscription,
futures::{SinkExt, StreamExt, channel::mpsc},
};
use futures_util::select;
use std::{any::TypeId, time::Duration};
use upower_dbus::UPowerProxy;
use upower_dbus::{BatteryState, BatteryType, UPowerProxy};
use zbus::{Connection, Result};
pub fn subscription() -> Subscription<Option<(String, f64)>> {
pub fn subscription() -> Subscription<Option<(f64, bool, bool)>> {
struct PowerSubscription;
Subscription::run_with(TypeId::of::<PowerSubscription>(), |_| {
@ -29,30 +30,55 @@ pub fn subscription() -> Subscription<Option<(String, f64)>> {
}
//TODO: use never type?
pub async fn handler(msg_tx: &mut mpsc::Sender<Option<(String, f64)>>) -> Result<()> {
pub async fn handler(msg_tx: &mut mpsc::Sender<Option<(f64, bool, bool)>>) -> Result<()> {
let zbus = Connection::system().await?;
let upower = UPowerProxy::new(&zbus).await?;
let dev = upower.get_display_device().await?;
let mut icon_name_changed = dev.receive_icon_name_changed().await;
let mut percentage_changed = dev.receive_percentage_changed().await;
let mut percentage_changed = dev.receive_percentage_changed().await.boxed().fuse();
let mut state_changed = dev.receive_state_changed().await.boxed().fuse();
let mut charge_threshold_enabled_changed = dev
.receive_charge_threshold_enabled_changed()
.await
.boxed()
.fuse();
let mut interval = tokio::time::interval(Duration::from_secs(1));
let has_battery = dev.type_().await? == BatteryType::Battery && dev.power_supply().await?;
if !has_battery {
return Ok(());
}
loop {
let mut info_opt = None;
if let Ok(percent) = dev.percentage().await {
if let Ok(icon_name) = dev.icon_name().await {
if !icon_name.is_empty() && !icon_name.eq("battery-missing-symbolic") {
info_opt = Some((icon_name, percent));
if let Ok(mut percent) = dev.percentage().await {
if let Ok(state) = dev.state().await {
let threshold_enabled = dev.charge_threshold_enabled().await.unwrap_or_default();
let capacity = dev.capacity().await.unwrap_or(100.);
// compensate for declining battery capacity
percent = percent * 100. / capacity;
if matches!(state, BatteryState::FullyCharged) || percent > 100. {
percent = 100.;
}
info_opt = Some((
percent,
state == BatteryState::Discharging,
threshold_enabled,
));
}
}
msg_tx.send(info_opt).await.unwrap();
// Waits until icon or percentage have changed, and at least one second has passed.
futures_util::future::select(icon_name_changed.next(), percentage_changed.next()).await;
select! {
_ = state_changed.next() => {},
_ = percentage_changed.next() => {},
_ = charge_threshold_enabled_changed.next() => {}
}
interval.tick().await;
}
}