use std::path::Path; use crate::widgets::components::TextArea; use ratatui::buffer::Buffer; use ratatui::layout::{Constraint, Direction, Layout, Margin, Rect}; use ratatui::prelude::{StatefulWidget, Widget}; use ratatui::widgets::{Block, Borders}; #[derive(Clone)] pub struct AddFolderPopup { pub textarea: TextArea, } impl AddFolderPopup { pub fn new() -> Self { let mut textarea = TextArea::new( "Folder Path", "", |v| { let path = Path::new(v); path.exists() && path.is_dir() } ); textarea.state.is_active = true; Self { textarea } } pub fn get_folder_value(&mut self) -> Option { let value = self.textarea.get_value(); if value.is_none() { return None; } if let Some(path) = value && !path.is_empty() { return Some(path); } None } } impl StatefulWidget for AddFolderPopup { type State = AddFolderPopup; fn render(self, area: Rect, buf: &mut Buffer, state: &mut Self::State) where Self: Sized, { let popup_area = Rect { x: area.width / 4, y: area.height / 3, width: area.width / 2, height: area.height / 3, }; let block = Block::default() .title("Add New Folder") .borders(Borders::ALL); block.render(popup_area, buf); let chunks = Layout::default() .direction(Direction::Vertical) .constraints(vec![Constraint::Length(1)]) .split(popup_area.inner(Margin::new(1, 1))); self.textarea.render(chunks[0], buf, &mut state.textarea.state); } }