| 1 | use super::BAR_HEIGHT; |
| 2 | use crate::config::{SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, TAGS}; |
| 3 | use anyhow::Result; |
| 4 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 5 | use x11rb::connection::Connection; |
| 6 | use x11rb::protocol::xproto::*; |
| 7 | |
| 8 | pub struct Bar { |
| 9 | window: Window, |
| 10 | width: u16, |
| 11 | height: u16, |
| 12 | graphics_context: Gcontext, |
| 13 | |
| 14 | tag_widths: Vec<u16>, |
| 15 | needs_redraw: bool, |
| 16 | } |
| 17 | |
| 18 | impl Bar { |
| 19 | pub fn new<C: Connection>(connection: &C, screen: &Screen) -> Result<Self> { |
| 20 | let window = connection.generate_id()?; |
| 21 | let graphics_context = connection.generate_id()?; |
| 22 | |
| 23 | let width = screen.width_in_pixels; |
| 24 | let height = BAR_HEIGHT; |
| 25 | |
| 26 | connection.create_window( |
| 27 | COPY_DEPTH_FROM_PARENT, |
| 28 | window, |
| 29 | screen.root, |
| 30 | 0, |
| 31 | 0, |
| 32 | width, |
| 33 | height, |
| 34 | 0, |
| 35 | WindowClass::INPUT_OUTPUT, |
| 36 | screen.root_visual, |
| 37 | &CreateWindowAux::new() |
| 38 | .background_pixel(SCHEME_NORMAL.background) |
| 39 | .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) |
| 40 | .override_redirect(1), |
| 41 | )?; |
| 42 | |
| 43 | connection.create_gc( |
| 44 | graphics_context, |
| 45 | window, |
| 46 | &CreateGCAux::new() |
| 47 | .foreground(SCHEME_NORMAL.foreground) |
| 48 | .background(SCHEME_NORMAL.background), |
| 49 | )?; |
| 50 | |
| 51 | connection.map_window(window)?; |
| 52 | connection.flush()?; |
| 53 | |
| 54 | // TODO: actual text width calculation when we add fonts |
| 55 | let tag_widths = TAGS.iter().map(|tag| (tag.len() as u16 * 8) + 10).collect(); |
| 56 | |
| 57 | Ok(Bar { |
| 58 | window, |
| 59 | width, |
| 60 | height, |
| 61 | graphics_context, |
| 62 | tag_widths, |
| 63 | needs_redraw: true, |
| 64 | }) |
| 65 | } |
| 66 | |
| 67 | pub fn window(&self) -> Window { |
| 68 | self.window |
| 69 | } |
| 70 | |
| 71 | pub fn height(&self) -> u16 { |
| 72 | self.height |
| 73 | } |
| 74 | |
| 75 | pub fn invalidate(&mut self) { |
| 76 | self.needs_redraw = true; |
| 77 | } |
| 78 | |
| 79 | pub fn draw<C: Connection>( |
| 80 | &mut self, |
| 81 | connection: &C, |
| 82 | current_tags: u32, |
| 83 | occupied_tags: u32, |
| 84 | ) -> Result<()> { |
| 85 | if !self.needs_redraw { |
| 86 | return Ok(()); |
| 87 | } |
| 88 | |
| 89 | connection.change_gc( |
| 90 | self.graphics_context, |
| 91 | &ChangeGCAux::new().foreground(SCHEME_NORMAL.background), |
| 92 | )?; |
| 93 | connection.poly_fill_rectangle( |
| 94 | self.window, |
| 95 | self.graphics_context, |
| 96 | &[Rectangle { |
| 97 | x: 0, |
| 98 | y: 0, |
| 99 | width: self.width, |
| 100 | height: self.height, |
| 101 | }], |
| 102 | )?; |
| 103 | |
| 104 | let mut x_position: i16 = 0; |
| 105 | |
| 106 | for (tag_index, tag) in TAGS.iter().enumerate() { |
| 107 | let tag_mask = 1 << tag_index; |
| 108 | let is_selected = (current_tags & tag_mask) != 0; |
| 109 | let is_occupied = (occupied_tags & tag_mask) != 0; |
| 110 | |
| 111 | let tag_width = self.tag_widths[tag_index]; |
| 112 | |
| 113 | let scheme = if is_selected { |
| 114 | &SCHEME_SELECTED |
| 115 | } else if is_occupied { |
| 116 | &SCHEME_OCCUPIED |
| 117 | } else { |
| 118 | &SCHEME_NORMAL |
| 119 | }; |
| 120 | |
| 121 | if is_selected { |
| 122 | connection.change_gc( |
| 123 | self.graphics_context, |
| 124 | &ChangeGCAux::new().foreground(scheme.background), |
| 125 | )?; |
| 126 | connection.poly_fill_rectangle( |
| 127 | self.window, |
| 128 | self.graphics_context, |
| 129 | &[Rectangle { |
| 130 | x: x_position, |
| 131 | y: 0, |
| 132 | width: tag_width, |
| 133 | height: self.height, |
| 134 | }], |
| 135 | )?; |
| 136 | } else if is_occupied { |
| 137 | connection.change_gc( |
| 138 | self.graphics_context, |
| 139 | &ChangeGCAux::new().foreground(scheme.border), |
| 140 | )?; |
| 141 | connection.poly_fill_rectangle( |
| 142 | self.window, |
| 143 | self.graphics_context, |
| 144 | &[Rectangle { |
| 145 | x: x_position, |
| 146 | y: 0, |
| 147 | width: tag_width, |
| 148 | height: 2, |
| 149 | }], |
| 150 | )?; |
| 151 | } |
| 152 | |
| 153 | connection.change_gc( |
| 154 | self.graphics_context, |
| 155 | &ChangeGCAux::new().foreground(scheme.foreground), |
| 156 | )?; |
| 157 | |
| 158 | // TODO: Replace with actual font rendering later |
| 159 | connection.image_text8( |
| 160 | self.window, |
| 161 | self.graphics_context, |
| 162 | x_position + 5, |
| 163 | self.height as i16 - 5, |
| 164 | tag.as_bytes(), |
| 165 | )?; |
| 166 | |
| 167 | x_position += tag_width as i16; |
| 168 | } |
| 169 | |
| 170 | connection.flush()?; |
| 171 | self.needs_redraw = false; |
| 172 | |
| 173 | Ok(()) |
| 174 | } |
| 175 | |
| 176 | pub fn handle_click(&self, click_x: i16) -> Option<usize> { |
| 177 | let mut current_x_position = 0; |
| 178 | |
| 179 | for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() { |
| 180 | if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 { |
| 181 | return Some(tag_index); |
| 182 | } |
| 183 | current_x_position += tag_width as i16; |
| 184 | } |
| 185 | None |
| 186 | } |
| 187 | } |