widgets.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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), 4.0);
  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. }
  35. pub trait Widget {
  36. fn draw(&self, d: &Drawing, loc: Located) -> i32;
  37. }
  38. #[derive(Debug)]
  39. pub struct Time {
  40. fmt: String,
  41. }
  42. impl Time {
  43. pub fn new() -> Time {
  44. Time {
  45. fmt: format!("%a %b %d %H:%M")
  46. }
  47. }
  48. }
  49. impl Widget for Time {
  50. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  51. let now = chrono::Local::now();
  52. loc.draw_text(d, &format!("{}", &now.format(&self.fmt)))
  53. }
  54. }
  55. #[derive(Debug)]
  56. pub struct Stdin;
  57. impl Stdin {
  58. pub fn new() -> Stdin {
  59. Stdin
  60. }
  61. }
  62. impl Widget for Stdin {
  63. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  64. loc.draw_text(d, &d.stdin)
  65. }
  66. }
  67. pub struct SmallBox;
  68. impl Widget for SmallBox {
  69. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  70. let sz = d.size.ht - 8;
  71. let x = loc.target_x(d, sz);
  72. d.ctx.rectangle(x, 4.0, sz as f64, sz as f64);
  73. d.ctx.fill();
  74. sz
  75. }
  76. }
  77. pub struct Battery {
  78. file_list: Vec<std::path::PathBuf>,
  79. charging: Option<std::path::PathBuf>
  80. }
  81. impl Battery {
  82. pub fn new() -> Result<Battery, failure::Error> {
  83. use std::fs;
  84. let mut batteries = Vec::new();
  85. for entry in fs::read_dir("/sys/class/power_supply")? {
  86. let e = entry?;
  87. if e.file_name().to_string_lossy().starts_with("BAT") {
  88. let mut path = e.path();
  89. path.push("capacity");
  90. batteries.push(path);
  91. }
  92. }
  93. let ac_path = std::path::Path::new("/sys/class/power_supply/AC/online");
  94. Ok(Battery {
  95. file_list: batteries,
  96. charging: if ac_path.exists() {
  97. Some(ac_path.to_path_buf())
  98. } else {
  99. None
  100. },
  101. })
  102. }
  103. fn is_charging(&self) -> Result<bool, failure::Error> {
  104. if let Some(path) = &self.charging {
  105. let is_connected: i32 =
  106. std::fs::read_to_string(path)?.trim().parse()?;
  107. Ok(is_connected != 0)
  108. } else {
  109. Ok(false)
  110. }
  111. }
  112. fn read_status(&self) -> Result<f64, failure::Error> {
  113. let charges: Result<Vec<i32>, failure::Error> =
  114. self.file_list.iter().map(|path| {
  115. Ok(std::fs::read_to_string(path)?.trim().parse()?)
  116. }).collect();
  117. let charges = charges?;
  118. let len = charges.len() as f64;
  119. let sum: i32 = charges.into_iter().sum();
  120. Ok(sum as f64 / len / 100.0)
  121. }
  122. }
  123. impl Widget for Battery {
  124. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  125. let amt = self.read_status();
  126. let sz = d.size.ht - 8;
  127. let x = loc.target_x(d, sz);
  128. match amt {
  129. _ if self.is_charging().unwrap_or(false) =>
  130. d.ctx.set_source_rgb(0.5, 0.5, 1.0),
  131. Ok(x) if x < 0.1 =>
  132. d.ctx.set_source_rgb(1.0, 0.0, 0.0),
  133. Ok(x) if x < 0.5 =>
  134. d.ctx.set_source_rgb(1.0, 1.0, 0.0),
  135. Ok(_) =>
  136. d.ctx.set_source_rgb(0.0, 1.0, 0.5),
  137. Err(_) =>
  138. d.ctx.set_source_rgb(0.0, 0.0, 0.0),
  139. }
  140. d.ctx.rectangle(
  141. x,
  142. 8.0,
  143. sz as f64 * amt.unwrap_or(1.0),
  144. sz as f64 - 8.0,
  145. );
  146. d.ctx.fill();
  147. d.ctx.set_source_rgb(1.0, 1.0, 1.0);
  148. d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
  149. d.ctx.stroke();
  150. sz
  151. }
  152. }