tony / oxwm

Branch: 2a0c2b9
Switch branches/tags
master
tonybanters updating padding 2a0c2b9 2025-10-03
214 lines · 6.2 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() + 4;
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 display = unsafe { x11::xlib::XOpenDisplay(std::ptr::null()) };
67 if display.is_null() {
68 anyhow::bail!("Failed to open X11 display for XFT");
69 }
70
71 let font = Font::new(display, screen_num as i32, FONT)?;
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 tag_widths = TAGS
79 .iter()
80 .map(|tag| {
81 let text_width = font.text_width(tag);
82 let left_padding = 10;
83 let right_padding = 10;
84 text_width + left_padding + right_padding
85 })
86 .collect();
87
88 Ok(Bar {
89 window,
90 width,
91 height,
92 graphics_context,
93 font,
94 font_draw,
95 display,
96 tag_widths,
97 needs_redraw: true,
98 })
99 }
100
101 pub fn window(&self) -> Window {
102 self.window
103 }
104
105 pub fn height(&self) -> u16 {
106 self.height
107 }
108
109 pub fn invalidate(&mut self) {
110 self.needs_redraw = true;
111 }
112
113 pub fn draw(
114 &mut self,
115 connection: &RustConnection,
116 current_tags: u32,
117 occupied_tags: u32,
118 ) -> Result<()> {
119 if !self.needs_redraw {
120 return Ok(());
121 }
122
123 connection.change_gc(
124 self.graphics_context,
125 &ChangeGCAux::new().foreground(SCHEME_NORMAL.background),
126 )?;
127 connection.poly_fill_rectangle(
128 self.window,
129 self.graphics_context,
130 &[Rectangle {
131 x: 0,
132 y: 0,
133 width: self.width,
134 height: self.height,
135 }],
136 )?;
137
138 let mut x_position: i16 = 0;
139
140 for (tag_index, tag) in TAGS.iter().enumerate() {
141 let tag_mask = 1 << tag_index;
142 let is_selected = (current_tags & tag_mask) != 0;
143 let is_occupied = (occupied_tags & tag_mask) != 0;
144
145 let tag_width = self.tag_widths[tag_index];
146
147 let scheme = if is_selected {
148 &SCHEME_SELECTED
149 } else if is_occupied {
150 &SCHEME_OCCUPIED
151 } else {
152 &SCHEME_NORMAL
153 };
154
155 let text_width = self.font.text_width(tag);
156 let left_padding = (tag_width - text_width) / 2;
157 let text_x = x_position + left_padding as i16;
158
159 let text_height = self.font.height();
160 let top_margin = (self.height as i16 - text_height as i16) / 4;
161 let text_y = top_margin + self.font.ascent();
162
163 self.font_draw
164 .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
165
166 if is_selected {
167 let underline_height = 3;
168 let bottom_margin = 4;
169 let underline_y = self.height as i16 - underline_height as i16 - bottom_margin;
170
171 let underline_padding = 4;
172 let underline_width = tag_width - underline_padding;
173
174 connection.change_gc(
175 self.graphics_context,
176 &ChangeGCAux::new().foreground(scheme.border),
177 )?;
178 connection.poly_fill_rectangle(
179 self.window,
180 self.graphics_context,
181 &[Rectangle {
182 x: x_position + (underline_padding / 2) as i16,
183 y: underline_y,
184 width: underline_width,
185 height: underline_height,
186 }],
187 )?;
188 }
189
190 x_position += tag_width as i16;
191 }
192 connection.flush()?;
193
194 unsafe {
195 x11::xlib::XFlush(self.display);
196 }
197
198 self.needs_redraw = false;
199
200 Ok(())
201 }
202
203 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
204 let mut current_x_position = 0;
205
206 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
207 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
208 return Some(tag_index);
209 }
210 current_x_position += tag_width as i16;
211 }
212 None
213 }
214 }