widgets.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. }
  80. impl Battery {
  81. pub fn new() -> Result<Battery, failure::Error> {
  82. use std::fs;
  83. let mut batteries = Vec::new();
  84. for entry in fs::read_dir("/sys/class/power_supply")? {
  85. let e = entry?;
  86. if e.file_name().to_string_lossy().starts_with("BAT") {
  87. let mut path = e.path();
  88. path.push("capacity");
  89. batteries.push(path);
  90. }
  91. }
  92. Ok(Battery {
  93. file_list: batteries,
  94. })
  95. }
  96. fn read_status(&self) -> Result<f64, failure::Error> {
  97. let charges: Result<Vec<i32>, failure::Error> =
  98. self.file_list.iter().map(|path| {
  99. Ok(std::fs::read_to_string(path)?.trim().parse()?)
  100. }).collect();
  101. let charges = charges?;
  102. let len = charges.len() as f64;
  103. let sum: i32 = charges.into_iter().sum();
  104. Ok(sum as f64 / len / 100.0)
  105. }
  106. }
  107. impl Widget for Battery {
  108. fn draw(&self, d: &Drawing, loc: Located) -> i32 {
  109. let amt = self.read_status();
  110. let sz = d.size.ht - 8;
  111. let x = loc.target_x(d, sz);
  112. match amt {
  113. Ok(x) if x < 0.1 =>
  114. d.ctx.set_source_rgb(1.0, 0.0, 0.0),
  115. Ok(x) if x < 0.5 =>
  116. d.ctx.set_source_rgb(1.0, 1.0, 0.0),
  117. Ok(_) =>
  118. d.ctx.set_source_rgb(0.0, 1.0, 0.5),
  119. Err(_) =>
  120. d.ctx.set_source_rgb(0.0, 0.0, 0.0),
  121. }
  122. d.ctx.rectangle(
  123. x,
  124. 8.0,
  125. sz as f64 * amt.unwrap_or(1.0),
  126. sz as f64 - 8.0,
  127. );
  128. d.ctx.fill();
  129. d.ctx.set_source_rgb(1.0, 1.0, 1.0);
  130. d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
  131. d.ctx.stroke();
  132. sz
  133. }
  134. }