tony / oxwm

Branch: 597aa85
Switch branches/tags
master
tonybanters fixed bar 597aa85 2025-10-03
207 lines · 5.9 KB Raw Blame History
1 use super::font::{Font, FontDraw};
2 use crate::config::{FONT, SCHEME_NORMAL, SCHEME_OCCUPIED, SCHEME_SELECTED, TAGS};
3 use anyhow::Result;
4 use x11rb::COPY_DEPTH_FROM_PARENT;
5 use x11rb::connection::Connection;
6 use x11rb::protocol::xproto::*;
7 use x11rb::rust_connection::RustConnection;
8
9 pub struct Bar {
10 window: Window,
11 width: u16,
12 height: u16,
13 graphics_context: Gcontext,
14
15 font: Font,
16 font_draw: FontDraw,
17 display: *mut x11::xlib::Display,
18
19 tag_widths: Vec<u16>,
20 needs_redraw: bool,
21 }
22
23 impl Bar {
24 pub fn new(connection: &RustConnection, screen: &Screen, screen_num: usize) -> Result<Self> {
25 let window = connection.generate_id()?;
26 let graphics_context = connection.generate_id()?;
27
28 let width = screen.width_in_pixels;
29
30 let display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
31 if display.is_null() {
32 anyhow::bail!("Failed to open X11 display for XFT");
33 }
34 let font = Font::new(display, screen_num as i32, FONT)?;
35
36 let height = (font.height() as f32 * 1.25) as u16;
37
38 connection.create_window(
39 COPY_DEPTH_FROM_PARENT,
40 window,
41 screen.root,
42 0,
43 0,
44 width,
45 height,
46 0,
47 WindowClass::INPUT_OUTPUT,
48 screen.root_visual,
49 &CreateWindowAux::new()
50 .background_pixel(SCHEME_NORMAL.background)
51 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
52 .override_redirect(1),
53 )?;
54
55 connection.create_gc(
56 graphics_context,
57 window,
58 &CreateGCAux::new()
59 .foreground(SCHEME_NORMAL.foreground)
60 .background(SCHEME_NORMAL.background),
61 )?;
62
63 connection.map_window(window)?;
64 connection.flush()?;
65
66 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
67 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
68
69 let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
70
71 let horizontal_padding = (font.height() as f32 * 0.4) as u16;
72
73 let tag_widths = TAGS
74 .iter()
75 .map(|tag| {
76 let text_width = font.text_width(tag);
77 text_width + (horizontal_padding * 2)
78 })
79 .collect();
80
81 Ok(Bar {
82 window,
83 width,
84 height,
85 graphics_context,
86 font,
87 font_draw,
88 display,
89 tag_widths,
90 needs_redraw: true,
91 })
92 }
93
94 pub fn window(&self) -> Window {
95 self.window
96 }
97
98 pub fn height(&self) -> u16 {
99 self.height
100 }
101
102 pub fn invalidate(&mut self) {
103 self.needs_redraw = true;
104 }
105
106 pub fn draw(
107 &mut self,
108 connection: &RustConnection,
109 current_tags: u32,
110 occupied_tags: u32,
111 ) -> Result<()> {
112 if !self.needs_redraw {
113 return Ok(());
114 }
115
116 connection.change_gc(
117 self.graphics_context,
118 &ChangeGCAux::new().foreground(SCHEME_NORMAL.background),
119 )?;
120 connection.poly_fill_rectangle(
121 self.window,
122 self.graphics_context,
123 &[Rectangle {
124 x: 0,
125 y: 0,
126 width: self.width,
127 height: self.height,
128 }],
129 )?;
130
131 let mut x_position: i16 = 0;
132
133 for (tag_index, tag) in TAGS.iter().enumerate() {
134 let tag_mask = 1 << tag_index;
135 let is_selected = (current_tags & tag_mask) != 0;
136 let is_occupied = (occupied_tags & tag_mask) != 0;
137
138 let tag_width = self.tag_widths[tag_index];
139
140 let scheme = if is_selected {
141 &SCHEME_SELECTED
142 } else if is_occupied {
143 &SCHEME_OCCUPIED
144 } else {
145 &SCHEME_NORMAL
146 };
147
148 let text_width = self.font.text_width(tag);
149 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
150
151 let top_padding = 4;
152 let text_y = top_padding + self.font.ascent();
153
154 self.font_draw
155 .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
156
157 if is_selected {
158 let font_height = self.font.height();
159 let underline_height = font_height / 8;
160 let bottom_gap = 3;
161 let underline_y = self.height as i16 - underline_height as i16 - bottom_gap;
162
163 let underline_padding = 4;
164 let underline_width = tag_width - underline_padding;
165 let underline_x = x_position + (underline_padding / 2) as i16;
166
167 connection.change_gc(
168 self.graphics_context,
169 &ChangeGCAux::new().foreground(scheme.border),
170 )?;
171 connection.poly_fill_rectangle(
172 self.window,
173 self.graphics_context,
174 &[Rectangle {
175 x: underline_x,
176 y: underline_y,
177 width: underline_width,
178 height: underline_height,
179 }],
180 )?;
181 }
182
183 x_position += tag_width as i16;
184 }
185 connection.flush()?;
186
187 unsafe {
188 x11::xlib::XFlush(self.display);
189 }
190
191 self.needs_redraw = false;
192
193 Ok(())
194 }
195
196 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
197 let mut current_x_position = 0;
198
199 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
200 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
201 return Some(tag_index);
202 }
203 current_x_position += tag_width as i16;
204 }
205 None
206 }
207 }