tony / oxwm

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