tony / oxwm

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