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    Dialog,
22}
23
24impl OverlayKind {
25    /// Footer label for this overlay kind.
26    pub fn label(&self) -> &'static str {
27        match self {
28            OverlayKind::NoteBrowser => "NOTE BROWSER",
29            OverlayKind::SavedSearches => "SAVED SEARCHES",
30            OverlayKind::CommandPalette => "COMMANDS",
31            OverlayKind::Dialog => "DIALOG",
32        }
33    }
34}
35
36/// Outcome of routing an `AppEvent` to the active overlay. Overlays never
37/// request their own dismissal here: dialogs close by emitting the
38/// `AppEvent::CloseOverlay` event, which the editor handles separately.
39#[derive(Debug)]
40pub enum OverlayMsg {
41    /// The overlay did not recognise the message.
42    NotConsumed,
43    /// The overlay handled the message and stays open.
44    Consumed,
45}
46
47// `Send` because a screen holds `Box<dyn Overlay>` and screens are `Send` (see
48// `AppScreen` in `app_screen/mod.rs`). Every overlay already satisfied it.
49pub trait Overlay: Send {
50    fn kind(&self) -> OverlayKind;
51    fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState;
52    /// Receive an **Overlay data** result addressed to this overlay (see
53    /// CONTEXT.md). `NotConsumed` means the data was not for this overlay
54    /// (or is stale) — the host drops it; nothing else ever sees it.
55    fn handle_data(
56        &mut self,
57        _data: &OverlayData,
58        _vault: &Arc<NoteVault>,
59        _tx: &AppTx,
60    ) -> OverlayMsg {
61        OverlayMsg::NotConsumed
62    }
63    fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme);
64    fn hint_shortcuts(&self) -> Vec<(String, String)> {
65        vec![]
66    }
67    /// The query string this overlay holds, if it is query-backed (the note
68    /// browser). Used by the editor's save-current-query action to source the
69    /// query from the active overlay. Defaults to `None` for non-query overlays.
70    fn query(&self) -> Option<&str> {
71        None
72    }
73    /// The saved-search name this overlay's query came from (its breadcrumb
74    /// provenance), if any. Used to pre-fill the save-search dialog's name.
75    /// Defaults to `None` for overlays without a breadcrumb.
76    fn saved_search_provenance(&self) -> Option<&str> {
77        None
78    }
79}