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