bohay 0.8.0

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
//! 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,
    PaneTitles,
    ResumeWs,
    #[cfg(windows)]
    Shell,
    LeftVisible,
    RightVisible,
    RightWidth,
    Dock(DockKind),
}

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::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 {
        #[cfg(windows)]
        {
            6
        }
        #[cfg(not(windows))]
        {
            5
        }
    }

    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;
    }

    /// 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 => 5,
            SettingsTab::Keys => crate::app::Cmd::ALL.len(),
            SettingsTab::Modules => self.modules.modules.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(_))
                ),
                _ => 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 < 4 => 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),
        }
    }

    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 == 4 => self.test_notification(),
            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),
        }
    }

    /// 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)]
    }

    /// Enable/disable the module at `cursor` in the Modules tab.
    fn toggle_module(&mut self, cursor: usize) {
        if let Some(m) = self.modules.modules.get(cursor) {
            let (id, on) = (m.id.clone(), !m.enabled);
            let _ = self.module_set_enabled(&id, on);
        }
    }

    // ── 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::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);
    }

    fn toggle_notify(&mut self, cursor: usize) {
        match cursor {
            0 => self.config.notifications.enabled = !self.config.notifications.enabled,
            1 => self.config.notifications.on_blocked = !self.config.notifications.on_blocked,
            2 => self.config.notifications.on_done = !self.config.notifications.on_done,
            3 => self.config.notifications.sound = !self.config.notifications.sound,
            _ => {}
        }
        config::save(&self.config);
    }

    /// Fire a one-off desktop notification so the user can confirm it works.
    /// Bypasses the enabled toggle — it's an explicit manual test.
    fn test_notification(&mut self) {
        self.pending_notify
            .push("bohay — test notification".to_string());
    }

    /// 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)
}