tony / oxwm

Branch: c55f819
Switch branches/tags
master
tonybanters updating padding c55f819 2025-10-03
207 lines · 6 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 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 tag_widths = TAGS
72 .iter()
73 .map(|tag| {
74 let text_width = font.text_width(tag);
75 let left_padding = 10;
76 let right_padding = 10;
77 text_width + left_padding + right_padding
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 left_padding = (tag_width - text_width) / 2;
150 let text_x = x_position + left_padding as i16;
151
152 let text_height = self.font.height();
153 let top_margin = (self.height as i16 - text_height as i16) / 4;
154 let text_y = top_margin + self.font.ascent();
155
156 self.font_draw
157 .draw_text(&self.font, scheme.foreground, text_x, text_y, tag);
158
159 if is_selected {
160 let underline_height = 3;
161 let bottom_margin = 4;
162 let underline_y = self.height as i16 - underline_height as i16 - bottom_margin;
163
164 let text_width = self.font.text_width(tag);
165 let underline_width = text_width + 4;
166 let underline_x = x_position + ((tag_width - underline_width) / 2) as i16;
167
168 connection.change_gc(
169 self.graphics_context,
170 &ChangeGCAux::new().foreground(scheme.border),
171 )?;
172 connection.poly_fill_rectangle(
173 self.window,
174 self.graphics_context,
175 &[Rectangle {
176 x: underline_x,
177 y: underline_y,
178 width: underline_width,
179 height: underline_height,
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 }