bohay 0.8.3

Next-Gen Agents multiplexer
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
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
//! The Settings modal — transient UI state plus open/close, key & click
//! handling, and the per-tab apply logic that mutates `App.config`, applies the
//! change live, and persists it. See docs/15.

use super::*;
use crate::config;
use crate::ui::theme;

#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum SettingsTab {
    Theme,
    Layout,
    Notifications,
    Keys,
    Modules,
    Integrations,
    Language,
}

impl SettingsTab {
    pub const ALL: [SettingsTab; 7] = [
        SettingsTab::Theme,
        SettingsTab::Layout,
        SettingsTab::Notifications,
        SettingsTab::Keys,
        SettingsTab::Modules,
        SettingsTab::Integrations,
        SettingsTab::Language,
    ];

    pub fn icon(self) -> &'static str {
        match self {
            SettingsTab::Theme => "",
            SettingsTab::Layout => "",
            SettingsTab::Notifications => "",
            SettingsTab::Keys => "",
            SettingsTab::Modules => "",
            SettingsTab::Integrations => "",
            SettingsTab::Language => "",
        }
    }

    /// The tab label in the active UI language (docs/21).
    pub fn label(self, cat: &crate::i18n::Catalog) -> &'static str {
        match self {
            SettingsTab::Theme => cat.tab_theme,
            SettingsTab::Layout => cat.tab_layout,
            SettingsTab::Notifications => cat.tab_notify,
            SettingsTab::Keys => cat.tab_keys,
            SettingsTab::Modules => cat.tab_modules,
            SettingsTab::Integrations => cat.tab_agents,
            SettingsTab::Language => cat.tab_language,
        }
    }

    fn index(self) -> usize {
        Self::ALL.iter().position(|t| *t == self).unwrap_or(0)
    }

    fn from_index(i: usize) -> SettingsTab {
        Self::ALL[i % Self::ALL.len()]
    }
}

/// Transient state of the open Settings modal.
pub struct SettingsUi {
    pub tab: SettingsTab,
    pub cursor: usize,
    /// In the Keys tab: capturing the next key press to rebind `cursor`'s command.
    pub capturing: bool,
}

/// A selectable row in the Layout tab (docs/15 + docs/29). The pane-layout rows
/// come first, then a `── Docks ──` divider, then the sidebar + dock controls.
/// `Dock` rows carry `[Left] [Right]` place buttons.
#[derive(Clone)]
pub enum LayoutRow {
    SidebarWidth,
    ColGap,
    RowGap,
    Scrollback,
    PaneTitles,
    ResumeWs,
    #[cfg(windows)]
    Shell,
    LeftVisible,
    RightVisible,
    RightWidth,
    Dock(DockKind),
}

/// A selectable row in the Modules tab (docs/13 §3.6): a module, or one of the
/// settings it declares (indented beneath it while the module is enabled).
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
pub enum ModuleRow {
    Module(usize),
    Setting(usize, usize),
}

/// Cap a module setting's typed value, so a pathological paste can't bloat the
/// module's `settings.json`.
const MODULE_SETTING_MAX: usize = 512;

impl App {
    /// The Layout tab's ordered selectable rows (docs/29). The first index of the
    /// dock section (used to draw the `── Docks ──` divider) is `dock_section_start`.
    pub fn layout_rows(&self) -> Vec<LayoutRow> {
        let mut v = vec![
            LayoutRow::SidebarWidth,
            LayoutRow::ColGap,
            LayoutRow::RowGap,
            LayoutRow::Scrollback,
            LayoutRow::PaneTitles,
            LayoutRow::ResumeWs,
        ];
        #[cfg(windows)]
        v.push(LayoutRow::Shell);
        v.push(LayoutRow::LeftVisible);
        v.push(LayoutRow::RightVisible);
        v.push(LayoutRow::RightWidth);
        for k in self.available_docks() {
            v.push(LayoutRow::Dock(k));
        }
        v
    }

    /// Index of the first dock-section row (where the `── Docks ──` divider goes).
    pub fn dock_section_start(&self) -> usize {
        // Keep in step with `layout_rows`: the pane-layout rows before the docks
        // section (sidebar width, gaps, scrollback, titles, resume, +shell).
        #[cfg(windows)]
        {
            7
        }
        #[cfg(not(windows))]
        {
            6
        }
    }

    pub fn open_settings(&mut self) {
        let cursor = theme_cursor(&self.config.theme);
        self.settings = Some(SettingsUi {
            tab: SettingsTab::Theme,
            cursor,
            capturing: false,
        });
    }

    pub fn close_settings(&mut self) {
        self.settings = None;
        self.module_setting_edit = None;
    }

    /// Number of selectable control rows in `tab` (for cursor clamping + render).
    pub fn settings_rows(&self, tab: SettingsTab) -> usize {
        match tab {
            SettingsTab::Theme => theme::THEMES.len(),
            SettingsTab::Layout => self.layout_rows().len(),
            SettingsTab::Notifications => 3,
            SettingsTab::Keys => crate::app::Cmd::ALL.len(),
            SettingsTab::Modules => self.module_rows().len(),
            SettingsTab::Integrations => crate::integration::AGENTS.len(),
            SettingsTab::Language => crate::i18n::LANGS.len(),
        }
    }

    pub fn handle_settings_key(&mut self, key: KeyEvent) {
        let Some(&SettingsUi {
            tab,
            cursor,
            capturing,
        }) = self.settings.as_ref()
        else {
            return;
        };
        // Keys tab: while capturing, the next key press *is* the new binding
        // (Esc cancels). This must intercept before the normal handling so keys
        // like Tab / digits can themselves be bound.
        if capturing {
            if key.code != KeyCode::Esc {
                if let Some(s) = keys::key_string(&key) {
                    self.rebind(Self::keys_cmd_at(cursor), s);
                }
            }
            if let Some(ui) = self.settings.as_mut() {
                ui.capturing = false;
            }
            return;
        }
        match key.code {
            KeyCode::Esc => self.close_settings(),
            KeyCode::Tab => self.settings_set_tab(SettingsTab::from_index(tab.index() + 1)),
            KeyCode::BackTab => self.settings_set_tab(SettingsTab::from_index(
                tab.index() + SettingsTab::ALL.len() - 1,
            )),
            KeyCode::Up => self.settings_move(-1),
            KeyCode::Down => self.settings_move(1),
            KeyCode::Left => self.settings_adjust(cursor, -1),
            KeyCode::Right => self.settings_adjust(cursor, 1),
            KeyCode::Enter | KeyCode::Char(' ') => self.settings_activate(cursor),
            // In the Keys tab, Backspace/Delete resets a binding to its default.
            KeyCode::Backspace | KeyCode::Delete if tab == SettingsTab::Keys => {
                self.reset_binding(Self::keys_cmd_at(cursor));
            }
            KeyCode::Char(c) if ('1'..='7').contains(&c) => {
                self.settings_set_tab(SettingsTab::from_index(c as usize - '1' as usize));
            }
            _ => {}
        }
    }

    /// Route a click while the modal is open (close / switch tab / hit a control).
    pub fn handle_settings_click(&mut self, c: u16, r: u16) {
        let hit = |rect: Rect| c >= rect.x && c < rect.right() && r >= rect.y && r < rect.bottom();
        if self.settings_close_rect.is_some_and(hit) {
            self.close_settings();
            return;
        }
        // A click outside the modal dismisses it.
        if self.settings_modal_rect.is_some_and(|m| !hit(m)) {
            self.close_settings();
            return;
        }
        if let Some((tab, _)) = self
            .settings_tab_rects
            .iter()
            .find(|(_, rect)| hit(*rect))
            .copied()
        {
            self.settings_set_tab(tab);
            return;
        }
        // A click on a slider arrow steps that control in its direction.
        if let Some((i, delta, _)) = self
            .settings_arrow_rects
            .iter()
            .find(|(_, _, rect)| hit(*rect))
            .copied()
        {
            if let Some(ui) = self.settings.as_mut() {
                ui.cursor = i;
            }
            self.settings_adjust(i, delta);
            return;
        }
        // A click on a control row selects it, and activates it unless it's a
        // slider (those only change via their ‹ › arrows).
        if let Some((i, _)) = self
            .settings_ctl_rects
            .iter()
            .find(|(_, rect)| hit(*rect))
            .map(|(i, rect)| (*i, *rect))
        {
            let tab = self.settings.as_ref().map(|u| u.tab);
            if let Some(ui) = self.settings.as_mut() {
                ui.cursor = i;
            }
            // Slider/button rows only change via their arrows/buttons, so a click
            // on the row body just selects it: the Layout width sliders and dock
            // `[Left] [Right]` place rows.
            let is_slider = match tab {
                Some(SettingsTab::Layout) => matches!(
                    self.layout_rows().get(i),
                    Some(LayoutRow::SidebarWidth)
                        | Some(LayoutRow::RightWidth)
                        | Some(LayoutRow::Dock(_))
                ),
                // Number/enum module settings likewise only move via `‹ ›`.
                Some(SettingsTab::Modules) => self.module_row_is_slider(i),
                _ => false,
            };
            if !is_slider {
                self.settings_activate(i);
            }
        }
    }

    fn settings_set_tab(&mut self, tab: SettingsTab) {
        let cursor = match tab {
            SettingsTab::Theme => theme_cursor(&self.config.theme),
            SettingsTab::Language => lang_cursor(&self.config.language),
            _ => 0,
        };
        if let Some(ui) = self.settings.as_mut() {
            ui.tab = tab;
            ui.cursor = cursor;
        }
    }

    fn settings_move(&mut self, delta: i32) {
        let Some(&SettingsUi { tab, cursor, .. }) = self.settings.as_ref() else {
            return;
        };
        let rows = self.settings_rows(tab);
        if rows == 0 {
            return;
        }
        let new = (cursor as i32 + delta).clamp(0, rows as i32 - 1) as usize;
        if let Some(ui) = self.settings.as_mut() {
            ui.cursor = new;
        }
        // Theme / Language preview live as the selection moves.
        if tab == SettingsTab::Theme {
            self.apply_theme(theme::THEMES[new]);
        } else if tab == SettingsTab::Language {
            self.apply_language(crate::i18n::LANGS[new]);
        }
    }

    fn settings_adjust(&mut self, cursor: usize, delta: i32) {
        let Some(tab) = self.settings.as_ref().map(|u| u.tab) else {
            return;
        };
        match tab {
            // radio tabs: ‹ › move the selection like up/down
            SettingsTab::Theme | SettingsTab::Language => self.settings_move(delta),
            SettingsTab::Layout => self.adjust_layout(cursor, delta),
            SettingsTab::Notifications if cursor < 2 => self.toggle_notify(cursor),
            SettingsTab::Notifications => {} // the Test row only reacts to Enter/click
            SettingsTab::Keys => {}          // rebind is Enter (capture), not ‹ ›
            SettingsTab::Integrations => self.settings_activate(cursor),
            SettingsTab::Modules => self.toggle_module(cursor, Some(delta)),
        }
    }

    fn settings_activate(&mut self, cursor: usize) {
        let Some(tab) = self.settings.as_ref().map(|u| u.tab) else {
            return;
        };
        match tab {
            SettingsTab::Theme => {
                self.apply_theme(theme::THEMES[cursor.min(theme::THEMES.len() - 1)])
            }
            SettingsTab::Language => {
                self.apply_language(crate::i18n::LANGS[cursor.min(crate::i18n::LANGS.len() - 1)])
            }
            SettingsTab::Layout => self.activate_layout(cursor),
            SettingsTab::Notifications if cursor == 2 => self.test_sound(),
            SettingsTab::Notifications => self.toggle_notify(cursor),
            // Enter on a Keys row starts capturing the next key as its binding.
            SettingsTab::Keys => {
                if let Some(ui) = self.settings.as_mut() {
                    ui.capturing = true;
                }
            }
            SettingsTab::Integrations => self.install_integration(cursor),
            SettingsTab::Modules => self.toggle_module(cursor, None),
        }
    }

    /// The command at row `cursor` in the Keys tab.
    fn keys_cmd_at(cursor: usize) -> crate::app::Cmd {
        let all = crate::app::Cmd::ALL;
        all[cursor.min(all.len() - 1)]
    }

    /// The Modules tab's dynamic row model: one row per installed module,
    /// followed by an indented row per setting it declares while it is enabled.
    /// Disabled modules collapse, so the list stays short.
    pub fn module_rows(&self) -> Vec<ModuleRow> {
        let mut v = Vec::new();
        for (mi, m) in self.modules.modules.iter().enumerate() {
            v.push(ModuleRow::Module(mi));
            if m.enabled && m.warning.is_none() {
                v.extend((0..m.manifest.settings.len()).map(|si| ModuleRow::Setting(mi, si)));
            }
        }
        v
    }

    /// Whether Modules row `i` is a `‹ ›` stepper (number/enum), which a click on
    /// the row body should only select, not change.
    fn module_row_is_slider(&self, i: usize) -> bool {
        use crate::module::manifest::SettingKind;
        let Some(ModuleRow::Setting(mi, si)) = self.module_rows().get(i).copied() else {
            return false;
        };
        self.modules
            .modules
            .get(mi)
            .and_then(|m| m.manifest.settings.get(si))
            .is_some_and(|s| matches!(s.kind, SettingKind::Number | SettingKind::Enum))
    }

    /// Enable/disable the module at `cursor`, or step its setting. `delta` is
    /// the direction for a `‹ ›` press; `None` means "activate" (Enter/click),
    /// which toggles a bool and opens the prompt for a string.
    fn toggle_module(&mut self, cursor: usize, delta: Option<i32>) {
        match self.module_rows().get(cursor).copied() {
            Some(ModuleRow::Module(mi)) => {
                if let Some(m) = self.modules.modules.get(mi) {
                    let (id, on) = (m.id.clone(), !m.enabled);
                    let _ = self.module_set_enabled(&id, on);
                    // Collapsing a module can leave the cursor past the end.
                    self.clamp_settings_cursor();
                }
            }
            Some(ModuleRow::Setting(mi, si)) => self.adjust_module_setting(mi, si, delta),
            None => {}
        }
    }

    /// Apply a step (or an activation) to one declared module setting.
    fn adjust_module_setting(&mut self, mi: usize, si: usize, delta: Option<i32>) {
        use crate::module::manifest::SettingKind;
        let Some((id, spec)) = self.modules.modules.get(mi).and_then(|m| {
            m.manifest
                .settings
                .get(si)
                .map(|s| (m.id.clone(), s.clone()))
        }) else {
            return;
        };
        let current = crate::module::settings::get(
            &self.modules.find(&id).unwrap().manifest.clone(),
            &id,
            &spec.key,
        )
        .unwrap_or_else(|| spec.default_value());

        // A string setting has nothing to step — Enter (and either arrow) opens
        // the inline prompt instead.
        if spec.kind == SettingKind::String {
            self.module_setting_edit = Some(ModuleSettingEdit {
                module_id: id,
                key: spec.key.clone(),
                title: spec.title.clone(),
                // A secret starts empty rather than revealing the stored value.
                buffer: if spec.secret {
                    String::new()
                } else {
                    current.as_str().unwrap_or_default().to_string()
                },
                secret: spec.secret,
            });
            return;
        }
        // Enter on a bool flips it; on a number/enum it advances one step.
        let step = delta.unwrap_or(1) as i64;
        let next = crate::module::settings::stepped(&spec, &current, step);
        if let Err(e) = self.module_set_setting(&id, &spec.key, next) {
            self.show_toast(e);
        }
    }

    /// Key handling for the inline module-setting prompt (docs/13 §3.6).
    /// `Enter` saves, `Esc` cancels — the same contract as the rename modals.
    pub fn handle_module_setting_key(&mut self, key: KeyEvent) {
        match key.code {
            KeyCode::Esc => self.module_setting_edit = None,
            KeyCode::Enter => {
                if let Some(e) = self.module_setting_edit.take() {
                    let v = Value::String(e.buffer);
                    if let Err(err) = self.module_set_setting(&e.module_id, &e.key, v) {
                        self.show_toast(err);
                    }
                }
            }
            KeyCode::Backspace => {
                if let Some(e) = self.module_setting_edit.as_mut() {
                    e.buffer.pop();
                }
            }
            KeyCode::Char(c) => {
                if let Some(e) = self.module_setting_edit.as_mut() {
                    if e.buffer.chars().count() < MODULE_SETTING_MAX {
                        e.buffer.push(c);
                    }
                }
            }
            _ => {}
        }
    }

    /// Keep the settings cursor inside the current tab's row count (rows can
    /// shrink under it when a module collapses).
    fn clamp_settings_cursor(&mut self) {
        let Some(tab) = self.settings.as_ref().map(|u| u.tab) else {
            return;
        };
        let max = self.settings_rows(tab).saturating_sub(1);
        if let Some(ui) = self.settings.as_mut() {
            ui.cursor = ui.cursor.min(max);
        }
    }

    // ── apply helpers (mutate config, apply live, persist) ───────────────────

    fn apply_theme(&mut self, name: &str) {
        self.config.theme = name.to_string();
        self.theme = theme::by_name(name);
        if self.downsample {
            self.theme = self.theme.to_256();
        }
        config::save(&self.config);
    }

    /// Swap the UI language live + persist (docs/21) — mirrors `apply_theme`.
    fn apply_language(&mut self, code: &str) {
        self.config.language = code.to_string();
        self.catalog = crate::i18n::by_code(code);
        config::save(&self.config);
    }

    /// Layout tab ‹ ›/click on a row's control (docs/29). Width sliders step by
    /// `delta`; toggles flip; a `Dock` row's `[Left]`/`[Right]` buttons (which map
    /// to `delta < 0` / `delta > 0`) place the dock on that side.
    fn adjust_layout(&mut self, cursor: usize, delta: i32) {
        let Some(row) = self.layout_rows().get(cursor).cloned() else {
            return;
        };
        match row {
            LayoutRow::SidebarWidth => {
                let w = (self.sidebars.left.width as i32 + 2 * delta)
                    .clamp(SIDEBAR_WIDTH_MIN as i32, SIDEBAR_WIDTH_MAX as i32)
                    as u16;
                self.set_side_width(Side::Left, w);
            }
            LayoutRow::RightWidth => {
                let w = (self.sidebars.right.width as i32 + 2 * delta)
                    .clamp(SIDEBAR_WIDTH_MIN as i32, SIDEBAR_WIDTH_MAX as i32)
                    as u16;
                self.set_side_width(Side::Right, w);
            }
            LayoutRow::ColGap => {
                self.config.layout.col_gap ^= 1;
                self.apply_gaps();
            }
            LayoutRow::RowGap => {
                self.config.layout.row_gap ^= 1;
                self.apply_gaps();
            }
            LayoutRow::Scrollback => {
                let step = config::SCROLLBACK_STEP as i64;
                let next = (self.config.layout.scrollback as i64 + step * delta as i64)
                    .clamp(config::SCROLLBACK_MIN as i64, config::SCROLLBACK_MAX as i64)
                    as usize;
                self.config.layout.scrollback = next;
                self.apply_scrollback();
                config::save(&self.config);
            }
            LayoutRow::PaneTitles => {
                self.config.layout.show_titles = !self.config.layout.show_titles;
                config::save(&self.config);
            }
            LayoutRow::ResumeWs => {
                self.config.layout.resume_in_new_workspace =
                    !self.config.layout.resume_in_new_workspace;
                config::save(&self.config);
            }
            #[cfg(windows)]
            LayoutRow::Shell => self.cycle_shell(delta),
            LayoutRow::LeftVisible => {
                self.sidebars.left.visible = !self.sidebars.left.visible;
                self.save_sidebars();
            }
            LayoutRow::RightVisible => {
                self.sidebars.right.visible = !self.sidebars.right.visible;
                self.save_sidebars();
            }
            LayoutRow::Dock(kind) => {
                // Buttons encode the target as `delta`: -1 = Left, +1 = Right,
                // +2 = Off (unmount). `←`/`→` keys (∓1) place left/right.
                if delta <= -1 {
                    self.move_dock(&kind, Side::Left);
                } else if delta == 1 {
                    self.move_dock(&kind, Side::Right);
                } else {
                    self.unmount_dock(&kind);
                }
            }
        }
    }

    /// Enter/click on a Layout row: bump a slider, flip a toggle, or (for a dock)
    /// cycle Left → Right → Off → Left.
    fn activate_layout(&mut self, cursor: usize) {
        match self.layout_rows().get(cursor).cloned() {
            Some(LayoutRow::Dock(kind)) => match self.sidebars.side_of(&kind) {
                Some(Side::Left) => self.move_dock(&kind, Side::Right),
                Some(Side::Right) => self.unmount_dock(&kind),
                None => self.move_dock(&kind, Side::Left),
            },
            _ => self.adjust_layout(cursor, 1),
        }
    }

    /// Cycle the configured shell (applies to newly opened panes). Windows-only.
    #[cfg(windows)]
    fn cycle_shell(&mut self, delta: i32) {
        let choices = crate::platform::shell_choices();
        let n = choices.len() as i32;
        let cur = choices
            .iter()
            .position(|(k, _)| *k == self.config.shell)
            .unwrap_or(0) as i32;
        let next = (((cur + delta) % n + n) % n) as usize;
        self.config.shell = choices[next].0.to_string();
        config::save(&self.config);
    }

    fn apply_gaps(&mut self) {
        crate::layout::set_gaps(self.config.layout.col_gap, self.config.layout.row_gap);
        config::save(&self.config);
    }

    /// Push the scrollback limit to every live pane. Alacritty's
    /// `Grid::update_history` shrinks retained history when the limit drops, so
    /// lowering this frees memory now rather than only for new panes.
    fn apply_scrollback(&mut self) {
        let lines = self.config.scrollback();
        for pane in self.panes.values() {
            pane.set_scrollback(lines);
        }
    }

    fn toggle_notify(&mut self, cursor: usize) {
        match cursor {
            0 => self.config.notifications.sound_on_done = !self.config.notifications.sound_on_done,
            1 => {
                self.config.notifications.sound_on_blocked =
                    !self.config.notifications.sound_on_blocked
            }
            _ => {}
        }
        config::save(&self.config);
    }

    /// Play the retro chime once so the user can hear it before turning it on.
    /// Bypasses both sound toggles — it's an explicit manual test.
    fn test_sound(&mut self) {
        self.pending_sound = true;
    }

    /// Toggle an agent's integration hook: install if absent, uninstall if present.
    /// Uninstall removes only bohay's hook — never the agent itself.
    fn install_integration(&mut self, cursor: usize) {
        if let Some(agent) = crate::integration::AGENTS.get(cursor) {
            if crate::integration::is_installed(agent) {
                let _ = crate::integration::uninstall(agent);
            } else {
                let _ = crate::integration::install(agent);
            }
        }
    }
}

fn theme_cursor(name: &str) -> usize {
    theme::THEMES.iter().position(|n| *n == name).unwrap_or(0)
}

fn lang_cursor(code: &str) -> usize {
    crate::i18n::LANGS
        .iter()
        .position(|c| *c == code)
        .unwrap_or(0)
}

#[cfg(test)]
mod tests {
    use super::*;

    // The Notifications tab is three rows: the two sound toggles (persisted)
    // and a Test row that rings the chime immediately, regardless of toggles.
    #[test]
    fn notifications_tab_toggles_sounds_and_tests_the_chime() {
        let _env = crate::persist::test_env("notify-tab");
        let (tx, _rx) = std::sync::mpsc::channel();
        let mut app = crate::app::App::new(80, 24, tx).unwrap();
        app.open_settings();
        if let Some(ui) = app.settings.as_mut() {
            ui.tab = SettingsTab::Notifications;
        }
        assert_eq!(app.settings_rows(SettingsTab::Notifications), 3);
        app.settings_activate(0);
        assert!(app.config.notifications.sound_on_done, "row 0 toggles done");
        app.settings_activate(1);
        assert!(
            app.config.notifications.sound_on_blocked,
            "row 1 toggles blocked"
        );
        assert!(!app.pending_sound);
        app.settings_activate(2);
        assert!(app.pending_sound, "the Test row rings the chime");
    }
}