diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 28 | ||||
| -rw-r--r-- | src/module.rs | 17 | ||||
| -rw-r--r-- | src/module/time.rs | 13 |
3 files changed, 42 insertions, 16 deletions
diff --git a/src/main.rs b/src/main.rs index 2d3c1d1..38a993f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,7 +57,11 @@ struct Dock { font: Arc<Font>, baseline_position: i16, facilities: FacilityMap, - time_module: Arc<Mutex<TimeModule>>, + modules: Vec<(Arc<Mutex<Box<dyn Module>>>, ModuleFraming)>, +} + +struct ModuleFraming { + alignment: Alignment, } unsafe impl Send for Dock { } @@ -175,7 +179,12 @@ impl Dock { let facilities = FacilityMap::new(); - let time_module = TimeModule::new()?; + let mut modules = Vec::new(); + modules.push((Arc::new(Mutex::new(TimeModule::new()?.as_box())), + ModuleFraming + { + alignment: Alignment::Right, + })); /* We start is_visible at false. Since redraw events set it to true, this * won't make us miss any drawing. @@ -187,7 +196,7 @@ impl Dock { font, baseline_position, facilities, - time_module, + modules, }; dock.set_string_property(&xcb::x::ATOM_WM_NAME, b"sandbar")?; @@ -297,13 +306,16 @@ impl Dock { self.draw_text("playful cool fic tech nonfic", Alignment::Left)?; - let time = self.time_module.lock()?.format(); - self.draw_text(&time, Alignment::Right)?; + for (module, framing) in &self.modules { + let text = module.lock()?.format(); + self.draw_text(&text, framing.alignment)?; + } let mut shortest_duration = None; - - let next_duration = self.time_module.lock()?.sleep_duration(); - shortest_duration = shorter_duration(shortest_duration, next_duration); + for (module, _) in &self.modules { + let next_duration = module.lock()?.sleep_duration(); + shortest_duration = shorter_duration(shortest_duration, next_duration); + } Ok(shortest_duration) } diff --git a/src/module.rs b/src/module.rs index c859bd3..d10e436 100644 --- a/src/module.rs +++ b/src/module.rs @@ -10,10 +10,23 @@ pub use time::TimeModule; pub mod time; -pub trait Module { +/* As with Facility, we need a separate trait for the stuff that wouldn't be + * dyn-compatible. + */ +pub trait CreatableModule: Module + Sized { const REQUIRED_FACILITIES: FacilityType; - fn new() -> Result<Arc<Mutex<Self>>>; + fn new() -> Result<Self>; + + /* The only way to coerce to dyn is to return as it! */ + fn as_box<'a>(self) -> Box<dyn Module + 'a> + where Self: 'a + { + Box::new(self) + } +} + +pub trait Module { fn format(&mut self) -> String; fn sleep_duration(&self) -> Option<Duration>; } diff --git a/src/module/time.rs b/src/module/time.rs index f334fd0..0388f62 100644 --- a/src/module/time.rs +++ b/src/module/time.rs @@ -1,8 +1,7 @@ use crate::error::Result; use crate::facility::FacilityType; -use crate::module::Module; +use crate::module::{ CreatableModule, Module }; -use std::sync::{ Arc, Mutex }; use std::time::Duration; use time::OffsetDateTime; @@ -15,22 +14,24 @@ pub struct TimeModule { } -impl Module for TimeModule { +impl CreatableModule for TimeModule { const REQUIRED_FACILITIES: FacilityType = FacilityType::empty(); - fn new() -> Result<Arc<Mutex<TimeModule>>> { + fn new() -> Result<TimeModule> { let format_string = "[weekday repr:short] [year]-[month]-[day] ".to_owned() + "[hour repr:24]:[minute]:[second]"; let time_format = parse_owned::<3>(&format_string).unwrap(); - Ok(Arc::new(Mutex::new(TimeModule { + Ok(TimeModule { time_format, most_recent_report: None, - }))) + }) } +} +impl Module for TimeModule { fn format(&mut self) -> String { let time = OffsetDateTime::now_local().unwrap(); |