tony / oxwm

Branch: 5b55c46
Switch branches/tags
master
tonybanters asdf 5b55c46 2026-01-15
270 lines · 7.7 KB Raw Blame History
1 use crate::ColorScheme;
2 use crate::bar::font::{DrawingSurface, Font};
3 use crate::errors::X11Error;
4 use crate::layout::tabbed::TAB_BAR_HEIGHT;
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 TabBar {
11 window: Window,
12 width: u16,
13 height: u16,
14 x_offset: i16,
15 y_offset: i16,
16 graphics_context: Gcontext,
17 display: *mut x11::xlib::Display,
18 surface: DrawingSurface,
19 scheme_normal: ColorScheme,
20 scheme_selected: ColorScheme,
21 }
22
23 impl TabBar {
24 pub fn new(
25 connection: &RustConnection,
26 screen: &Screen,
27 screen_num: usize,
28 display: *mut x11::xlib::Display,
29 _font: &Font,
30 x: i16,
31 y: i16,
32 width: u16,
33 scheme_normal: ColorScheme,
34 scheme_selected: ColorScheme,
35 cursor: u32,
36 ) -> Result<Self, X11Error> {
37 let window = connection.generate_id()?;
38 let graphics_context = connection.generate_id()?;
39
40 let height = TAB_BAR_HEIGHT as u16;
41
42 connection.create_window(
43 COPY_DEPTH_FROM_PARENT,
44 window,
45 screen.root,
46 x,
47 y,
48 width,
49 height,
50 0,
51 WindowClass::INPUT_OUTPUT,
52 screen.root_visual,
53 &CreateWindowAux::new()
54 .background_pixel(scheme_normal.background)
55 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
56 .override_redirect(1),
57 )?;
58
59 connection.create_gc(
60 graphics_context,
61 window,
62 &CreateGCAux::new()
63 .foreground(scheme_normal.foreground)
64 .background(scheme_normal.background),
65 )?;
66
67 // Set cursor using xlib after window creation
68 unsafe {
69 x11::xlib::XDefineCursor(display, window as u64, cursor as u64);
70 }
71
72 connection.map_window(window)?;
73 connection.flush()?;
74
75 let visual = unsafe { x11::xlib::XDefaultVisual(display, screen_num as i32) };
76 let colormap = unsafe { x11::xlib::XDefaultColormap(display, screen_num as i32) };
77
78 let surface = DrawingSurface::new(
79 display,
80 window as x11::xlib::Drawable,
81 width as u32,
82 height as u32,
83 visual,
84 colormap,
85 )?;
86
87 Ok(Self {
88 window,
89 width,
90 height,
91 x_offset: x,
92 y_offset: y,
93 graphics_context,
94 display,
95 surface,
96 scheme_normal,
97 scheme_selected,
98 })
99 }
100
101 pub fn window(&self) -> Window {
102 self.window
103 }
104
105 pub fn draw(
106 &mut self,
107 connection: &RustConnection,
108 font: &Font,
109 windows: &[(Window, String)],
110 focused_window: Option<Window>,
111 ) -> Result<(), X11Error> {
112 connection.change_gc(
113 self.graphics_context,
114 &ChangeGCAux::new().foreground(self.scheme_normal.background),
115 )?;
116 connection.flush()?;
117
118 unsafe {
119 let gc = x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut());
120 x11::xlib::XSetForeground(self.display, gc, self.scheme_normal.background as u64);
121 x11::xlib::XFillRectangle(
122 self.display,
123 self.surface.pixmap(),
124 gc,
125 0,
126 0,
127 self.width as u32,
128 self.height as u32,
129 );
130 x11::xlib::XFreeGC(self.display, gc);
131 }
132
133 if windows.is_empty() {
134 self.copy_pixmap_to_window();
135 return Ok(());
136 }
137
138 let tab_width = self.width / windows.len() as u16;
139 let mut x_position: i16 = 0;
140
141 for (index, &(window, ref title)) in windows.iter().enumerate() {
142 let is_focused = Some(window) == focused_window;
143 let scheme = if is_focused {
144 &self.scheme_selected
145 } else {
146 &self.scheme_normal
147 };
148
149 let display_title = if title.is_empty() {
150 format!("Window {}", index + 1)
151 } else {
152 title.clone()
153 };
154
155 let text_width = font.text_width(&display_title);
156 let text_x = x_position + ((tab_width.saturating_sub(text_width)) / 2) as i16;
157
158 let top_padding = 6;
159 let text_y = top_padding + font.ascent();
160
161 self.surface
162 .font_draw()
163 .draw_text(font, scheme.foreground, text_x, text_y, &display_title);
164
165 if is_focused {
166 let underline_height = 3;
167 let underline_y = self.height as i16 - underline_height;
168
169 unsafe {
170 let gc =
171 x11::xlib::XCreateGC(self.display, self.surface.pixmap(), 0, std::ptr::null_mut());
172 x11::xlib::XSetForeground(self.display, gc, scheme.underline as u64);
173 x11::xlib::XFillRectangle(
174 self.display,
175 self.surface.pixmap(),
176 gc,
177 x_position as i32,
178 underline_y as i32,
179 tab_width as u32,
180 underline_height as u32,
181 );
182 x11::xlib::XFreeGC(self.display, gc);
183 }
184 }
185
186 x_position += tab_width as i16;
187 }
188
189 self.copy_pixmap_to_window();
190 Ok(())
191 }
192
193 fn copy_pixmap_to_window(&self) {
194 unsafe {
195 let gc =
196 x11::xlib::XCreateGC(self.display, self.window as u64, 0, std::ptr::null_mut());
197 x11::xlib::XCopyArea(
198 self.display,
199 self.surface.pixmap(),
200 self.window as u64,
201 gc,
202 0,
203 0,
204 self.width as u32,
205 self.height as u32,
206 0,
207 0,
208 );
209 x11::xlib::XFreeGC(self.display, gc);
210 }
211 }
212
213 pub fn get_clicked_window(&self, windows: &[(Window, String)], click_x: i16) -> Option<Window> {
214 if windows.is_empty() {
215 return None;
216 }
217
218 let tab_width = self.width / windows.len() as u16;
219 let tab_index = (click_x as u16 / tab_width) as usize;
220
221 windows.get(tab_index).map(|&(win, _)| win)
222 }
223
224 pub fn reposition(
225 &mut self,
226 connection: &RustConnection,
227 x: i16,
228 y: i16,
229 width: u16,
230 ) -> Result<(), X11Error> {
231 self.x_offset = x;
232 self.y_offset = y;
233 self.width = width;
234
235 connection.configure_window(
236 self.window,
237 &ConfigureWindowAux::new()
238 .x(x as i32)
239 .y(y as i32)
240 .width(width as u32),
241 )?;
242
243 let visual = unsafe { x11::xlib::XDefaultVisual(self.display, 0) };
244 let colormap = unsafe { x11::xlib::XDefaultColormap(self.display, 0) };
245
246 self.surface = DrawingSurface::new(
247 self.display,
248 self.window as x11::xlib::Drawable,
249 width as u32,
250 self.height as u32,
251 visual,
252 colormap,
253 )?;
254
255 connection.flush()?;
256 Ok(())
257 }
258
259 pub fn hide(&self, connection: &RustConnection) -> Result<(), X11Error> {
260 connection.unmap_window(self.window)?;
261 connection.flush()?;
262 Ok(())
263 }
264
265 pub fn show(&self, connection: &RustConnection) -> Result<(), X11Error> {
266 connection.map_window(self.window)?;
267 connection.flush()?;
268 Ok(())
269 }
270 }