1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
//! Input handling for [`App`](super::App): key & mouse events, the prefix-key
//! command map, and crossterm→PTY key encoding.
use super::*;
impl App {
pub fn handle_event(&mut self, ev: AppEvent) {
// Closing the last node empties `workspaces` and sets `should_quit`; the
// loop drains the rest of the event batch before it checks that flag, so
// ignore events here once there's nothing left to act on (`layout()`
// would otherwise index an empty `workspaces`).
if self.workspaces.is_empty() {
return;
}
match ev {
AppEvent::Key(k) => self.handle_key(k),
AppEvent::Mouse(m) => self.handle_mouse(m),
AppEvent::Paste(s) => {
if let Some(p) = self.focused() {
p.send(s.as_bytes());
}
}
AppEvent::Resize(_, _) => {}
AppEvent::PtyData(id) => {
if let Some(s) = self.status.get_mut(&id) {
s.last_activity = Instant::now();
}
}
AppEvent::PtyExit(id) => self.close_pane(id),
AppEvent::ModuleCommandFinished {
log_id,
code,
out,
err,
} => self.module_command_finished(log_id, code, out, err),
AppEvent::GitData { view, payload } => self.git_data(view, payload),
// Handled by the server loop; never reaches here at runtime.
AppEvent::ClientConnected { .. } | AppEvent::ClientDetach { .. } => {}
}
}
fn handle_mouse(&mut self, m: ratatui::crossterm::event::MouseEvent) {
use ratatui::crossterm::event::{MouseButton, MouseEventKind};
// Track the cursor for hover affordances (e.g. the session delete ✕).
self.hover = Some((m.column, m.row));
// Any click dismisses the help overlay.
if self.help_open {
if let MouseEventKind::Down(MouseButton::Left) = m.kind {
self.help_open = false;
}
return;
}
// While the Settings modal is open it owns the mouse: clicks hit the
// modal (or dismiss it); everything else is swallowed.
if self.settings.is_some() {
if let MouseEventKind::Down(MouseButton::Left) = m.kind {
self.handle_settings_click(m.column, m.row);
}
return;
}
// The folder picker likewise owns the mouse while open.
if self.picker.is_some() {
match m.kind {
MouseEventKind::Down(MouseButton::Left) => {
let (c, r) = (m.column, m.row);
let hit = self
.picker_rects
.iter()
.find(|(_, rect)| {
c >= rect.x && c < rect.right() && r >= rect.y && r < rect.bottom()
})
.map(|(i, _)| *i);
match hit {
Some(i) => self.picker_click(i),
None => self.close_folder_picker(), // click outside cancels
}
}
// Wheel scrolls the browse list (moves the cursor, which the
// render keeps in view).
MouseEventKind::ScrollUp => self.picker_scroll(-1),
MouseEventKind::ScrollDown => self.picker_scroll(1),
_ => {}
}
return;
}
let scroll: i32 = match m.kind {
MouseEventKind::Down(MouseButton::Left) => 0,
MouseEventKind::ScrollUp => -3,
MouseEventKind::ScrollDown => 3,
_ => return, // motion / release: hover updated, nothing else to do
};
let (c, r) = (m.column, m.row);
let hit = |rect: Rect| c >= rect.x && c < rect.right() && r >= rect.y && r < rect.bottom();
if scroll != 0 {
// Wheel over a sidebar list scrolls it one item per notch (the next
// render clamps the offset to the list length).
let step = |off: usize| {
if scroll < 0 {
off.saturating_sub(1)
} else {
off + 1
}
};
if hit(self.nodes_area) {
self.nodes_scroll = step(self.nodes_scroll);
return;
}
if hit(self.agents_area) {
self.agents_scroll = step(self.agents_scroll);
return;
}
// Wheel over a git tab scrolls its active view (docs/17).
if self.active_is_git() && hit(self.last_pane_area) {
self.git_scroll(scroll);
return;
}
// Otherwise forward scroll as arrow keys to the pane under the cursor.
if let Some((id, _)) = self.pane_rects.iter().find(|(_, rect)| hit(*rect)) {
if let Some(pane) = self.panes.get(id) {
let seq: &[u8] = if scroll < 0 { b"\x1b[A" } else { b"\x1b[B" };
for _ in 0..scroll.abs() {
pane.send(seq);
}
}
}
return;
}
// The sidebar gear opens Settings.
if self.settings_icon_rect.is_some_and(hit) {
self.open_settings();
return;
}
// Left click: close/add buttons first, then tabs → agents → ws → panes.
if let Some((i, _)) = self.tab_close_rects.iter().find(|(_, rect)| hit(*rect)) {
self.close_tab(*i);
return;
}
// The focused pane's ✕ button closes the active pane.
if self.pane_close_rect.is_some_and(hit) {
self.close_pane(self.layout().focus);
return;
}
// Tab-bar scroll arrows: step to the previous / next tab.
if self.tab_prev_rect.is_some_and(hit) {
let a = self.ws().active_tab;
if a > 0 {
self.switch_tab(a - 1);
}
return;
}
if self.tab_next_rect.is_some_and(hit) {
let a = self.ws().active_tab;
if a + 1 < self.ws().tabs.len() {
self.switch_tab(a + 1);
}
return;
}
if let Some(rect) = self.new_ws_rect {
if hit(rect) {
self.open_folder_picker(); // "+" → choose a folder to open as a node
return;
}
}
if let Some((i, _)) = self.tab_rects.iter().find(|(_, rect)| hit(*rect)) {
let i = *i;
if i >= self.ws().tabs.len() {
self.new_tab(); // the "+" button
} else {
self.switch_tab(i);
}
return;
}
// The AGENTS All/Active filter toggle.
if let Some((val, _)) = self.agents_filter_rects.iter().find(|(_, rect)| hit(*rect)) {
let val = *val;
if self.agents_active_only != val {
self.agents_active_only = val;
self.agents_scroll = 0;
}
return;
}
if let Some((id, _)) = self.agent_rects.iter().find(|(_, rect)| hit(*rect)) {
let id = *id;
self.focus_pane_global(id);
return;
}
// The hovered row's ✕ removes the session from the list (checked first,
// since it sits on top of the row).
if let Some((i, _)) = self.session_del_rects.iter().find(|(_, rect)| hit(*rect)) {
let i = *i;
self.dismiss_session(i);
return;
}
// Clicking a resumable session row reopens it into a pane.
if let Some((i, _)) = self.session_rects.iter().find(|(_, rect)| hit(*rect)) {
let i = *i;
self.resume_session(i);
return;
}
// Clicking a node's branch opens its git tab (docs/17).
if let Some((i, _)) = self.node_branch_rects.iter().find(|(_, rect)| hit(*rect)) {
let i = *i;
self.open_git_tab(i);
return;
}
if let Some((i, _)) = self.ws_rects.iter().find(|(_, rect)| hit(*rect)) {
let i = (*i).min(self.workspaces.len().saturating_sub(1));
self.active_ws = i;
return;
}
// Clicking a view-selector tab in the git tab switches section (docs/17).
if self.active_is_git() {
if let Some((s, _)) = self.git_section_rects.iter().find(|(_, rect)| hit(*rect)) {
let s = *s;
self.git_click_section(s);
return;
}
}
if let Some((id, _)) = self.pane_rects.iter().find(|(_, rect)| hit(*rect)) {
let id = *id;
self.layout_mut().focus = id;
self.mode = Mode::Normal;
}
}
fn handle_key(&mut self, key: KeyEvent) {
if key.kind == KeyEventKind::Release {
return;
}
// The help cheat-sheet overlay swallows the next key press and closes.
if self.help_open {
self.help_open = false;
return;
}
// The Settings modal captures all input while open.
if self.settings.is_some() {
self.handle_settings_key(key);
return;
}
// The folder picker captures all input while open.
if self.picker.is_some() {
self.handle_picker_key(key);
return;
}
// The new-worktree branch prompt captures all input while open.
if self.worktree_prompt.is_some() {
self.handle_worktree_prompt_key(key);
return;
}
// A focused git tab captures normal-mode keys (its own j/k/⏎/…); the
// `Ctrl+Space` prefix still works for global ops (switch tab/node, …).
if self.mode == Mode::Normal && self.active_is_git() {
if is_prefix(&key) {
self.mode = Mode::Prefix;
} else {
self.handle_git_key(key);
}
return;
}
match self.mode {
Mode::Prefix => {
self.mode = Mode::Normal;
// Pressing the prefix twice sends a literal Ctrl-Space (NUL).
if is_prefix(&key) {
if let Some(p) = self.focused() {
p.send(&[0x00]);
}
return;
}
// Fixed convenience keys (not rebindable): `1`–`9` jump to a tab,
// `?` opens the shortcut cheat-sheet.
if let KeyCode::Char(c) = key.code {
if c.is_ascii_digit() && c != '0' {
self.switch_tab(c as usize - '1' as usize);
return;
}
if c == '?' {
self.help_open = true;
return;
}
}
// Everything else resolves through the keybinding registry
// (defaults + user overrides; see `app/keys.rs`). `key_string`
// ignores modifiers, so the command key works whether you
// released Ctrl after the prefix (`Ctrl+Space` then `c`) or kept
// it held as a fast chord (`Ctrl+Space`+`Ctrl+c`).
if let Some(cmd) = keys::key_string(&key).and_then(|s| self.keymap.get(&s).copied())
{
self.run_cmd(cmd);
}
}
Mode::Normal => {
if is_prefix(&key) {
self.mode = Mode::Prefix;
return;
}
if let Some(bytes) = encode_key(&key) {
if let Some(p) = self.focused() {
p.send(&bytes);
}
}
}
}
}
}
/// True if `key` is the prefix chord (Ctrl+Space). Terminals and OSes report
/// this chord inconsistently — modern Unix terminals send `Char(' ')` + Ctrl,
/// while the Windows console / some VT terminals send `Char('@')` + Ctrl or a
/// bare `Null` (the NUL byte Ctrl+Space produces). Accept them all so the prefix
/// works the same everywhere.
fn is_prefix(key: &KeyEvent) -> bool {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
matches!(key.code, KeyCode::Null)
|| (ctrl && matches!(key.code, KeyCode::Char(' ') | KeyCode::Char('@')))
}
/// Encode a crossterm key event into the bytes a terminal program expects.
fn encode_key(key: &KeyEvent) -> Option<Vec<u8>> {
let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
let alt = key.modifiers.contains(KeyModifiers::ALT);
let bytes: Vec<u8> = match key.code {
KeyCode::Char(c) => {
if ctrl {
let b = match c.to_ascii_lowercase() {
'a'..='z' => (c.to_ascii_uppercase() as u8) & 0x1f,
' ' | '@' => 0,
'[' => 0x1b,
'\\' => 0x1c,
']' => 0x1d,
'^' => 0x1e,
'_' => 0x1f,
_ => return None,
};
vec![b]
} else {
let mut s = c.to_string().into_bytes();
if alt {
let mut v = vec![0x1b];
v.append(&mut s);
v
} else {
s
}
}
}
KeyCode::Enter => vec![b'\r'],
KeyCode::Tab => vec![b'\t'],
KeyCode::BackTab => vec![0x1b, b'[', b'Z'],
KeyCode::Backspace => vec![0x7f],
KeyCode::Esc => vec![0x1b],
KeyCode::Left => csi(b'D'),
KeyCode::Right => csi(b'C'),
KeyCode::Up => csi(b'A'),
KeyCode::Down => csi(b'B'),
KeyCode::Home => csi(b'H'),
KeyCode::End => csi(b'F'),
KeyCode::Delete => vec![0x1b, b'[', b'3', b'~'],
KeyCode::Insert => vec![0x1b, b'[', b'2', b'~'],
KeyCode::PageUp => vec![0x1b, b'[', b'5', b'~'],
KeyCode::PageDown => vec![0x1b, b'[', b'6', b'~'],
_ => return None,
};
Some(bytes)
}
fn csi(final_byte: u8) -> Vec<u8> {
vec![0x1b, b'[', final_byte]
}