tony / oxwm

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