|
|
@@ -1,4 +1,4 @@ |
| 1 |
|
-use crate::keyboard::{self, KeyAction}; // Add KeyAction to the import |
|
1 |
+use crate::keyboard::{self, Arg, KeyAction}; |
| 2 |
2 |
use crate::layout::Layout; |
| 3 |
3 |
use crate::layout::tiling::TilingLayout; |
| 4 |
4 |
|
|
|
@@ -15,7 +15,7 @@ pub struct WindowManager { |
| 15 |
15 |
root: Window, |
| 16 |
16 |
screen: Screen, |
| 17 |
17 |
windows: Vec<Window>, |
| 18 |
|
- pub focused_window: Option<Window>, |
|
18 |
+ focused_window: Option<Window>, |
| 19 |
19 |
layout: Box<dyn Layout>, |
| 20 |
20 |
} |
| 21 |
21 |
|
|
|
@@ -60,13 +60,15 @@ impl WindowManager { |
| 60 |
60 |
} |
| 61 |
61 |
} |
| 62 |
62 |
|
| 63 |
|
- fn handle_key_action(&mut self, action: keyboard::KeyAction) -> Result<()> { |
|
63 |
+ fn handle_key_action(&mut self, action: KeyAction, arg: &Arg) -> Result<()> { |
| 64 |
64 |
match action { |
| 65 |
|
- keyboard::KeyAction::SpawnTerminal => { |
| 66 |
|
- println!("Spawning terminal"); |
| 67 |
|
- std::process::Command::new("xclock").spawn()?; |
|
65 |
+ KeyAction::Spawn => { |
|
66 |
+ if let Arg::Str(command) = arg { |
|
67 |
+ println!("Spawning: {}", command); |
|
68 |
+ std::process::Command::new(command).spawn()?; |
|
69 |
+ } |
| 68 |
70 |
} |
| 69 |
|
- keyboard::KeyAction::CloseWindow => { |
|
71 |
+ KeyAction::KillClient => { |
| 70 |
72 |
println!("Closing focused window"); |
| 71 |
73 |
if let Some(focused) = self.focused_window { |
| 72 |
74 |
match self.connection.kill_client(focused) { |
|
|
@@ -80,28 +82,42 @@ impl WindowManager { |
| 80 |
82 |
} |
| 81 |
83 |
} |
| 82 |
84 |
} |
| 83 |
|
- keyboard::KeyAction::CycleWindow => { |
| 84 |
|
- println!("Cycling focus"); |
| 85 |
|
- self.cycle_focus()?; |
|
85 |
+ KeyAction::FocusStack => { |
|
86 |
+ if let Arg::Int(direction) = arg { |
|
87 |
+ println!("FocusStack called with direction: {}", direction); |
|
88 |
+ println!("Windows: {:?}", self.windows); |
|
89 |
+ println!("Current focused: {:?}", self.focused_window); |
|
90 |
+ self.cycle_focus(*direction)?; |
|
91 |
+ println!("New focused: {:?}", self.focused_window); |
|
92 |
+ } |
| 86 |
93 |
} |
| 87 |
|
- keyboard::KeyAction::Quit => { |
|
94 |
+ KeyAction::Quit => { |
| 88 |
95 |
println!("Quitting window manager"); |
| 89 |
96 |
std::process::exit(0); |
| 90 |
97 |
} |
| 91 |
|
- keyboard::KeyAction::None => { |
|
98 |
+ KeyAction::None => { |
| 92 |
99 |
//no-op |
| 93 |
100 |
} |
| 94 |
101 |
} |
| 95 |
102 |
Ok(()) |
| 96 |
103 |
} |
| 97 |
|
- pub fn cycle_focus(&mut self) -> Result<()> { |
|
104 |
+ |
|
105 |
+ pub fn cycle_focus(&mut self, direction: i32) -> Result<()> { |
| 98 |
106 |
if self.windows.is_empty() { |
| 99 |
107 |
return Ok(()); |
| 100 |
108 |
} |
| 101 |
109 |
|
| 102 |
110 |
let next_window = if let Some(current) = self.focused_window { |
| 103 |
111 |
if let Some(current_index) = self.windows.iter().position(|&w| w == current) { |
| 104 |
|
- let next_index = (current_index + 1) % self.windows.len(); |
|
112 |
+ let next_index = if direction > 0 { |
|
113 |
+ (current_index + 1) % self.windows.len() |
|
114 |
+ } else { |
|
115 |
+ if current_index == 0 { |
|
116 |
+ self.windows.len() - 1 |
|
117 |
+ } else { |
|
118 |
+ current_index - 1 |
|
119 |
+ } |
|
120 |
+ }; |
| 105 |
121 |
self.windows[next_index] |
| 106 |
122 |
} else { |
| 107 |
123 |
self.windows[0] |
|
|
@@ -115,11 +131,13 @@ impl WindowManager { |
| 115 |
131 |
} |
| 116 |
132 |
|
| 117 |
133 |
pub fn set_focus(&mut self, window: Option<Window>) -> Result<()> { |
|
134 |
+ println!("set_focus called with: {:?}", window); |
| 118 |
135 |
self.focused_window = window; |
| 119 |
136 |
|
| 120 |
137 |
if let Some(win) = window { |
| 121 |
138 |
self.connection |
| 122 |
139 |
.set_input_focus(InputFocus::POINTER_ROOT, win, x11rb::CURRENT_TIME)?; |
|
140 |
+ self.connection.flush()?; |
| 123 |
141 |
} |
| 124 |
142 |
|
| 125 |
143 |
self.update_focus_visuals()?; |
|
|
@@ -127,20 +145,30 @@ impl WindowManager { |
| 127 |
145 |
} |
| 128 |
146 |
|
| 129 |
147 |
fn update_focus_visuals(&self) -> Result<()> { |
|
148 |
+ println!("Updating focus visuals for {} windows", self.windows.len()); |
| 130 |
149 |
for &window in &self.windows { |
| 131 |
|
- let border_color = if self.focused_window == Some(window) { |
| 132 |
|
- 0xff0000 |
|
150 |
+ let (border_color, border_width) = if self.focused_window == Some(window) { |
|
151 |
+ println!("Window {} is FOCUSED (red border)", window); |
|
152 |
+ (0xff0000, 2) |
| 133 |
153 |
} else { |
| 134 |
|
- 0x888888 |
|
154 |
+ println!("Window {} is unfocused (gray border)", window); |
|
155 |
+ (0x888888, 2) |
| 135 |
156 |
}; |
| 136 |
157 |
|
|
158 |
+ self.connection.configure_window( |
|
159 |
+ window, |
|
160 |
+ &ConfigureWindowAux::new().border_width(border_width), |
|
161 |
+ )?; |
|
162 |
+ |
| 137 |
163 |
self.connection.change_window_attributes( |
| 138 |
164 |
window, |
| 139 |
165 |
&ChangeWindowAttributesAux::new().border_pixel(border_color), |
| 140 |
166 |
)?; |
| 141 |
167 |
} |
|
168 |
+ self.connection.flush()?; |
| 142 |
169 |
Ok(()) |
| 143 |
170 |
} |
|
171 |
+ |
| 144 |
172 |
fn handle_event(&mut self, event: Event) -> Result<()> { |
| 145 |
173 |
match event { |
| 146 |
174 |
Event::MapRequest(event) => { |
|
|
@@ -148,7 +176,6 @@ impl WindowManager { |
| 148 |
176 |
self.windows.push(event.window); |
| 149 |
177 |
self.apply_layout()?; |
| 150 |
178 |
self.set_focus(Some(event.window))?; |
| 151 |
|
- self.connection.flush()?; |
| 152 |
179 |
} |
| 153 |
180 |
Event::UnmapNotify(event) => { |
| 154 |
181 |
if self.windows.contains(&event.window) { |
|
|
@@ -163,32 +190,38 @@ impl WindowManager { |
| 163 |
190 |
} |
| 164 |
191 |
} |
| 165 |
192 |
Event::KeyPress(event) => { |
| 166 |
|
- let action = keyboard::handle_key_press(event)?; // Remove &self.connection |
| 167 |
|
- self.handle_key_action(action)?; |
|
193 |
+ let (action, arg) = keyboard::handle_key_press(event)?; |
|
194 |
+ self.handle_key_action(action, arg)?; |
| 168 |
195 |
} |
| 169 |
196 |
_ => {} |
| 170 |
197 |
} |
| 171 |
198 |
Ok(()) |
| 172 |
199 |
} |
|
200 |
+ |
| 173 |
201 |
fn apply_layout(&self) -> Result<()> { |
| 174 |
202 |
let screen_width = self.screen.width_in_pixels as u32; |
| 175 |
203 |
let screen_height = self.screen.height_in_pixels as u32; |
|
204 |
+ let border_width = 2u32; |
| 176 |
205 |
|
| 177 |
206 |
let geometries = self |
| 178 |
207 |
.layout |
| 179 |
208 |
.arrange(&self.windows, screen_width, screen_height); |
| 180 |
209 |
|
| 181 |
210 |
for (window, geometry) in self.windows.iter().zip(geometries.iter()) { |
|
211 |
+ // Adjust for full borders |
|
212 |
+ let adjusted_width = geometry.width.saturating_sub(2 * border_width); |
|
213 |
+ let adjusted_height = geometry.height.saturating_sub(2 * border_width); |
|
214 |
+ |
| 182 |
215 |
self.connection.configure_window( |
| 183 |
216 |
*window, |
| 184 |
217 |
&ConfigureWindowAux::new() |
| 185 |
218 |
.x(geometry.x_coordinate) |
| 186 |
219 |
.y(geometry.y_coordinate) |
| 187 |
|
- .width(geometry.width) |
| 188 |
|
- .height(geometry.height) |
| 189 |
|
- .border_width(1), |
|
220 |
+ .width(adjusted_width) |
|
221 |
+ .height(adjusted_height), |
| 190 |
222 |
)?; |
| 191 |
223 |
} |
|
224 |
+ self.connection.flush()?; |
| 192 |
225 |
return Ok(()); |
| 193 |
226 |
} |
| 194 |
227 |
|
|
|
@@ -200,15 +233,15 @@ impl WindowManager { |
| 200 |
233 |
println!("Removed window {} from management", window); |
| 201 |
234 |
|
| 202 |
235 |
if self.focused_window == Some(window) { |
| 203 |
|
- self.focused_window = if self.windows.is_empty() { |
|
236 |
+ let new_focus = if self.windows.is_empty() { |
| 204 |
237 |
None |
| 205 |
238 |
} else { |
| 206 |
239 |
Some(self.windows[self.windows.len() - 1]) |
| 207 |
240 |
}; |
|
241 |
+ self.set_focus(new_focus)?; |
| 208 |
242 |
} |
| 209 |
243 |
|
| 210 |
244 |
self.apply_layout()?; |
| 211 |
|
- self.connection.flush()?; |
| 212 |
245 |
} |
| 213 |
246 |
|
| 214 |
247 |
Ok(()) |