@main
#ui
#ui/draw
#ui/layout
#ui/widgets
demo #"github.com/xulang-org/examples/ui_demo"

struct Model{
    button widgets.Button
    checkbox widgets.Checkbox
    progress widgets.Progress
    scroll widgets.Scroll
    button_label draw.TextLayout
    checkbox_label draw.TextLayout
    theme widgets.Theme
    context draw.Context
    count int
    header_id int
    button_id int
    check_id int
    progress_id int
    scroll_id int
    row_ids []int
}
fn (model $&Model) button_input(kind int, input &ui.InputEvent) widgets.Action{
    button := $&model.button
    << button.route(kind, input) or >>
}
fn (model $&Model) checkbox_input(kind int, input &ui.InputEvent) widgets.Action{
    checkbox := $&model.checkbox
    << checkbox.route(kind, input) or >>
}
fn (model $&Model) scroll_input(kind int, input &ui.InputEvent) widgets.Action{
    scroll := $&model.scroll
    << scroll.route(kind, input) or >>
}
fn (model $&Model) input(id int, kind int, input &ui.InputEvent) widgets.Action{
    if id == model.button_id{ << model.button_input(kind, input) or >> }
    if id == model.check_id{ << model.checkbox_input(kind, input) or >> }
    if id == model.scroll_id{ << model.scroll_input(kind, input) or >> }
    << widgets.Action{kind: widgets.ACTION_NONE, id: id, needs_paint: false, request_focus: false, consumed: false, value: 0.0}
}
fn (model $&Model) place_button(bounds &layout.Rect) int {
    button := $&model.button
    << button.set_bounds(bounds) or >>
}
fn (model $&Model) place_checkbox(bounds &layout.Rect) int {
    checkbox := $&model.checkbox
    << checkbox.set_bounds(bounds) or >>
}
fn (model $&Model) place_progress(bounds &layout.Rect) int {
    progress := $&model.progress
    << progress.set_bounds(bounds) or >>
}
fn (model $&Model) paint(id int, bounds &layout.Rect, frame $&draw.Frame) bool {
    model.draw_node(id, bounds, frame) or(err) {eprintln(err.msg())
        << false}
    << true
}
fn (model $&Model) draw_node(id int, bounds &layout.Rect, frame $&draw.Frame) int {
    if id == model.header_id{
        demo.text(&model.context, frame, 0.0, 0.0, 14.0, demo.accent(), "03 / RETAINED LAYOUT + CONTROLS") or >>
        demo.text(&model.context, frame, 0.0, 28.0, 24.0, demo.ink(), "Actions completed: " + str(model.count)) or >>
    } else if id == model.button_id{
        model.place_button(bounds) or >>
        model.button.paint(frame, &model.button_label, &model.theme) or >>
    } else if id == model.check_id{
        model.place_checkbox(bounds) or >>
        model.checkbox.paint(frame, &model.checkbox_label, &model.theme) or >>
    } else if id == model.progress_id{
        model.place_progress(bounds) or >>
        model.progress.paint(frame, &model.theme) or >>
    } else if id == model.scroll_id{
        scroll := $&model.scroll
        scroll.set_bounds(bounds) or >>
        frame.fill_rect(0.0, 0.0, bounds.width, bounds.height, draw.rgb(26, 37, 50)) or >>
    } else {
        for i in 0:model.row_ids.len{
            if id == model.row_ids[i] {demo.text(&model.context, frame, 12.0, 5.0, 15.0, demo.muted(), "Retained row " + str(i + 1) + " / scroll, Tab, PageDown, Home") or >> }
        }
    }
    << 0
}
fn (model $&Model) advance() int {
    model.count = model.count + 1
    println("Action count: " + str(model.count))
    value := f64(model.count % 11) / 10.0
    progress := $&model.progress
    << progress.set_value(value) or >>
}
fn (model $&Model) enable_button(enabled bool) {
    button := $&model.button
    button.set_enabled(enabled)
}
fn (model $&Model) acknowledge_button() {
    control := $&model.button
    control.painted()
}
fn (model $&Model) acknowledge_checkbox() {
    control := $&model.checkbox
    control.painted()
}
fn (model $&Model) acknowledge_progress() {
    control := $&model.progress
    control.painted()
}
fn (model $&Model) acknowledge_scroll() {
    control := $&model.scroll
    control.painted()
}
fn (model $&Model) acknowledge() {
    model.acknowledge_button()
    model.acknowledge_checkbox()
    model.acknowledge_progress()
    model.acknowledge_scroll()
}
fn scene(ech chan ui.Event) int {
    ready := demo.ready(ech) or >>
    if ready.kind == ui.EV_EXIT{ << 0}
    identity := ui.canvas_of(&ready) or >>
    context := draw.context_of(&ready) or >>
    host := widgets.Host{context: context, identity: identity}
    $column := layout.box_style(layout.BOX_COLUMN)
    column.padding = layout.insets(28.0) or >>
    column.gap = 14.0
    column.cross_alignment = layout.CROSS_STRETCH
    $view := host.view(&column, demo.background()) or >>
    leaf := layout.box_style(layout.BOX_LEAF)
    header := view.add(view.root(), &leaf, &layout.Size{width: 400.0, height: 72.0}) or >>
    button_id := view.add(view.root(), &leaf, &layout.Size{width: 280.0, height: 44.0}) or >>
    check_id := view.add(view.root(), &leaf, &layout.Size{width: 280.0, height: 40.0}) or >>
    progress_id := view.add(view.root(), &leaf, &layout.Size{width: 280.0, height: 12.0}) or >>
    $viewport := layout.box_style(layout.BOX_COLUMN)
    viewport.height = 220.0
    viewport.clip = true
    viewport.cross_alignment = layout.CROSS_STRETCH
    viewport.gap = 6.0
    scroll_id := view.add(view.root(), &viewport, &layout.Size{width: 400.0, height: 220.0}) or >>
    $rows :=[]int {}
    for _ in 0:20 {
        row := view.add(scroll_id, &leaf, &layout.Size{width: 400.0, height: 32.0}) or >>
        rows.push(row)
        view.set_boundary(row, true) or >>
    }
    for id in[header, button_id, check_id, progress_id, scroll_id] {view.set_boundary(id, true) or >> }
    for id in[button_id, check_id, scroll_id] {view.set_input(id, true, true) or >> }
    empty := layout.Rect{x: 0.0, y: 0.0, width: 0.0, height: 0.0}
    options := draw.text_options(15.0)
    first := "Complete one action"
    second := "Enable action button"
    $model := Model{button: widgets.button(&identity, button_id, &empty) or >>,
        checkbox: widgets.checkbox(&identity, check_id, &empty, true) or >>,
        progress: widgets.progress(&empty, 0.0) or >>,
        scroll: widgets.scroll(&identity, scroll_id, &empty, widgets.AXIS_VERTICAL, 760.0) or >>,
        button_label: context.layout_text(&first, &options) or >>,
        checkbox_label: context.layout_text(&second, &options) or >>,
        theme: widgets.default_theme(), context: context, count: 0, header_id: header, button_id: button_id, check_id: check_id, progress_id: progress_id, scroll_id: scroll_id, row_ids: rows}
    dispatcher := register_actions(&model, &view) or >>
    $width := 700.0
    $height := 600.0
    while true {
        constraints := layout.tight(width, height) or >>
        submitted := widgets.present($&view, &constraints, $&model) or >>
        if submitted{model.acknowledge()}
        event := demo.next(ech)
        if event.kind == ui.EV_EXIT{ <- }
        if event.window_id != identity.window_id{ -> }
        if event.kind == ui.EV_CANVAS_METRICS{
            metrics := ui.metrics_of(&event) or >>
            if metrics.width > 0.0 && metrics.height > 0.0 {width = metrics.width
                height = metrics.height}
        }
        actions := widgets.dispatch_event($&view, &event, $&model) or >>
        for action in actions{_ := dispatcher.dispatch(&action, $&model, $&view) or >> }
        if event.kind == ui.EV_CANVAS_CLOSED{ <- }
        if ui.is_input_event(event.kind) {
            input := ui.input_of(&event) or >>
            if input.kind == ui.EV_KEY_DOWN && input.key == ui.KEY_ESCAPE{demo.quit() or >> }
        }
        stats := view.stats()
        if stats.recorded_boundaries > 0 || stats.reused_boundaries > 0 {
            println("View: " + str(stats.recorded_boundaries) + " recorded / " + str(stats.reused_boundaries) + " reused boundaries")
        }
    }
    view.dispose()
    // Native close revokes resources; only release user handles while live.
    if demo.alive(&identity) {
        label := $&model.button_label
        label.dispose() or >>
        check_label := $&model.checkbox_label
        check_label.dispose() or >>
    }
    << 0
}
fn worker(ech chan ui.Event) {scene(ech) or(err) {demo.fail(err.msg())
        << }}
fn launch() int {
    $app := demo.app("Xu UI / Retained Controls", 700, 600) or >>
    xu worker(app.event_ch)
    << app.run() or >>
}
fn main() int { << launch() or(err) {eprintln(err.msg())
        << 1}}
