curd-editor 0.27.1

A terminal code editor: file tree, tabs, git, language servers, run and jump to errors, and a panel for Claude or any other assistant
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
//! What comes in: keys, the mouse, and messages from background work.

use super::*;

impl Workspace {
    // ---- Events --------------------------------------------------------

    pub fn run(&mut self, events: &Receiver<AppEvent>) -> std::io::Result<()> {
        self.refresh_git();
        while !self.should_quit {
            self.size = Terminal::size()?;
            let (frame, cursor) = self.render(self.size.0, self.size.1);
            self.screen.draw(frame, self.size, cursor)?;
            if let Some(text) = self.clipboard_out.take() {
                Terminal::copy(&text)?;
            }
            if let Some((words, what)) = self.terminal_job.take() {
                self.run_in_terminal(&words, &what)?;
                continue;
            }

            // Sleep until something happens. With edits waiting to go to a
            // language server, wake up after a short pause in typing instead.
            let waiting_to_sync = self
                .servers
                .iter()
                .any(|server| server.client.borrow().has_pending_changes());
            let event = if waiting_to_sync || self.agent.running {
                // While Claude works, wake up to show the time passing.
                let pause = if waiting_to_sync { LSP_DEBOUNCE } else { AGENT_TICK };
                match events.recv_timeout(pause) {
                    Ok(event) => Some(event),
                    Err(RecvTimeoutError::Timeout) => None,
                    Err(RecvTimeoutError::Disconnected) => break,
                }
            } else {
                match events.recv() {
                    Ok(event) => Some(event),
                    Err(_) => break,
                }
            };
            match event {
                Some(event) => {
                    self.handle_event(event);
                    // Handle everything already queued (a paste, held keys)
                    // before drawing once, rather than drawing per key.
                    while let Ok(event) = events.try_recv() {
                        self.handle_event(event);
                    }
                }
                None => self.editors.iter_mut().for_each(Editor::sync_lsp),
            }
        }
        self.stop_every_agent();
        self.stop_command();
        Ok(())
    }

    pub(super) fn handle_event(&mut self, event: AppEvent) {
        match event {
            AppEvent::Input(Event::Key(key)) => self.handle_key(key),
            AppEvent::Input(Event::Mouse(mouse)) => self.handle_mouse(mouse),
            AppEvent::Input(Event::Paste(text)) => self.paste(&text),
            AppEvent::Input(Event::FocusGained) => {
                // Coming back from another window: files and git state may
                // have changed there.
                self.tree.refresh();
                self.reload_changed();
                self.refresh_git();
            }
            AppEvent::Input(_) => {}
            AppEvent::Lsp(id, message) => self.handle_lsp(id, message),
            AppEvent::Git(git) => {
                self.git = git;
                self.show_branch();
            }
            AppEvent::GitDone(message) => self.git_done(message),
            AppEvent::Agent(run, line) => self.handle_agent(run, line),
            AppEvent::Run(run, event) => self.handle_run(run, event),
            AppEvent::Account(index, printed, succeeded) => {
                let account = agent::account_state(&printed, succeeded);
                match index == self.agent_index {
                    true => self.agent.account = account,
                    false => {
                        if let Some(parked) = self.parked.get_mut(index) {
                            parked.panel.account = account;
                        }
                    }
                }
            }
            AppEvent::Committed(path, lines) => {
                if let Some(editor) = self
                    .editors
                    .iter_mut()
                    .find(|editor| editor.abs_path.as_deref() == Some(path.as_str()))
                {
                    editor.set_git_base(lines);
                }
            }
        }
    }

    pub(super) fn handle_key(&mut self, key: KeyEvent) {
        // Windows reports both key press and key release; only act on press.
        if key.kind == KeyEventKind::Release {
            return;
        }
        if self.help.is_some() {
            return self.help_key(key);
        }
        if self.finder.is_some() {
            self.finder_key(key);
            return;
        }
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        let alt = key.modifiers.contains(KeyModifiers::ALT);

        // Messages and confirmations only last until the next key.
        let quit_was_pending = std::mem::take(&mut self.quit_pending);
        let close_was_pending = std::mem::take(&mut self.close_pending);
        let delete_was_pending = std::mem::take(&mut self.delete_pending);
        self.message.clear();
        if let Some(editor) = self.editors.get_mut(self.active) {
            editor.message.clear();
        }

        // Commands for the whole window come first. Each has an Alt form
        // as well, because some terminals keep Ctrl keys for themselves.
        match key.code {
            KeyCode::Char('q') if ctrl => self.quit(quit_was_pending),
            KeyCode::Char('p') if ctrl || alt => {
                self.picker = Picker::Files;
                self.finder = Some(Finder::new(self.tree.root()));
            }
            KeyCode::Char('s') if alt => self.pick(Picker::Search, Vec::new()),
            KeyCode::Char('g') if ctrl => self.open_git_menu(),
            KeyCode::Char('v') if alt => self.open_git_menu(),
            KeyCode::Char('v') if ctrl => {
                let text = self.clipboard.clone();
                self.paste(&text);
            }
            KeyCode::Char('b') if ctrl || alt => self.toggle_tree(),
            KeyCode::Char('e') if alt => self.switch_focus(),
            KeyCode::Char('i') if alt => self.toggle_agent(),
            KeyCode::F(1) => self.list_commands(),
            KeyCode::Char('x') if alt => self.list_commands(),
            KeyCode::Char('?') if alt => self.help = Some(0),
            KeyCode::Char('t') if ctrl => self.ask_command(),
            KeyCode::Char('t') if alt => self.toggle_output(),
            KeyCode::F(5) => self.run_again(),
            KeyCode::F(8) => self.next_error(!key.modifiers.contains(KeyModifiers::SHIFT)),
            // Git, each with a key of its own: what changed (in this file;
            // with Shift, everywhere), fetch, pull, push, switch branch.
            KeyCode::F(4 | 6 | 7 | 9 | 10) if self.git.is_none() => {
                self.say("This folder is not in git".into());
            }
            KeyCode::F(4) if key.modifiers.contains(KeyModifiers::SHIFT) => self.git_command(11),
            KeyCode::F(4) => self.git_command(10),
            KeyCode::F(6) => self.git_command(9),
            KeyCode::F(7) => self.git_command(7),
            KeyCode::F(9) => self.git_command(8),
            KeyCode::F(10) => self.git_command(5),
            KeyCode::Char('w') if ctrl || alt => self.close_active(close_was_pending),
            KeyCode::Char('.') if alt => self.next_tab(1),
            KeyCode::Char(',') if alt => self.next_tab(-1),
            KeyCode::PageDown if ctrl => self.next_tab(1),
            KeyCode::PageUp if ctrl => self.next_tab(-1),
            KeyCode::Char(digit @ '1'..='9') if alt => self.activate(digit as usize - '1' as usize),
            _ if self.focus == Focus::Agent => self.agent_key(key),
            _ if self.focus == Focus::Output => self.output_key(key),
            // In the tree, letters are commands for the selected entry.
            KeyCode::Char('d') | KeyCode::Delete if self.focus == Focus::Tree && !ctrl && !alt => {
                self.delete_selected(delete_was_pending);
            }
            _ if self.focus == Focus::Tree => self.tree_key(key),
            _ => self.editor_key(key),
        }
    }

    /// A key for the file in front.
    pub(super) fn editor_key(&mut self, key: KeyEvent) {
        let Some(editor) = self.editors.get_mut(self.active) else {
            return;
        };
        // In a view of changes, Enter goes to the line in its file.
        if key.code == KeyCode::Enter
            && let Some((file, line)) = editor.change_at_cursor()
        {
            let path = self.tree.root().join(&file);
            if path.is_file() && self.open(&path) {
                self.editors[self.active].jump_to(0, line);
            } else {
                self.say(format!("{file} is not there any more"));
            }
            return;
        }
        editor.handle_key(key);
        if let Some(text) = editor.copied.take() {
            self.clipboard = text.clone();
            self.clipboard_out = Some(text);
        }
        let saved = std::mem::take(&mut editor.just_saved);
        let renamed = std::mem::take(&mut editor.renamed);
        if renamed {
            // The file has a name now, so it can have a language server
            // and a place in the tree.
            let mut editor = self.editors.remove(self.active);
            self.attach_lsp(&mut editor);
            self.editors.insert(self.active, editor);
        }
        if saved {
            // A save can create a file and always changes its git state.
            self.tree.refresh();
            self.refresh_git();
        }
        if renamed && let Some(path) = self.editors[self.active].abs_path.clone() {
            self.tree.reveal(Path::new(&path));
        }
    }

    pub(super) fn tree_key(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Up => self.tree.move_by(-1),
            KeyCode::Down => self.tree.move_by(1),
            KeyCode::PageUp => self.tree.move_by(-10),
            KeyCode::PageDown => self.tree.move_by(10),
            KeyCode::Home => self.tree.move_by(isize::MIN / 2),
            KeyCode::End => self.tree.move_by(isize::MAX / 2),
            KeyCode::Left => self.tree.left(),
            KeyCode::Right => self.tree.right(),
            KeyCode::Enter => {
                if let Some(path) = self.tree.activate() {
                    self.open(&path);
                }
            }
            KeyCode::Char('n') => self.ask_new(Picker::NewFile),
            KeyCode::Char('f') => self.ask_new(Picker::NewFolder),
            KeyCode::Char('r') | KeyCode::F(2) => self.ask_rename(),
            KeyCode::Esc if self.has_files() => self.focus = Focus::Editor,
            _ => {}
        }
    }

    pub(super) fn finder_key(&mut self, key: KeyEvent) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        let Some(finder) = &mut self.finder else {
            return;
        };
        match key.code {
            KeyCode::Esc => self.finder = None,
            KeyCode::Char('p' | 'q' | 'c') if ctrl => self.finder = None,
            KeyCode::Char('p') if key.modifiers.contains(KeyModifiers::ALT) => self.finder = None,
            // F1 a second time, in the list of commands: all of them at once.
            KeyCode::F(1) if self.picker == Picker::Commands => {
                self.finder = None;
                self.help = Some(0);
            }
            KeyCode::Enter => self.accept_picked(),
            KeyCode::Up => finder.move_by(-1),
            KeyCode::Down => finder.move_by(1),
            KeyCode::Backspace => finder.backspace(),
            KeyCode::Char(c) if !ctrl => finder.type_char(c),
            _ => {}
        }
    }

    /// Enter in the list over the text (or a click on one of its lines):
    /// acts on what is selected or was typed.
    pub(super) fn accept_picked(&mut self) {
        let Some(finder) = &self.finder else {
            return;
        };
        let typed = finder.query.trim().to_string();
        let index = finder.chosen_index();
        let chosen = finder.chosen().map(String::from);
        self.finder = None;
        match (self.picker, index, chosen) {
            (picker, ..) if picker.is_question() && typed.is_empty() => {}
            (Picker::Search, ..) => self.search_project(&typed),
            // A command that starts with `!` gets the whole terminal, for
            // one that asks questions.
            (Picker::Run, ..) => match typed.strip_prefix('!') {
                Some(command) => {
                    let shell = if cfg!(windows) { ["cmd", "/C"] } else { ["sh", "-c"] };
                    let words = vec![shell[0].to_string(), shell[1].to_string(), command.trim().to_string()];
                    self.terminal_job = Some((words, command.trim().to_string()));
                }
                None => self.run_command(&typed),
            },
            (Picker::NewFile, ..) => self.make_new(&typed, false),
            (Picker::NewFolder, ..) => self.make_new(&typed, true),
            (Picker::Rename, ..) => self.rename_to(&typed),
            (Picker::Commands, Some(index), _) => self.run_listed(index),
            (Picker::Slash, index, chosen) => self.run_slash(index, chosen, &typed),
            (Picker::Attach, _, Some(file)) => self.agent.attach(file),
            (Picker::Model, Some(index), _) => self.choose_model(index),
            (Picker::Agent, Some(index), _) => self.open_agent(index),
            (Picker::ModelName, ..) => {
                self.agent.say(Who::Note, format!("The next answers come from {typed}"));
                self.agent.model = Some(typed);
            }
            (Picker::Commit, ..) => {
                let message = typed.clone();
                self.run_git(format!("Committed: {typed}"), move |root| {
                    git::action(root, &["commit", "--message", &message])
                });
            }
            (Picker::NewBranch, ..) => {
                let name = typed.clone();
                self.run_git(format!("On new branch {typed}"), move |root| {
                    git::action(root, &["switch", "--create", &name])
                });
            }
            (Picker::Files, _, Some(file)) => {
                let path = self.tree.root().join(file);
                self.open(&path);
            }
            (Picker::Results, Some(index), _) => self.open_hit(index),
            (Picker::Git, Some(index), _) => self.git_command(index),
            (Picker::Sessions, Some(index), _) => self.resume_session(index),
            (Picker::Branches, _, Some(branch)) => {
                let name = branch.clone();
                self.run_git(format!("Switched to {branch}"), move |root| {
                    git::action(root, &["switch", &name])
                });
            }
            _ => {}
        }
    }

    pub(super) fn paste(&mut self, text: &str) {
        if let Some(finder) = &mut self.finder {
            text.chars()
                .filter(|c| !c.is_control())
                .for_each(|c| finder.type_char(c));
        } else if self.focus == Focus::Agent {
            // Pasted text keeps its lines.
            for c in text.chars().filter(|c| *c == '\n' || !c.is_control()) {
                self.agent.type_char(c);
            }
        } else if self.focus == Focus::Editor
            && let Some(editor) = self.editors.get_mut(self.active)
        {
            editor.paste(text);
        }
    }

    /// Shows or hides the file tree. Showing it puts the keys there.
    pub(super) fn toggle_tree(&mut self) {
        if self.show_tree && self.has_files() {
            self.show_tree = false;
            self.focus = Focus::Editor;
        } else {
            self.show_folder();
            self.tree.refresh();
            self.refresh_git();
        }
    }

    /// Moves the keys between the file tree and the file in front.
    pub(super) fn switch_focus(&mut self) {
        if self.focus != Focus::Editor && self.has_files() {
            self.focus = Focus::Editor;
        } else {
            self.show_folder();
        }
    }

    /// True if the git branch is shown at column `x`, row `y`: in the
    /// status bar of the file in front, or on the last row of the tree.
    pub(super) fn branch_is_at(&self, x: usize, y: usize, layout: &Layout) -> bool {
        if x < layout.tree {
            return layout.tree > 0 && y + 1 == self.size.1;
        }
        let status_row = (layout.tabs + layout.pane_height).saturating_sub(1);
        let columns = self
            .editors
            .get(self.active)
            .and_then(|editor| editor.branch_columns(layout.pane_width));
        y == status_row && columns.is_some_and(|(from, to)| (from..to).contains(&(x - layout.tree)))
    }

    /// The mouse while the list over the text is open: a click on a line
    /// picks it, the wheel moves the selection, a click elsewhere closes it.
    pub(super) fn picker_mouse(&mut self, mouse: MouseEvent) {
        let popup = self.popup(self.size.0, self.size.1);
        let Some(finder) = &mut self.finder else {
            return;
        };
        let (x, y) = (mouse.column as usize, mouse.row as usize);
        let inside = (popup.x..popup.x + popup.width).contains(&x);
        match mouse.kind {
            MouseEventKind::ScrollDown => finder.move_by(1),
            MouseEventKind::ScrollUp => finder.move_by(-1),
            MouseEventKind::Down(MouseButton::Left) => {
                let row = y.checked_sub(popup.list_y).filter(|_| inside);
                if row.is_some_and(|row| finder.select_row(row, popup.capacity)) {
                    self.accept_picked();
                } else if !(inside && y == popup.input_y) {
                    self.finder = None;
                }
            }
            _ => {}
        }
    }

    /// How many rows the open list shows: its matches, or one row to say
    /// that there are none.
    pub(super) fn popup_rows(&self, capacity: usize) -> usize {
        let Some(finder) = &self.finder else {
            return 0;
        };
        let shown = finder.visible(capacity).len();
        if shown == 0 && !self.picker.is_question() { 1.min(capacity) } else { shown }
    }

    /// Where the open list goes on a `width` x `height` screen. A list
    /// that belongs to the Claude panel sits in the panel, growing up
    /// from the question, so it is where the eyes and the typing are. Any
    /// other is a box at the top of the text, in the middle.
    pub(super) fn popup(&self, width: usize, height: usize) -> Popup {
        let layout = self.layout(width, height);
        // The panel: its left edge, width, top row and height.
        let panel = if layout.agent_side > 0 {
            let top = layout.tabs;
            Some((layout.tree + layout.pane_width + 1, layout.agent_side - 1, top, height.saturating_sub(top)))
        } else if layout.agent > 0 {
            Some((layout.tree, layout.pane_width, layout.tabs + layout.pane_height, layout.agent))
        } else {
            None
        };
        if let (true, Some((x, panel_width, top, panel_height))) = (self.picker.is_for_claude(), panel) {
            let question = self.agent.box_rows(panel_width, panel_height);
            // Above the question, below the panel's title.
            let input_y = (top + panel_height).saturating_sub(question + 1);
            let capacity = FINDER_ROWS.min(input_y.saturating_sub(top + 1));
            if input_y > top {
                let list_y = input_y - self.popup_rows(capacity);
                return Popup { x, width: panel_width, input_y, list_y, capacity };
            }
        }
        let box_width = layout.pane_width.min(POPUP_WIDTH);
        Popup {
            x: layout.tree + (layout.pane_width - box_width) / 2,
            width: box_width,
            input_y: layout.tabs,
            list_y: layout.tabs + 1,
            capacity: FINDER_ROWS.min(layout.pane_height.saturating_sub(2)),
        }
    }

    pub(super) fn handle_mouse(&mut self, mouse: MouseEvent) {
        // On the page of shortcuts the wheel scrolls and a click closes it.
        if self.help.is_some() {
            let key = match mouse.kind {
                MouseEventKind::ScrollUp => KeyCode::Up,
                MouseEventKind::ScrollDown => KeyCode::Down,
                MouseEventKind::Down(_) => KeyCode::Esc,
                _ => return,
            };
            return self.help_key(KeyEvent::new(key, NONE));
        }
        let layout = self.layout(self.size.0, self.size.1);
        if self.finder.is_some() {
            return self.picker_mouse(mouse);
        }
        let (x, y) = (mouse.column as usize, mouse.row as usize);
        let over_tree = x < layout.tree && y >= layout.tabs;
        let over_tabs = y < layout.tabs;
        // The Claude panel: a click puts the keys there, the wheel scrolls.
        // Dragging its edge (its title row, or the line beside it) makes
        // it bigger or smaller.
        let beside = layout.agent_side > 0;
        let edge = layout.tree + layout.pane_width;
        if self.resizing_agent {
            match mouse.kind {
                MouseEventKind::Drag(MouseButton::Left) if beside => {
                    self.resize_agent(self.size.0.saturating_sub(x + 1));
                }
                MouseEventKind::Drag(MouseButton::Left) => {
                    self.resize_agent(self.size.1.saturating_sub(y));
                }
                _ => self.resizing_agent = false,
            }
            return;
        }
        // What the command printed: a click goes to the place a line names.
        let over_output = layout.output > 0
            && y >= layout.tabs + layout.pane_height
            && (layout.tree..edge).contains(&x);
        if over_output {
            match mouse.kind {
                MouseEventKind::Down(MouseButton::Left) => {
                    self.focus = Focus::Output;
                    if let Some(row) = (y - layout.tabs - layout.pane_height).checked_sub(1) {
                        self.output.click(row);
                        if let Some(place) = self.output.selected_location() {
                            self.go_to_place(Some(place));
                        }
                    }
                }
                MouseEventKind::ScrollUp => self.output.move_by(-WHEEL_LINES),
                MouseEventKind::ScrollDown => self.output.move_by(WHEEL_LINES),
                _ => {}
            }
            return;
        }
        let over_agent = if beside {
            x >= edge && y >= layout.tabs
        } else {
            layout.agent > 0 && !over_tree && y >= layout.tabs + layout.pane_height
        };
        if over_agent {
            // A click on an assistant's tab brings its panel to the front.
            let tab = self.agent_tabs.windows(2).find(|spots| {
                let ((row, from, _), (_, to, _)) = (spots[0], spots[1]);
                row == y && (from..to).contains(&x)
            });
            if let (MouseEventKind::Down(MouseButton::Left), Some(spots)) = (mouse.kind, tab) {
                return self.open_agent(spots[0].2);
            }
            match mouse.kind {
                MouseEventKind::Down(MouseButton::Left) => {
                    self.focus = Focus::Agent;
                    self.resizing_agent = if beside {
                        x == edge
                    } else {
                        y == layout.tabs + layout.pane_height
                    };
                }
                MouseEventKind::ScrollUp => self.agent.scroll_by(WHEEL_LINES),
                MouseEventKind::ScrollDown => self.agent.scroll_by(-WHEEL_LINES),
                _ => {}
            }
            return;
        }
        match mouse.kind {
            // The middle button on a tab closes it.
            MouseEventKind::Down(MouseButton::Middle) if over_tabs => {
                if let Some((tab, _)) = self.tab_at(x) {
                    self.close_tab(tab);
                }
            }
            // The wheel over the tab bar goes through the tabs.
            MouseEventKind::ScrollDown if over_tabs => self.next_tab(1),
            MouseEventKind::ScrollUp if over_tabs => self.next_tab(-1),
            MouseEventKind::Down(MouseButton::Left) => {
                if over_tabs {
                    match self.tab_at(x) {
                        Some((tab, true)) => self.close_tab(tab),
                        Some((tab, false)) => self.activate(tab),
                        None => {}
                    }
                } else if self.git.is_some() && self.branch_is_at(x, y, &layout) {
                    // A click on the branch's name lists the branches.
                    self.git_command(5);
                } else if over_tree {
                    self.focus = Focus::Tree;
                    // The first row of the tree is its title.
                    if let Some(row) = (y - layout.tabs).checked_sub(1)
                        && let Some(path) = self.tree.click(row)
                    {
                        self.open(&path);
                    }
                } else if let Some(editor) = self.editors.get_mut(self.active) {
                    self.focus = Focus::Editor;
                    editor.click(x - layout.tree, y - layout.tabs);
                    // A second click on the same spot selects the word.
                    let now = Instant::now();
                    let double = self.last_click.is_some_and(|(at, last_x, last_y)| {
                        (last_x, last_y) == (x, y) && now.duration_since(at) < DOUBLE_CLICK
                    });
                    if double {
                        editor.select_word();
                    }
                    self.last_click = (!double).then_some((now, x, y));
                }
            }
            // Dragging over the text selects it.
            MouseEventKind::Drag(MouseButton::Left)
                if self.focus == Focus::Editor && y >= layout.tabs && x >= layout.tree =>
            {
                if let Some(editor) = self.editors.get_mut(self.active) {
                    editor.drag(x - layout.tree, y - layout.tabs);
                }
            }
            MouseEventKind::ScrollDown | MouseEventKind::ScrollUp => {
                let lines = match mouse.kind {
                    MouseEventKind::ScrollDown => WHEEL_LINES,
                    _ => -WHEEL_LINES,
                };
                if over_tree {
                    self.tree.move_by(lines);
                } else if let Some(editor) = self.editors.get_mut(self.active) {
                    editor.wheel(lines);
                }
            }
            _ => {}
        }
    }
}