mirror of
https://gitlab.com/gabmus/envision.git
synced 2025-08-09 17:48:49 +00:00
feat: battery icon with percentage (still inactive, missing piece from monado/libmonado)
This commit is contained in:
parent
06d472729d
commit
03de81d2fb
4 changed files with 62 additions and 4 deletions
35
src/ui/battery_status.rs
Normal file
35
src/ui/battery_status.rs
Normal 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",
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
ui::devices_box::DevicesBoxMsg,
|
||||
ui::{battery_status::BatteryStatus, devices_box::DevicesBoxMsg},
|
||||
xr_devices::{XRDevice, XRDeviceRole},
|
||||
};
|
||||
use adw::prelude::*;
|
||||
|
@ -42,6 +42,7 @@ pub struct DeviceRowModel {
|
|||
subtitle: String,
|
||||
state: DeviceRowState,
|
||||
suffix: Option<gtk::Widget>,
|
||||
battery_status: Option<BatteryStatus>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default)]
|
||||
|
@ -50,6 +51,7 @@ pub struct DeviceRowModelInit {
|
|||
pub subtitle: Option<String>,
|
||||
pub state: Option<DeviceRowState>,
|
||||
pub suffix: Option<gtk::Widget>,
|
||||
pub battery_status: Option<BatteryStatus>,
|
||||
}
|
||||
|
||||
impl DeviceRowModelInit {
|
||||
|
@ -57,6 +59,9 @@ 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)),
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
@ -86,6 +91,22 @@ impl AsyncFactoryComponent for DeviceRowModel {
|
|||
add_prefix: icon = >k::Image {
|
||||
set_icon_name: Some(self.state.icon()),
|
||||
},
|
||||
add_suffix: batt_info = >k::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_subtitle: self.subtitle.as_str(),
|
||||
}
|
||||
|
@ -120,6 +141,7 @@ impl AsyncFactoryComponent for DeviceRowModel {
|
|||
title: init.title.unwrap_or_default(),
|
||||
subtitle: init.subtitle.unwrap_or_default(),
|
||||
state: init.state.unwrap_or_default(),
|
||||
battery_status: init.battery_status,
|
||||
suffix: init.suffix,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
mod about_dialog;
|
||||
mod alert;
|
||||
pub mod app;
|
||||
mod battery_status;
|
||||
mod build_window;
|
||||
pub mod cmdline_opts;
|
||||
mod debug_view;
|
||||
|
|
|
@ -229,8 +229,8 @@ pub struct XRDevice {
|
|||
pub dev_type: XRDeviceRole,
|
||||
pub name: String,
|
||||
pub id: String,
|
||||
pub battery: f32, // battery percentage, from 0 to 1 maybe
|
||||
// still need to implement it in monado & gui
|
||||
pub battery: Option<f32>, // battery percentage, from 0 to 1 maybe
|
||||
// still need to implement it in monado
|
||||
}
|
||||
|
||||
impl Default for XRDevice {
|
||||
|
@ -239,7 +239,7 @@ impl Default for XRDevice {
|
|||
dev_type: XRDeviceRole::GenericTracker,
|
||||
name: String::default(),
|
||||
id: String::default(),
|
||||
battery: f32::default(),
|
||||
battery: Option::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue