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// `Send`: an `OverlayHost` lives in `EditorScreen`, whose `#[async_trait]`
48// `AppScreen` methods desugar to `Send` futures — so the boxed overlay must be `Send`.
49pub trait Overlay: Send {
50    fn kind(&self) -> OverlayKind;
51    fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState;
52    fn handle_app_message(
53        &mut self,
54        _msg: &AppEvent,
55        _vault: &Arc<NoteVault>,
56        _tx: &AppTx,
57    ) -> OverlayMsg {
58        OverlayMsg::NotConsumed
59    }
60    fn render(&mut self, f: &mut Frame, area: Rect, theme: &Theme);
61    fn hint_shortcuts(&self) -> Vec<(String, String)> {
62        vec![]
63    }
64    /// The query string this overlay holds, if it is query-backed (the note
65    /// browser). Used by the editor's save-current-query action to source the
66    /// query from the active overlay. Defaults to `None` for non-query overlays.
67    fn query(&self) -> Option<&str> {
68        None
69    }
70    /// The saved-search name this overlay's query came from (its breadcrumb
71    /// provenance), if any. Used to pre-fill the save-search dialog's name.
72    /// Defaults to `None` for overlays without a breadcrumb.
73    fn saved_search_provenance(&self) -> Option<&str> {
74        None
75    }
76}