91 lines
2.8 KiB
Rust
91 lines
2.8 KiB
Rust
mod day_details;
|
|
mod event_adding;
|
|
mod tag_management;
|
|
|
|
use crate::day_info::Event;
|
|
use crate::{database::DB, events::AppEvent, focused::Focused};
|
|
use chrono::NaiveDate;
|
|
use ratatui::crossterm::event::KeyEvent;
|
|
use ratatui::{buffer::Buffer, layout::Rect, widgets::Widget};
|
|
|
|
pub use day_details::DayDetails;
|
|
pub use event_adding::EventAdding;
|
|
pub use tag_management::TagManagement;
|
|
|
|
pub enum PopupType<'a> {
|
|
TagManagement(TagManagement<'a>),
|
|
DayDetails(DayDetails),
|
|
EventAdding(EventAdding<'a>),
|
|
}
|
|
|
|
impl Default for PopupType<'_> {
|
|
fn default() -> Self {
|
|
PopupType::TagManagement(TagManagement::default())
|
|
}
|
|
}
|
|
|
|
#[derive(Default)]
|
|
pub struct Popup<'a> {
|
|
pub self_state: PopupType<'a>,
|
|
pub focused: bool,
|
|
}
|
|
|
|
impl Popup<'_> {
|
|
pub fn ready_to_render(&mut self, area: Rect) {
|
|
match &mut self.self_state {
|
|
PopupType::TagManagement(tm) => tm.ready_to_render(area),
|
|
PopupType::DayDetails(dd) => dd.ready_to_render(area),
|
|
PopupType::EventAdding(ea) => ea.ready_to_render(area),
|
|
}
|
|
}
|
|
|
|
pub fn add_event(&mut self, start: NaiveDate, end: NaiveDate, tags: Vec<(u64, String)>) {
|
|
self.self_state = PopupType::EventAdding(EventAdding::add_event(start, end, tags));
|
|
}
|
|
|
|
pub fn show_day(&mut self, day: NaiveDate, events: Vec<Event>, tag_names: Vec<String>) {
|
|
self.self_state = PopupType::DayDetails(DayDetails::show_day(day, events, tag_names));
|
|
}
|
|
}
|
|
|
|
impl Focused for Popup<'_> {
|
|
fn take_focus(&mut self) {
|
|
self.focused = true;
|
|
match &mut self.self_state {
|
|
PopupType::TagManagement(tm) => tm.take_focus(),
|
|
PopupType::DayDetails(dd) => dd.take_focus(),
|
|
PopupType::EventAdding(ea) => ea.take_focus(),
|
|
}
|
|
}
|
|
|
|
fn lose_focus(&mut self) {
|
|
self.focused = false;
|
|
match &mut self.self_state {
|
|
PopupType::TagManagement(tm) => tm.lose_focus(),
|
|
PopupType::DayDetails(dd) => dd.lose_focus(),
|
|
PopupType::EventAdding(ea) => ea.lose_focus(),
|
|
}
|
|
}
|
|
|
|
fn handle_input(&mut self, key_event: KeyEvent, db: &mut DB) -> Option<Vec<AppEvent>> {
|
|
match &mut self.self_state {
|
|
PopupType::TagManagement(tm) => tm.handle_input(key_event, db),
|
|
PopupType::DayDetails(dd) => dd.handle_input(key_event, db),
|
|
PopupType::EventAdding(ea) => ea.handle_input(key_event, db),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Widget for &Popup<'_> {
|
|
fn render(self, area: Rect, buf: &mut Buffer) {
|
|
if !self.focused {
|
|
return;
|
|
}
|
|
|
|
match &self.self_state {
|
|
PopupType::TagManagement(tm) => tm.render(area, buf, self.focused),
|
|
PopupType::DayDetails(dd) => dd.render(area, buf, self.focused),
|
|
PopupType::EventAdding(ea) => ea.render(area, buf, self.focused),
|
|
}
|
|
}
|
|
}
|