feat: battery icon with percentage (still inactive, missing piece from monado/libmonado)

This commit is contained in:
Gabriele Musco 2024-02-12 13:27:46 +01:00
commit 03de81d2fb
No known key found for this signature in database
GPG key ID: 1068D795C80E51DE
4 changed files with 62 additions and 4 deletions

35
src/ui/battery_status.rs Normal file
View file

@ -0,0 +1,35 @@
use std::fmt::Display;
#[derive(Debug, Clone, Default)]
pub struct BatteryStatus {
/// between 0 and 100
pub percentage: u8,
}
impl Display for BatteryStatus {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("{}%", self.percentage))
}
}
impl BatteryStatus {
pub fn new(percentage: u8) -> Self {
Self { percentage }
}
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",
_ => "battery-level-0-symbolic",
}
}
}

View file

@ -1,5 +1,5 @@
use crate::{ use crate::{
ui::devices_box::DevicesBoxMsg, ui::{battery_status::BatteryStatus, devices_box::DevicesBoxMsg},
xr_devices::{XRDevice, XRDeviceRole}, xr_devices::{XRDevice, XRDeviceRole},
}; };
use adw::prelude::*; use adw::prelude::*;
@ -42,6 +42,7 @@ pub struct DeviceRowModel {
subtitle: String, subtitle: String,
state: DeviceRowState, state: DeviceRowState,
suffix: Option<gtk::Widget>, suffix: Option<gtk::Widget>,
battery_status: Option<BatteryStatus>,
} }
#[derive(Debug, Default)] #[derive(Debug, Default)]
@ -50,6 +51,7 @@ pub struct DeviceRowModelInit {
pub subtitle: Option<String>, pub subtitle: Option<String>,
pub state: Option<DeviceRowState>, pub state: Option<DeviceRowState>,
pub suffix: Option<gtk::Widget>, pub suffix: Option<gtk::Widget>,
pub battery_status: Option<BatteryStatus>,
} }
impl DeviceRowModelInit { impl DeviceRowModelInit {
@ -57,6 +59,9 @@ impl DeviceRowModelInit {
Self { Self {
title: Some(d.dev_type.to_string()), title: Some(d.dev_type.to_string()),
subtitle: Some(d.name.clone()), subtitle: Some(d.name.clone()),
battery_status: d
.battery
.map(|bat| BatteryStatus::new((bat * 100.0).trunc() as u8)),
..Default::default() ..Default::default()
} }
} }
@ -86,6 +91,22 @@ impl AsyncFactoryComponent for DeviceRowModel {
add_prefix: icon = &gtk::Image { add_prefix: icon = &gtk::Image {
set_icon_name: Some(self.state.icon()), set_icon_name: Some(self.state.icon()),
}, },
add_suffix: batt_info = &gtk::Box {
set_visible: self.battery_status.is_some(),
set_orientation: gtk::Orientation::Horizontal,
set_spacing: 6,
gtk::Image {
set_icon_name: self.battery_status
.as_ref()
.map(|bs| bs.icon()),
},
gtk::Label {
set_text: &self.battery_status
.as_ref()
.map(|bs| bs.to_string())
.unwrap_or_default(),
},
},
set_title: self.title.as_str(), set_title: self.title.as_str(),
set_subtitle: self.subtitle.as_str(), set_subtitle: self.subtitle.as_str(),
} }
@ -120,6 +141,7 @@ impl AsyncFactoryComponent for DeviceRowModel {
title: init.title.unwrap_or_default(), title: init.title.unwrap_or_default(),
subtitle: init.subtitle.unwrap_or_default(), subtitle: init.subtitle.unwrap_or_default(),
state: init.state.unwrap_or_default(), state: init.state.unwrap_or_default(),
battery_status: init.battery_status,
suffix: init.suffix, suffix: init.suffix,
} }
} }

View file

@ -1,6 +1,7 @@
mod about_dialog; mod about_dialog;
mod alert; mod alert;
pub mod app; pub mod app;
mod battery_status;
mod build_window; mod build_window;
pub mod cmdline_opts; pub mod cmdline_opts;
mod debug_view; mod debug_view;

View file

@ -229,8 +229,8 @@ pub struct XRDevice {
pub dev_type: XRDeviceRole, pub dev_type: XRDeviceRole,
pub name: String, pub name: String,
pub id: String, pub id: String,
pub battery: f32, // battery percentage, from 0 to 1 maybe pub battery: Option<f32>, // battery percentage, from 0 to 1 maybe
// still need to implement it in monado & gui // still need to implement it in monado
} }
impl Default for XRDevice { impl Default for XRDevice {
@ -239,7 +239,7 @@ impl Default for XRDevice {
dev_type: XRDeviceRole::GenericTracker, dev_type: XRDeviceRole::GenericTracker,
name: String::default(), name: String::default(),
id: String::default(), id: String::default(),
battery: f32::default(), battery: Option::default(),
} }
} }
} }