| 1 | use super::blocks::Block; |
| 2 | use super::font::{Font, FontDraw}; |
| 3 | use crate::Config; |
| 4 | use anyhow::Result; |
| 5 | use std::time::Instant; |
| 6 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 7 | use x11rb::connection::Connection; |
| 8 | use x11rb::protocol::xproto::*; |
| 9 | use x11rb::rust_connection::RustConnection; |
| 10 | |
| 11 | pub struct Bar { |
| 12 | window: Window, |
| 13 | width: u16, |
| 14 | height: u16, |
| 15 | graphics_context: Gcontext, |
| 16 | |
| 17 | font: Font, |
| 18 | font_draw: FontDraw, |
| 19 | display: *mut x11::xlib::Display, |
| 20 | |
| 21 | tag_widths: Vec<u16>, |
| 22 | needs_redraw: bool, |
| 23 | |
| 24 | blocks: Vec<Box<dyn Block>>, |
| 25 | block_last_updates: Vec<Instant>, |
| 26 | block_underlines: Vec<bool>, |
| 27 | status_text: String, |
| 28 | |
| 29 | tags: Vec<String>, |
| 30 | scheme_normal: crate::ColorScheme, |
| 31 | scheme_occupied: crate::ColorScheme, |
| 32 | scheme_selected: crate::ColorScheme, |
| 33 | } |
| 34 | |
| 35 | impl Bar { |
| 36 | pub fn new( |
| 37 | connection: &RustConnection, |
| 38 | screen: &Screen, |
| 39 | screen_num: usize, |
| 40 | config: &Config, |
| 41 | ) -> Result<Self> { |
| 42 | let window = connection.generate_id()?; |
| 43 | let graphics_context = connection.generate_id()?; |
| 44 | |
| 45 | let width = screen.width_in_pixels; |
| 46 | |
| 47 | let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) }; |
| 48 | if display.is_null() { |
| 49 | anyhow::bail!("Failed to open X11 display for XFT"); |
| 50 | } |
| 51 | let font = Font::new(display, screen_num as i32, &config.font)?; |
| 52 | |
| 53 | let height = (font.height() as f32 * 1.4) as u16; |
| 54 | |
| 55 | connection.create_window( |
| 56 | COPY_DEPTH_FROM_PARENT, |
| 57 | window, |
| 58 | screen.root, |
| 59 | 0, |
| 60 | 0, |
| 61 | width, |
| 62 | height, |
| 63 | 0, |
| 64 | WindowClass::INPUT_OUTPUT, |
| 65 | screen.root_visual, |
| 66 | &CreateWindowAux::new() |
| 67 | .background_pixel(config.scheme_normal.background) |
| 68 | .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) |
| 69 | .override_redirect(1), |
| 70 | )?; |
| 71 | |
| 72 | connection.create_gc( |
| 73 | graphics_context, |
| 74 | window, |
| 75 | &CreateGCAux::new() |
| 76 | .foreground(config.scheme_normal.foreground) |
| 77 | .background(config.scheme_normal.background), |
| 78 | )?; |
| 79 | |
| 80 | connection.map_window(window)?; |
| 81 | connection.flush()?; |
| 82 | |
| 83 | let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; |
| 84 | let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; |
| 85 | |
| 86 | let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?; |
| 87 | |
| 88 | let horizontal_padding = (font.height() as f32 * 0.4) as u16; |
| 89 | |
| 90 | let tag_widths = config |
| 91 | .tags |
| 92 | .iter() |
| 93 | .map(|tag| { |
| 94 | let text_width = font.text_width(tag); |
| 95 | text_width + (horizontal_padding * 2) |
| 96 | }) |
| 97 | .collect(); |
| 98 | |
| 99 | let blocks: Vec<Box<dyn Block>> = config |
| 100 | .status_blocks |
| 101 | .iter() |
| 102 | .map(|block_config| block_config.to_block()) |
| 103 | .collect(); |
| 104 | |
| 105 | let block_underlines: Vec<bool> = config |
| 106 | .status_blocks |
| 107 | .iter() |
| 108 | .map(|block_config| block_config.underline) |
| 109 | .collect(); |
| 110 | |
| 111 | let block_last_updates = vec![Instant::now(); blocks.len()]; |
| 112 | |
| 113 | Ok(Bar { |
| 114 | window, |
| 115 | width, |
| 116 | height, |
| 117 | graphics_context, |
| 118 | font, |
| 119 | font_draw, |
| 120 | display, |
| 121 | tag_widths, |
| 122 | needs_redraw: true, |
| 123 | blocks, |
| 124 | block_last_updates, |
| 125 | block_underlines, |
| 126 | status_text: String::new(), |
| 127 | tags: config.tags.clone(), |
| 128 | scheme_normal: config.scheme_normal, |
| 129 | scheme_occupied: config.scheme_occupied, |
| 130 | scheme_selected: config.scheme_selected, |
| 131 | }) |
| 132 | } |
| 133 | |
| 134 | pub fn window(&self) -> Window { |
| 135 | self.window |
| 136 | } |
| 137 | |
| 138 | pub fn height(&self) -> u16 { |
| 139 | self.height |
| 140 | } |
| 141 | |
| 142 | pub fn invalidate(&mut self) { |
| 143 | self.needs_redraw = true; |
| 144 | } |
| 145 | |
| 146 | pub fn update_blocks(&mut self) -> Result<()> { |
| 147 | let now = Instant::now(); |
| 148 | let mut changed = false; |
| 149 | |
| 150 | for (i, block) in self.blocks.iter_mut().enumerate() { |
| 151 | let elapsed = now.duration_since(self.block_last_updates[i]); |
| 152 | |
| 153 | if elapsed >= block.interval() { |
| 154 | if block.content().is_ok() { |
| 155 | self.block_last_updates[i] = now; |
| 156 | changed = true; |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | if changed { |
| 162 | let mut parts = Vec::new(); |
| 163 | for block in &mut self.blocks { |
| 164 | if let Ok(text) = block.content() { |
| 165 | parts.push(text); |
| 166 | } |
| 167 | } |
| 168 | self.status_text = parts.join(""); |
| 169 | self.needs_redraw = true; |
| 170 | } |
| 171 | |
| 172 | Ok(()) |
| 173 | } |
| 174 | |
| 175 | pub fn draw( |
| 176 | &mut self, |
| 177 | connection: &RustConnection, |
| 178 | current_tags: u32, |
| 179 | occupied_tags: u32, |
| 180 | ) -> Result<()> { |
| 181 | if !self.needs_redraw { |
| 182 | return Ok(()); |
| 183 | } |
| 184 | |
| 185 | connection.change_gc( |
| 186 | self.graphics_context, |
| 187 | &ChangeGCAux::new().foreground(self.scheme_normal.background), |
| 188 | )?; |
| 189 | connection.poly_fill_rectangle( |
| 190 | self.window, |
| 191 | self.graphics_context, |
| 192 | &[Rectangle { |
| 193 | x: 0, |
| 194 | y: 0, |
| 195 | width: self.width, |
| 196 | height: self.height, |
| 197 | }], |
| 198 | )?; |
| 199 | |
| 200 | let mut x_position: i16 = 0; |
| 201 | |
| 202 | for (tag_index, tag) in self.tags.iter().enumerate() { |
| 203 | let tag_mask = 1 << tag_index; |
| 204 | let is_selected = (current_tags & tag_mask) != 0; |
| 205 | let is_occupied = (occupied_tags & tag_mask) != 0; |
| 206 | |
| 207 | let tag_width = self.tag_widths[tag_index]; |
| 208 | |
| 209 | let scheme = if is_selected { |
| 210 | &self.scheme_selected |
| 211 | } else if is_occupied { |
| 212 | &self.scheme_occupied |
| 213 | } else { |
| 214 | &self.scheme_normal |
| 215 | }; |
| 216 | |
| 217 | let text_width = self.font.text_width(tag); |
| 218 | let text_x = x_position + ((tag_width - text_width) / 2) as i16; |
| 219 | |
| 220 | let top_padding = 4; |
| 221 | let text_y = top_padding + self.font.ascent(); |
| 222 | |
| 223 | self.font_draw |
| 224 | .draw_text(&self.font, scheme.foreground, text_x, text_y, tag); |
| 225 | |
| 226 | if is_selected { |
| 227 | let font_height = self.font.height(); |
| 228 | let underline_height = font_height / 8; |
| 229 | let bottom_gap = 3; |
| 230 | let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; |
| 231 | |
| 232 | let underline_padding = 4; |
| 233 | let underline_width = tag_width - underline_padding; |
| 234 | let underline_x = x_position + (underline_padding / 2) as i16; |
| 235 | |
| 236 | connection.change_gc( |
| 237 | self.graphics_context, |
| 238 | &ChangeGCAux::new().foreground(scheme.underline), |
| 239 | )?; |
| 240 | connection.poly_fill_rectangle( |
| 241 | self.window, |
| 242 | self.graphics_context, |
| 243 | &[Rectangle { |
| 244 | x: underline_x, |
| 245 | y: underline_y, |
| 246 | width: underline_width, |
| 247 | height: underline_height, |
| 248 | }], |
| 249 | )?; |
| 250 | } |
| 251 | |
| 252 | x_position += tag_width as i16; |
| 253 | } |
| 254 | |
| 255 | if !self.status_text.is_empty() { |
| 256 | let padding = 10; |
| 257 | let mut x_position = self.width as i16 - padding; |
| 258 | |
| 259 | for (i, block) in self.blocks.iter_mut().enumerate().rev() { |
| 260 | if let Ok(text) = block.content() { |
| 261 | let text_width = self.font.text_width(&text); |
| 262 | x_position -= text_width as i16; |
| 263 | |
| 264 | let top_padding = 4; |
| 265 | let text_y = top_padding + self.font.ascent(); |
| 266 | |
| 267 | self.font_draw |
| 268 | .draw_text(&self.font, block.color(), x_position, text_y, &text); |
| 269 | |
| 270 | if self.block_underlines[i] { |
| 271 | let font_height = self.font.height(); |
| 272 | let underline_height = font_height / 8; |
| 273 | let bottom_gap = 3; |
| 274 | let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; |
| 275 | |
| 276 | let underline_padding = 8; |
| 277 | let underline_width = text_width + underline_padding; |
| 278 | let underline_x = x_position - (underline_padding / 2) as i16; |
| 279 | |
| 280 | connection.change_gc( |
| 281 | self.graphics_context, |
| 282 | &ChangeGCAux::new().foreground(block.color()), |
| 283 | )?; |
| 284 | |
| 285 | connection.poly_fill_rectangle( |
| 286 | self.window, |
| 287 | self.graphics_context, |
| 288 | &[Rectangle { |
| 289 | x: underline_x, |
| 290 | y: underline_y, |
| 291 | width: underline_width, |
| 292 | height: underline_height, |
| 293 | }], |
| 294 | )?; |
| 295 | } |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | connection.flush()?; |
| 300 | unsafe { |
| 301 | x11::xlib::XFlush(self.display); |
| 302 | } |
| 303 | self.needs_redraw = false; |
| 304 | |
| 305 | Ok(()) |
| 306 | } |
| 307 | |
| 308 | pub fn handle_click(&self, click_x: i16) -> Option<usize> { |
| 309 | let mut current_x_position = 0; |
| 310 | |
| 311 | for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() { |
| 312 | if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 { |
| 313 | return Some(tag_index); |
| 314 | } |
| 315 | current_x_position += tag_width as i16; |
| 316 | } |
| 317 | None |
| 318 | } |
| 319 | |
| 320 | pub fn needs_redraw(&self) -> bool { |
| 321 | self.needs_redraw |
| 322 | } |
| 323 | } |