Skip to main content

kimun_notes/components/
overlay.rs

1//! The `Overlay` trait and its supporting types — the contract every editor
2//! overlay (note browser, Saved Searches modal, or dialog) implements so the
3//! `OverlayHost` can route input / app-messages / render to it uniformly.
4
5use std::sync::Arc;
6
7use kimun_core::NoteVault;
8use ratatui::Frame;
9use ratatui::layout::Rect;
10
11use crate::components::event_state::EventState;
12use crate::components::events::{AppTx, InputEvent, OverlayData};
13use crate::settings::themes::Theme;
14
15/// Identifies which overlay is active — used for toggle, focus label, and hints.
16#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum OverlayKind {
18    NoteBrowser,
19    SavedSearches,
20    CommandPalette,
21    RagAnswer,
22    Dialog,
23}
24
25impl OverlayKind {
26    /// Footer label for this overlay kind.
27    pub fn label(&self) -> &'static str {
28        match self {
29            OverlayKind::NoteBrowser => "NOTE BROWSER",
30            OverlayKind::SavedSearches => "SAVED SEARCHES",
31            OverlayKind::CommandPalette => "COMMANDS",
32            OverlayKind::RagAnswer => "ASK (RAG)",
33            OverlayKind::Dialog => "DIALOG",
34        }
35    }
36}
37
38/// Outcome of routing an `AppEvent` to the active overlay. Overlays never
39/// request their own dismissal here: dialogs close by emitting the
40/// `AppEvent::CloseOverlay` event, which the editor handles separately.
41#[derive(Debug)]
42pub enum OverlayMsg {
43    /// The overlay did not recognise the message.
44    NotConsumed,
45    /// The overlay handled the message and stays open.
46    Consumed,
47}
48
49// No `Send` bound: `EditorScreen` (which hosts overlays) is itself non-`Send`
50// because of its `ratatui-textarea` buffer (see `AppScreen` in `app_screen/mod.rs`),
51// and it is only ever driven on the main `block_on` future, never spawned.
52pub trait Overlay {
53    fn kind(&self) -> OverlayKind;
54    fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState;
55    /// Receive an **Overlay data** result addressed to this overlay (see
56    /// CONTEXT.md). `NotConsumed` means the data was not for this overlay
57    /// (or is stale) — the host drops it; nothing else ever sees it.
58    fn handle_data(
59        &mut self,
60        _data: &OverlayData,
61        _vault: &Arc<NoteVault>,
62        _tx: &AppTx,
63    ) -> OverlayMsg {
64        OverlayMsg::NotConsumed
65    }
66    fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme);
67    fn hint_shortcuts(&self) -> Vec<(String, String)> {
68        vec![]
69    }
70    /// The query string this overlay holds, if it is query-backed (the note
71    /// browser). Used by the editor's save-current-query action to source the
72    /// query from the active overlay. Defaults to `None` for non-query overlays.
73    fn query(&self) -> Option<&str> {
74        None
75    }
76    /// The saved-search name this overlay's query came from (its breadcrumb
77    /// provenance), if any. Used to pre-fill the save-search dialog's name.
78    /// Defaults to `None` for overlays without a breadcrumb.
79    fn saved_search_provenance(&self) -> Option<&str> {
80        None
81    }
82}