Files
sus-manager/ui/src/widgets/popups/folder.rs
2025-12-14 21:34:06 +08:00

62 lines
1.7 KiB
Rust
Executable File

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",
"",
|x| {
let path = Path::new(x);
path.exists() && path.is_dir()
}
);
textarea.state.is_active = true;
Self { textarea }
}
pub fn get_folder_value(&mut self) -> Option<String> {
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);
}
}