tony / oxwm

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