tony / oxwm

Branch: 5d24423
Switch branches/tags
master
emzywastaken add error handling TODOs 618817d 2025-10-13
328 lines · 9.9 KB Raw Blame History
1 use super::blocks::Block;
2 use super::font::{Font, FontDraw};
3 use crate::Config;
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 block_underlines: Vec<bool>,
27 status_text: String,
28
29 tags: Vec<String>,
30 scheme_normal: crate::ColorScheme,
31 scheme_occupied: crate::ColorScheme,
32 scheme_selected: crate::ColorScheme,
33 }
34
35 // Potential Errors:
36 // ::new -> x11rb::errors::ReplyOrIdError
37 // ::new -> x11rb::errors::ConnectionError
38 impl Bar {
39 pub fn new(
40 connection: &RustConnection,
41 screen: &Screen,
42 screen_num: usize,
43 config: &Config,
44 ) -> Result<Self> {
45 let window = connection.generate_id()?;
46 let graphics_context = connection.generate_id()?;
47
48 let width = screen.width_in_pixels;
49
50 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
51 if display.is_null() {
52 anyhow::bail!("Failed to open X11 display for XFT");
53 }
54 // TODO: Identify errors
55 let font = Font::new(display, screen_num as i32, &config.font)?;
56
57 let height = (font.height() as f32 * 1.4) as u16;
58
59 connection.create_window(
60 COPY_DEPTH_FROM_PARENT,
61 window,
62 screen.root,
63 0,
64 0,
65 width,
66 height,
67 0,
68 WindowClass::INPUT_OUTPUT,
69 screen.root_visual,
70 &CreateWindowAux::new()
71 .background_pixel(config.scheme_normal.background)
72 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
73 .override_redirect(1),
74 )?;
75
76 connection.create_gc(
77 graphics_context,
78 window,
79 &CreateGCAux::new()
80 .foreground(config.scheme_normal.foreground)
81 .background(config.scheme_normal.background),
82 )?;
83
84 connection.map_window(window)?;
85 connection.flush()?;
86
87 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
88 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
89
90 // TODO: Identify errors
91 let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
92
93 let horizontal_padding = (font.height() as f32 * 0.4) as u16;
94
95 let tag_widths = config
96 .tags
97 .iter()
98 .map(|tag| {
99 let text_width = font.text_width(tag);
100 text_width + (horizontal_padding * 2)
101 })
102 .collect();
103
104 let blocks: Vec<Box<dyn Block>> = config
105 .status_blocks
106 .iter()
107 .map(|block_config| block_config.to_block())
108 .collect();
109
110 let block_underlines: Vec<bool> = config
111 .status_blocks
112 .iter()
113 .map(|block_config| block_config.underline)
114 .collect();
115
116 let block_last_updates = vec![Instant::now(); blocks.len()];
117
118 Ok(Bar {
119 window,
120 width,
121 height,
122 graphics_context,
123 font,
124 font_draw,
125 display,
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 })
137 }
138
139 pub fn window(&self) -> Window {
140 self.window
141 }
142
143 pub fn height(&self) -> u16 {
144 self.height
145 }
146
147 pub fn invalidate(&mut self) {
148 self.needs_redraw = true;
149 }
150
151 pub fn update_blocks(&mut self) -> Result<()> {
152 let now = Instant::now();
153 let mut changed = false;
154
155 for (i, block) in self.blocks.iter_mut().enumerate() {
156 let elapsed = now.duration_since(self.block_last_updates[i]);
157
158 if elapsed >= block.interval() {
159 if block.content().is_ok() {
160 self.block_last_updates[i] = now;
161 changed = true;
162 }
163 }
164 }
165
166 if changed {
167 let mut parts = Vec::new();
168 for block in &mut self.blocks {
169 if let Ok(text) = block.content() {
170 parts.push(text);
171 }
172 }
173 self.status_text = parts.join("");
174 self.needs_redraw = true;
175 }
176
177 Ok(())
178 }
179
180 pub fn draw(
181 &mut self,
182 connection: &RustConnection,
183 current_tags: u32,
184 occupied_tags: u32,
185 ) -> Result<()> {
186 if !self.needs_redraw {
187 return Ok(());
188 }
189
190 connection.change_gc(
191 self.graphics_context,
192 &ChangeGCAux::new().foreground(self.scheme_normal.background),
193 )?;
194 connection.poly_fill_rectangle(
195 self.window,
196 self.graphics_context,
197 &[Rectangle {
198 x: 0,
199 y: 0,
200 width: self.width,
201 height: self.height,
202 }],
203 )?;
204
205 let mut x_position: i16 = 0;
206
207 for (tag_index, tag) in self.tags.iter().enumerate() {
208 let tag_mask = 1 << tag_index;
209 let is_selected = (current_tags & tag_mask) != 0;
210 let is_occupied = (occupied_tags & tag_mask) != 0;
211
212 let tag_width = self.tag_widths[tag_index];
213
214 let scheme = if is_selected {
215 &self.scheme_selected
216 } else if is_occupied {
217 &self.scheme_occupied
218 } else {
219 &self.scheme_normal
220 };
221
222 let text_width = self.font.text_width(tag);
223 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
224
225 let top_padding = 4;
226 let text_y = top_padding + self.font.ascent();
227
228 self.font_draw
229 .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
230
231 if is_selected {
232 let font_height = self.font.height();
233 let underline_height = font_height / 8;
234 let bottom_gap = 3;
235 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
236
237 let underline_padding = 4;
238 let underline_width = tag_width - underline_padding;
239 let underline_x = x_position + (underline_padding / 2) as i16;
240
241 connection.change_gc(
242 self.graphics_context,
243 &ChangeGCAux::new().foreground(scheme.underline),
244 )?;
245 connection.poly_fill_rectangle(
246 self.window,
247 self.graphics_context,
248 &[Rectangle {
249 x: underline_x,
250 y: underline_y,
251 width: underline_width,
252 height: underline_height,
253 }],
254 )?;
255 }
256
257 x_position += tag_width as i16;
258 }
259
260 if !self.status_text.is_empty() {
261 let padding = 10;
262 let mut x_position = self.width as i16 - padding;
263
264 for (i, block) in self.blocks.iter_mut().enumerate().rev() {
265 if let Ok(text) = block.content() {
266 let text_width = self.font.text_width(&text);
267 x_position -= text_width as i16;
268
269 let top_padding = 4;
270 let text_y = top_padding + self.font.ascent();
271
272 self.font_draw
273 .draw_text(&self.font, block.color(), x_position, text_y, &text);
274
275 if self.block_underlines[i] {
276 let font_height = self.font.height();
277 let underline_height = font_height / 8;
278 let bottom_gap = 3;
279 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
280
281 let underline_padding = 8;
282 let underline_width = text_width + underline_padding;
283 let underline_x = x_position - (underline_padding / 2) as i16;
284
285 connection.change_gc(
286 self.graphics_context,
287 &ChangeGCAux::new().foreground(block.color()),
288 )?;
289
290 connection.poly_fill_rectangle(
291 self.window,
292 self.graphics_context,
293 &[Rectangle {
294 x: underline_x,
295 y: underline_y,
296 width: underline_width,
297 height: underline_height,
298 }],
299 )?;
300 }
301 }
302 }
303 }
304 connection.flush()?;
305 unsafe {
306 x11::xlib::XFlush(self.display);
307 }
308 self.needs_redraw = false;
309
310 Ok(())
311 }
312
313 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
314 let mut current_x_position = 0;
315
316 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
317 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
318 return Some(tag_index);
319 }
320 current_x_position += tag_width as i16;
321 }
322 None
323 }
324
325 pub fn needs_redraw(&self) -> bool {
326 self.needs_redraw
327 }
328 }