| 1 | use super::blocks::Block; |
| 2 | use super::font::{Font, FontDraw}; |
| 3 | use crate::Config; |
| 4 | use crate::errors::X11Error; |
| 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 | pixmap: x11::xlib::Pixmap, |
| 17 | display: *mut x11::xlib::Display, |
| 18 | |
| 19 | font_draw: FontDraw, |
| 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 | scheme_urgent: crate::ColorScheme, |
| 34 | } |
| 35 | |
| 36 | impl Bar { |
| 37 | pub fn new( |
| 38 | connection: &RustConnection, |
| 39 | screen: &Screen, |
| 40 | screen_num: usize, |
| 41 | config: &Config, |
| 42 | display: *mut x11::xlib::Display, |
| 43 | font: &Font, |
| 44 | x: i16, |
| 45 | y: i16, |
| 46 | width: u16, |
| 47 | ) -> Result<Self, X11Error> { |
| 48 | let window = connection.generate_id()?; |
| 49 | let graphics_context = connection.generate_id()?; |
| 50 | |
| 51 | let height = (font.height() as f32 * 1.4) as u16; |
| 52 | |
| 53 | connection.create_window( |
| 54 | COPY_DEPTH_FROM_PARENT, |
| 55 | window, |
| 56 | screen.root, |
| 57 | x, |
| 58 | y, |
| 59 | width, |
| 60 | height, |
| 61 | 0, |
| 62 | WindowClass::INPUT_OUTPUT, |
| 63 | screen.root_visual, |
| 64 | &CreateWindowAux::new() |
| 65 | .background_pixel(config.scheme_normal.background) |
| 66 | .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) |
| 67 | .override_redirect(1), |
| 68 | )?; |
| 69 | |
| 70 | connection.create_gc( |
| 71 | graphics_context, |
| 72 | window, |
| 73 | &CreateGCAux::new() |
| 74 | .foreground(config.scheme_normal.foreground) |
| 75 | .background(config.scheme_normal.background), |
| 76 | )?; |
| 77 | |
| 78 | connection.map_window(window)?; |
| 79 | connection.flush()?; |
| 80 | |
| 81 | let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; |
| 82 | let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; |
| 83 | let depth = unsafe { x11::xlib::XDefaultDepth(display, screen_num as i32) }; |
| 84 | |
| 85 | let pixmap = unsafe { |
| 86 | x11::xlib::XCreatePixmap( |
| 87 | display, |
| 88 | window as x11::xlib::Drawable, |
| 89 | width as u32, |
| 90 | height as u32, |
| 91 | depth as u32, |
| 92 | ) |
| 93 | }; |
| 94 | |
| 95 | let font_draw = FontDraw::new(display, pixmap, visual, colormap)?; |
| 96 | |
| 97 | let horizontal_padding = (font.height() as f32 * 0.4) as u16; |
| 98 | |
| 99 | let tag_widths = config |
| 100 | .tags |
| 101 | .iter() |
| 102 | .map(|tag| { |
| 103 | let text_width = font.text_width(tag); |
| 104 | text_width + (horizontal_padding * 2) |
| 105 | }) |
| 106 | .collect(); |
| 107 | |
| 108 | let blocks: Vec<Box<dyn Block>> = config |
| 109 | .status_blocks |
| 110 | .iter() |
| 111 | .map(|block_config| block_config.to_block()) |
| 112 | .collect(); |
| 113 | |
| 114 | let block_underlines: Vec<bool> = config |
| 115 | .status_blocks |
| 116 | .iter() |
| 117 | .map(|block_config| block_config.underline) |
| 118 | .collect(); |
| 119 | |
| 120 | let block_last_updates = vec![Instant::now(); blocks.len()]; |
| 121 | |
| 122 | Ok(Bar { |
| 123 | window, |
| 124 | width, |
| 125 | height, |
| 126 | graphics_context, |
| 127 | pixmap, |
| 128 | display, |
| 129 | font_draw, |
| 130 | tag_widths, |
| 131 | needs_redraw: true, |
| 132 | blocks, |
| 133 | block_last_updates, |
| 134 | block_underlines, |
| 135 | status_text: String::new(), |
| 136 | tags: config.tags.clone(), |
| 137 | scheme_normal: config.scheme_normal, |
| 138 | scheme_occupied: config.scheme_occupied, |
| 139 | scheme_selected: config.scheme_selected, |
| 140 | scheme_urgent: config.scheme_urgent, |
| 141 | }) |
| 142 | } |
| 143 | |
| 144 | pub fn window(&self) -> Window { |
| 145 | self.window |
| 146 | } |
| 147 | |
| 148 | pub fn height(&self) -> u16 { |
| 149 | self.height |
| 150 | } |
| 151 | |
| 152 | pub fn invalidate(&mut self) { |
| 153 | self.needs_redraw = true; |
| 154 | } |
| 155 | |
| 156 | pub fn update_blocks(&mut self) { |
| 157 | let now = Instant::now(); |
| 158 | let mut changed = false; |
| 159 | |
| 160 | for (i, block) in self.blocks.iter_mut().enumerate() { |
| 161 | let elapsed = now.duration_since(self.block_last_updates[i]); |
| 162 | |
| 163 | if elapsed >= block.interval() && block.content().is_ok() { |
| 164 | self.block_last_updates[i] = now; |
| 165 | changed = true; |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | if changed { |
| 170 | let mut parts = Vec::new(); |
| 171 | for block in &mut self.blocks { |
| 172 | if let Ok(text) = block.content() { |
| 173 | parts.push(text); |
| 174 | } |
| 175 | } |
| 176 | self.status_text = parts.join(""); |
| 177 | self.needs_redraw = true; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | pub fn draw( |
| 182 | &mut self, |
| 183 | connection: &RustConnection, |
| 184 | font: &Font, |
| 185 | display: *mut x11::xlib::Display, |
| 186 | current_tags: u32, |
| 187 | occupied_tags: u32, |
| 188 | urgent_tags: u32, |
| 189 | draw_blocks: bool, |
| 190 | layout_symbol: &str, |
| 191 | keychord_indicator: Option<&str>, |
| 192 | ) -> Result<(), X11Error> { |
| 193 | if !self.needs_redraw { |
| 194 | return Ok(()); |
| 195 | } |
| 196 | |
| 197 | connection.change_gc( |
| 198 | self.graphics_context, |
| 199 | &ChangeGCAux::new().foreground(self.scheme_normal.background), |
| 200 | )?; |
| 201 | connection.flush()?; |
| 202 | |
| 203 | unsafe { |
| 204 | let gc = x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); |
| 205 | x11::xlib::XSetForeground(display, gc, self.scheme_normal.background as u64); |
| 206 | x11::xlib::XFillRectangle( |
| 207 | display, |
| 208 | self.pixmap, |
| 209 | gc, |
| 210 | 0, |
| 211 | 0, |
| 212 | self.width as u32, |
| 213 | self.height as u32, |
| 214 | ); |
| 215 | x11::xlib::XFreeGC(display, gc); |
| 216 | } |
| 217 | |
| 218 | let mut x_position: i16 = 0; |
| 219 | |
| 220 | for (tag_index, tag) in self.tags.iter().enumerate() { |
| 221 | let tag_mask = 1 << tag_index; |
| 222 | let is_selected = (current_tags & tag_mask) != 0; |
| 223 | let is_occupied = (occupied_tags & tag_mask) != 0; |
| 224 | let is_urgent = (urgent_tags & tag_mask) != 0; |
| 225 | |
| 226 | let tag_width = self.tag_widths[tag_index]; |
| 227 | |
| 228 | let scheme = if is_selected { |
| 229 | &self.scheme_selected |
| 230 | } else if is_urgent { |
| 231 | &self.scheme_urgent |
| 232 | } else if is_occupied { |
| 233 | &self.scheme_occupied |
| 234 | } else { |
| 235 | &self.scheme_normal |
| 236 | }; |
| 237 | |
| 238 | let text_width = font.text_width(tag); |
| 239 | let text_x = x_position + ((tag_width - text_width) / 2) as i16; |
| 240 | |
| 241 | let top_padding = 4; |
| 242 | let text_y = top_padding + font.ascent(); |
| 243 | |
| 244 | self.font_draw |
| 245 | .draw_text(font, scheme.foreground, text_x, text_y, tag); |
| 246 | |
| 247 | if is_selected || is_urgent { |
| 248 | let font_height = font.height(); |
| 249 | let underline_height = font_height / 8; |
| 250 | let bottom_gap = 3; |
| 251 | let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; |
| 252 | |
| 253 | let underline_padding = 4; |
| 254 | let underline_width = tag_width - underline_padding; |
| 255 | let underline_x = x_position + (underline_padding / 2) as i16; |
| 256 | |
| 257 | unsafe { |
| 258 | let gc = x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); |
| 259 | x11::xlib::XSetForeground(display, gc, scheme.underline as u64); |
| 260 | x11::xlib::XFillRectangle( |
| 261 | display, |
| 262 | self.pixmap, |
| 263 | gc, |
| 264 | underline_x as i32, |
| 265 | underline_y as i32, |
| 266 | underline_width as u32, |
| 267 | underline_height as u32, |
| 268 | ); |
| 269 | x11::xlib::XFreeGC(display, gc); |
| 270 | } |
| 271 | } |
| 272 | |
| 273 | x_position += tag_width as i16; |
| 274 | } |
| 275 | |
| 276 | x_position += 10; |
| 277 | |
| 278 | let text_x = x_position; |
| 279 | let top_padding = 4; |
| 280 | let text_y = top_padding + font.ascent(); |
| 281 | |
| 282 | self.font_draw.draw_text( |
| 283 | font, |
| 284 | self.scheme_normal.foreground, |
| 285 | text_x, |
| 286 | text_y, |
| 287 | layout_symbol, |
| 288 | ); |
| 289 | |
| 290 | x_position += font.text_width(layout_symbol) as i16; |
| 291 | |
| 292 | if let Some(indicator) = keychord_indicator { |
| 293 | x_position += 10; |
| 294 | |
| 295 | let text_x = x_position; |
| 296 | let text_y = top_padding + font.ascent(); |
| 297 | |
| 298 | self.font_draw.draw_text( |
| 299 | font, |
| 300 | self.scheme_selected.foreground, |
| 301 | text_x, |
| 302 | text_y, |
| 303 | indicator, |
| 304 | ); |
| 305 | } |
| 306 | |
| 307 | if draw_blocks && !self.status_text.is_empty() { |
| 308 | let padding = 10; |
| 309 | let mut x_position = self.width as i16 - padding; |
| 310 | |
| 311 | for (i, block) in self.blocks.iter_mut().enumerate().rev() { |
| 312 | if let Ok(text) = block.content() { |
| 313 | let text_width = font.text_width(&text); |
| 314 | x_position -= text_width as i16; |
| 315 | |
| 316 | let top_padding = 4; |
| 317 | let text_y = top_padding + font.ascent(); |
| 318 | |
| 319 | self.font_draw |
| 320 | .draw_text(font, block.color(), x_position, text_y, &text); |
| 321 | |
| 322 | if self.block_underlines[i] { |
| 323 | let font_height = font.height(); |
| 324 | let underline_height = font_height / 8; |
| 325 | let bottom_gap = 3; |
| 326 | let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; |
| 327 | |
| 328 | let underline_padding = 8; |
| 329 | let underline_width = text_width + underline_padding; |
| 330 | let underline_x = x_position - (underline_padding / 2) as i16; |
| 331 | |
| 332 | unsafe { |
| 333 | let gc = |
| 334 | x11::xlib::XCreateGC(display, self.pixmap, 0, std::ptr::null_mut()); |
| 335 | x11::xlib::XSetForeground(display, gc, block.color() as u64); |
| 336 | x11::xlib::XFillRectangle( |
| 337 | display, |
| 338 | self.pixmap, |
| 339 | gc, |
| 340 | underline_x as i32, |
| 341 | underline_y as i32, |
| 342 | underline_width as u32, |
| 343 | underline_height as u32, |
| 344 | ); |
| 345 | x11::xlib::XFreeGC(display, gc); |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | unsafe { |
| 353 | let gc = x11::xlib::XCreateGC( |
| 354 | display, |
| 355 | self.window as x11::xlib::Drawable, |
| 356 | 0, |
| 357 | std::ptr::null_mut(), |
| 358 | ); |
| 359 | x11::xlib::XCopyArea( |
| 360 | display, |
| 361 | self.pixmap, |
| 362 | self.window as x11::xlib::Drawable, |
| 363 | gc, |
| 364 | 0, |
| 365 | 0, |
| 366 | self.width as u32, |
| 367 | self.height as u32, |
| 368 | 0, |
| 369 | 0, |
| 370 | ); |
| 371 | x11::xlib::XFreeGC(display, gc); |
| 372 | x11::xlib::XSync(display, 0); |
| 373 | } |
| 374 | |
| 375 | self.needs_redraw = false; |
| 376 | |
| 377 | Ok(()) |
| 378 | } |
| 379 | |
| 380 | pub fn handle_click(&self, click_x: i16) -> Option<usize> { |
| 381 | let mut current_x_position = 0; |
| 382 | |
| 383 | for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() { |
| 384 | if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 { |
| 385 | return Some(tag_index); |
| 386 | } |
| 387 | current_x_position += tag_width as i16; |
| 388 | } |
| 389 | None |
| 390 | } |
| 391 | |
| 392 | pub fn needs_redraw(&self) -> bool { |
| 393 | self.needs_redraw |
| 394 | } |
| 395 | |
| 396 | pub fn update_from_config(&mut self, config: &Config) { |
| 397 | self.blocks = config |
| 398 | .status_blocks |
| 399 | .iter() |
| 400 | .map(|block_config| block_config.to_block()) |
| 401 | .collect(); |
| 402 | |
| 403 | self.block_underlines = config |
| 404 | .status_blocks |
| 405 | .iter() |
| 406 | .map(|block_config| block_config.underline) |
| 407 | .collect(); |
| 408 | |
| 409 | self.block_last_updates = vec![Instant::now(); self.blocks.len()]; |
| 410 | |
| 411 | self.tags = config.tags.clone(); |
| 412 | self.scheme_normal = config.scheme_normal; |
| 413 | self.scheme_occupied = config.scheme_occupied; |
| 414 | self.scheme_selected = config.scheme_selected; |
| 415 | self.scheme_urgent = config.scheme_urgent; |
| 416 | |
| 417 | self.status_text.clear(); |
| 418 | self.needs_redraw = true; |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | impl Drop for Bar { |
| 423 | fn drop(&mut self) { |
| 424 | unsafe { |
| 425 | x11::xlib::XFreePixmap(self.display, self.pixmap); |
| 426 | } |
| 427 | } |
| 428 | } |