| 1 | use crate::ColorScheme; |
| 2 | use crate::bar::font::{DrawingSurface, Font}; |
| 3 | use crate::errors::X11Error; |
| 4 | use crate::layout::tabbed::TAB_BAR_HEIGHT; |
| 5 | use x11rb::COPY_DEPTH_FROM_PARENT; |
| 6 | use x11rb::connection::Connection; |
| 7 | use x11rb::protocol::xproto::*; |
| 8 | use x11rb::rust_connection::RustConnection; |
| 9 | |
| 10 | pub struct TabBar { |
| 11 | window: Window, |
| 12 | width: u16, |
| 13 | height: u16, |
| 14 | x_offset: i16, |
| 15 | y_offset: i16, |
| 16 | graphics_context: Gcontext, |
| 17 | display: *mut x11::xlib::Display, |
| 18 | surface: DrawingSurface, |
| 19 | scheme_normal: ColorScheme, |
| 20 | scheme_selected: ColorScheme, |
| 21 | } |
| 22 | |
| 23 | impl TabBar { |
| 24 | pub fn new( |
| 25 | connection: &RustConnection, |
| 26 | screen: &Screen, |
| 27 | screen_num: usize, |
| 28 | display: *mut x11::xlib::Display, |
| 29 | _font: &Font, |
| 30 | x: i16, |
| 31 | y: i16, |
| 32 | width: u16, |
| 33 | scheme_normal: ColorScheme, |
| 34 | scheme_selected: ColorScheme, |
| 35 | cursor: u32, |
| 36 | ) -> Result<Self, X11Error> { |
| 37 | let window = connection.generate_id()?; |
| 38 | let graphics_context = connection.generate_id()?; |
| 39 | |
| 40 | let height = TAB_BAR_HEIGHT as u16; |
| 41 | |
| 42 | connection.create_window( |
| 43 | COPY_DEPTH_FROM_PARENT, |
| 44 | window, |
| 45 | screen.root, |
| 46 | x, |
| 47 | y, |
| 48 | width, |
| 49 | height, |
| 50 | 0, |
| 51 | WindowClass::INPUT_OUTPUT, |
| 52 | screen.root_visual, |
| 53 | &CreateWindowAux::new() |
| 54 | .background_pixel(scheme_normal.background) |
| 55 | .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS) |
| 56 | .override_redirect(1) |
| 57 | .cursor(cursor), |
| 58 | )?; |
| 59 | |
| 60 | connection.create_gc( |
| 61 | graphics_context, |
| 62 | window, |
| 63 | &CreateGCAux::new() |
| 64 | .foreground(scheme_normal.foreground) |
| 65 | .background(scheme_normal.background), |
| 66 | )?; |
| 67 | |
| 68 | connection.map_window(window)?; |
| 69 | connection.flush()?; |
| 70 | |
| 71 | let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) }; |
| 72 | let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) }; |
| 73 | |
| 74 | let surface = DrawingSurface::new( |
| 75 | display, |
| 76 | window as x11::xlib::Drawable, |
| 77 | width as u32, |
| 78 | height as u32, |
| 79 | visual, |
| 80 | colormap, |
| 81 | )?; |
| 82 | |
| 83 | Ok(Self { |
| 84 | window, |
| 85 | width, |
| 86 | height, |
| 87 | x_offset: x, |
| 88 | y_offset: y, |
| 89 | graphics_context, |
| 90 | display, |
| 91 | surface, |
| 92 | scheme_normal, |
| 93 | scheme_selected, |
| 94 | }) |
| 95 | } |
| 96 | |
| 97 | pub fn window(&self) -> Window { |
| 98 | self.window |
| 99 | } |
| 100 | |
| 101 | pub fn draw( |
| 102 | &mut self, |
| 103 | connection: &RustConnection, |
| 104 | font: &Font, |
| 105 | windows: &[(Window, String)], |
| 106 | focused_window: Option<Window>, |
| 107 | ) -> Result<(), X11Error> { |
| 108 | connection.change_gc( |
| 109 | self.graphics_context, |
| 110 | &ChangeGCAux::new().foreground(self.scheme_normal.background), |
| 111 | )?; |
| 112 | connection.flush()?; |
| 113 | |
| 114 | unsafe { |
| 115 | let gc = x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut()); |
| 116 | x11::xlib::XSetForeground(self.display, gc, self.scheme_normal.background as u64); |
| 117 | x11::xlib::XFillRectangle( |
| 118 | self.display, |
| 119 | self.surface.pixmap(), |
| 120 | gc, |
| 121 | 0, |
| 122 | 0, |
| 123 | self.width as u32, |
| 124 | self.height as u32, |
| 125 | ); |
| 126 | x11::xlib::XFreeGC(self.display, gc); |
| 127 | } |
| 128 | |
| 129 | if windows.is_empty() { |
| 130 | self.copy_pixmap_to_window(); |
| 131 | return Ok(()); |
| 132 | } |
| 133 | |
| 134 | let tab_width = self.width / windows.len() as u16; |
| 135 | let mut x_position: i16 = 0; |
| 136 | |
| 137 | for (index, &(window, ref title)) in windows.iter().enumerate() { |
| 138 | let is_focused = Some(window) == focused_window; |
| 139 | let scheme = if is_focused { |
| 140 | &self.scheme_selected |
| 141 | } else { |
| 142 | &self.scheme_normal |
| 143 | }; |
| 144 | |
| 145 | let display_title = if title.is_empty() { |
| 146 | format!("Window {}", index + 1) |
| 147 | } else { |
| 148 | title.clone() |
| 149 | }; |
| 150 | |
| 151 | let text_width = font.text_width(&display_title); |
| 152 | let text_x = x_position + ((tab_width.saturating_sub(text_width)) / 2) as i16; |
| 153 | |
| 154 | let top_padding = 6; |
| 155 | let text_y = top_padding + font.ascent(); |
| 156 | |
| 157 | self.surface |
| 158 | .font_draw() |
| 159 | .draw_text(font, scheme.foreground, text_x, text_y, &display_title); |
| 160 | |
| 161 | if is_focused { |
| 162 | let underline_height = 3; |
| 163 | let underline_y = self.height as i16 - underline_height; |
| 164 | |
| 165 | unsafe { |
| 166 | let gc = |
| 167 | x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut()); |
| 168 | x11::xlib::XSetForeground(self.display, gc, scheme.underline as u64); |
| 169 | x11::xlib::XFillRectangle( |
| 170 | self.display, |
| 171 | self.surface.pixmap(), |
| 172 | gc, |
| 173 | x_position as i32, |
| 174 | underline_y as i32, |
| 175 | tab_width as u32, |
| 176 | underline_height as u32, |
| 177 | ); |
| 178 | x11::xlib::XFreeGC(self.display, gc); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | x_position += tab_width as i16; |
| 183 | } |
| 184 | |
| 185 | self.copy_pixmap_to_window(); |
| 186 | Ok(()) |
| 187 | } |
| 188 | |
| 189 | fn copy_pixmap_to_window(&self) { |
| 190 | unsafe { |
| 191 | let gc = |
| 192 | x11::xlib::XCreateGC(self.display, self.window as u64, 0, std::ptr::null_mut()); |
| 193 | x11::xlib::XCopyArea( |
| 194 | self.display, |
| 195 | self.surface.pixmap(), |
| 196 | self.window as u64, |
| 197 | gc, |
| 198 | 0, |
| 199 | 0, |
| 200 | self.width as u32, |
| 201 | self.height as u32, |
| 202 | 0, |
| 203 | 0, |
| 204 | ); |
| 205 | x11::xlib::XFreeGC(self.display, gc); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | pub fn get_clicked_window(&self, windows: &[(Window, String)], click_x: i16) -> Option<Window> { |
| 210 | if windows.is_empty() { |
| 211 | return None; |
| 212 | } |
| 213 | |
| 214 | let tab_width = self.width / windows.len() as u16; |
| 215 | let tab_index = (click_x as u16 / tab_width) as usize; |
| 216 | |
| 217 | windows.get(tab_index).map(|&(win, _)| win) |
| 218 | } |
| 219 | |
| 220 | pub fn reposition( |
| 221 | &mut self, |
| 222 | connection: &RustConnection, |
| 223 | x: i16, |
| 224 | y: i16, |
| 225 | width: u16, |
| 226 | ) -> Result<(), X11Error> { |
| 227 | self.x_offset = x; |
| 228 | self.y_offset = y; |
| 229 | self.width = width; |
| 230 | |
| 231 | connection.configure_window( |
| 232 | self.window, |
| 233 | &ConfigureWindowAux::new() |
| 234 | .x(x as i32) |
| 235 | .y(y as i32) |
| 236 | .width(width as u32), |
| 237 | )?; |
| 238 | |
| 239 | let visual = unsafe { x11::xlib::XDefaultVisual(self.display, 0) }; |
| 240 | let colormap = unsafe { x11::xlib::XDefaultColormap(self.display, 0) }; |
| 241 | |
| 242 | self.surface = DrawingSurface::new( |
| 243 | self.display, |
| 244 | self.window as x11::xlib::Drawable, |
| 245 | width as u32, |
| 246 | self.height as u32, |
| 247 | visual, |
| 248 | colormap, |
| 249 | )?; |
| 250 | |
| 251 | connection.flush()?; |
| 252 | Ok(()) |
| 253 | } |
| 254 | |
| 255 | pub fn hide(&self, connection: &RustConnection) -> Result<(), X11Error> { |
| 256 | connection.unmap_window(self.window)?; |
| 257 | connection.flush()?; |
| 258 | Ok(()) |
| 259 | } |
| 260 | |
| 261 | pub fn show(&self, connection: &RustConnection) -> Result<(), X11Error> { |
| 262 | connection.map_window(self.window)?; |
| 263 | connection.flush()?; |
| 264 | Ok(()) |
| 265 | } |
| 266 | } |