1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//!
//! Render a month of a calendar.
//!
use crate::_private::NonExhaustive;
use chrono::NaiveDate;
use rat_focus::{FocusFlag, HasFocusFlag};
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::prelude::{StatefulWidget, Style};
use ratatui::widgets::StatefulWidgetRef;

pub use rat_input::calendar::MonthStyle;

#[derive(Debug, Default)]
pub struct RMonth {
    widget: rat_input::calendar::Month,
}

#[derive(Debug, Clone)]
pub struct RMonthState {
    pub widget: rat_input::calendar::MonthState,
    pub non_exhaustive: NonExhaustive,
}

impl RMonth {
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the starting date.
    #[inline]
    pub fn date(mut self, s: NaiveDate) -> Self {
        self.widget = self.widget.date(s);
        self
    }

    #[inline]
    pub fn locale(mut self, loc: chrono::Locale) -> Self {
        self.widget = self.widget.locale(loc);
        self
    }

    /// Set the composite style.
    #[inline]
    pub fn style(mut self, s: MonthStyle) -> Self {
        self.widget = self.widget.style(s);
        self
    }

    /// Sets a closure that is called to calculate the day style.
    #[inline]
    pub fn day_style(mut self, s: Box<dyn Fn(NaiveDate) -> Style>) -> Self {
        self.widget = self.widget.day_style(s);
        self
    }

    /// Set the week number style
    #[inline]
    pub fn week_style(mut self, s: impl Into<Style>) -> Self {
        self.widget = self.widget.week_style(s);
        self
    }

    /// Set the month-name style.
    #[inline]
    pub fn title_style(mut self, s: impl Into<Style>) -> Self {
        self.widget = self.widget.title_style(s);
        self
    }

    /// Required width for the widget.
    #[inline]
    pub fn width(&self) -> usize {
        self.widget.width()
    }

    /// Required height for the widget. Varies.
    #[inline]
    pub fn height(&self) -> usize {
        self.widget.height()
    }
}

impl StatefulWidgetRef for RMonth {
    type State = RMonthState;

    fn render_ref(&self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        self.widget.render_ref(area, buf, &mut state.widget);
    }
}

impl StatefulWidget for RMonth {
    type State = RMonthState;

    fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) {
        self.widget.render(area, buf, &mut state.widget)
    }
}

impl Default for RMonthState {
    fn default() -> Self {
        Self {
            widget: Default::default(),
            non_exhaustive: NonExhaustive,
        }
    }
}

impl HasFocusFlag for RMonthState {
    #[inline]
    fn focus(&self) -> &FocusFlag {
        &self.widget.focus
    }

    #[inline]
    fn area(&self) -> Rect {
        self.widget.area
    }
}