tony / oxwm

Branch: ff60f36
Switch branches/tags
master
tonybanters updating padding ff60f36 2025-10-03
190 lines · 5.4 KB Raw Blame History
1 use super::font::{Font, FontDraw};
2 use crate::config::{BAR_HEIGHT, FONT, SCHEME_NORMAL, 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) + 20).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
108 connection.change_gc(
109 self.graphics_context,
110 &ChangeGCAux::new().foreground(SCHEME_NORMAL.background),
111 )?;
112 connection.poly_fill_rectangle(
113 self.window,
114 self.graphics_context,
115 &[Rectangle {
116 x: 0,
117 y: 0,
118 width: self.width,
119 height: self.height,
120 }],
121 )?;
122
123 let mut x_position: i16 = 0;
124
125 for (tag_index, tag) in TAGS.iter().enumerate() {
126 let tag_mask = 1 << tag_index;
127 let is_selected = (current_tags & tag_mask) != 0;
128 let is_occupied = (occupied_tags & tag_mask) != 0;
129
130 let tag_width = self.tag_widths[tag_index];
131
132 let scheme = if is_selected || is_occupied {
133 &SCHEME_SELECTED
134 } else {
135 &SCHEME_NORMAL
136 };
137
138 let text_height = self.font.height();
139 let top_margin = (self.height as i16 - text_height as i16) / 4; // Smaller top margin
140 let text_y = top_margin + self.font.ascent();
141
142 self.font_draw
143 .draw_text(&self.font, scheme.foreground, x_position + 5, text_y, tag);
144
145 if is_selected {
146 let underline_height = 3;
147 let bottom_margin = 4;
148 let underline_y = self.height as i16 - underline_height as i16 - bottom_margin;
149
150 connection.change_gc(
151 self.graphics_context,
152 &ChangeGCAux::new().foreground(scheme.border),
153 )?;
154 connection.poly_fill_rectangle(
155 self.window,
156 self.graphics_context,
157 &[Rectangle {
158 x: x_position,
159 y: underline_y,
160 width: tag_width,
161 height: underline_height,
162 }],
163 )?;
164 }
165 x_position += tag_width as i16;
166 }
167
168 connection.flush()?;
169
170 unsafe {
171 x11::xlib::XFlush(self.display);
172 }
173
174 self.needs_redraw = false;
175
176 Ok(())
177 }
178
179 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
180 let mut current_x_position = 0;
181
182 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
183 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
184 return Some(tag_index);
185 }
186 current_x_position += tag_width as i16;
187 }
188 None
189 }
190 }