/// RFC 21 验收 example — Input widget

use lv_tui::prelude::*;
use lv_tui::widgets::{Column, Input, Label};

struct Root { body: Column }

impl Root {
    fn new(body: Column) -> Self { Self { body } }
}

impl Component for Root {
    fn render(&self, cx: &mut RenderCx) { self.body.render(cx); }
    fn layout(&mut self, r: Rect, cx: &mut LayoutCx) { self.body.layout(r, cx); }
    fn focusable(&self) -> bool { false }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); }
        if let Event::Key(_) = e { self.body.event(e, cx); }
    }
    fn for_each_child(&self, f: &mut dyn FnMut(&Node)) { self.body.for_each_child(f); }
    fn for_each_child_mut(&mut self, f: &mut dyn FnMut(&mut Node)) { self.body.for_each_child_mut(f); }
}

fn main() -> lv_tui::Result<()> {
    let body = Column::new()
        .padding(1).gap(1)
        .child(Label::new("=== Input Widget Demo ==="))
        .child(Label::new("Name:"))
        .child(Input::new().placeholder("Enter your name..."))
        .child(Label::new("Email:"))
        .child(Input::new().placeholder("Enter your email..."))
        .child(Label::new("Tab to switch, type to edit, q to quit"));

    lv_tui::app::App::new(Root::new(body)).run()
}
