use lv_tui::prelude::*;
use lv_tui::widgets::*;

struct App { list: VirtualList }

impl App {
    fn new() -> Self {
        let items: Vec<String> = (0..500).map(|i| format!("Item number {}", i)).collect();
        Self { list: VirtualList::new(items).show_numbers(true).highlight_symbol("> ") }
    }
}

impl Component for App {
    fn render(&self, cx: &mut RenderCx) {
        cx.line("Virtual list: 500 items, ↑↓ to scroll, q to quit");
        cx.line("");
        self.list.render(cx);
    }
    fn event(&mut self, e: &Event, cx: &mut EventCx) {
        if e.is_key(Key::Char('q')) { cx.quit(); return; }
        self.list.event(e, cx);
    }
    fn layout(&mut self, r: lv_tui::geom::Rect, cx: &mut lv_tui::component::LayoutCx) {
        self.list.layout(lv_tui::geom::Rect { x: r.x, y: r.y.saturating_add(2), width: r.width, height: r.height.saturating_sub(2) }, cx);
    }
    fn measure(&self, c: lv_tui::layout::Constraint, _cx: &mut lv_tui::component::MeasureCx) -> lv_tui::geom::Size {
        lv_tui::geom::Size { width: c.max.width, height: c.max.height }
    }
    fn focusable(&self) -> bool { false }
}

fn main() -> lv_tui::Result<()> {
    lv_tui::app::App::new(App::new()).run()
}
