phosphor-tui 0.3.2

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
610
611
612
613
614
//! App methods: keys.

use super::*;

impl App {

    pub(crate) fn handle_event(&mut self, event: Event) {
        use crate::debug_log as dbg;

        let Event::Key(key) = event else { return };

        if key.code == KeyCode::Char('c') && key.modifiers.contains(KeyModifiers::CONTROL) {
            dbg::user("Ctrl+C → quit");
            self.running = false;
            return;
        }

        // Ctrl+S → quick save
        if key.code == KeyCode::Char('s') && key.modifiers.contains(KeyModifiers::CONTROL) {
            dbg::user("Ctrl+S → save");
            self.handle_save();
            return;
        }

        // Undo (u) / Redo (Ctrl+r) — works globally except in modals
        if key.code == KeyCode::Char('u') {
            dbg::user(&format!("u key received: modifiers={:?} space={} input={} confirm={} instr={} fx={}",
                key.modifiers, self.nav.space_menu.open, self.nav.input_modal.open,
                self.nav.confirm_modal.open, self.nav.instrument_modal.open, self.nav.fx_menu.open));
        }
        if !self.nav.space_menu.open && !self.nav.input_modal.open && !self.nav.confirm_modal.open
            && !self.nav.instrument_modal.open && !self.nav.fx_menu.open
        {
            if key.code == KeyCode::Char('u') && !key.modifiers.contains(KeyModifiers::SHIFT) {
                dbg::user("u → performing undo");
                self.perform_undo();
                return;
            }
            if key.code == KeyCode::Char('r') && key.modifiers.contains(KeyModifiers::CONTROL) {
                self.perform_redo();
                return;
            }
        }

        // Confirmation modal — y/n
        if self.nav.confirm_modal.open {
            match key.code {
                KeyCode::Char('y') | KeyCode::Char('Y') => {
                    let kind = self.nav.confirm_modal.kind;
                    self.nav.confirm_modal.close();
                    self.execute_delete(kind);
                }
                KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => {
                    self.nav.confirm_modal.close();
                }
                _ => {}
            }
            return;
        }

        // Input modal active — capture all keys for text entry
        if self.nav.input_modal.open {
            match key.code {
                KeyCode::Esc => {
                    self.nav.input_modal.close();
                }
                KeyCode::Enter => {
                    let path = self.nav.input_modal.value().to_string();
                    let kind = self.nav.input_modal.kind;
                    self.nav.input_modal.close();
                    if !path.is_empty() {
                        match kind {
                            InputModalKind::SaveAs => self.do_save(&path),
                            InputModalKind::Open => self.do_load(&path),
                        }
                    }
                }
                KeyCode::Backspace => { self.nav.input_modal.backspace(); }
                KeyCode::Delete => { self.nav.input_modal.delete(); }
                KeyCode::Left => { self.nav.input_modal.move_left(); }
                KeyCode::Right => { self.nav.input_modal.move_right(); }
                KeyCode::Home => { self.nav.input_modal.move_home(); }
                KeyCode::End => { self.nav.input_modal.move_end(); }
                KeyCode::Char(ch) => { self.nav.input_modal.type_char(ch); }
                _ => {}
            }
            return;
        }

        // Loop editor active — controls locked to loop markers
        // BUT Space passes through to open the space menu (so user can play/pause)
        if self.nav.loop_editor.active
            && key.code != KeyCode::Char(' ')
            && key.code != KeyCode::Tab
            && key.code != KeyCode::BackTab
        {
            let shift = key.modifiers.contains(KeyModifiers::SHIFT);
            match key.code {
                KeyCode::Esc => {
                    dbg::user("loop editor: Esc → unfocus");
                    self.nav.loop_editor.unfocus();
                }
                KeyCode::Enter => {
                    self.nav.loop_editor.toggle_enabled();
                    dbg::user(&format!("loop editor: Enter → enabled={}", self.nav.loop_editor.enabled));
                    self.sync_loop_to_transport();
                    self.log_transport_state();
                }
                KeyCode::Char('h') | KeyCode::Left => {
                    if shift {
                        dbg::user("loop editor: Shift+h → move end left");
                        self.nav.loop_editor.move_end_left();
                    } else {
                        dbg::user("loop editor: h → move start left");
                        self.nav.loop_editor.move_start_left();
                    }
                    dbg::system(&format!("loop range: {}", self.nav.loop_editor.display()));
                    self.sync_loop_to_transport();
                }
                KeyCode::Char('l') | KeyCode::Right => {
                    if shift {
                        dbg::user("loop editor: Shift+l → move end right");
                        self.nav.loop_editor.move_end_right();
                    } else {
                        dbg::user("loop editor: l → move start right");
                        self.nav.loop_editor.move_start_right();
                    }
                    dbg::system(&format!("loop range: {}", self.nav.loop_editor.display()));
                    self.sync_loop_to_transport();
                }
                KeyCode::Char('H') => {
                    dbg::user("loop editor: H → move end left");
                    self.nav.loop_editor.move_end_left();
                    dbg::system(&format!("loop range: {}", self.nav.loop_editor.display()));
                    self.sync_loop_to_transport();
                }
                KeyCode::Char('L') => {
                    dbg::user("loop editor: L → move end right");
                    self.nav.loop_editor.move_end_right();
                    dbg::system(&format!("loop range: {}", self.nav.loop_editor.display()));
                    self.sync_loop_to_transport();
                }
                _ => {
                    dbg::user(&format!("loop editor: ignored key {:?}", key.code));
                }
            }
            return;
        }

        // Instrument modal open
        if self.nav.instrument_modal.open {
            match key.code {
                KeyCode::Esc => {
                    dbg::user("instrument modal: Esc → close");
                    self.nav.escape();
                }
                KeyCode::Char('j') | KeyCode::Down => self.nav.move_down(),
                KeyCode::Char('k') | KeyCode::Up => self.nav.move_up(),
                KeyCode::Enter => {
                    let instrument = self.nav.instrument_modal.selected();
                    dbg::user(&format!("instrument modal: Enter → selected {:?}", instrument));
                    self.nav.instrument_modal.open = false;
                    self.create_instrument_track(instrument);
                }
                _ => {}
            }
            return;
        }

        // Space menu open
        if self.nav.space_menu.open {
            match key.code {
                KeyCode::Char(' ') | KeyCode::Esc => {
                    dbg::user("space menu: close");
                    self.nav.space_menu.open = false;
                }
                KeyCode::Char('j') | KeyCode::Down => self.nav.move_down(),
                KeyCode::Char('k') | KeyCode::Up => self.nav.move_up(),
                KeyCode::Tab => self.nav.space_menu.switch_section(),
                KeyCode::Enter => {
                    if let Some(action) = self.nav.enter() {
                        dbg::user(&format!("space menu: Enter → {:?}", action));
                        self.handle_space_action(action);
                    }
                }
                KeyCode::Char(ch) => {
                    dbg::user(&format!("space menu: '{ch}'"));
                    if let Some(action) = self.nav.space_menu_handle(ch) {
                        dbg::system(&format!("space action: {:?}", action));
                        self.handle_space_action(action);
                    }
                }
                _ => {}
            }
            return;
        }

        // Space → open space menu
        if key.code == KeyCode::Char(' ') {
            dbg::user("Space → open space menu");
            self.nav.toggle_space_menu();
            return;
        }

        // Tab — blocked while piano roll is in column/row editing mode
        match key.code {
            KeyCode::Tab if self.nav.focused_pane == Pane::ClipView
                && self.nav.clip_view.piano_roll.focus != PianoRollFocus::Navigation => {
                // Tab blocked in column/row mode — controls are locked
                return;
            }
            KeyCode::Tab if self.nav.focused_pane == Pane::ClipView => {
                dbg::user("Tab → cycle clip view tab");
                self.nav.cycle_tab();
                return;
            }
            KeyCode::Tab => {
                dbg::user("Tab → next pane");
                self.nav.focus_next_pane();
                return;
            }
            KeyCode::BackTab => {
                dbg::user("Shift+Tab → prev pane");
                self.nav.focus_pane(self.nav.focused_pane.prev());
                return;
            }
            _ => {}
        }

        // Global BPM adjustment (+/- always work)
        match key.code {
            KeyCode::Char('+') | KeyCode::Char('=') => {
                let bpm = self.engine.transport.tempo_bpm() + 1.0;
                self.engine.transport.set_tempo(bpm);
                dbg::system(&format!("bpm={:.0}", bpm));
                return;
            }
            KeyCode::Char('-') => {
                let bpm = (self.engine.transport.tempo_bpm() - 1.0).max(20.0);
                self.engine.transport.set_tempo(bpm);
                dbg::system(&format!("bpm={:.0}", bpm));
                return;
            }
            _ => {}
        }

        match self.nav.focused_pane {
            Pane::Transport => self.handle_transport_keys(key),
            Pane::Tracks => self.handle_tracks_keys(key),
            Pane::ClipView => self.handle_clip_view_keys(key),
        }
    }


    pub(crate) fn handle_transport_keys(&mut self, key: crossterm::event::KeyEvent) {
        use crate::debug_log as dbg;
        let tu = &mut self.nav.transport_ui;

        if tu.editing {
            // Controls locked to the current element
            match tu.element {
                TransportElement::Bpm => match key.code {
                    KeyCode::Char('l') | KeyCode::Right => {
                        let bpm = self.engine.transport.tempo_bpm() + 1.0;
                        self.engine.transport.set_tempo(bpm);
                        dbg::system(&format!("bpm={:.0}", bpm));
                    }
                    KeyCode::Char('h') | KeyCode::Left => {
                        let bpm = (self.engine.transport.tempo_bpm() - 1.0).max(20.0);
                        self.engine.transport.set_tempo(bpm);
                        dbg::system(&format!("bpm={:.0}", bpm));
                    }
                    KeyCode::Esc | KeyCode::Enter => {
                        dbg::user("transport: release BPM edit");
                        tu.editing = false;
                    }
                    _ => {}
                },
                TransportElement::Loop => {
                    // Delegate to loop editor
                    // Enter on loop when already editing → just unfocus
                    match key.code {
                        KeyCode::Esc | KeyCode::Enter => {
                            tu.editing = false;
                        }
                        _ => {}
                    }
                }
                _ => {
                    // Record and Metronome don't have editing mode, just release
                    match key.code {
                        KeyCode::Esc | KeyCode::Enter => { tu.editing = false; }
                        _ => {}
                    }
                }
            }
            return;
        }

        // Not editing — navigate between elements
        match key.code {
            KeyCode::Char('h') | KeyCode::Left => {
                tu.element = tu.element.move_left();
                dbg::user(&format!("transport: → {}", tu.element.label()));
            }
            KeyCode::Char('l') | KeyCode::Right => {
                tu.element = tu.element.move_right();
                dbg::user(&format!("transport: → {}", tu.element.label()));
            }
            KeyCode::Enter => {
                dbg::user(&format!("transport: Enter on {}", tu.element.label()));
                match tu.element {
                    TransportElement::Bpm => { tu.editing = true; }
                    TransportElement::Record => {
                        self.engine.transport.toggle_record();
                        dbg::system(&format!("recording={}", self.engine.transport.is_recording()));
                    }
                    TransportElement::Loop => {
                        self.nav.loop_editor.focus();
                    }
                    TransportElement::Metronome => {
                        self.engine.transport.toggle_metronome();
                        dbg::system(&format!("metronome={}", self.engine.transport.is_metronome_on()));
                    }
                }
            }
            KeyCode::Char('q') => { self.running = false; }
            KeyCode::Esc => { dbg::user("transport: Esc → deselect"); }
            _ => {}
        }
    }


    pub(crate) fn handle_tracks_keys(&mut self, key: crossterm::event::KeyEvent) {
        use crate::debug_log as dbg;

        match key.code {
            KeyCode::Char('q') if !self.nav.track_selected && !self.nav.fx_menu.open => {
                dbg::user("q → quit");
                self.running = false;
            }
            KeyCode::Esc => {
                dbg::user("Esc → back");
                self.nav.escape();
            }
            KeyCode::Char('j') | KeyCode::Down => {
                dbg::user(&format!("j/Down → move down (cursor was {})", self.nav.track_cursor));
                self.nav.move_down();
            }
            KeyCode::Char('k') | KeyCode::Up => {
                dbg::user(&format!("k/Up → move up (cursor was {})", self.nav.track_cursor));
                self.nav.move_up();
            }
            KeyCode::Char('h') | KeyCode::Left => self.nav.move_left(),
            KeyCode::Char('l') | KeyCode::Right => self.nav.move_right(),
            KeyCode::Enter => {
                dbg::user(&format!("Enter → select (track_selected={})", self.nav.track_selected));
                self.nav.enter();
            }
            KeyCode::Char('m') if !self.nav.fx_menu.open => {
                dbg::user("m → toggle mute");
                self.nav.toggle_mute();
            }
            KeyCode::Char('s') if !self.nav.fx_menu.open => {
                dbg::user("s → toggle solo");
                self.nav.toggle_solo();
            }
            KeyCode::Char('r') if !self.nav.fx_menu.open => {
                dbg::user("r → toggle arm");
                self.nav.toggle_arm();
            }
            KeyCode::Char('R') if !self.nav.fx_menu.open => {
                dbg::user("R → toggle loop record");
                self.toggle_loop_record();
                self.log_transport_state();
            }
            KeyCode::Char(ch @ '0'..='9') if self.nav.track_selected && !self.nav.fx_menu.open => {
                self.nav.digit_input(ch);
            }
            _ => {}
        }
    }


    pub(crate) fn handle_clip_view_keys(&mut self, key: crossterm::event::KeyEvent) {
        use crate::debug_log as dbg;
        use crate::state::PianoRollFocus;

        // If we're in the FX panel side, use the old synth/fx controls
        if self.nav.clip_view.focus == ClipViewFocus::FxPanel {
            match key.code {
                KeyCode::Esc | KeyCode::Char('q') => self.nav.escape(),
                KeyCode::Char('j') | KeyCode::Down => self.nav.move_down(),
                KeyCode::Char('k') | KeyCode::Up => self.nav.move_up(),
                KeyCode::Char('h') | KeyCode::Left => {
                    self.nav.move_left();
                    self.send_synth_param_update();
                }
                KeyCode::Char('l') | KeyCode::Right => {
                    self.nav.move_right();
                    self.send_synth_param_update();
                }
                _ => {}
            }
            return;
        }

        // Piano roll side — route by focus level
        // Read focus level and state before any mutable borrows
        let focus = self.nav.clip_view.piano_roll.focus;
        let col = self.nav.clip_view.piano_roll.column;
        let cursor_note = self.nav.clip_view.piano_roll.cursor_note;

        match focus {
            // Browsing: h/l navigates columns, Enter selects a column
            PianoRollFocus::Navigation => {
                let shift = key.modifiers.contains(KeyModifiers::SHIFT);
                match key.code {
                    KeyCode::Esc | KeyCode::Char('q') => {
                        // Clear any highlights first, otherwise escape
                        let pr = &self.nav.clip_view.piano_roll;
                        if pr.highlight_start.is_some() || pr.row_highlight_low.is_some() {
                            self.nav.clip_view.piano_roll.clear_all_highlights();
                        } else {
                            self.nav.escape();
                        }
                    }
                    KeyCode::Char('J') if shift => {
                        // Shift+j: start or expand row highlight downward
                        self.nav.clip_view.piano_roll.highlight_down();
                        dbg::user(&format!("piano roll: row highlight {:?}", self.nav.clip_view.piano_roll.row_highlight_range()));
                    }
                    KeyCode::Char('K') if shift => {
                        // Shift+k: start or expand row highlight upward
                        self.nav.clip_view.piano_roll.highlight_up();
                        dbg::user(&format!("piano roll: row highlight {:?}", self.nav.clip_view.piano_roll.row_highlight_range()));
                    }
                    KeyCode::Char('j') | KeyCode::Down => self.nav.clip_view.piano_roll.move_down(),
                    KeyCode::Char('k') | KeyCode::Up => self.nav.clip_view.piano_roll.move_up(),
                    KeyCode::Char('H') if shift => {
                        // Shift+h: start or expand highlight left
                        self.nav.clip_view.piano_roll.start_highlight();
                        self.nav.clip_view.piano_roll.highlight_left();
                        dbg::user(&format!("piano roll: highlight left, range {:?}", self.nav.clip_view.piano_roll.highlight_range()));
                    }
                    KeyCode::Char('L') if shift => {
                        // Shift+l: start or expand highlight right
                        self.nav.clip_view.piano_roll.start_highlight();
                        self.nav.clip_view.piano_roll.highlight_right();
                        dbg::user(&format!("piano roll: highlight right, range {:?}", self.nav.clip_view.piano_roll.highlight_range()));
                    }
                    KeyCode::Char('h') | KeyCode::Left => {
                        self.nav.clip_view.piano_roll.move_column_left();
                        dbg::user(&format!("piano roll: col {}", self.nav.clip_view.piano_roll.column_display()));
                    }
                    KeyCode::Char('l') | KeyCode::Right => {
                        self.nav.clip_view.piano_roll.move_column_right();
                        dbg::user(&format!("piano roll: col {}", self.nav.clip_view.piano_roll.column_display()));
                    }
                    KeyCode::Char('d') => {
                        // Delete notes in highlighted region (columns, rows, or both)
                        let col_range = self.nav.clip_view.piano_roll.highlight_range();
                        let row_range = self.nav.clip_view.piano_roll.row_highlight_range();
                        if col_range.is_some() || row_range.is_some() {
                            self.delete_selected_notes(col_range, row_range);
                            self.nav.clip_view.piano_roll.clear_all_highlights();
                            self.send_clip_update();
                            dbg::user("piano roll: deleted highlighted notes");
                        }
                    }
                    KeyCode::Char('y') => {
                        // Yank notes in highlighted region
                        let col_range = self.nav.clip_view.piano_roll.highlight_range();
                        let row_range = self.nav.clip_view.piano_roll.row_highlight_range();
                        if col_range.is_some() || row_range.is_some() {
                            self.yank_selected_notes(col_range, row_range);
                            self.nav.clip_view.piano_roll.clear_all_highlights();
                            dbg::user("piano roll: yanked highlighted notes");
                        }
                    }
                    KeyCode::Char('p') => {
                        // Paste yanked notes at highlighted position or cursor
                        let col_start = self.nav.clip_view.piano_roll.highlight_range()
                            .map(|(s, _)| s)
                            .unwrap_or(self.nav.clip_view.piano_roll.column);

                        // Row offset: shift yanked notes so the highest yanked note
                        // lands on the highest highlighted row (or cursor note)
                        let yank_buf = &self.nav.clip_view.piano_roll.yank_buffer;
                        let yank_max = yank_buf.iter().map(|n| n.note).max().unwrap_or(60);
                        let target_note = self.nav.clip_view.piano_roll.row_highlight_range()
                            .map(|(_, hi)| hi)
                            .unwrap_or(self.nav.clip_view.piano_roll.cursor_note);
                        let row_offset = Some(target_note as i16 - yank_max as i16);

                        self.paste_selected_notes(col_start, row_offset);
                        self.nav.clip_view.piano_roll.clear_all_highlights();
                        self.send_clip_update();
                        dbg::user(&format!("piano roll: pasted notes (shift={})", row_offset.unwrap_or(0)));
                    }
                    KeyCode::Enter => {
                        let indices = self.note_indices_in_column(col);
                        dbg::user(&format!("piano roll: Enter → column {} selected ({} notes)", self.nav.clip_view.piano_roll.column_display(), indices.len()));
                        self.nav.clip_view.piano_roll.enter(indices);
                    }
                    KeyCode::Char(ch @ '0'..='9') => {
                        if self.nav.clip_view.piano_roll.type_digit(ch) {
                            dbg::user(&format!("piano roll: jump to col {}", self.nav.clip_view.piano_roll.column_display()));
                        }
                    }
                    _ => {}
                }
            }

            // Column selected (Right Left Trick):
            //   h/l = adjust LEFT edge of ALL notes in column
            //   H/L = adjust RIGHT edge of ALL notes in column
            //   j/k = go deeper → individual note (Row mode)
            //   Esc = back to Browsing
            PianoRollFocus::Selected => {
                let shift = key.modifiers.contains(KeyModifiers::SHIFT);
                match key.code {
                    KeyCode::Esc => {
                        dbg::user("piano roll: Esc → browsing");
                        self.nav.clip_view.piano_roll.escape();
                    }
                    KeyCode::Char('h') | KeyCode::Left if !shift => {
                        self.adjust_column_edges(-0.01, false);
                        self.send_clip_update();
                        dbg::user("piano roll: col left \u{2190}");
                    }
                    KeyCode::Char('l') | KeyCode::Right if !shift => {
                        self.adjust_column_edges(0.01, false);
                        self.send_clip_update();
                        dbg::user("piano roll: col left \u{2192}");
                    }
                    KeyCode::Char('H') | KeyCode::Char('h') | KeyCode::Left => {
                        self.adjust_column_edges(-0.01, true);
                        self.send_clip_update();
                        dbg::user("piano roll: col right \u{2190}");
                    }
                    KeyCode::Char('L') | KeyCode::Char('l') | KeyCode::Right => {
                        self.adjust_column_edges(0.01, true);
                        self.send_clip_update();
                        dbg::user("piano roll: col right \u{2192}");
                    }
                    KeyCode::Char('j') | KeyCode::Down => {
                        // Enter row mode starting at the top of the visible area
                        let pr = &mut self.nav.clip_view.piano_roll;
                        let top = pr.view_bottom_note.saturating_add(pr.view_height).saturating_sub(1);
                        pr.cursor_note = top.min(127);
                        pr.enter_row();
                        dbg::user(&format!("piano roll: → row at note {}", pr.cursor_note));
                    }
                    KeyCode::Char('k') | KeyCode::Up => {
                        let pr = &mut self.nav.clip_view.piano_roll;
                        let top = pr.view_bottom_note.saturating_add(pr.view_height).saturating_sub(1);
                        pr.cursor_note = top.min(127);
                        pr.enter_row();
                        dbg::user(&format!("piano roll: → row at note {}", pr.cursor_note));
                    }
                    _ => {}
                }
            }

            // Row selected (Right Left Trick on single note):
            //   h/l = adjust LEFT edge of this note
            //   H/L = adjust RIGHT edge of this note
            //   j/k = move to next/prev note in column
            //   Esc = back to Column (column-level control restored)
            PianoRollFocus::Row => {
                let shift = key.modifiers.contains(KeyModifiers::SHIFT);
                match key.code {
                    KeyCode::Esc => {
                        dbg::user("piano roll: Esc → column mode");
                        self.nav.clip_view.piano_roll.escape();
                    }
                    KeyCode::Char('h') | KeyCode::Left if !shift => {
                        self.adjust_note_edge(col, cursor_note, -0.01, false);
                        self.send_clip_update();
                        dbg::user("piano roll: note left \u{2190}");
                    }
                    KeyCode::Char('l') | KeyCode::Right if !shift => {
                        self.adjust_note_edge(col, cursor_note, 0.01, false);
                        self.send_clip_update();
                        dbg::user("piano roll: note left \u{2192}");
                    }
                    KeyCode::Char('H') | KeyCode::Char('h') | KeyCode::Left => {
                        self.adjust_note_edge(col, cursor_note, -0.01, true);
                        self.send_clip_update();
                        dbg::user("piano roll: note right \u{2190}");
                    }
                    KeyCode::Char('L') | KeyCode::Char('l') | KeyCode::Right => {
                        self.adjust_note_edge(col, cursor_note, 0.01, true);
                        self.send_clip_update();
                        dbg::user("piano roll: note right \u{2192}");
                    }
                    KeyCode::Char('j') | KeyCode::Down => {
                        self.nav.clip_view.piano_roll.move_down();
                    }
                    KeyCode::Char('k') | KeyCode::Up => {
                        self.nav.clip_view.piano_roll.move_up();
                    }
                    KeyCode::Char('n') => {
                        self.draw_note(col, cursor_note);
                        self.send_clip_update();
                        dbg::user(&format!("piano roll: draw note {} at col {}", cursor_note, col + 1));
                    }
                    _ => {}
                }
            }
        }
    }
}