tony / oxwm

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