tony / oxwm

Branch: 98c4ade
Switch branches/tags
master
tonybanters updates 98c4ade 2025-10-03
189 lines · 5.3 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) + 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
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 top_padding = 8;
139 let text_y = top_padding + self.font.ascent();
140
141 self.font_draw
142 .draw_text(&self.font, scheme.foreground, x_position + 5, text_y, tag);
143
144 if is_selected {
145 let underline_height = 3;
146 let gap_above_underline = 3;
147 let underline_y =
148 self.height as i16 - underline_height as i16 - gap_above_underline;
149 connection.change_gc(
150 self.graphics_context,
151 &ChangeGCAux::new().foreground(scheme.border),
152 )?;
153 connection.poly_fill_rectangle(
154 self.window,
155 self.graphics_context,
156 &[Rectangle {
157 x: x_position,
158 y: underline_y,
159 width: tag_width,
160 height: underline_height,
161 }],
162 )?;
163 }
164 x_position += tag_width as i16;
165 }
166
167 connection.flush()?;
168
169 unsafe {
170 x11::xlib::XFlush(self.display);
171 }
172
173 self.needs_redraw = false;
174
175 Ok(())
176 }
177
178 pub fn handle_click(&self, click_x: i16) -> Option<usize> {
179 let mut current_x_position = 0;
180
181 for (tag_index, &tag_width) in self.tag_widths.iter().enumerate() {
182 if click_x >= current_x_position && click_x < current_x_position + tag_width as i16 {
183 return Some(tag_index);
184 }
185 current_x_position += tag_width as i16;
186 }
187 None
188 }
189 }