Skip to main content

kimun_notes/components/
mod.rs

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