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