tony / oxwm

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