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 height: u16, 14 height: u16,
15 graphics_context: Gcontext, 15 graphics_context: Gcontext,
16 16
17 - font: Font,
18 font_draw: FontDraw, 17 font_draw: FontDraw,
19 - display: *mut x11::xlib::Display,
20 18
21 tag_widths: Vec<u16>, 19 tag_widths: Vec<u16>,
22 needs_redraw: bool, 20 needs_redraw: bool,
@@ -38,6 +36,8 @@ impl Bar {
38 screen: &Screen, 36 screen: &Screen,
39 screen_num: usize, 37 screen_num: usize,
40 config: &Config, 38 config: &Config,
39 + display: *mut x11::xlib::Display,
40 + font: &Font,
41 x: i16, 41 x: i16,
42 y: i16, 42 y: i16,
43 width: u16, 43 width: u16,
@@ -45,13 +45,6 @@ impl Bar {
45 let window = connection.generate_id()?; 45 let window = connection.generate_id()?;
46 let graphics_context = connection.generate_id()?; 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 let height = (font.height() as f32 * 1.4) as u16; 48 let height = (font.height() as f32 * 1.4) as u16;
56 49
57 connection.create_window( 50 connection.create_window(
@@ -117,9 +110,7 @@ impl Bar {
117 width, 110 width,
118 height, 111 height,
119 graphics_context, 112 graphics_context,
120 - font,
121 font_draw, 113 font_draw,
122 - display,
123 tag_widths, 114 tag_widths,
124 needs_redraw: true, 115 needs_redraw: true,
125 blocks, 116 blocks,
@@ -175,6 +166,8 @@ impl Bar {
175 pub fn draw( 166 pub fn draw(
176 &mut self, 167 &mut self,
177 connection: &RustConnection, 168 connection: &RustConnection,
169 + font: &Font,
170 + display: *mut x11::xlib::Display,
178 current_tags: u32, 171 current_tags: u32,
179 occupied_tags: u32, 172 occupied_tags: u32,
180 draw_blocks: bool, 173 draw_blocks: bool,
@@ -215,17 +208,17 @@ impl Bar {
215 &self.scheme_normal 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 let text_x = x_position + ((tag_width - text_width) / 2) as i16; 212 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
220 213
221 let top_padding = 4; 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 self.font_draw 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 if is_selected { 220 if is_selected {
228 - let font_height = self.font.height(); 221 + let font_height = font.height();
229 let underline_height = font_height / 8; 222 let underline_height = font_height / 8;
230 let bottom_gap = 3; 223 let bottom_gap = 3;
231 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; 224 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -259,17 +252,17 @@ impl Bar {
259 252
260 for (i, block) in self.blocks.iter_mut().enumerate().rev() { 253 for (i, block) in self.blocks.iter_mut().enumerate().rev() {
261 if let Ok(text) = block.content() { 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 x_position -= text_width as i16; 256 x_position -= text_width as i16;
264 257
265 let top_padding = 4; 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 self.font_draw 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 if self.block_underlines[i] { 264 if self.block_underlines[i] {
272 - let font_height = self.font.height(); 265 + let font_height = font.height();
273 let underline_height = font_height / 8; 266 let underline_height = font_height / 8;
274 let bottom_gap = 3; 267 let bottom_gap = 3;
275 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap; 268 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -299,7 +292,7 @@ impl Bar {
299 } 292 }
300 connection.flush()?; 293 connection.flush()?;
301 unsafe { 294 unsafe {
302 - x11::xlib::XFlush(self.display); 295 + x11::xlib::XFlush(display);
303 } 296 }
304 self.needs_redraw = false; 297 self.needs_redraw = false;
305 298
+1 −1 src/bar/mod.rs
@@ -1,6 +1,6 @@
1 mod bar; 1 mod bar;
2 mod blocks; 2 mod blocks;
3 -mod font; 3 +pub mod font;
4 4
5 pub use bar::Bar; 5 pub use bar::Bar;
6 pub use blocks::{BlockCommand, BlockConfig}; 6 pub use blocks::{BlockCommand, BlockConfig};
+14 −1 src/window_manager.rs
@@ -68,6 +68,8 @@ pub struct WindowManager {
68 selected_monitor: usize, 68 selected_monitor: usize,
69 atoms: AtomCache, 69 atoms: AtomCache,
70 previous_focused: Option<Window>, 70 previous_focused: Option<Window>,
71 + display: *mut x11::xlib::Display,
72 + font: crate::bar::font::Font,
71 } 73 }
72 74
73 type WmResult<T> = Result<T, WmError>; 75 type WmResult<T> = Result<T, WmError>;
@@ -133,6 +135,13 @@ impl WindowManager {
133 monitors[0].selected_tags = selected_tags; 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 let mut bars = Vec::new(); 145 let mut bars = Vec::new();
137 for monitor in &monitors { 146 for monitor in &monitors {
138 let bar = Bar::new( 147 let bar = Bar::new(
@@ -140,6 +149,8 @@ impl WindowManager {
140 &screen, 149 &screen,
141 screen_number, 150 screen_number,
142 &config, 151 &config,
152 + display,
153 + &font,
143 monitor.x as i16, 154 monitor.x as i16,
144 monitor.y as i16, 155 monitor.y as i16,
145 monitor.width as u16, 156 monitor.width as u16,
@@ -169,6 +180,8 @@ impl WindowManager {
169 selected_monitor: 0, 180 selected_monitor: 0,
170 atoms, 181 atoms,
171 previous_focused: None, 182 previous_focused: None,
183 + display,
184 + font,
172 }; 185 };
173 186
174 window_manager.scan_existing_windows()?; 187 window_manager.scan_existing_windows()?;
@@ -430,7 +443,7 @@ impl WindowManager {
430 443
431 let draw_blocks = monitor_index == self.selected_monitor; 444 let draw_blocks = monitor_index == self.selected_monitor;
432 bar.invalidate(); 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 Ok(()) 449 Ok(())