tony / oxwm

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