Skip to main content

kimun_notes/components/
mod.rs

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