Adjust some other drawing primitives using the computed size
Getty Ritter
6 years ago
| 14 | 14 | bg_color: (f64, f64, f64), |
| 15 | 15 | fg_color: (f64, f64, f64), |
| 16 | 16 | font: String, |
| 17 | height: i32, | |
| 18 | buffer: i32, | |
| 17 | 19 | } |
| 18 | 20 | |
| 19 | 21 | pub fn color_from_hex(input: &str) -> Result<(f64, f64, f64), failure::Error> { |
| 44 | 46 | bg_color: defaults::BG_COLOR, |
| 45 | 47 | fg_color: defaults::FG_COLOR, |
| 46 | 48 | font: format!("{} {}", defaults::FONT_FAMILY, defaults::FONT_SIZE), |
| 49 | height: 0, | |
| 50 | buffer: 0, | |
| 47 | 51 | }; |
| 48 | 52 | let table = input.as_table().ok_or(format_err!("invalid config"))?; |
| 49 | 53 | let widgets = &table["widgets"]; |
| 69 | 73 | conf.font = font.as_str().ok_or(format_err!("`font` not a str"))?.to_string(); |
| 70 | 74 | } |
| 71 | 75 | 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; | |
| 72 | 81 | Ok(conf) |
| 73 | 82 | } |
| 74 | 83 | |
| 105 | 114 | lyt: &layout, |
| 106 | 115 | size, |
| 107 | 116 | stdin, |
| 117 | buffer: self.buffer as f64, | |
| 108 | 118 | }; |
| 109 | 119 | |
| 110 | 120 | let mut offset = 10; |
| 123 | 133 | &self.font |
| 124 | 134 | } |
| 125 | 135 | |
| 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 { | |
| 127 | 141 | use pango::LayoutExt; |
| 128 | 142 | |
| 129 | 143 | // we get the height here by making a fake surface, rendering |
| 138 | 152 | layout.set_font_description(&font); |
| 139 | 153 | layout.set_text("lj"); |
| 140 | 154 | let (_, h) = layout.get_size(); |
| 141 |
(h / pango::SCALE) |
|
| 155 | (h / pango::SCALE) | |
| 142 | 156 | } |
| 143 | 157 | } |
| 15 | 15 | // set up the display and the window |
| 16 | 16 | let config = config::Config::find_config()?; |
| 17 | 17 | let height = config.get_height(); |
| 18 | println!("height is {}", height); | |
| 19 | 18 | |
| 20 | 19 | let mut d = Display::create()?; |
| 21 | 20 | let mut ws = Vec::new(); |
| 18 | 18 | fn draw_text(&self, d: &Drawing, msg: &str) -> i32 { |
| 19 | 19 | d.lyt.set_text(msg); |
| 20 | 20 | let (w, _) = d.lyt.get_size(); |
| 21 |
d.ctx.move_to(self.target_x(d, w / pango::SCALE), |
|
| 21 | d.ctx.move_to(self.target_x(d, w / pango::SCALE), d.buffer); | |
| 22 | 22 | pangocairo::functions::show_layout(d.ctx, d.lyt); |
| 23 | 23 | w / pango::SCALE |
| 24 | 24 | } |
| 36 | 36 | pub lyt: &'t pango::Layout, |
| 37 | 37 | pub size: Size, |
| 38 | 38 | pub stdin: &'t str, |
| 39 | pub buffer: f64, | |
| 39 | 40 | } |
| 40 | 41 | |
| 41 | 42 | pub trait Widget { |
| 83 | 84 | |
| 84 | 85 | impl Widget for SmallBox { |
| 85 | 86 | fn draw(&self, d: &Drawing, loc: Located) -> i32 { |
| 86 |
let sz = d.size.ht - |
|
| 87 | let sz = d.size.ht - (d.buffer as i32 * 2); | |
| 87 | 88 | let x = loc.target_x(d, sz); |
| 88 |
d.ctx.rectangle(x, |
|
| 89 | d.ctx.rectangle(x, d.buffer, sz as f64, sz as f64); | |
| 89 | 90 | d.ctx.fill(); |
| 90 | 91 | sz |
| 91 | 92 | } |
| 149 | 150 | impl Widget for Battery { |
| 150 | 151 | fn draw(&self, d: &Drawing, loc: Located) -> i32 { |
| 151 | 152 | let amt = self.read_status(); |
| 152 |
let sz = d.size.ht - |
|
| 153 | let sz = d.size.ht - (d.buffer as i32 * 2); | |
| 153 | 154 | let x = loc.target_x(d, sz); |
| 154 | 155 | match amt { |
| 155 | 156 | _ if self.is_charging().unwrap_or(false) => |
| 166 | 167 | |
| 167 | 168 | d.ctx.rectangle( |
| 168 | 169 | x, |
| 169 |
|
|
| 170 | d.buffer * 2.0, | |
| 170 | 171 | sz as f64 * amt.unwrap_or(1.0), |
| 171 |
sz as f64 - |
|
| 172 | sz as f64 - d.buffer * 2.0, | |
| 172 | 173 | ); |
| 173 | 174 | d.ctx.fill(); |
| 174 | 175 | |
| 175 | 176 | d.ctx.set_source_rgb(1.0, 1.0, 1.0); |
| 176 |
d.ctx.rectangle(x, |
|
| 177 | d.ctx.rectangle(x, d.buffer * 2.0, sz as f64, sz as f64 - (d.buffer * 2.0)); | |
| 177 | 178 | d.ctx.stroke(); |
| 178 | 179 | |
| 179 | 180 | sz |