gdritter repos knurling / befffba
Adjust some other drawing primitives using the computed size Getty Ritter 5 years ago
3 changed file(s) with 24 addition(s) and 10 deletion(s). Collapse all Expand all
1414 bg_color: (f64, f64, f64),
1515 fg_color: (f64, f64, f64),
1616 font: String,
17 height: i32,
18 buffer: i32,
1719 }
1820
1921 pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> {
4446 bg_color: defaults::BG_COLOR,
4547 fg_color: defaults::FG_COLOR,
4648 font: format!("{} {}", defaults::FONT_FAMILY, defaults::FONT_SIZE),
49 height: 0,
50 buffer: 0,
4751 };
4852 let table = input.as_table().ok_or(format_err!("invalid config"))?;
4953 let widgets = &table["widgets"];
6973 conf.font = font.as_str().ok_or(format_err!("`font` not a str"))?.to_string();
7074 }
7175 conf.right.reverse();
76
77 let text_height = conf.calc_text_height();
78 let buffer = text_height / 4;
79 conf.height = conf.calc_text_height() + buffer * 2;
80 conf.buffer = buffer;
7281 Ok(conf)
7382 }
7483
105114 lyt: &layout,
106115 size,
107116 stdin,
117 buffer: self.buffer as f64,
108118 };
109119
110120 let mut offset = 10;
123133 &self.font
124134 }
125135
126 pub fn get_height(&self) -> i32 {
136 pub fn get_height(&self) -> i32{
137 self.height
138 }
139
140 fn calc_text_height(&self) -> i32 {
127141 use pango::LayoutExt;
128142
129143 // we get the height here by making a fake surface, rendering
138152 layout.set_font_description(&font);
139153 layout.set_text("lj");
140154 let (_, h) = layout.get_size();
141 (h / pango::SCALE) + 8
155 (h / pango::SCALE)
142156 }
143157 }
1515 // set up the display and the window
1616 let config = config::Config::find_config()?;
1717 let height = config.get_height();
18 println!("height is {}", height);
1918
2019 let mut d = Display::create()?;
2120 let mut ws = Vec::new();
1818 fn draw_text(&self, d: &Drawing, msg: &str) -> i32 {
1919 d.lyt.set_text(msg);
2020 let (w, _) = d.lyt.get_size();
21 d.ctx.move_to(self.target_x(d, w / pango::SCALE), 4.0);
21 d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer);
2222 pangocairo::functions::show_layout(d.ctx, d.lyt);
2323 w / pango::SCALE
2424 }
3636 pub lyt: &'t pango::Layout,
3737 pub size: Size,
3838 pub stdin: &'t str,
39 pub buffer: f64,
3940 }
4041
4142 pub trait Widget {
8384
8485 impl Widget for SmallBox {
8586 fn draw(&self, d: &Drawing, loc: Located) -> i32 {
86 let sz = d.size.ht - 8;
87 let sz = d.size.ht - (d.buffer as i32 * 2);
8788 let x = loc.target_x(d, sz);
88 d.ctx.rectangle(x, 4.0, sz as f64, sz as f64);
89 d.ctx.rectangle(x, d.buffer, sz as f64, sz as f64);
8990 d.ctx.fill();
9091 sz
9192 }
149150 impl Widget for Battery {
150151 fn draw(&self, d: &Drawing, loc: Located) -> i32 {
151152 let amt = self.read_status();
152 let sz = d.size.ht - 8;
153 let sz = d.size.ht - (d.buffer as i32 * 2);
153154 let x = loc.target_x(d, sz);
154155 match amt {
155156 _ if self.is_charging().unwrap_or(false) =>
166167
167168 d.ctx.rectangle(
168169 x,
169 8.0,
170 d.buffer * 2.0,
170171 sz as f64 * amt.unwrap_or(1.0),
171 sz as f64 - 8.0,
172 sz as f64 - d.buffer * 2.0,
172173 );
173174 d.ctx.fill();
174175
175176 d.ctx.set_source_rgb(1.0, 1.0, 1.0);
176 d.ctx.rectangle(x, 8.0, sz as f64, sz as f64 - 8.0);
177 d.ctx.rectangle(x, d.buffer * 2.0, sz as f64, sz as f64 - (d.buffer * 2.0));
177178 d.ctx.stroke();
178179
179180 sz