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::{AppEvent, AppTx, InputEvent};
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// No `Send` bound: `EditorScreen` (which hosts overlays) is itself non-`Send`
48// because of its `ratatui-textarea` buffer (see `AppScreen` in `app_screen/mod.rs`),
49// and it is only ever driven on the main `block_on` future, never spawned.
50pub trait Overlay {
51    fn kind(&self) -> OverlayKind;
52    fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState;
53    fn handle_app_message(
54        &mut self,
55        _msg: &AppEvent,
56        _vault: &Arc<NoteVault>,
57        _tx: &AppTx,
58    ) -> OverlayMsg {
59        OverlayMsg::NotConsumed
60    }
61    fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme);
62    fn hint_shortcuts(&self) -> Vec<(String, String)> {
63        vec![]
64    }
65    /// The query string this overlay holds, if it is query-backed (the note
66    /// browser). Used by the editor's save-current-query action to source the
67    /// query from the active overlay. Defaults to `None` for non-query overlays.
68    fn query(&self) -> Option<&str> {
69        None
70    }
71    /// The saved-search name this overlay's query came from (its breadcrumb
72    /// provenance), if any. Used to pre-fill the save-search dialog's name.
73    /// Defaults to `None` for overlays without a breadcrumb.
74    fn saved_search_provenance(&self) -> Option<&str> {
75        None
76    }
77}