tony / oxwm

attempting to call draw monitors once, not n times per monitor Browse files
tonybtw committed 2025-10-21 parent 3f51577 commit 7604855b43c3fc7ab180eff55260fd71ca672500
Showing 3 changed files with 28 additions and 22 deletions. UnifiedSplit
+13 −20 src/bar/bar.rs
@@ -14,9 +14,7 @@ pub struct Bar {
14 14 height: u16,
15 15 graphics_context: Gcontext,
16 16
17 - font: Font,
18 17 font_draw: FontDraw,
19 - display: *mut x11::xlib::Display,
20 18
21 19 tag_widths: Vec<u16>,
22 20 needs_redraw: bool,
@@ -38,6 +36,8 @@ impl Bar {
38 36 screen: &Screen,
39 37 screen_num: usize,
40 38 config: &Config,
39 + display: *mut x11::xlib::Display,
40 + font: &Font,
41 41 x: i16,
42 42 y: i16,
43 43 width: u16,
@@ -45,13 +45,6 @@ impl Bar {
45 45 let window = connection.generate_id()?;
46 46 let graphics_context = connection.generate_id()?;
47 47
48 - let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
49 - if display.is_null() {
50 - return Err(X11Error::DisplayOpenFailed.into());
51 - }
52 -
53 - let font = Font::new(display, screen_num as i32, &config.font)?;
54 -
55 48 let height = (font.height() as f32 * 1.4) as u16;
56 49
57 50 connection.create_window(
@@ -117,9 +110,7 @@ impl Bar {
117 110 width,
118 111 height,
119 112 graphics_context,
120 - font,
121 113 font_draw,
122 - display,
123 114 tag_widths,
124 115 needs_redraw: true,
125 116 blocks,
@@ -175,6 +166,8 @@ impl Bar {
175 166 pub fn draw(
176 167 &mut self,
177 168 connection: &RustConnection,
169 + font: &Font,
170 + display: *mut x11::xlib::Display,
178 171 current_tags: u32,
179 172 occupied_tags: u32,
180 173 draw_blocks: bool,
@@ -215,17 +208,17 @@ impl Bar {
215 208 &self.scheme_normal
216 209 };
217 210
218 - let text_width = self.font.text_width(tag);
211 + let text_width = font.text_width(tag);
219 212 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
220 213
221 214 let top_padding = 4;
222 - let text_y = top_padding + self.font.ascent();
215 + let text_y = top_padding + font.ascent();
223 216
224 217 self.font_draw
225 - .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
218 + .draw_text(font, scheme.foreground, text_x, text_y, tag);
226 219
227 220 if is_selected {
228 - let font_height = self.font.height();
221 + let font_height = font.height();
229 222 let underline_height = font_height / 8;
230 223 let bottom_gap = 3;
231 224 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -259,17 +252,17 @@ impl Bar {
259 252
260 253 for (i, block) in self.blocks.iter_mut().enumerate().rev() {
261 254 if let Ok(text) = block.content() {
262 - let text_width = self.font.text_width(&text);
255 + let text_width = font.text_width(&text);
263 256 x_position -= text_width as i16;
264 257
265 258 let top_padding = 4;
266 - let text_y = top_padding + self.font.ascent();
259 + let text_y = top_padding + font.ascent();
267 260
268 261 self.font_draw
269 - .draw_text(&self.font, block.color(), x_position, text_y, &text);
262 + .draw_text(font, block.color(), x_position, text_y, &text);
270 263
271 264 if self.block_underlines[i] {
272 - let font_height = self.font.height();
265 + let font_height = font.height();
273 266 let underline_height = font_height / 8;
274 267 let bottom_gap = 3;
275 268 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -299,7 +292,7 @@ impl Bar {
299 292 }
300 293 connection.flush()?;
301 294 unsafe {
302 - x11::xlib::XFlush(self.display);
295 + x11::xlib::XFlush(display);
303 296 }
304 297 self.needs_redraw = false;
305 298
+1 −1 src/bar/mod.rs
@@ -1,6 +1,6 @@
1 1 mod bar;
2 2 mod blocks;
3 -mod font;
3 +pub mod font;
4 4
5 5 pub use bar::Bar;
6 6 pub use blocks::{BlockCommand, BlockConfig};
+14 −1 src/window_manager.rs
@@ -68,6 +68,8 @@ pub struct WindowManager {
68 68 selected_monitor: usize,
69 69 atoms: AtomCache,
70 70 previous_focused: Option<Window>,
71 + display: *mut x11::xlib::Display,
72 + font: crate::bar::font::Font,
71 73 }
72 74
73 75 type WmResult<T> = Result<T, WmError>;
@@ -133,6 +135,13 @@ impl WindowManager {
133 135 monitors[0].selected_tags = selected_tags;
134 136 }
135 137
138 + let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
139 + if display.is_null() {
140 + return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed));
141 + }
142 +
143 + let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?;
144 +
136 145 let mut bars = Vec::new();
137 146 for monitor in &monitors {
138 147 let bar = Bar::new(
@@ -140,6 +149,8 @@ impl WindowManager {
140 149 &screen,
141 150 screen_number,
142 151 &config,
152 + display,
153 + &font,
143 154 monitor.x as i16,
144 155 monitor.y as i16,
145 156 monitor.width as u16,
@@ -169,6 +180,8 @@ impl WindowManager {
169 180 selected_monitor: 0,
170 181 atoms,
171 182 previous_focused: None,
183 + display,
184 + font,
172 185 };
173 186
174 187 window_manager.scan_existing_windows()?;
@@ -430,7 +443,7 @@ impl WindowManager {
430 443
431 444 let draw_blocks = monitor_index == self.selected_monitor;
432 445 bar.invalidate();
433 - bar.draw(&self.connection, monitor.selected_tags, occupied_tags, draw_blocks)?;
446 + bar.draw(&self.connection, &self.font, self.display, monitor.selected_tags, occupied_tags, draw_blocks)?;
434 447 }
435 448 }
436 449 Ok(())