tony / oxwm

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