tony / oxwm

Branch: 06c6741
Switch branches/tags
master
tonybtw Restructured blocks to be more sane.. 06c6741 2025-10-04
279 lines · 8.1 KB Raw Blame History
1 use super::blocks::Block;
2 use super::font::{Font, FontDraw};
3 use crate::config::{FONT, SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, STATUS_BLOCKS, TAGS};
4 use anyhow::Result;
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
17 font: Font,
18 font_draw: FontDraw,
19 display: *mut x11::xlib::Display,
20
21 tag_widths: Vec<u16>,
22 needs_redraw: bool,
23
24 blocks: Vec<Box<dyn Block>>,
25 block_last_updates: Vec<Instant>,
26 status_text: String,
27 }
28
29 impl Bar {
30 pub fn new(connection: &RustConnection, screen: &Screen, screen_num: usize) -> Result<Self> {
31 let window = connection.generate_id()?;
32 let graphics_context = connection.generate_id()?;
33
34 let width = screen.width_in_pixels;
35
36 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
37 if display.is_null() {
38 anyhow::bail!("Failed to open X11 display for XFT");
39 }
40 let font = Font::new(display, screen_num as i32, FONT)?;
41
42 let height = (font.height() as f32 * 1.5) as u16;
43
44 connection.create_window(
45 COPY_DEPTH_FROM_PARENT,
46 window,
47 screen.root,
48 0,
49 0,
50 width,
51 height,
52 0,
53 WindowClass::INPUT_OUTPUT,
54 screen.root_visual,
55 &CreateWindowAux::new()
56 .background_pixel(SCHEME_NORMAL.background)
57 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
58 .override_redirect(1),
59 )?;
60
61 connection.create_gc(
62 graphics_context,
63 window,
64 &CreateGCAux::new()
65 .foreground(SCHEME_NORMAL.foreground)
66 .background(SCHEME_NORMAL.background),
67 )?;
68
69 connection.map_window(window)?;
70 connection.flush()?;
71
72 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
73 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
74
75 let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
76
77 let horizontal_padding = (font.height() as f32 * 0.4) as u16;
78
79 let tag_widths = TAGS
80 .iter()
81 .map(|tag| {
82 let text_width = font.text_width(tag);
83 text_width + (horizontal_padding * 2)
84 })
85 .collect();
86
87 let blocks: Vec<Box<dyn Block>> = STATUS_BLOCKS
88 .iter()
89 .map(|config| config.to_block())
90 .collect();
91
92 let block_last_updates = vec![Instant::now(); blocks.len()];
93 // let blocks: Vec<Box<dyn Block>> = vec![
94 // Box::new(Uname::new(UNAME_PREFIX, UNAME_COLOR)),
95 // Box::new(Sep::new(SEPARATOR, SEP_COLOR)),
96 // Box::new(Clock::new(CLOCK_FORMAT, CLOCK_COLOR)),
97 // ];
98
99 Ok(Bar {
100 window,
101 width,
102 height,
103 graphics_context,
104 font,
105 font_draw,
106 display,
107 tag_widths,
108 needs_redraw: true,
109 blocks,
110 block_last_updates,
111 status_text: String::new(),
112 })
113 }
114
115 pub fn window(&self) -> Window {
116 self.window
117 }
118
119 pub fn height(&self) -> u16 {
120 self.height
121 }
122
123 pub fn invalidate(&mut self) {
124 self.needs_redraw = true;
125 }
126
127 pub fn update_blocks(&mut self) -> Result<()> {
128 let now = Instant::now();
129 let mut changed = false;
130
131 for (i, block) in self.blocks.iter_mut().enumerate() {
132 let elapsed = now.duration_since(self.block_last_updates[i]);
133
134 if elapsed >= block.interval() {
135 if let Ok(_) = block.content() {
136 self.block_last_updates[i] = now;
137 changed = true;
138 }
139 }
140 }
141
142 if changed {
143 let mut parts = Vec::new();
144 for block in &mut self.blocks {
145 if let Ok(text) = block.content() {
146 parts.push(text);
147 }
148 }
149 self.status_text = parts.join("");
150 self.needs_redraw = true;
151 }
152
153 Ok(())
154 }
155
156 pub fn draw(
157 &mut self,
158 connection: &RustConnection,
159 current_tags: u32,
160 occupied_tags: u32,
161 ) -> Result<()> {
162 if !self.needs_redraw {
163 return Ok(());
164 }
165
166 connection.change_gc(
167 self.graphics_context,
168 &ChangeGCAux::new().foreground(SCHEME_NORMAL.background),
169 )?;
170 connection.poly_fill_rectangle(
171 self.window,
172 self.graphics_context,
173 &[Rectangle {
174 x: 0,
175 y: 0,
176 width: self.width,
177 height: self.height,
178 }],
179 )?;
180
181 let mut x_position: i16 = 0;
182
183 for (tag_index, tag) in TAGS.iter().enumerate() {
184 let tag_mask = 1 << tag_index;
185 let is_selected = (current_tags & tag_mask) != 0;
186 let is_occupied = (occupied_tags & tag_mask) != 0;
187
188 let tag_width = self.tag_widths[tag_index];
189
190 let scheme = if is_selected {
191 &SCHEME_SELECTED
192 } else if is_occupied {
193 &SCHEME_OCCUPIED
194 } else {
195 &SCHEME_NORMAL
196 };
197
198 let text_width = self.font.text_width(tag);
199 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
200
201 let top_padding = 4;
202 let text_y = top_padding + self.font.ascent();
203
204 self.font_draw
205 .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
206
207 if is_selected {
208 let font_height = self.font.height();
209 let underline_height = font_height / 8;
210 let bottom_gap = 3;
211 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
212
213 let underline_padding = 4;
214 let underline_width = tag_width - underline_padding;
215 let underline_x = x_position + (underline_padding / 2) as i16;
216
217 connection.change_gc(
218 self.graphics_context,
219 &ChangeGCAux::new().foreground(scheme.border),
220 )?;
221 connection.poly_fill_rectangle(
222 self.window,
223 self.graphics_context,
224 &[Rectangle {
225 x: underline_x,
226 y: underline_y,
227 width: underline_width,
228 height: underline_height,
229 }],
230 )?;
231 }
232
233 x_position += tag_width as i16;
234 }
235
236 if !self.status_text.is_empty() {
237 let padding = 10;
238 let mut x_position = self.width as i16 - padding;
239
240 for block in self.blocks.iter_mut().rev() {
241 if let Ok(text) = block.content() {
242 let text_width = self.font.text_width(&text);
243 x_position -= text_width as i16;
244
245 let top_padding = 4;
246 let text_y = top_padding + self.font.ascent();
247
248 self.font_draw
249 .draw_text(&self.font, block.color(), x_position, text_y, &text);
250 }
251 }
252 }
253
254 connection.flush()?;
255
256 unsafe {
257 x11::xlib::XFlush(self.display);
258 }
259
260 self.needs_redraw = false;
261
262 Ok(())
263 }
264
265 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
266 let mut current_x_position = 0;
267
268 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
269 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
270 return Some(tag_index);
271 }
272 current_x_position += tag_width as i16;
273 }
274 None
275 }
276 pub fn needs_redraw(&self) -> bool {
277 self.needs_redraw
278 }
279 }