tony / oxwm

Branch: c847ca5
Switch branches/tags
master
tonybanters fixing underline width.. c847ca5 2025-10-03
204 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 let height = font.height() + 4;
36
37 connection.create_window(
38 COPY_DEPTH_FROM_PARENT,
39 window,
40 screen.root,
41 0,
42 0,
43 width,
44 height,
45 0,
46 WindowClass::INPUT_OUTPUT,
47 screen.root_visual,
48 &CreateWindowAux::new()
49 .background_pixel(SCHEME_NORMAL.background)
50 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
51 .override_redirect(1),
52 )?;
53
54 connection.create_gc(
55 graphics_context,
56 window,
57 &CreateGCAux::new()
58 .foreground(SCHEME_NORMAL.foreground)
59 .background(SCHEME_NORMAL.background),
60 )?;
61
62 connection.map_window(window)?;
63 connection.flush()?;
64
65 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
66 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
67
68 let font_draw = FontDraw::new(display, window as x11::xlib::Drawable, visual, colormap)?;
69
70 let horizontal_padding = (font.height() as f32 * 0.4) as u16;
71
72 let tag_widths = TAGS
73 .iter()
74 .map(|tag| {
75 let text_width = font.text_width(tag);
76 text_width + (horizontal_padding * 2)
77 })
78 .collect();
79
80 Ok(Bar {
81 window,
82 width,
83 height,
84 graphics_context,
85 font,
86 font_draw,
87 display,
88 tag_widths,
89 needs_redraw: true,
90 })
91 }
92
93 pub fn window(&self) -> Window {
94 self.window
95 }
96
97 pub fn height(&self) -> u16 {
98 self.height
99 }
100
101 pub fn invalidate(&mut self) {
102 self.needs_redraw = true;
103 }
104
105 pub fn draw(
106 &mut self,
107 connection: &RustConnection,
108 current_tags: u32,
109 occupied_tags: u32,
110 ) -> Result<()> {
111 if !self.needs_redraw {
112 return Ok(());
113 }
114
115 connection.change_gc(
116 self.graphics_context,
117 &ChangeGCAux::new().foreground(SCHEME_NORMAL.background),
118 )?;
119 connection.poly_fill_rectangle(
120 self.window,
121 self.graphics_context,
122 &[Rectangle {
123 x: 0,
124 y: 0,
125 width: self.width,
126 height: self.height,
127 }],
128 )?;
129
130 let mut x_position: i16 = 0;
131
132 for (tag_index, tag) in TAGS.iter().enumerate() {
133 let tag_mask = 1 << tag_index;
134 let is_selected = (current_tags & tag_mask) != 0;
135 let is_occupied = (occupied_tags & tag_mask) != 0;
136
137 let tag_width = self.tag_widths[tag_index];
138
139 let scheme = if is_selected {
140 &SCHEME_SELECTED
141 } else if is_occupied {
142 &SCHEME_OCCUPIED
143 } else {
144 &SCHEME_NORMAL
145 };
146
147 let text_width = self.font.text_width(tag);
148 let text_x = x_position + ((tag_width - text_width) / 2) as i16;
149
150 let text_height = self.font.height();
151 let top_margin = (self.height as i16 - text_height as i16) / 4;
152 let text_y = top_margin + 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 underline_height = (self.font.height() / 8).max(2);
159 let bottom_margin = 4;
160 let underline_y = self.height as i16 - underline_height as i16 - bottom_margin;
161
162 let underline_width = text_width + 6;
163 let underline_x = text_x - 3;
164
165 connection.change_gc(
166 self.graphics_context,
167 &ChangeGCAux::new().foreground(scheme.border),
168 )?;
169 connection.poly_fill_rectangle(
170 self.window,
171 self.graphics_context,
172 &[Rectangle {
173 x: underline_x,
174 y: underline_y,
175 width: underline_width,
176 height: underline_height,
177 }],
178 )?;
179 }
180 x_position += tag_width as i16;
181 }
182 connection.flush()?;
183
184 unsafe {
185 x11::xlib::XFlush(self.display);
186 }
187
188 self.needs_redraw = false;
189
190 Ok(())
191 }
192
193 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
194 let mut current_x_position = 0;
195
196 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
197 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
198 return Some(tag_index);
199 }
200 current_x_position += tag_width as i16;
201 }
202 None
203 }
204 }