sensors.rs 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. use config::Config;
  2. use super::super::sensors;
  3. use super::super::sensors::Sensor;
  4. use ui::widget;
  5. const MARGIN: u16 = 7;
  6. pub struct Sensors {
  7. context: widget::DrawContext,
  8. sensors: Vec<Box<Sensor>>
  9. }
  10. impl Sensors {
  11. pub fn new(context: widget::DrawContext, config: &Config) -> Sensors {
  12. Sensors {
  13. context: context,
  14. sensors: sensors::sensor_list(config)
  15. }
  16. }
  17. }
  18. impl widget::Widget for Sensors {
  19. fn render(&mut self, x: u16) {
  20. let mut offset = x;
  21. for ref sensor in self.sensors.iter() {
  22. let icon_width = self.context.measure_text(&sensor.icon());
  23. let status_width = self.context.measure_text(&sensor.status());
  24. self.context.set_bg_color(0x6666, 0x6666, 0x6666, 0xFFFF);
  25. self.context.draw_bg(offset, icon_width + MARGIN * 2);
  26. self.context.draw_text(&sensor.icon(), offset + MARGIN);
  27. offset += icon_width + MARGIN * 2;
  28. self.context.set_bg_color(0x0, 0x0, 0x0, 0xFFFF);
  29. self.context.draw_text(&sensor.status(), offset + MARGIN);
  30. offset += status_width + MARGIN * 2;
  31. }
  32. }
  33. fn width(&mut self) -> u16 {
  34. let mut sum = 0;
  35. for ref sensor in self.sensors.iter() {
  36. let text = format!("{}{}", sensor.icon(), sensor.status());
  37. sum += self.context.measure_text(&text) + MARGIN * 4;
  38. }
  39. sum
  40. }
  41. fn handle_event(&mut self, event: &widget::Message) -> bool {
  42. match event {
  43. &widget::Message::Update =>
  44. for ref mut sensor in self.sensors.iter_mut() {
  45. sensor.process()
  46. },
  47. _ => {}
  48. }
  49. false
  50. }
  51. }