Browse Source

Adjust some other drawing primitives using the computed size

Getty Ritter 4 years ago
parent
commit
befffba7be
3 changed files with 24 additions and 10 deletions
  1. 16 2
      src/config.rs
  2. 0 1
      src/main.rs
  3. 8 7
      src/widgets.rs

+ 16 - 2
src/config.rs

@@ -14,6 +14,8 @@ pub struct Config {
     bg_color: (f64, f64, f64),
     fg_color: (f64, f64, f64),
     font: String,
+    height: i32,
+    buffer: i32,
 }
 
 pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> {
@@ -44,6 +46,8 @@ impl Config {
             bg_color: defaults::BG_COLOR,
             fg_color: defaults::FG_COLOR,
             font: format!("{} {}", defaults::FONT_FAMILY, defaults::FONT_SIZE),
+            height: 0,
+            buffer: 0,
         };
         let table = input.as_table().ok_or(format_err!("invalid config"))?;
         let widgets = &table["widgets"];
@@ -69,6 +73,11 @@ impl Config {
             conf.font = font.as_str().ok_or(format_err!("`font` not a str"))?.to_string();
         }
         conf.right.reverse();
+
+        let text_height = conf.calc_text_height();
+        let buffer = text_height / 4;
+        conf.height = conf.calc_text_height() + buffer * 2;
+        conf.buffer = buffer;
         Ok(conf)
     }
 
@@ -105,6 +114,7 @@ impl Config {
             lyt: &layout,
             size,
             stdin,
+            buffer: self.buffer as f64,
         };
 
         let mut offset = 10;
@@ -123,7 +133,11 @@ impl Config {
         &self.font
     }
 
-    pub fn get_height(&self) -> i32 {
+    pub fn get_height(&self) -> i32{
+        self.height
+    }
+
+    fn calc_text_height(&self) -> i32 {
         use pango::LayoutExt;
 
         // we get the height here by making a fake surface, rendering
@@ -138,6 +152,6 @@ impl Config {
         layout.set_font_description(&font);
         layout.set_text("lj");
         let (_, h) = layout.get_size();
-        (h / pango::SCALE) + 8
+        (h / pango::SCALE)
     }
 }

+ 0 - 1
src/main.rs

@@ -15,7 +15,6 @@ fn main() -> Result<(), failure::Error> {
     // set up the display and the window
     let config = config::Config::find_config()?;
     let height = config.get_height();
-    println!("height is {}", height);
 
     let mut d = Display::create()?;
     let mut ws = Vec::new();

+ 8 - 7
src/widgets.rs

@@ -18,7 +18,7 @@ impl Located {
     fn draw_text(&self, d: &Drawing, msg: &str) -> i32 {
         d.lyt.set_text(msg);
         let (w, _) = d.lyt.get_size();
-        d.ctx.move_to(self.target_x(d, w / pango::SCALE), 4.0);
+        d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer);
         pangocairo::functions::show_layout(d.ctx, d.lyt);
         w / pango::SCALE
     }
@@ -36,6 +36,7 @@ pub struct Drawing<'t> {
     pub lyt: &'t pango::Layout,
     pub size: Size,
     pub stdin: &'t str,
+    pub buffer: f64,
 }
 
 pub trait Widget {
@@ -83,9 +84,9 @@ pub struct SmallBox;
 
 impl Widget for SmallBox {
     fn draw(&self, d: &Drawing, loc: Located) -> i32 {
-        let sz = d.size.ht - 8;
+        let sz = d.size.ht - (d.buffer as i32 * 2);
         let x = loc.target_x(d, sz);
-        d.ctx.rectangle(x, 4.0, sz as f64, sz as f64);
+        d.ctx.rectangle(x, d.buffer, sz as f64, sz as f64);
         d.ctx.fill();
         sz
     }
@@ -149,7 +150,7 @@ impl Battery {
 impl Widget for Battery {
     fn draw(&self, d: &Drawing, loc: Located) -> i32 {
         let amt = self.read_status();
-        let sz = d.size.ht - 8;
+        let sz = d.size.ht - (d.buffer as i32 * 2);
         let x = loc.target_x(d, sz);
         match amt {
             _ if self.is_charging().unwrap_or(false) =>
@@ -166,14 +167,14 @@ impl Widget for Battery {
 
         d.ctx.rectangle(
             x,
-            8.0,
+            d.buffer * 2.0,
             sz as f64 * amt.unwrap_or(1.0),
-            sz as f64 - 8.0,
+            sz as f64 - d.buffer * 2.0,
         );
         d.ctx.fill();
 
         d.ctx.set_source_rgb(1.0, 1.0, 1.0);
-        d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
+        d.ctx.rectangle(x, d.buffer * 2.0, sz as f64, sz as f64 - (d.buffer * 2.0));
         d.ctx.stroke();
 
         sz