rust_kanban/ui/rendering/popup/
change_date_format.rs

1use crate::{
2    app::{
3        state::{Focus, KeyBindingEnum},
4        App, DateTimeFormat,
5    },
6    constants::LIST_SELECTED_SYMBOL,
7    ui::{
8        rendering::{
9            common::{render_blank_styled_canvas, render_close_button},
10            popup::ChangeDateFormat,
11            utils::{
12                calculate_mouse_list_select_index, centered_rect_with_percentage,
13                check_if_active_and_get_style, check_if_mouse_is_in_area,
14            },
15        },
16        Renderable,
17    },
18};
19use ratatui::{
20    layout::{Alignment, Constraint, Direction, Layout},
21    text::{Line, Span},
22    widgets::{Block, BorderType, Borders, List, ListItem, Paragraph},
23    Frame,
24};
25
26impl Renderable for ChangeDateFormat {
27    fn render(rect: &mut Frame, app: &mut App, is_active: bool) {
28        let render_area = centered_rect_with_percentage(70, 70, rect.area());
29
30        let chunks = Layout::default()
31            .direction(Direction::Vertical)
32            .constraints([Constraint::Fill(1), Constraint::Length(5)].as_ref())
33            .split(render_area);
34
35        let general_style = check_if_active_and_get_style(
36            is_active,
37            app.current_theme.inactive_text_style,
38            app.current_theme.general_style,
39        );
40        let help_key_style = check_if_active_and_get_style(
41            is_active,
42            app.current_theme.inactive_text_style,
43            app.current_theme.help_key_style,
44        );
45        let help_text_style = check_if_active_and_get_style(
46            is_active,
47            app.current_theme.inactive_text_style,
48            app.current_theme.help_text_style,
49        );
50        let list_select_style = check_if_active_and_get_style(
51            is_active,
52            app.current_theme.inactive_text_style,
53            app.current_theme.list_select_style,
54        );
55        let keyboard_focus_style = check_if_active_and_get_style(
56            is_active,
57            app.current_theme.inactive_text_style,
58            app.current_theme.keyboard_focus_style,
59        );
60
61        let all_date_formats = DateTimeFormat::get_all_date_formats();
62        let all_date_formats = all_date_formats
63            .iter()
64            .map(|s| ListItem::new(vec![Line::from(s.to_human_readable_string().to_string())]))
65            .collect::<Vec<ListItem>>();
66
67        if check_if_mouse_is_in_area(&app.state.current_mouse_coordinates, &render_area) {
68            app.state.mouse_focus = Some(Focus::ChangeDateFormatPopup);
69            app.state.set_focus(Focus::ChangeDateFormatPopup);
70            calculate_mouse_list_select_index(
71                app.state.current_mouse_coordinates.1,
72                &all_date_formats,
73                render_area,
74                &mut app.state.app_list_states.date_format_selector,
75            );
76        }
77        let date_formats = List::new(all_date_formats)
78            .block(
79                Block::default()
80                    .title("Change Date Format")
81                    .style(general_style)
82                    .borders(Borders::ALL)
83                    .border_type(BorderType::Rounded),
84            )
85            .highlight_style(list_select_style)
86            .highlight_symbol(LIST_SELECTED_SYMBOL);
87
88        let up_key = app
89            .get_first_keybinding(KeyBindingEnum::Up)
90            .unwrap_or("".to_string());
91        let down_key = app
92            .get_first_keybinding(KeyBindingEnum::Down)
93            .unwrap_or("".to_string());
94        let accept_key = app
95            .get_first_keybinding(KeyBindingEnum::Accept)
96            .unwrap_or("".to_string());
97        let cancel_key = app
98            .get_first_keybinding(KeyBindingEnum::GoToPreviousViewOrCancel)
99            .unwrap_or("".to_string());
100
101        let help_spans = Line::from(vec![
102            Span::styled("Use ", help_text_style),
103            Span::styled(up_key, help_key_style),
104            Span::styled(" or ", help_text_style),
105            Span::styled(down_key, help_key_style),
106            Span::styled(
107                " to navigate or use the mouse cursor. Press ",
108                help_text_style,
109            ),
110            Span::styled(accept_key, help_key_style),
111            Span::styled(" or ", help_text_style),
112            Span::styled("<Mouse Left Click>", help_key_style),
113            Span::styled(" To select a Default Date Format. Press ", help_text_style),
114            Span::styled(cancel_key, help_key_style),
115            Span::styled(" to cancel", help_text_style),
116        ]);
117
118        let default_date_picker_help = Paragraph::new(help_spans)
119            .alignment(Alignment::Left)
120            .block(
121                Block::default()
122                    .title("Help")
123                    .borders(Borders::ALL)
124                    .style(general_style)
125                    .border_type(BorderType::Rounded),
126            )
127            .alignment(Alignment::Center)
128            .wrap(ratatui::widgets::Wrap { trim: true });
129
130        let clear_area = centered_rect_with_percentage(80, 80, rect.area());
131        let clear_area_border = Block::default()
132            .title("Default Date Format Picker")
133            .style(general_style)
134            .borders(Borders::ALL)
135            .border_style(keyboard_focus_style)
136            .border_type(BorderType::Rounded);
137        render_blank_styled_canvas(rect, &app.current_theme, clear_area, is_active);
138        rect.render_widget(clear_area_border, clear_area);
139        rect.render_stateful_widget(
140            date_formats,
141            chunks[0],
142            &mut app.state.app_list_states.date_format_selector,
143        );
144        rect.render_widget(default_date_picker_help, chunks[1]);
145        if app.config.enable_mouse_support {
146            render_close_button(rect, app, is_active);
147        }
148    }
149}