phosphor-tui 0.3.5

Terminal UI frontend for the Phosphor DAW
Documentation
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
602
603
604
605
606
607
608
609
//! App methods: piano roll edit mode — note navigation, selection, movement.
//!
//! Navigation (column-locked):
//!   j/k = cycle notes within the current column
//!   h/l = jump to next/prev column that has notes
//! Selection (hold shift):
//!   Shift+dir = start/extend selection (adds notes as you navigate)
//!   Release shift + dir = move selected notes
//! Moving:
//!   h/l = move by grid step, j/k = move by semitone
//!   Esc = lock notes in place, clear selection

use super::*;
use crate::state::{EditSubMode, Pane, ClipViewFocus, ClipTab};

#[derive(Clone, Copy)]
enum Dir { Left, Right, Up, Down }

impl App {
    /// Enter edit mode. Selects the top-left note if any exist.
    pub(crate) fn enter_edit_mode(&mut self) {
        use crate::debug_log as dbg;

        if self.nav.clip_view_target.is_none() {
            self.status_message = Some(("no clip open".into(), std::time::Instant::now()));
            return;
        }

        self.nav.focused_pane = Pane::ClipView;
        self.nav.clip_view.clip_tab = ClipTab::PianoRoll;
        self.nav.clip_view.focus = ClipViewFocus::PianoRoll;

        self.nav.clip_view.piano_roll.edit_mode = true;
        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Navigate;
        self.nav.clip_view.piano_roll.edit_selected.clear();

        // Find top-left note (highest pitch, then earliest start)
        let best = self.nav.active_clip().and_then(|clip| {
            if clip.notes.is_empty() { return None; }
            let mut b = 0usize;
            for (i, n) in clip.notes.iter().enumerate() {
                let bn = &clip.notes[b];
                if n.note > bn.note || (n.note == bn.note && n.start_frac < bn.start_frac) {
                    b = i;
                }
            }
            Some((b, clip.notes[b].note))
        });

        if let Some((idx, note)) = best {
            self.nav.clip_view.piano_roll.edit_cursor = idx;
            self.nav.clip_view.piano_roll.cursor_note = note;
            dbg::system(&format!("edit mode: entered, cursor note={} idx={}", note, idx));
        } else {
            self.nav.clip_view.piano_roll.edit_cursor = 0;
            dbg::system("edit mode: entered (no notes)");
        }

        self.status_message = Some(("edit mode".into(), std::time::Instant::now()));
    }

    pub(crate) fn exit_edit_mode(&mut self) {
        let pr = &mut self.nav.clip_view.piano_roll;
        pr.edit_mode = false;
        pr.edit_selected.clear();
        pr.edit_sub = EditSubMode::Navigate;
        self.status_message = Some(("edit mode off".into(), std::time::Instant::now()));
    }

    /// Handle keys while in edit mode.
    ///
    /// State machine:
    ///   Navigate + Shift+dir → Selecting (add current note, navigate, add destination)
    ///   Selecting + Shift+dir → keep selecting (navigate, add destination)
    ///   Selecting + plain dir → Moving (start moving selected notes)
    ///   Moving + plain dir → keep moving
    ///   Moving/Selecting + Esc → lock notes, clear selection, → Navigate
    ///   Navigate + Esc → exit edit mode
    pub(crate) fn handle_edit_mode_keys(&mut self, key: crossterm::event::KeyEvent) {
        use crossterm::event::{KeyCode, KeyModifiers};
        use crate::debug_log as dbg;

        let shift = key.modifiers.contains(KeyModifiers::SHIFT);

        dbg::user(&format!(
            "edit key: {:?} shift={} sub={:?} cursor={} selected={:?}",
            key.code, shift, self.nav.clip_view.piano_roll.edit_sub,
            self.nav.clip_view.piano_roll.edit_cursor,
            self.nav.clip_view.piano_roll.edit_selected,
        ));

        // Normalize direction from key code (works for both Char and arrow keys)
        let dir = match key.code {
            KeyCode::Char('h') | KeyCode::Char('H') | KeyCode::Left => Some(Dir::Left),
            KeyCode::Char('l') | KeyCode::Char('L') | KeyCode::Right => Some(Dir::Right),
            KeyCode::Char('j') | KeyCode::Char('J') | KeyCode::Down => Some(Dir::Down),
            KeyCode::Char('k') | KeyCode::Char('K') | KeyCode::Up => Some(Dir::Up),
            _ => None,
        };

        match self.nav.clip_view.piano_roll.edit_sub {
            EditSubMode::Navigate => {
                match key.code {
                    KeyCode::Esc | KeyCode::Char('e') => {
                        self.exit_edit_mode();
                        return;
                    }
                    KeyCode::Char('d') => {
                        self.edit_delete_cursor_note();
                        return;
                    }
                    KeyCode::Char('u') => {
                        self.perform_undo();
                        return;
                    }
                    // Enter = toggle selection on cursor note (single-note select)
                    KeyCode::Enter => {
                        self.add_cursor_to_selection();
                        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Moving;
                        dbg::system(&format!(
                            "edit: selected cursor note, now {:?}",
                            self.nav.clip_view.piano_roll.edit_selected
                        ));
                        return;
                    }
                    _ => {}
                }
                if let Some(d) = dir {
                    if shift {
                        // Shift+direction: start selecting
                        self.add_cursor_to_selection();
                        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Selecting;
                        self.edit_navigate_dir(d);
                        self.add_cursor_to_selection();
                        dbg::system(&format!(
                            "edit: started selecting, now {:?}",
                            self.nav.clip_view.piano_roll.edit_selected
                        ));
                    } else {
                        self.edit_navigate_dir(d);
                    }
                }
            }
            EditSubMode::Selecting => {
                match key.code {
                    KeyCode::Esc => {
                        self.nav.clip_view.piano_roll.edit_selected.clear();
                        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Navigate;
                        dbg::user("edit: selection cleared");
                        return;
                    }
                    KeyCode::Char('d') => {
                        self.edit_delete_selected_notes();
                        return;
                    }
                    _ => {}
                }
                if let Some(d) = dir {
                    if shift {
                        // Still holding shift: keep selecting
                        self.edit_navigate_dir(d);
                        self.add_cursor_to_selection();
                        dbg::system(&format!(
                            "edit: extended selection, now {:?}",
                            self.nav.clip_view.piano_roll.edit_selected
                        ));
                    } else {
                        // Released shift: transition to Moving, apply first move
                        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Moving;
                        let (gs, st) = Self::dir_to_move(d);
                        self.move_selected_notes(gs, st);
                        dbg::system("edit: → moving");
                    }
                }
            }
            EditSubMode::Moving => {
                match key.code {
                    KeyCode::Esc => {
                        self.nav.clip_view.piano_roll.edit_selected.clear();
                        self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Navigate;
                        self.send_clip_update();
                        dbg::user("edit: notes locked");
                        return;
                    }
                    KeyCode::Char('d') => {
                        self.edit_delete_selected_notes();
                        return;
                    }
                    _ => {}
                }
                let step = self.nav.clip_view.piano_roll.grid.step_frac(
                    self.nav.clip_view.piano_roll.column_count
                );
                if let Some(d) = dir {
                    if shift {
                        // Shift+h/l = stretch right edge, Shift+j/k = stretch left edge
                        // This matches the Right-Left-Trick: shift = right edge
                        match d {
                            Dir::Left => self.stretch_selected_edit_notes(-step, true),
                            Dir::Right => self.stretch_selected_edit_notes(step, true),
                            // Shift+j/k = adjust left edge (start position)
                            Dir::Up => self.stretch_selected_edit_notes(-step, false),
                            Dir::Down => self.stretch_selected_edit_notes(step, false),
                        }
                    } else {
                        // Plain direction = move notes
                        let (gs, st) = Self::dir_to_move(d);
                        self.move_selected_notes(gs, st);
                    }
                }
            }
        }
    }

    /// Convert a direction to (grid_steps, semitones) for move_selected_notes.
    fn dir_to_move(dir: Dir) -> (i32, i32) {
        match dir {
            Dir::Left => (-1, 0),
            Dir::Right => (1, 0),
            Dir::Up => (0, 1),
            Dir::Down => (0, -1),
        }
    }

    /// Add the current edit_cursor to edit_selected if not already there.
    fn add_cursor_to_selection(&mut self) {
        let pr = &mut self.nav.clip_view.piano_roll;
        let cursor = pr.edit_cursor;
        if !pr.edit_selected.contains(&cursor) {
            pr.edit_selected.push(cursor);
        }
    }

    /// Navigate in the given direction using column-locked rules.
    fn edit_navigate_dir(&mut self, dir: Dir) {
        match dir {
            Dir::Up => self.edit_move_up_in_column(),
            Dir::Down => self.edit_move_down_in_column(),
            Dir::Left => self.edit_move_to_prev_column(),
            Dir::Right => self.edit_move_to_next_column(),
        }
    }

    /// Move cursor UP within the same column (higher pitch).
    pub(crate) fn edit_move_up_in_column(&mut self) {
        use crate::debug_log as dbg;
        let col_frac = self.current_cursor_column_frac();
        let col_w = self.edit_column_width();
        let notes = match self.nav.active_clip() {
            Some(c) => c.notes.clone(),
            None => return,
        };
        let pr = &self.nav.clip_view.piano_roll;
        let cur_idx = pr.edit_cursor;
        if cur_idx >= notes.len() { return; }
        let cur_note = notes[cur_idx].note;

        // Find next higher note in the same column
        let mut best: Option<(usize, u8)> = None;
        for (i, n) in notes.iter().enumerate() {
            if i == cur_idx { continue; }
            if !Self::same_column(n.start_frac, col_frac, col_w) { continue; }
            if n.note <= cur_note { continue; }
            if best.map_or(true, |(_, bn)| n.note < bn) {
                best = Some((i, n.note));
            }
        }

        dbg::system(&format!(
            "edit up: col_frac={:.4} col_w={:.4} cur_note={} found={:?}",
            col_frac, col_w, cur_note, best
        ));

        if let Some((idx, note)) = best {
            let pr = &mut self.nav.clip_view.piano_roll;
            pr.edit_cursor = idx;
            pr.cursor_note = note;
            self.auto_scroll_edit_cursor();
        }
    }

    /// Move cursor DOWN within the same column (lower pitch).
    pub(crate) fn edit_move_down_in_column(&mut self) {
        use crate::debug_log as dbg;
        let col_frac = self.current_cursor_column_frac();
        let col_w = self.edit_column_width();
        let notes = match self.nav.active_clip() {
            Some(c) => c.notes.clone(),
            None => return,
        };
        let pr = &self.nav.clip_view.piano_roll;
        let cur_idx = pr.edit_cursor;
        if cur_idx >= notes.len() { return; }
        let cur_note = notes[cur_idx].note;

        let mut best: Option<(usize, u8)> = None;
        for (i, n) in notes.iter().enumerate() {
            if i == cur_idx { continue; }
            if !Self::same_column(n.start_frac, col_frac, col_w) { continue; }
            if n.note >= cur_note { continue; }
            if best.map_or(true, |(_, bn)| n.note > bn) {
                best = Some((i, n.note));
            }
        }

        dbg::system(&format!(
            "edit down: col_frac={:.4} col_w={:.4} cur_note={} found={:?}",
            col_frac, col_w, cur_note, best
        ));

        if let Some((idx, note)) = best {
            let pr = &mut self.nav.clip_view.piano_roll;
            pr.edit_cursor = idx;
            pr.cursor_note = note;
            self.auto_scroll_edit_cursor();
        }
    }

    /// Move cursor to the nearest note in the previous column (left).
    pub(crate) fn edit_move_to_prev_column(&mut self) {
        let notes = match self.nav.active_clip() {
            Some(c) => c.notes.clone(),
            None => return,
        };
        let pr = &self.nav.clip_view.piano_roll;
        let cur_idx = pr.edit_cursor;
        if cur_idx >= notes.len() { return; }
        let cur_frac = notes[cur_idx].start_frac;
        let cur_note_val = notes[cur_idx].note;
        let col_w = self.edit_column_width();

        // Find the nearest note strictly to the left (different column)
        let mut best: Option<(usize, f64)> = None;
        for (i, n) in notes.iter().enumerate() {
            if n.start_frac >= cur_frac - col_w * 0.5 { continue; }
            let dx = cur_frac - n.start_frac;
            let dy = (n.note as f64 - cur_note_val as f64).abs() * 0.0001;
            let dist = dx + dy;
            if best.map_or(true, |(_, d)| dist < d) {
                best = Some((i, dist));
            }
        }

        if let Some((idx, _)) = best {
            let pr = &mut self.nav.clip_view.piano_roll;
            pr.edit_cursor = idx;
            pr.cursor_note = notes[idx].note;
            self.auto_scroll_edit_cursor();
        }
    }

    /// Move cursor to the nearest note in the next column (right).
    pub(crate) fn edit_move_to_next_column(&mut self) {
        let notes = match self.nav.active_clip() {
            Some(c) => c.notes.clone(),
            None => return,
        };
        let pr = &self.nav.clip_view.piano_roll;
        let cur_idx = pr.edit_cursor;
        if cur_idx >= notes.len() { return; }
        let cur_frac = notes[cur_idx].start_frac;
        let cur_note_val = notes[cur_idx].note;
        let col_w = self.edit_column_width();

        let mut best: Option<(usize, f64)> = None;
        for (i, n) in notes.iter().enumerate() {
            if n.start_frac <= cur_frac + col_w * 0.5 { continue; }
            let dx = n.start_frac - cur_frac;
            let dy = (n.note as f64 - cur_note_val as f64).abs() * 0.0001;
            let dist = dx + dy;
            if best.map_or(true, |(_, d)| dist < d) {
                best = Some((i, dist));
            }
        }

        if let Some((idx, _)) = best {
            let pr = &mut self.nav.clip_view.piano_roll;
            pr.edit_cursor = idx;
            pr.cursor_note = notes[idx].note;
            self.auto_scroll_edit_cursor();
        }
    }

    /// Get the column center frac for the note at the current edit cursor.
    fn current_cursor_column_frac(&self) -> f64 {
        if let Some(clip) = self.nav.active_clip() {
            let idx = self.nav.clip_view.piano_roll.edit_cursor;
            if let Some(n) = clip.notes.get(idx) {
                return n.start_frac;
            }
        }
        0.0
    }

    /// Column width based on grid resolution.
    fn edit_column_width(&self) -> f64 {
        let pr = &self.nav.clip_view.piano_roll;
        pr.grid.step_frac(pr.column_count)
    }

    /// Check if two notes are in the same column.
    /// Uses 90% of column width as tolerance to handle imprecise recorded timing.
    fn same_column(frac_a: f64, frac_b: f64, col_w: f64) -> bool {
        (frac_a - frac_b).abs() < col_w * 0.9
    }

    fn auto_scroll_edit_cursor(&mut self) {
        let pr = &mut self.nav.clip_view.piano_roll;
        let top = pr.view_bottom_note.saturating_add(pr.view_height);
        if pr.cursor_note < pr.view_bottom_note {
            pr.view_bottom_note = pr.cursor_note;
        } else if pr.cursor_note >= top {
            pr.view_bottom_note = pr.cursor_note - pr.view_height + 1;
        }
    }

    /// Move all selected notes by grid steps horizontally and semitones vertically.
    /// Pushes undo on first move, updates audio, scrolls view.
    pub(crate) fn move_selected_notes(&mut self, grid_steps: i32, semitones: i32) {
        use crate::debug_log as dbg;
        let pr = &self.nav.clip_view.piano_roll;
        let total_beats = pr.column_count;
        let grid = pr.grid;
        let snap = pr.snap_enabled;
        let target = self.nav.clip_view_target;

        let mut indices: Vec<usize> = pr.edit_selected.clone();
        if !indices.contains(&pr.edit_cursor) {
            indices.push(pr.edit_cursor);
        }

        let step = grid.step_frac(total_beats);

        // Capture before-state for undo (only on first move of a selection)
        let before: Vec<(usize, phosphor_core::clip::NoteSnapshot)> = if let Some(clip) = self.nav.active_clip() {
            indices.iter()
                .filter_map(|&idx| clip.notes.get(idx).map(|n| (idx, *n)))
                .collect()
        } else {
            Vec::new()
        };

        // Apply the move
        if let Some(clip) = self.nav.active_clip_mut() {
            for &idx in &indices {
                if let Some(note) = clip.notes.get_mut(idx) {
                    if grid_steps != 0 {
                        let new_frac = note.start_frac + grid_steps as f64 * step;
                        note.start_frac = if snap {
                            grid.snap(new_frac, total_beats).clamp(0.0, 1.0 - note.duration_frac)
                        } else {
                            new_frac.clamp(0.0, 1.0 - note.duration_frac)
                        };
                    }
                    if semitones != 0 {
                        let new_note = note.note as i32 + semitones;
                        note.note = new_note.clamp(0, 127) as u8;
                    }
                }
            }
        }

        // Push undo
        if !before.is_empty() {
            if let Some((ti, ci)) = target {
                self.nav.undo_stack.push(crate::state::undo::UndoAction::MoveNotes {
                    track_idx: ti, clip_idx: ci, before,
                });
            }
        }

        // Update the cursor note to track the moved note's new pitch
        let cursor_idx = self.nav.clip_view.piano_roll.edit_cursor;
        if let Some(clip) = self.nav.active_clip() {
            if let Some(note) = clip.notes.get(cursor_idx) {
                self.nav.clip_view.piano_roll.cursor_note = note.note;
            }
        }

        // Scroll view to follow the cursor
        self.auto_scroll_edit_cursor();

        // Sync to audio thread
        self.send_clip_update();
        dbg::system(&format!("edit move: steps={} semi={} notes={}", grid_steps, semitones, indices.len()));
    }

    /// Stretch selected notes' edges. Reuses apply_edge_delta from piano_roll.
    fn stretch_selected_edit_notes(&mut self, delta: f64, right_edge: bool) {
        use crate::debug_log as dbg;
        let pr = &self.nav.clip_view.piano_roll;
        let target = self.nav.clip_view_target;

        let mut indices: Vec<usize> = pr.edit_selected.clone();
        if !indices.contains(&pr.edit_cursor) {
            indices.push(pr.edit_cursor);
        }

        // Capture before-state
        let before: Vec<(usize, phosphor_core::clip::NoteSnapshot)> = if let Some(clip) = self.nav.active_clip() {
            indices.iter()
                .filter_map(|&idx| clip.notes.get(idx).map(|n| (idx, *n)))
                .collect()
        } else {
            Vec::new()
        };

        // Apply stretch
        if let Some(clip) = self.nav.active_clip_mut() {
            for &idx in &indices {
                if let Some(note) = clip.notes.get_mut(idx) {
                    Self::apply_edge_delta(note, delta, right_edge);
                }
            }
        }

        // Undo + sync
        if !before.is_empty() {
            if let Some((ti, ci)) = target {
                self.nav.undo_stack.push(crate::state::undo::UndoAction::MoveNotes {
                    track_idx: ti, clip_idx: ci, before,
                });
            }
            self.send_clip_update();
            dbg::system(&format!("edit stretch: edge={} delta={:.4} notes={}",
                if right_edge { "right" } else { "left" }, delta, indices.len()));
        }
    }

    /// Delete the note at the edit cursor. Pushes undo, syncs audio, kills sound.
    pub(crate) fn edit_delete_cursor_note(&mut self) {
        use crate::debug_log as dbg;
        let target = self.nav.clip_view_target;
        let cursor = self.nav.clip_view.piano_roll.edit_cursor;

        if let Some(clip) = self.nav.active_clip_mut() {
            if cursor < clip.notes.len() {
                let removed = clip.notes.remove(cursor);
                dbg::system(&format!("edit delete: removed note {} at frac {:.4}", removed.note, removed.start_frac));

                // Push undo
                if let Some((ti, ci)) = target {
                    self.nav.undo_stack.push(crate::state::undo::UndoAction::RemoveNote {
                        track_idx: ti, clip_idx: ci, note: removed,
                    });
                }

                // Fix cursor if it's now past the end
                let len = self.nav.active_clip().map(|c| c.notes.len()).unwrap_or(0);
                if len == 0 {
                    self.nav.clip_view.piano_roll.edit_cursor = 0;
                } else if cursor >= len {
                    self.nav.clip_view.piano_roll.edit_cursor = len - 1;
                }

                self.send_clip_update();
                self.engine.panic(); // kill any in-flight note-on
                self.status_message = Some(("note deleted".into(), std::time::Instant::now()));
            }
        }
    }

    /// Delete all selected notes (+ cursor note). Pushes undo, syncs audio.
    fn edit_delete_selected_notes(&mut self) {
        use crate::debug_log as dbg;
        let target = self.nav.clip_view_target;
        let pr = &self.nav.clip_view.piano_roll;
        let mut indices: Vec<usize> = pr.edit_selected.clone();
        if !indices.contains(&pr.edit_cursor) {
            indices.push(pr.edit_cursor);
        }
        // Sort descending so removing by index doesn't shift later indices
        indices.sort_unstable();
        indices.dedup();
        indices.reverse();

        let mut removed_notes = Vec::new();
        if let Some(clip) = self.nav.active_clip_mut() {
            for &idx in &indices {
                if idx < clip.notes.len() {
                    removed_notes.push(clip.notes.remove(idx));
                }
            }
        }

        if !removed_notes.is_empty() {
            let count = removed_notes.len();
            if let Some((ti, ci)) = target {
                self.nav.undo_stack.push(crate::state::undo::UndoAction::DeleteNotes {
                    track_idx: ti, clip_idx: ci, notes: removed_notes,
                });
            }

            // Reset edit state
            let len = self.nav.active_clip().map(|c| c.notes.len()).unwrap_or(0);
            self.nav.clip_view.piano_roll.edit_selected.clear();
            self.nav.clip_view.piano_roll.edit_sub = EditSubMode::Navigate;
            self.nav.clip_view.piano_roll.edit_cursor = if len > 0 { 0 } else { 0 };

            self.send_clip_update();
            self.engine.panic();
            dbg::system(&format!("edit delete: removed {} notes", count));
            self.status_message = Some((
                format!("{} note{} deleted", count, if count == 1 { "" } else { "s" }),
                std::time::Instant::now(),
            ));
        }
    }
}