Skip to main content

kimun_notes/components/
mod.rs

1pub mod activity_rail;
2pub mod autocomplete;
3pub mod autosave_timer;
4pub mod backlinks_panel;
5pub mod command_palette;
6pub mod dialogs;
7pub mod drawer;
8pub mod drawer_views;
9pub mod event_state;
10pub mod events;
11pub mod file_list;
12pub mod footer_bar;
13pub mod hints;
14pub mod indexing;
15pub mod note_browser;
16pub mod overlay;
17pub mod panel;
18pub mod preferences;
19pub mod query_highlight;
20pub mod query_list_panel;
21pub mod query_vars;
22pub mod rich_row;
23pub mod saved_search_breadcrumb;
24pub mod saved_searches_modal;
25pub mod search_list;
26pub mod sidebar;
27pub mod single_line_input;
28pub mod text_editor;
29pub mod which_key;
30
31use ratatui::Frame;
32use ratatui::layout::Rect;
33
34use crate::components::event_state::EventState;
35use crate::components::events::{AppTx, InputEvent};
36use crate::settings::themes::Theme;
37
38/// Centre a popup occupying `percent_x`% × `percent_y`% of `area`.
39/// A centered rect of fixed cell size, clamped to `r` — the counterpart to
40/// the percentage-based [`centered_rect`] for dialogs with intrinsic sizes.
41pub fn fixed_centered_rect(width: u16, height: u16, r: Rect) -> Rect {
42    let width = width.min(r.width);
43    let height = height.min(r.height);
44    Rect {
45        x: r.x + (r.width - width) / 2,
46        y: r.y + (r.height - height) / 2,
47        width,
48        height,
49    }
50}
51
52pub fn centered_rect(percent_x: u16, percent_y: u16, area: Rect) -> Rect {
53    let popup_height = (area.height as u32 * percent_y as u32 / 100) as u16;
54    let popup_width = (area.width as u32 * percent_x as u32 / 100) as u16;
55    Rect {
56        x: area.x + (area.width.saturating_sub(popup_width)) / 2,
57        y: area.y + (area.height.saturating_sub(popup_height)) / 2,
58        width: popup_width,
59        height: popup_height,
60    }
61}
62
63pub trait Component {
64    /// Handle an event. Send `AppEvent`s through `tx` for app-level effects.
65    /// Returns whether this component consumed the event.
66    fn handle_input(&mut self, event: &InputEvent, tx: &AppTx) -> EventState {
67        let _ = (event, tx);
68        EventState::NotConsumed
69    }
70
71    fn render(&mut self, f: &mut Frame, rect: Rect, theme: &Theme, focused: bool);
72
73    /// Context-sensitive shortcut hints shown in the hints bar when this
74    /// component is focused.  Each entry is `(key_display, label)`.
75    fn hint_shortcuts(&self) -> Vec<(String, String)> {
76        vec![]
77    }
78}
79
80#[cfg(test)]
81mod tests {
82    use super::*;
83
84    #[test]
85    fn centered_rect_is_centered() {
86        let area = Rect {
87            x: 0,
88            y: 0,
89            width: 100,
90            height: 40,
91        };
92        let r = centered_rect(80, 75, area);
93        assert_eq!(r.width, 80);
94        assert_eq!(r.height, 30);
95        assert_eq!(r.x, 10); // (100 - 80) / 2
96        assert_eq!(r.y, 5); // (40 - 30) / 2
97    }
98
99    #[test]
100    fn centered_rect_does_not_underflow() {
101        // Very small area — must not panic.
102        let area = Rect {
103            x: 0,
104            y: 0,
105            width: 5,
106            height: 5,
107        };
108        let _ = centered_rect(80, 75, area);
109    }
110}