huddle 0.7.0

Decentralized, terminal-native chat rooms — LAN mDNS or direct dial, Megolm-encrypted.
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
615
616
617
618
619
620
621
622
623
624
625
626
627
628
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};

use crate::app::{Modal, Pane, SidebarFocus, SidebarItem, TuiApp};

#[derive(Debug)]
pub enum Action {
    Nothing,
    Quit,
    OpenQuitConfirm,
    CloseModal,
    OpenStartRoom,
    OpenHelp,
    // Lobby / Sidebar (huddle 0.7 — names retained for back-compat;
    // semantics rewritten to drive the new sidebar widget).
    LobbyNavigateUp,
    LobbyNavigateDown,
    LobbyJoinSelected,
    LobbyRefresh,
    LobbyFocusToggle,
    LobbyReconnectPeer,
    LobbyForgetPeer,
    OpenDialPeer,
    // huddle 0.7: sidebar-specific helpers
    SidebarSectionPrev,
    SidebarToggleExpand,
    JumpToPeoplePane,
    JumpToSettingsPane,
    OpenComposeDm,
    ComposeDmTypeChar(char),
    ComposeDmBackspace,
    ComposeDmConfirm,
    ComposeDmCancel,
    ToggleMemberMargin,
    PeopleFocusNext,
    PeoplePersonReconnect,
    PeoplePersonBlock,
    PeoplePersonUnblock,
    PeoplePersonForget,
    PeoplePersonStartDm,
    // Start room modal
    StartRoomNextField,
    StartRoomToggleEncrypted,
    StartRoomTypeChar(char),
    StartRoomBackspace,
    StartRoomConfirm,
    // Join room modal
    JoinRoomTypeChar(char),
    JoinRoomBackspace,
    JoinRoomConfirm,
    // Dial peer modal
    DialPeerTypeChar(char),
    DialPeerBackspace,
    DialPeerConfirm,
    // In-room
    TabNext,
    TabPrev,
    TabSelect(usize),
    BackToLobby,
    LeaveRoom,
    FocusInput,
    BlurInput,
    ScrollUp,
    ScrollDown,
    PageUp,
    PageDown,
    JumpTop,
    JumpBottom,
    ChatTypeChar(char),
    ChatBackspace,
    ChatSend,
    ChatInsertNewline,
    // File cards
    ToggleCardFocus,
    CardNext,
    CardPrev,
    ActivateFocusedCard,
    OpenFocusedCard,
    CancelFocusedCard,
    SaveAgainFocusedCard,
    OpenAttachmentPicker,
    // Attach picker modal
    AttachPickerUp,
    AttachPickerDown,
    AttachPickerAscend,
    AttachPickerDescendOrPick,
    // Rotation
    OpenRotateRoom,
    RotateRoomTypeChar(char),
    RotateRoomBackspace,
    RotateRoomConfirm,
    AcceptRotationTypeChar(char),
    AcceptRotationBackspace,
    AcceptRotationConfirm,
    // Verify modal
    OpenVerify,
    VerifyNext,
    VerifyPrev,
    VerifyToggle,
    // Mute
    ToggleMute,
    // QR identity
    OpenQrIdentity,
    // Search
    OpenSearch,
    SearchTypeChar(char),
    SearchBackspace,
    SearchSubmit,
    SearchNext,
    SearchPrev,
    // Phase A: inbound-dial accept gate
    InboundDialAccept,
    InboundDialReject,
    InboundDialTrust,
    // Phase B: soft owner role
    OpenKickPicker,
    OpenGrantPicker,
    MemberActionNext,
    MemberActionPrev,
    MemberActionConfirm,
    /// Phase B follow-up: list bans for the current room (owners only).
    /// Bound to `^B` in the room view; renders an Info modal so it
    /// dismisses on any key.
    ShowRoomBans,
    /// Phase A follow-up: clear every globally-blocked peer. Bound to
    /// `c` in the Settings modal. Sledgehammer for now — finer-grained
    /// per-peer unblock is a future refinement.
    ClearBlockedPeers,
    // Phase G: SAS verification
    VerifyStartSas,
    SasMatch,
    SasCancel,
    // Phase E: verified-only-mode toggles
    OpenSettings,
    SettingsToggleGlobalVerifiedOnly,
    ToggleRoomVerifiedOnly,
    // huddle 0.5: optional self-declared username
    OpenEditUsername,
    EditUsernameTypeChar(char),
    EditUsernameBackspace,
    EditUsernameConfirm,
    // huddle 0.5: go-dark account deletion flow
    OpenGoDarkModal,
    GoDarkNextField,
    GoDarkTypeChar(char),
    GoDarkBackspace,
    GoDarkConfirm,
    // huddle 0.5.1: add friend by HD ID or username
    OpenAddFriend,
    AddFriendTypeChar(char),
    AddFriendBackspace,
    AddFriendConfirm,
    // Phase F: short-lived join codes
    OpenGenerateJoinCode,
    OpenJoinWithCode,
    JoinWithCodeTypeChar(char),
    JoinWithCodeBackspace,
    JoinWithCodeConfirm,
    // Phase C: invite links
    GenerateInvite,
    OpenPasteInvite,
    PasteInviteTypeChar(char),
    PasteInviteBackspace,
    PasteInviteConfirm,
    ConfirmInviteAccept,
    // Phase H: first-launch onboarding card
    OnboardingNext,
    OnboardingPrev,
    OnboardingDismiss,
    // huddle 0.6: notification history overlay (Ctrl+H)
    OpenStatusHistory,
    StatusHistoryScrollUp,
    StatusHistoryScrollDown,
    StatusHistoryPageUp,
    StatusHistoryPageDown,
    ClearStatusHistory,
    // huddle 0.6: command palette (Ctrl+P) — fuzzy action search
    OpenCommandPalette,
    CommandPaletteTypeChar(char),
    CommandPaletteBackspace,
    CommandPaletteNext,
    CommandPalettePrev,
    CommandPaletteConfirm,
    // huddle 0.6: re-open onboarding / "what's new" card (Shift+?)
    OpenWhatsNew,
    // huddle 0.6: mark every room's unread counter back to 0 (R in lobby)
    MarkAllRead,
    // huddle 0.6: help screen scroll
    HelpScrollUp,
    HelpScrollDown,
    HelpPageUp,
    HelpPageDown,
    // huddle 0.6: update-check opt-in
    UpdateCheckOptInYes,
    UpdateCheckOptInNo,
    ToggleUpdateCheck,
    DismissUpdateBanner,
}

pub fn map_key(key: KeyEvent, app: &TuiApp) -> Action {
    if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
        return Action::OpenQuitConfirm;
    }

    // huddle 0.6: global hotkeys that fire regardless of current
    // screen, as long as no modal is open (modal-internal Ctrl chords
    // would conflict otherwise). Ctrl+P = command palette,
    // Ctrl+H = status history, Shift+? = re-open onboarding.
    if matches!(app.modal, Modal::None) {
        if key.modifiers.contains(KeyModifiers::CONTROL) {
            // Ctrl+H — note: crossterm sometimes delivers this as
            // KeyCode::Backspace on certain terminals because of the
            // ASCII collision. We accept both.
            if matches!(key.code, KeyCode::Char('h') | KeyCode::Char('H')) {
                let input_active = app
                    .active_room()
                    .map(|r| r.input_active)
                    .unwrap_or(false);
                // Suppress when the user is typing — Ctrl+H = Backspace
                // on POSIX terminals and we don't want to eat it.
                if !input_active {
                    return Action::OpenStatusHistory;
                }
            }
            if matches!(key.code, KeyCode::Char('p') | KeyCode::Char('P')) {
                // Ctrl+P is bound to TabPrev inside a room; only override
                // when room input is NOT active so the palette is reachable.
                let input_active = app
                    .active_room()
                    .map(|r| r.input_active)
                    .unwrap_or(false);
                let in_room = matches!(app.pane, Pane::Dm(_) | Pane::Group(_));
                if !(in_room && input_active) {
                    return Action::OpenCommandPalette;
                }
            }
        }
    }

    match &app.modal {
        Modal::QuitConfirm => match key.code {
            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => Action::Quit,
            _ => Action::CloseModal,
        },
        Modal::Error(_) => match key.code {
            _ => Action::CloseModal,
        },
        Modal::Help => match key.code {
            KeyCode::Char('j') | KeyCode::Down => Action::HelpScrollDown,
            KeyCode::Char('k') | KeyCode::Up => Action::HelpScrollUp,
            KeyCode::PageDown => Action::HelpPageDown,
            KeyCode::PageUp => Action::HelpPageUp,
            _ => Action::CloseModal,
        },
        Modal::StartRoom(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Tab => Action::StartRoomNextField,
            KeyCode::Enter => {
                // On Encrypted field: Enter toggles; otherwise confirms.
                if let Modal::StartRoom(s) = &app.modal {
                    if matches!(s.focus, crate::app::StartField::Encrypted) {
                        return Action::StartRoomToggleEncrypted;
                    }
                }
                Action::StartRoomConfirm
            }
            KeyCode::Char(' ') => {
                if let Modal::StartRoom(s) = &app.modal {
                    if matches!(s.focus, crate::app::StartField::Encrypted) {
                        return Action::StartRoomToggleEncrypted;
                    }
                }
                Action::StartRoomTypeChar(' ')
            }
            KeyCode::Backspace => Action::StartRoomBackspace,
            KeyCode::Char(c) => Action::StartRoomTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::JoinRoom(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::JoinRoomConfirm,
            KeyCode::Backspace => Action::JoinRoomBackspace,
            KeyCode::Char(c) => Action::JoinRoomTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::DialPeer(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::DialPeerConfirm,
            KeyCode::Backspace => Action::DialPeerBackspace,
            KeyCode::Char(c) => Action::DialPeerTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::AttachPicker(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Char('j') | KeyCode::Down => Action::AttachPickerDown,
            KeyCode::Char('k') | KeyCode::Up => Action::AttachPickerUp,
            KeyCode::Char('h') | KeyCode::Backspace | KeyCode::Left => Action::AttachPickerAscend,
            KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => Action::AttachPickerDescendOrPick,
            _ => Action::Nothing,
        },
        Modal::RotateRoom(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::RotateRoomConfirm,
            KeyCode::Backspace => Action::RotateRoomBackspace,
            KeyCode::Char(c) => Action::RotateRoomTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::AcceptRotation(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::AcceptRotationConfirm,
            KeyCode::Backspace => Action::AcceptRotationBackspace,
            KeyCode::Char(c) => Action::AcceptRotationTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::Verify(_) => match key.code {
            KeyCode::Esc | KeyCode::Char('q') => Action::CloseModal,
            KeyCode::Char('j') | KeyCode::Down => Action::VerifyNext,
            KeyCode::Char('k') | KeyCode::Up => Action::VerifyPrev,
            KeyCode::Enter | KeyCode::Char(' ') => Action::VerifyToggle,
            KeyCode::Char('s') => Action::VerifyStartSas,
            _ => Action::Nothing,
        },
        Modal::Sas(_) => match key.code {
            KeyCode::Esc | KeyCode::Char('c') | KeyCode::Char('q') => Action::SasCancel,
            KeyCode::Char('m') | KeyCode::Enter => Action::SasMatch,
            _ => Action::Nothing,
        },
        Modal::Settings(_) => match key.code {
            KeyCode::Esc | KeyCode::Char('q') => Action::CloseModal,
            KeyCode::Char('v') | KeyCode::Enter | KeyCode::Char(' ') => {
                Action::SettingsToggleGlobalVerifiedOnly
            }
            KeyCode::Char('c') => Action::ClearBlockedPeers,
            KeyCode::Char('u') => Action::OpenEditUsername,
            // huddle 0.6: capital U = toggle update check; lowercase u
            // stays "edit username". The case split keeps both reachable
            // and matches the convention from R (mark all read) in lobby.
            KeyCode::Char('U') => Action::ToggleUpdateCheck,
            // huddle 0.6: W = replay onboarding from inside Settings.
            KeyCode::Char('w') | KeyCode::Char('W') => Action::OpenWhatsNew,
            KeyCode::Char('!') => Action::OpenGoDarkModal,
            _ => Action::Nothing,
        },
        Modal::EditUsername(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::EditUsernameConfirm,
            KeyCode::Backspace => Action::EditUsernameBackspace,
            KeyCode::Char(c) => Action::EditUsernameTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::GoDark(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Tab => Action::GoDarkNextField,
            KeyCode::Enter => Action::GoDarkConfirm,
            KeyCode::Backspace => Action::GoDarkBackspace,
            KeyCode::Char(c) => Action::GoDarkTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::AddFriend(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::AddFriendConfirm,
            KeyCode::Backspace => Action::AddFriendBackspace,
            KeyCode::Char(c) => Action::AddFriendTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::ShowJoinCode(_) => match key.code {
            _ => Action::CloseModal,
        },
        Modal::JoinWithCode(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::JoinWithCodeConfirm,
            KeyCode::Backspace => Action::JoinWithCodeBackspace,
            KeyCode::Char(c) => Action::JoinWithCodeTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::ShowInvite(_) => match key.code {
            _ => Action::CloseModal,
        },
        Modal::PasteInvite(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::PasteInviteConfirm,
            KeyCode::Backspace => Action::PasteInviteBackspace,
            KeyCode::Char(c) => Action::PasteInviteTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::ConfirmInvite(_) => match key.code {
            KeyCode::Esc | KeyCode::Char('c') | KeyCode::Char('C') => Action::CloseModal,
            KeyCode::Enter | KeyCode::Char('d') | KeyCode::Char('D') => {
                Action::ConfirmInviteAccept
            }
            _ => Action::Nothing,
        },
        Modal::Onboarding { .. } => match key.code {
            // Esc dismisses early; the user can re-read it via Shift+?
            // or from Settings. We still bump last_seen so it doesn't
            // re-pop next launch on this version.
            KeyCode::Esc | KeyCode::Char('q') => Action::OnboardingDismiss,
            KeyCode::Char('h') | KeyCode::Left | KeyCode::Backspace => Action::OnboardingPrev,
            KeyCode::Enter
            | KeyCode::Char(' ')
            | KeyCode::Char('l')
            | KeyCode::Right
            | KeyCode::Tab => Action::OnboardingNext,
            _ => Action::Nothing,
        },
        Modal::StatusHistory { .. } => match key.code {
            KeyCode::Esc | KeyCode::Char('q') => Action::CloseModal,
            KeyCode::Char('j') | KeyCode::Down => Action::StatusHistoryScrollDown,
            KeyCode::Char('k') | KeyCode::Up => Action::StatusHistoryScrollUp,
            KeyCode::PageDown => Action::StatusHistoryPageDown,
            KeyCode::PageUp => Action::StatusHistoryPageUp,
            KeyCode::Char('c') | KeyCode::Char('C') => Action::ClearStatusHistory,
            KeyCode::Char('G') | KeyCode::End => Action::StatusHistoryPageDown,
            KeyCode::Char('g') | KeyCode::Home => Action::StatusHistoryPageUp,
            _ => Action::Nothing,
        },
        Modal::CommandPalette(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::CommandPaletteConfirm,
            KeyCode::Down => Action::CommandPaletteNext,
            KeyCode::Up => Action::CommandPalettePrev,
            KeyCode::Backspace => Action::CommandPaletteBackspace,
            KeyCode::Char(c) => Action::CommandPaletteTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::UpdateCheckOptIn => match key.code {
            KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Enter => {
                Action::UpdateCheckOptInYes
            }
            KeyCode::Char('n') | KeyCode::Char('N') | KeyCode::Esc => Action::UpdateCheckOptInNo,
            _ => Action::Nothing,
        },
        Modal::Search(_) => match key.code {
            KeyCode::Esc => Action::CloseModal,
            KeyCode::Enter => Action::SearchSubmit,
            KeyCode::Down => Action::SearchNext,
            KeyCode::Up => Action::SearchPrev,
            KeyCode::Backspace => Action::SearchBackspace,
            KeyCode::Char(c) => Action::SearchTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::Info(_) => match key.code {
            _ => Action::CloseModal,
        },
        Modal::QrIdentity => match key.code {
            _ => Action::CloseModal,
        },
        Modal::InboundDial(_) => match key.code {
            // Esc = reject. Anything more permissive would defeat the
            // gate — we always require a positive decision.
            KeyCode::Esc | KeyCode::Char('r') | KeyCode::Char('R') => Action::InboundDialReject,
            KeyCode::Char('a') | KeyCode::Char('A') | KeyCode::Enter => Action::InboundDialAccept,
            KeyCode::Char('t') | KeyCode::Char('T') => Action::InboundDialTrust,
            _ => Action::Nothing,
        },
        Modal::MemberAction(_) => match key.code {
            KeyCode::Esc | KeyCode::Char('q') => Action::CloseModal,
            KeyCode::Char('j') | KeyCode::Down => Action::MemberActionNext,
            KeyCode::Char('k') | KeyCode::Up => Action::MemberActionPrev,
            KeyCode::Enter => Action::MemberActionConfirm,
            _ => Action::Nothing,
        },
        Modal::ComposeDm(_) => match key.code {
            KeyCode::Esc => Action::ComposeDmCancel,
            KeyCode::Enter => Action::ComposeDmConfirm,
            KeyCode::Backspace => Action::ComposeDmBackspace,
            KeyCode::Char(c) => Action::ComposeDmTypeChar(c),
            _ => Action::Nothing,
        },
        Modal::None => map_normal(key, app),
    }
}

fn map_normal(key: KeyEvent, app: &TuiApp) -> Action {
    // huddle 0.7: when the sidebar has focus OR we're on a non-chat pane,
    // we use sidebar navigation. Chat panes (DM/Group) with `pane` focus
    // route through `map_in_room`.
    let sidebar_has_focus = matches!(app.sidebar.focus, SidebarFocus::Sidebar);
    let chat_pane = matches!(app.pane, Pane::Dm(_) | Pane::Group(_));
    if !chat_pane || sidebar_has_focus {
        map_sidebar(key, app)
    } else {
        map_in_room(key, app)
    }
}

fn map_sidebar(key: KeyEvent, app: &TuiApp) -> Action {
    // Cross-pane shortcuts first — these work anywhere the sidebar has
    // focus, including from People/Activity/Settings panes.
    match key.code {
        KeyCode::Char('q') => return Action::OpenQuitConfirm,
        KeyCode::Char('s') | KeyCode::Char('g') => return Action::OpenStartRoom,
        KeyCode::Char('m') => return Action::OpenComposeDm,
        KeyCode::Char('?') => return Action::OpenHelp,
        KeyCode::Char(':') => return Action::OpenCommandPalette,
        KeyCode::Char('a') => return Action::OpenAddFriend,
        KeyCode::Char('d') => return Action::OpenDialPeer,
        KeyCode::Char('i') => return Action::OpenQrIdentity,
        KeyCode::Char(',') => return Action::JumpToSettingsPane,
        KeyCode::Char('p') => return Action::JumpToPeoplePane,
        KeyCode::Char('c') => return Action::OpenJoinWithCode,
        KeyCode::Char('I') => return Action::GenerateInvite,
        KeyCode::Char('v') => return Action::OpenPasteInvite,
        KeyCode::Char('R') => return Action::MarkAllRead,
        _ => {}
    }
    match key.code {
        KeyCode::Tab => Action::LobbyFocusToggle,
        KeyCode::BackTab => Action::SidebarSectionPrev,
        KeyCode::Char('j') | KeyCode::Down => Action::LobbyNavigateDown,
        KeyCode::Char('k') | KeyCode::Up => Action::LobbyNavigateUp,
        KeyCode::Char(' ') | KeyCode::Right | KeyCode::Left => Action::SidebarToggleExpand,
        KeyCode::Char('r') => {
            // Context-sensitive: on a Person, reconnect. On a section
            // header or any item, refresh discovered rooms + peers.
            match &app.sidebar.selection {
                SidebarItem::Person(_) => Action::LobbyReconnectPeer,
                _ => Action::LobbyRefresh,
            }
        }
        KeyCode::Char('x') => match &app.sidebar.selection {
            SidebarItem::Person(_) => Action::LobbyForgetPeer,
            _ => Action::Nothing,
        },
        KeyCode::Enter => Action::LobbyJoinSelected,
        KeyCode::Esc => Action::Nothing,
        _ => Action::Nothing,
    }
}

fn map_in_room(key: KeyEvent, app: &TuiApp) -> Action {
    let input_active = app.active_room().map(|r| r.input_active).unwrap_or(false);

    let card_focus = app.active_room().map(|r| r.card_focus).unwrap_or(false);

    // Ctrl chords first (apply regardless of input focus).
    if key.modifiers.contains(KeyModifiers::CONTROL) {
        return match key.code {
            // ^J inserts a newline in the input (the canonical
            // multiline trick; works on every terminal).
            KeyCode::Char('j') if input_active => Action::ChatInsertNewline,
            KeyCode::Char('l') => Action::LeaveRoom,
            KeyCode::Char('b') => Action::BackToLobby,
            KeyCode::Char('n') => Action::TabNext,
            KeyCode::Char('p') => Action::TabPrev,
            KeyCode::Char('a') if !input_active => Action::OpenAttachmentPicker,
            KeyCode::Char('r') if !input_active => Action::OpenRotateRoom,
            KeyCode::Char('v') if !input_active => Action::OpenVerify,
            KeyCode::Char('f') if !input_active => Action::OpenSearch,
            KeyCode::Char('m') if !input_active => Action::ToggleMute,
            KeyCode::Char('k') if !input_active => Action::OpenKickPicker,
            KeyCode::Char('g') if !input_active => Action::OpenGrantPicker,
            KeyCode::Char('o') if !input_active => Action::ToggleRoomVerifiedOnly,
            KeyCode::Char('j') if !input_active => Action::OpenGenerateJoinCode,
            KeyCode::Char('I') if !input_active => Action::GenerateInvite,
            _ => Action::Nothing,
        };
    }

    if key.code == KeyCode::Tab {
        return Action::TabNext;
    }

    // Numeric tab jump (only if input is not active).
    if !input_active {
        if let KeyCode::Char(c @ '1'..='9') = key.code {
            let n = (c as u8 - b'1') as usize;
            return Action::TabSelect(n);
        }
    }

    if input_active {
        // Alt+Enter (Option+Enter on macOS) and Shift+Enter both insert
        // a newline. Plain Enter sends.
        if matches!(key.code, KeyCode::Enter)
            && (key.modifiers.contains(KeyModifiers::ALT)
                || key.modifiers.contains(KeyModifiers::SHIFT))
        {
            return Action::ChatInsertNewline;
        }
        match key.code {
            KeyCode::Enter => Action::ChatSend,
            // Some terminals deliver Option+Enter as a literal LF char.
            KeyCode::Char('\n') => Action::ChatInsertNewline,
            KeyCode::Esc => Action::BlurInput,
            KeyCode::Backspace => Action::ChatBackspace,
            KeyCode::PageUp => Action::PageUp,
            KeyCode::PageDown => Action::PageDown,
            KeyCode::Char(c) => Action::ChatTypeChar(c),
            _ => Action::Nothing,
        }
    } else if card_focus {
        // Card-focus keystrokes when input is blurred and the user has
        // entered card navigation mode (via `f`). Esc / `f` exit.
        match key.code {
            KeyCode::Esc | KeyCode::Char('f') => Action::ToggleCardFocus,
            KeyCode::Char('j') | KeyCode::Down => Action::CardNext,
            KeyCode::Char('k') | KeyCode::Up => Action::CardPrev,
            KeyCode::Enter => Action::ActivateFocusedCard,
            KeyCode::Char('o') => Action::OpenFocusedCard,
            KeyCode::Char('c') => Action::CancelFocusedCard,
            KeyCode::Char('s') => Action::SaveAgainFocusedCard,
            KeyCode::Char('r') => Action::ActivateFocusedCard,
            _ => Action::Nothing,
        }
    } else {
        match key.code {
            KeyCode::Char('q') => Action::OpenQuitConfirm,
            KeyCode::Char('/') => Action::FocusInput,
            KeyCode::Char('?') => Action::OpenHelp,
            // huddle 0.6: vim-style command palette also reachable
            // from in-room chat mode.
            KeyCode::Char(':') => Action::OpenCommandPalette,
            KeyCode::Char('f') => Action::ToggleCardFocus,
            KeyCode::Char('j') | KeyCode::Down => Action::ScrollDown,
            KeyCode::Char('k') | KeyCode::Up => Action::ScrollUp,
            KeyCode::PageDown => Action::PageDown,
            KeyCode::PageUp => Action::PageUp,
            KeyCode::Home | KeyCode::Char('g') => Action::JumpTop,
            KeyCode::End | KeyCode::Char('G') => Action::JumpBottom,
            // Shift+b — owners-only view of bans for the current room.
            // Distinct from ^B (BackToLobby) since terminals collapse
            // Ctrl+Shift+b → Ctrl+b, making the Ctrl-chord ambiguous.
            KeyCode::Char('B') => Action::ShowRoomBans,
            KeyCode::Esc => Action::BackToLobby,
            _ => Action::Nothing,
        }
    }
}