1 | 1 |
use crate::widgets as w;
|
| 2 |
use std::time;
|
2 | 3 |
|
3 | 4 |
mod defaults {
|
4 | 5 |
pub const BG_COLOR: (f64, f64, f64) = (0.1, 0.1, 0.1);
|
|
9 | 10 |
}
|
10 | 11 |
|
11 | 12 |
pub struct Config {
|
12 | |
left: Vec<Box<dyn w::Widget>>,
|
13 | |
right: Vec<Box<dyn w::Widget>>,
|
| 13 |
left: Vec<WidgetWrapper>,
|
| 14 |
right: Vec<WidgetWrapper>,
|
14 | 15 |
bg_color: (f64, f64, f64),
|
15 | 16 |
fg_color: (f64, f64, f64),
|
16 | 17 |
font: String,
|
17 | 18 |
height: i32,
|
18 | 19 |
buffer: i32,
|
| 20 |
}
|
| 21 |
|
| 22 |
pub struct WidgetWrapper {
|
| 23 |
update: Option<(time::Duration, time::SystemTime)>,
|
| 24 |
widget: Box<dyn w::Widget>,
|
| 25 |
}
|
| 26 |
|
| 27 |
impl WidgetWrapper {
|
| 28 |
fn new(mut widget: Box<dyn w::Widget>) -> WidgetWrapper {
|
| 29 |
let update = if let Some(f) = widget.update_frequency() {
|
| 30 |
widget.update();
|
| 31 |
Some((time::Duration::new(f, 0), time::SystemTime::now()))
|
| 32 |
} else {
|
| 33 |
None
|
| 34 |
};
|
| 35 |
WidgetWrapper { update, widget }
|
| 36 |
}
|
| 37 |
|
| 38 |
fn update(&mut self) {
|
| 39 |
if let Some((freq, ref mut last)) = self.update {
|
| 40 |
if let Ok(since) = last.elapsed() {
|
| 41 |
if since > freq {
|
| 42 |
self.widget.update();
|
| 43 |
*last = time::SystemTime::now();
|
| 44 |
}
|
| 45 |
}
|
| 46 |
}
|
| 47 |
}
|
19 | 48 |
}
|
20 | 49 |
|
21 | 50 |
pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> {
|
|
65 | 94 |
if name == "sep" {
|
66 | 95 |
target = &mut conf.right;
|
67 | 96 |
} else {
|
68 | |
target.push(w::mk_widget(name, section)?);
|
| 97 |
target.push(WidgetWrapper::new(w::mk_widget(name, section)?));
|
69 | 98 |
}
|
70 | 99 |
}
|
71 | 100 |
|
|
142 | 171 |
|
143 | 172 |
let mut offset = 10;
|
144 | 173 |
for w in self.left.iter() {
|
145 | |
offset += 10 + w.draw(&d, w::Located::FromLeft(offset));
|
| 174 |
offset += 10 + w.widget.draw(&d, w::Located::FromLeft(offset));
|
146 | 175 |
}
|
147 | 176 |
offset = 10;
|
148 | 177 |
for w in self.right.iter() {
|
149 | |
offset += 10 + w.draw(&d, w::Located::FromRight(offset));
|
| 178 |
offset += 10 + w.widget.draw(&d, w::Located::FromRight(offset));
|
150 | 179 |
}
|
151 | 180 |
|
152 | 181 |
Ok(())
|
| 182 |
}
|
| 183 |
|
| 184 |
pub fn update(&mut self) {
|
| 185 |
for w in self.left.iter_mut() { w.update() }
|
| 186 |
for w in self.right.iter_mut() { w.update() }
|
153 | 187 |
}
|
154 | 188 |
|
155 | 189 |
pub fn font(&self) -> &str {
|