diff --git a/Cargo.lock b/Cargo.lock index a18c8ff..9cabc32 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1109,7 +1109,7 @@ dependencies = [ [[package]] name = "libmonado-rs" version = "0.1.0" -source = "git+https://github.com/technobaboo/libmonado-rs#626d8f52d218c59a915edc61e91f849b8dc8922e" +source = "git+https://github.com/technobaboo/libmonado-rs?rev=ddad003d5701c7139dd0de69c8195ed2105784ca#ddad003d5701c7139dd0de69c8195ed2105784ca" dependencies = [ "bindgen", "cmake", diff --git a/Cargo.toml b/Cargo.toml index 5f63955..79fd66a 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ git2 = "0.19.0" gtk4 = { version = "0.9.0", features = ["v4_10"] } lazy_static = "1.5.0" libadwaita = { version = "0.7.0", features = ["v1_5"] } -libmonado-rs = { git = "https://github.com/technobaboo/libmonado-rs" } +libmonado-rs = { git = "https://github.com/technobaboo/libmonado-rs", rev = "ddad003d5701c7139dd0de69c8195ed2105784ca" } rusb = "0.9.4" nix = { version = "0.29.0", features = ["fs", "signal"] } phf = "0.11.2" diff --git a/src/ui/battery_status.rs b/src/ui/battery_status.rs index 3b5db10..444a7c1 100644 --- a/src/ui/battery_status.rs +++ b/src/ui/battery_status.rs @@ -1,34 +1,39 @@ +use libmonado_rs::BatteryStatus; use std::fmt::Display; -#[derive(Debug, Clone, Default)] -pub struct BatteryStatus { - /// between 0 and 100 - pub percentage: u8, +#[derive(Debug, Clone)] +pub struct EnvisionBatteryStatus { + pub battery_status: BatteryStatus, } -impl Display for BatteryStatus { +impl Display for EnvisionBatteryStatus { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str(&format!("{}%", self.percentage)) + f.write_str(&format!( + "{}%", + (self.battery_status.charge * 100.0).round() + )) } } -impl BatteryStatus { - pub fn new(percentage: u8) -> Self { - Self { percentage } +impl From for EnvisionBatteryStatus { + fn from(battery_status: BatteryStatus) -> Self { + Self { battery_status } } +} +impl EnvisionBatteryStatus { pub fn icon(&self) -> &str { - match self.percentage { - n if n >= 100 => "battery-level-100-symbolic", - n if n >= 90 => "battery-level-90-symbolic", - n if n >= 80 => "battery-level-80-symbolic", - n if n >= 70 => "battery-level-70-symbolic", - n if n >= 60 => "battery-level-60-symbolic", - n if n >= 50 => "battery-level-50-symbolic", - n if n >= 40 => "battery-level-40-symbolic", - n if n >= 30 => "battery-level-30-symbolic", - n if n >= 20 => "battery-level-20-symbolic", - n if n >= 10 => "battery-level-10-symbolic", + match self.battery_status.charge { + n if n >= 1.0 => "battery-level-100-symbolic", + n if n >= 0.9 => "battery-level-90-symbolic", + n if n >= 0.8 => "battery-level-80-symbolic", + n if n >= 0.7 => "battery-level-70-symbolic", + n if n >= 0.6 => "battery-level-60-symbolic", + n if n >= 0.5 => "battery-level-50-symbolic", + n if n >= 0.4 => "battery-level-40-symbolic", + n if n >= 0.3 => "battery-level-30-symbolic", + n if n >= 0.2 => "battery-level-20-symbolic", + n if n >= 0.1 => "battery-level-10-symbolic", _ => "battery-level-0-symbolic", } } diff --git a/src/ui/devices_box.rs b/src/ui/devices_box.rs index ade4e75..4bd8cab 100644 --- a/src/ui/devices_box.rs +++ b/src/ui/devices_box.rs @@ -5,6 +5,7 @@ use relm4::{factory::AsyncFactoryVecDeque, prelude::*}; #[tracker::track] pub struct DevicesBox { + #[no_eq] devices: Vec, #[tracker::do_not_track] diff --git a/src/ui/factories/device_row_factory.rs b/src/ui/factories/device_row_factory.rs index fe90b10..5a07d52 100644 --- a/src/ui/factories/device_row_factory.rs +++ b/src/ui/factories/device_row_factory.rs @@ -1,5 +1,5 @@ use crate::{ - ui::battery_status::BatteryStatus, + ui::battery_status::EnvisionBatteryStatus, xr_devices::{XRDevice, XRDeviceRole}, }; use adw::prelude::*; @@ -42,7 +42,7 @@ pub struct DeviceRowModel { subtitle: String, state: DeviceRowState, suffix: Option, - battery_status: Option, + battery_status: Option, } #[derive(Debug, Default)] @@ -51,7 +51,7 @@ pub struct DeviceRowModelInit { pub subtitle: Option, pub state: Option, pub suffix: Option, - pub battery_status: Option, + pub battery_status: Option, } impl DeviceRowModelInit { @@ -59,9 +59,7 @@ impl DeviceRowModelInit { Self { title: Some(d.dev_type.to_string()), subtitle: Some(d.name.clone()), - battery_status: d - .battery - .map(|bat| BatteryStatus::new((bat * 100.0).trunc() as u8)), + battery_status: d.battery.map(EnvisionBatteryStatus::from), ..Default::default() } } diff --git a/src/xr_devices.rs b/src/xr_devices.rs index 3862202..1830ecb 100644 --- a/src/xr_devices.rs +++ b/src/xr_devices.rs @@ -1,4 +1,4 @@ -use libmonado_rs; +use libmonado_rs::{self, BatteryStatus}; use std::{fmt::Display, slice::Iter}; const GENERIC_TRACKER_PREFIX: &str = "Found generic tracker device: "; @@ -224,14 +224,13 @@ impl XRDeviceRole { } } -#[derive(Debug, Clone, PartialEq)] +#[derive(Debug, Clone)] pub struct XRDevice { pub dev_type: XRDeviceRole, pub name: String, pub index: String, pub serial: Option, - pub battery: Option, // battery percentage, from 0 to 1 maybe - // still need to implement it in monado + pub battery: Option, } impl Default for XRDevice { @@ -320,9 +319,9 @@ impl XRDevice { res.push(Self { index: dev.id.to_string(), serial: dev.serial().ok(), + battery: dev.battery_status().ok(), name: dev.name, dev_type: xrd, - ..Default::default() }) } });