oxwm

https://git.tonybtw.com/oxwm.git git://git.tonybtw.com/oxwm.git

attempting to call draw monitors once, not n times per monitor

Commit
7604855b43c3fc7ab180eff55260fd71ca672500
Parent
3f51577
Author
tonybtw <tonybtw@tonybtw.com>
Date
2025-10-21 06:16:37

Diff

diff --git a/src/bar/bar.rs b/src/bar/bar.rs
index b071c69..9857406 100644
--- a/src/bar/bar.rs
+++ b/src/bar/bar.rs
@@ -14,9 +14,7 @@ pub struct Bar {
     height: u16,
     graphics_context: Gcontext,
 
-    font: Font,
     font_draw: FontDraw,
-    display: *mut x11::xlib::Display,
 
     tag_widths: Vec<u16>,
     needs_redraw: bool,
@@ -38,6 +36,8 @@ impl Bar {
         screen: &Screen,
         screen_num: usize,
         config: &Config,
+        display: *mut x11::xlib::Display,
+        font: &Font,
         x: i16,
         y: i16,
         width: u16,
@@ -45,13 +45,6 @@ impl Bar {
         let window = connection.generate_id()?;
         let graphics_context = connection.generate_id()?;
 
-        let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
-        if display.is_null() {
-            return Err(X11Error::DisplayOpenFailed.into());
-        }
-
-        let font = Font::new(display, screen_num as i32, &config.font)?;
-
         let height = (font.height() as f32 * 1.4) as u16;
 
         connection.create_window(
@@ -117,9 +110,7 @@ impl Bar {
             width,
             height,
             graphics_context,
-            font,
             font_draw,
-            display,
             tag_widths,
             needs_redraw: true,
             blocks,
@@ -175,6 +166,8 @@ impl Bar {
     pub fn draw(
         &mut self,
         connection: &RustConnection,
+        font: &Font,
+        display: *mut x11::xlib::Display,
         current_tags: u32,
         occupied_tags: u32,
         draw_blocks: bool,
@@ -215,17 +208,17 @@ impl Bar {
                 &self.scheme_normal
             };
 
-            let text_width = self.font.text_width(tag);
+            let text_width = font.text_width(tag);
             let text_x = x_position + ((tag_width - text_width) / 2) as i16;
 
             let top_padding = 4;
-            let text_y = top_padding + self.font.ascent();
+            let text_y = top_padding + font.ascent();
 
             self.font_draw
-                .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
+                .draw_text(font, scheme.foreground, text_x, text_y, tag);
 
             if is_selected {
-                let font_height = self.font.height();
+                let font_height = font.height();
                 let underline_height = font_height / 8;
                 let bottom_gap = 3;
                 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -259,17 +252,17 @@ impl Bar {
 
             for (i, block) in self.blocks.iter_mut().enumerate().rev() {
                 if let Ok(text) = block.content() {
-                    let text_width = self.font.text_width(&text);
+                    let text_width = font.text_width(&text);
                     x_position -= text_width as i16;
 
                     let top_padding = 4;
-                    let text_y = top_padding + self.font.ascent();
+                    let text_y = top_padding + font.ascent();
 
                     self.font_draw
-                        .draw_text(&self.font, block.color(), x_position, text_y, &text);
+                        .draw_text(font, block.color(), x_position, text_y, &text);
 
                     if self.block_underlines[i] {
-                        let font_height = self.font.height();
+                        let font_height = font.height();
                         let underline_height = font_height / 8;
                         let bottom_gap = 3;
                         let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
@@ -299,7 +292,7 @@ impl Bar {
         }
         connection.flush()?;
         unsafe {
-            x11::xlib::XFlush(self.display);
+            x11::xlib::XFlush(display);
         }
         self.needs_redraw = false;
 
diff --git a/src/bar/mod.rs b/src/bar/mod.rs
index 3009746..a60db74 100644
--- a/src/bar/mod.rs
+++ b/src/bar/mod.rs
@@ -1,6 +1,6 @@
 mod bar;
 mod blocks;
-mod font;
+pub mod font;
 
 pub use bar::Bar;
 pub use blocks::{BlockCommand, BlockConfig};
diff --git a/src/window_manager.rs b/src/window_manager.rs
index 44fa5de..e7ada2a 100644
--- a/src/window_manager.rs
+++ b/src/window_manager.rs
@@ -68,6 +68,8 @@ pub struct WindowManager {
     selected_monitor: usize,
     atoms: AtomCache,
     previous_focused: Option<Window>,
+    display: *mut x11::xlib::Display,
+    font: crate::bar::font::Font,
 }
 
 type WmResult<T> = Result<T, WmError>;
@@ -133,6 +135,13 @@ impl WindowManager {
             monitors[0].selected_tags = selected_tags;
         }
 
+        let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
+        if display.is_null() {
+            return Err(WmError::X11(crate::errors::X11Error::DisplayOpenFailed));
+        }
+
+        let font = crate::bar::font::Font::new(display, screen_number as i32, &config.font)?;
+
         let mut bars = Vec::new();
         for monitor in &monitors {
             let bar = Bar::new(
@@ -140,6 +149,8 @@ impl WindowManager {
                 &screen,
                 screen_number,
                 &config,
+                display,
+                &font,
                 monitor.x as i16,
                 monitor.y as i16,
                 monitor.width as u16,
@@ -169,6 +180,8 @@ impl WindowManager {
             selected_monitor: 0,
             atoms,
             previous_focused: None,
+            display,
+            font,
         };
 
         window_manager.scan_existing_windows()?;
@@ -430,7 +443,7 @@ impl WindowManager {
 
                 let draw_blocks = monitor_index == self.selected_monitor;
                 bar.invalidate();
-                bar.draw(&self.connection, monitor.selected_tags, occupied_tags, draw_blocks)?;
+                bar.draw(&self.connection, &self.font, self.display, monitor.selected_tags, occupied_tags, draw_blocks)?;
             }
         }
         Ok(())