basic battery module/service
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
//! Battery module: prints the charge percentage from the battery service.
|
||||
|
||||
use iced::widget::text;
|
||||
use iced::{Element, Task};
|
||||
|
||||
use common::{BarModule, BatteryEvent, BatteryMsg, ModuleEffect, Service, Wire};
|
||||
|
||||
pub struct Battery {
|
||||
percent: Option<i32>,
|
||||
}
|
||||
|
||||
impl Battery {
|
||||
pub fn new() -> Self {
|
||||
Self { percent: None }
|
||||
}
|
||||
}
|
||||
|
||||
impl BarModule for Battery {
|
||||
fn id(&self) -> &'static str {
|
||||
"battery"
|
||||
}
|
||||
|
||||
fn view(&self, _window_id: Option<iced::window::Id>) -> Element<'_, Wire> {
|
||||
match self.percent {
|
||||
Some(percent) => text(format!("{percent}%")).into(),
|
||||
None => text("--").into(),
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, msg: Wire) -> Task<ModuleEffect> {
|
||||
if let Some(BatteryEvent::PercentageChanged(percent)) = msg.downcast::<BatteryEvent>()
|
||||
{
|
||||
self.percent = Some(*percent);
|
||||
}
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn services(&self) -> Vec<Service> {
|
||||
vec![BatteryMsg::Events.key()]
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
//! a new file under `src/` plus one entry in `all()` — core never changes.
|
||||
|
||||
mod audio;
|
||||
mod battery;
|
||||
mod clock;
|
||||
mod weather;
|
||||
mod workspaces;
|
||||
@@ -15,6 +16,7 @@ pub use weather::*;
|
||||
pub fn all(module_config: Table) -> Vec<Box<dyn BarModule>> {
|
||||
vec![
|
||||
Box::new(audio::Audio::new()),
|
||||
Box::new(battery::Battery::new()),
|
||||
Box::new(clock::Clock::new(Some(module_config.clone()))),
|
||||
Box::new(weather::WeatherModule::default()),
|
||||
Box::new(workspaces::Workspaces::new(Some(module_config))),
|
||||
|
||||
Reference in New Issue
Block a user