widgets.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. use pango::LayoutExt;
  2. #[derive(Debug,Clone,Copy)]
  3. pub struct Size {
  4. pub wd: i32,
  5. pub ht: i32,
  6. pub xo: i32,
  7. pub yo: i32,
  8. }
  9. #[derive(Debug,Clone,Copy)]
  10. pub enum Located {
  11. FromLeft(i32),
  12. FromRight(i32),
  13. }
  14. impl Located {
  15. fn draw_text(&self, d: &Drawing, msg: &str) -> i32 {
  16. d.lyt.set_text(msg);
  17. let (w, _) = d.lyt.get_size();
  18. d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer);
  19. pangocairo::functions::show_layout(d.ctx, d.lyt);
  20. w / pango::SCALE
  21. }
  22. fn target_x(&self, d: &Drawing, w: i32) -> f64 {
  23. match self {
  24. Located::FromLeft(x) => *x as f64,
  25. Located::FromRight(x) => (d.size.wd - (x + w)) as f64,
  26. }
  27. }
  28. }
  29. pub struct Drawing<'t> {
  30. pub ctx: &'t cairo::Context,
  31. pub lyt: &'t pango::Layout,
  32. pub size: Size,
  33. pub stdin: &'t str,
  34. pub buffer: f64,
  35. }
  36. pub trait Widget {
  37. fn draw(&self, d: &Drawing, loc: Located) -> i32;
  38. }
  39. #[derive(Debug)]
  40. pub struct Time {
  41. fmt: String,
  42. }
  43. impl Time {
  44. pub fn new() -> Time {
  45. Time {
  46. fmt: format!("%a %b %d %H:%M")
  47. }
  48. }
  49. }
  50. impl Widget for Time {
  51. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  52. let now = chrono::Local::now();
  53. loc.draw_text(d, &format!("{}", &now.format(&self.fmt)))
  54. }
  55. }
  56. #[derive(Debug)]
  57. pub struct Stdin;
  58. impl Stdin {
  59. pub fn new() -> Stdin {
  60. Stdin
  61. }
  62. }
  63. impl Widget for Stdin {
  64. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  65. loc.draw_text(d, &d.stdin)
  66. }
  67. }
  68. pub struct SmallBox;
  69. impl Widget for SmallBox {
  70. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  71. let sz = d.size.ht - (d.buffer as i32 * 2);
  72. let x = loc.target_x(d, sz);
  73. d.ctx.rectangle(x, d.buffer, sz as f64, sz as f64);
  74. d.ctx.fill();
  75. sz
  76. }
  77. }
  78. pub struct Battery {
  79. file_list: Vec<std::path::PathBuf>,
  80. charging: Option<std::path::PathBuf>
  81. }
  82. impl Battery {
  83. pub fn new() -> Result<Battery, failure::Error> {
  84. use std::fs;
  85. let mut batteries = Vec::new();
  86. for entry in fs::read_dir("/sys/class/power_supply")? {
  87. let e = entry?;
  88. if e.file_name().to_string_lossy().starts_with("BAT") {
  89. let mut path = e.path();
  90. path.push("capacity");
  91. batteries.push(path);
  92. }
  93. }
  94. let ac_path = std::path::Path::new("/sys/class/power_supply/AC/online");
  95. Ok(Battery {
  96. file_list: batteries,
  97. charging: if ac_path.exists() {
  98. Some(ac_path.to_path_buf())
  99. } else {
  100. None
  101. },
  102. })
  103. }
  104. fn is_charging(&self) -> Result<bool, failure::Error> {
  105. if let Some(path) = &self.charging {
  106. let is_connected: i32 =
  107. std::fs::read_to_string(path)?.trim().parse()?;
  108. Ok(is_connected != 0)
  109. } else {
  110. Ok(false)
  111. }
  112. }
  113. fn read_status(&self) -> Result<f64, failure::Error> {
  114. let charges: Result<Vec<i32>, failure::Error> =
  115. self.file_list.iter().map(|path| {
  116. Ok(std::fs::read_to_string(path)?.trim().parse()?)
  117. }).collect();
  118. let charges = charges?;
  119. let len = charges.len() as f64;
  120. let sum: i32 = charges.into_iter().sum();
  121. Ok(sum as f64 / len / 100.0)
  122. }
  123. }
  124. impl Widget for Battery {
  125. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  126. let amt = self.read_status();
  127. let sz = d.size.ht - (d.buffer as i32 * 2);
  128. let x = loc.target_x(d, sz);
  129. match amt {
  130. _ if self.is_charging().unwrap_or(false) =>
  131. d.ctx.set_source_rgb(0.5, 0.5, 1.0),
  132. Ok(x) if x < 0.1 =>
  133. d.ctx.set_source_rgb(1.0, 0.0, 0.0),
  134. Ok(x) if x < 0.5 =>
  135. d.ctx.set_source_rgb(1.0, 1.0, 0.0),
  136. Ok(_) =>
  137. d.ctx.set_source_rgb(0.0, 1.0, 0.5),
  138. Err(_) =>
  139. d.ctx.set_source_rgb(0.0, 0.0, 0.0),
  140. }
  141. d.ctx.rectangle(
  142. x,
  143. d.buffer * 2.0,
  144. sz as f64 * amt.unwrap_or(1.0),
  145. sz as f64 - d.buffer * 2.0,
  146. );
  147. d.ctx.fill();
  148. d.ctx.set_source_rgb(1.0, 1.0, 1.0);
  149. d.ctx.rectangle(x, d.buffer * 2.0, sz as f64, sz as f64 - (d.buffer * 2.0));
  150. d.ctx.stroke();
  151. sz
  152. }
  153. }