tony / oxwm

Branch: 53a436a
Switch branches/tags
master
tonybtw Added bar. 2mb! 8347143 2025-10-03
185 lines · 5.1 KB Raw Blame History
1 use super::BAR_HEIGHT;
2 use crate::config::TAGS;
3 use anyhow::Result;
4 use x11rb::COPY_DEPTH_FROM_PARENT;
5 use x11rb::connection::Connection;
6 use x11rb::protocol::xproto::*;
7
8 pub struct Bar {
9 window: Window,
10 width: u16,
11 height: u16,
12 graphics_context: Gcontext,
13
14 tag_widths: Vec<u16>,
15 needs_redraw: bool,
16 }
17
18 impl Bar {
19 pub fn new<C: Connection>(connection: &C, screen: &Screen) -> Result<Self> {
20 let window = connection.generate_id()?;
21 let graphics_context = connection.generate_id()?;
22
23 let width = screen.width_in_pixels;
24 let height = BAR_HEIGHT;
25
26 connection.create_window(
27 COPY_DEPTH_FROM_PARENT,
28 window,
29 screen.root,
30 0,
31 0,
32 width,
33 height,
34 0,
35 WindowClass::INPUT_OUTPUT,
36 screen.root_visual,
37 &CreateWindowAux::new()
38 .background_pixel(screen.black_pixel)
39 .event_mask(EventMask::EXPOSURE | EventMask::BUTTON_PRESS)
40 .override_redirect(1),
41 )?;
42
43 connection.create_gc(
44 graphics_context,
45 window,
46 &CreateGCAux::new()
47 .foreground(screen.white_pixel)
48 .background(screen.black_pixel),
49 )?;
50
51 connection.map_window(window)?;
52 connection.flush()?;
53
54 // TODO: actual text width calculation when we add fonts
55 let tag_widths = TAGS.iter().map(|tag| (tag.len() as u16 * 8) + 10).collect();
56
57 Ok(Bar {
58 window,
59 width,
60 height,
61 graphics_context,
62 tag_widths,
63 needs_redraw: true,
64 })
65 }
66
67 pub fn window(&self) -> Window {
68 self.window
69 }
70
71 pub fn height(&self) -> u16 {
72 self.height
73 }
74
75 pub fn invalidate(&mut self) {
76 self.needs_redraw = true;
77 }
78
79 pub fn draw<C: Connection>(
80 &mut self,
81 connection: &C,
82 current_tags: u32,
83 occupied_tags: u32,
84 ) -> Result<()> {
85 if !self.needs_redraw {
86 return Ok(());
87 }
88
89 connection.poly_fill_rectangle(
90 self.window,
91 self.graphics_context,
92 &[Rectangle {
93 x: 0,
94 y: 0,
95 width: self.width,
96 height: self.height,
97 }],
98 )?;
99
100 let mut x_position: i16 = 0;
101
102 for (tag_index, tag) in TAGS.iter().enumerate() {
103 let tag_mask = 1 << tag_index;
104 let is_selected = (current_tags & tag_mask) != 0;
105 let is_occupied = (occupied_tags & tag_mask) != 0;
106
107 let tag_width = self.tag_widths[tag_index];
108
109 if is_selected {
110 connection.change_gc(
111 self.graphics_context,
112 &ChangeGCAux::new().foreground(0xFFFFFF),
113 )?;
114 connection.poly_fill_rectangle(
115 self.window,
116 self.graphics_context,
117 &[Rectangle {
118 x: x_position,
119 y: 0,
120 width: tag_width,
121 height: self.height,
122 }],
123 )?;
124
125 connection.change_gc(
126 self.graphics_context,
127 &ChangeGCAux::new().foreground(0x000000),
128 )?;
129 } else if is_occupied {
130 connection.change_gc(
131 self.graphics_context,
132 &ChangeGCAux::new().foreground(0x888888),
133 )?;
134 connection.poly_fill_rectangle(
135 self.window,
136 self.graphics_context,
137 &[Rectangle {
138 x: x_position,
139 y: 0,
140 width: tag_width,
141 height: 2,
142 }],
143 )?;
144
145 connection.change_gc(
146 self.graphics_context,
147 &ChangeGCAux::new().foreground(0xFFFFFF),
148 )?;
149 } else {
150 connection.change_gc(
151 self.graphics_context,
152 &ChangeGCAux::new().foreground(0x666666),
153 )?;
154 }
155
156 // TODO: Replace with actual font rendering later
157 connection.image_text8(
158 self.window,
159 self.graphics_context,
160 x_position + 5,
161 self.height as i16 - 5,
162 tag.as_bytes(),
163 )?;
164
165 x_position += tag_width as i16;
166 }
167
168 connection.flush()?;
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 }