XU XUv0.16.0

#strconv

import: #strconv

counts: 27 functions, 0 methods, 0 types, 0 constants, 0 method sets

Numbers and quoting: atoi/itoa, parse/format, quote/unquote, and append_*.

Examples · API declarations

Examples

These sources come from the official examples repository and can be copied or downloaded. Run them with a local Xu SDK in the complete project; network, database, and desktop examples require the corresponding services or assets.

16_ui_webview/component_workshop/main.xu

Required SDK: Xu 0.10.13-dev.3 · Complete project · Download source · events.xu · events_tools.xu · layout.xu · model.xu · style.xu · view.xu

Xumain.xu
@main

#ui
#ui/widgets
#time
demo #"github.com/xulang-org/examples/ui_demo"

fn scene(events chan ui.Event) int {
    ready := demo.ready(events) >>>
    if ready.kind == ui.EV_EXIT{ << 0}
    $screen := widgets.screen(&ready) >>>
    screen.resize(1180.0, 900.0) >>> (err) {screen.dispose()
        >> err}
    screen.set_min_size(920, 780) >>> (err) {screen.dispose()
        >> err}
    $model := create($&screen) >>> (err) {screen.dispose()
        >> err}
    workshop_loop(events, $&model, $&screen) >>> (err) {screen.dispose()
        >> err}
    screen.dispose()
    << 0
}

fn worker(events chan ui.Event) {scene(events) >>> (err) {demo.fail(err.msg())
        << }}

fn launch() int {
    $app := demo.app("XUUI / Component Workshop", 1180, 900) >>>
    xu worker(app.event_ch)
    << app.run() >>>
}

fn main() int { << launch() >>> (err) {eprintln(err.msg())
        << 1}}

fn next_event(events chan ui.Event, deadline time.Instant) ui.Event{
    if deadline.is_zero() { << <- events}
    delay := deadline.since(time.instant())
    if delay.nanoseconds() <= 0 { << ui.Event{kind: ui.EV_NONE, window_id: 0, menu_id: "", width: 0, height: 0}}
    timer := delay.timer()
    select {event := <- events{timer.stop()
            << event}
        _ := <- timer.c{timer.stop()}}
    << ui.Event{kind: ui.EV_NONE, window_id: 0, menu_id: "", width: 0, height: 0}
}

fn workshop_loop(events chan ui.Event, model $&Workshop, screen $&widgets.Screen) int {
    model.apply_locale(screen) >>>
    screen.set_text(model.title, "XUUI  /  Field") >>>
    model.configure_controls(screen) >>>
    model.controls_dirty = false
    model.apply_appearance(screen) >>>
    model.appearance_dirty = false
    $dispatcher := register_events(& * model, & * screen) >>>
    model.layout(screen) >>>
    while screen.is_open() {
        register_notice_events(& * screen, $&dispatcher) >>>
        now := time.instant()
        model.work(screen, now) >>>
        if model.controls_dirty{model.configure_controls(screen) >>>
            model.controls_dirty = false}
        if model.appearance_dirty{model.apply_appearance(screen) >>>
            model.appearance_dirty = false}
        model.apply_page_options(screen) >>>
        model.layout(screen) >>>
        $deadline := screen.advance(now) >>>
        $timed_actions := false
        for action in screen.take_actions() {
            _ := dispatcher.dispatch_or_fallback(&action, model, screen) >>>
            timed_actions = true
        }
        // Reconcile timer-driven values/styles before computing the next wait.
        if timed_actions{ -> }
        _ := screen.render() >>>
        if model.running &&(deadline.is_zero() || model.deadline.before(deadline)) {deadline = model.deadline}
        event := next_event(events, deadline) >>>
        $actions := screen.event(&event) >>>
        $count := 0
        while screen.is_open() && count < 128 {pending := ui.poll_chan(events)
            if pending.kind == ui.EV_NONE{ <- }
            more := screen.event(&pending) >>>
            for action in more{actions.push(action)}
            count += 1}
        for action in actions{register_notice_events(& * screen, $&dispatcher) >>>
            _ := dispatcher.dispatch_or_fallback(&action, model, screen) >>> }
    }
    << 0
}
Xuevents_tools.xu
@main

#ui/widgets
#ui/webview
#time
#math
#errors
#strconv

fn tool_0_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(0)
    id := page.ids[0]
    $text := "精密 · 清晰 · 可靠。"
    if option == 0 {text = "长中文:输入、选择、复制与原生排版保持一致。Long English text wraps inside its content width. Family 👨‍👩‍👧‍👦 and developer 👩🏽‍💻 remain intact."}
    s.set_text(id, text) >>>
    << 0
}

fn tool_1_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(1)
    id := page.ids[0]
    s.set_selected_button(id, option == 0) >>>
    << 0
}

fn tool_2_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(2)
    id := page.ids[0]
    s.set_selected_button(id, option == 0) >>>
    << 0
}

fn tool_3_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 3
    page := m.page_snapshot(3)
    id := page.ids[0]
    if option == 0 {s.set_value(id, "") >>> }
    if option == 1 {$value := "Long text · 很长的中文与 English · 👩🏽‍💻 🙂 abcdefghijklmnopqrstuvwxyz"
        if index == 4 || index == 6 {value = value + "\n第二行\n第三行\n第四行\n第五行\n第六行\n第七行:内部滚动"}
        s.set_value(id, value) >>> }
    << 0
}

fn tool_4_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 4
    page := m.page_snapshot(4)
    id := page.ids[0]
    if option == 0 {s.set_value(id, "") >>> }
    if option == 1 {$value := "Long text · 很长的中文与 English · 👩🏽‍💻 🙂 abcdefghijklmnopqrstuvwxyz"
        if index == 4 || index == 6 {value = value + "\n第二行\n第三行\n第四行\n第五行\n第六行\n第七行:内部滚动"}
        s.set_value(id, value) >>> }
    << 0
}

fn tool_5_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 5
    page := m.page_snapshot(5)
    id := page.ids[0]
    if option == 0 {s.set_value(id, "") >>> }
    if option == 1 {$value := "Long text · 很长的中文与 English · 👩🏽‍💻 🙂 abcdefghijklmnopqrstuvwxyz"
        if index == 4 || index == 6 {value = value + "\n第二行\n第三行\n第四行\n第五行\n第六行\n第七行:内部滚动"}
        s.set_value(id, value) >>> }
    if option == 2 && index >= 5 {m.field_options_dirty = true
        m.field_options_page = index
        m.variant += 1}
    << 0
}

fn tool_6_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 6
    page := m.page_snapshot(6)
    id := page.ids[0]
    if option == 0 {s.set_value(id, "") >>> }
    if option == 1 {$value := "Long text · 很长的中文与 English · 👩🏽‍💻 🙂 abcdefghijklmnopqrstuvwxyz"
        if index == 4 || index == 6 {value = value + "\n第二行\n第三行\n第四行\n第五行\n第六行\n第七行:内部滚动"}
        s.set_value(id, value) >>> }
    if option == 2 && index >= 5 {m.field_options_dirty = true
        m.field_options_page = index
        m.variant += 1}
    << 0
}

fn tool_7_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(7)
    id := page.ids[0]
    if option == 0 {m.guided = !m.guided
        m.tree_options_dirty = true}
    if option == 1 {s.set_selected(id, 7) >>>
        s.focus(id) >>> }
    if option == 2 {s.set_value(page.ids[1], "") >>>
        s.set_tree_filter(id, "") >>> }
    << 0
}

fn tool_8_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(8)
    id := page.ids[0]
    if option == 0 {m.variant += 1
        s.set_task_progress_mode(id, m.variant % 3) >>> }
    if option == 1 {m.failure = !m.failure
        s.set_selected_button(m.tools[1], m.failure) >>> }
    if option == 2 && m.running{_ := s.complete_task(id, m.revision, widgets.TASK_CANCELLED) >>>
        m.running = false}
    << 0
}

fn tool_9_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(9)
    id := page.ids[0]
    s.set_checkbox_indeterminate(id, option == 0) >>>
    if option > 0 {s.set_toggle_value(id, option == 1) >>> }
    << 0
}

fn tool_10_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(10)
    id := page.ids[0]
    if option == 0 {m.pending_before = s.toggle_value(id) >>>
        s.set_switch_pending(id, true) >>>
        s.set_text(id, "Live preview · Saving…") >>> }
    else {s.set_switch_pending(id, false) >>>
        s.set_toggle_value(id, option == 1 || m.pending_before) >>>
        s.set_invalid(id, option == 2) >>>
        s.set_text(id, "Live preview 实时预览") >>> }
    << 0
}

fn tool_11_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(11)
    id := page.ids[0]
    s.set_toggle_value(id, option == 0) >>>
    << 0
}

fn tool_13_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(13)
    id := page.ids[0]
    if option > 0 {s.set_slider_commit_only(id, option == 1) >>> }
    else {text := s.text_value(page.ids[1]) >>>
        value := strconv.parse_float(&text) >>> (err) {s.set_field_invalid(page.ids[1], true) >>>
            << 0}
        if math.is_nan(value) || math.is_inf(value) || value < 0.0 || value > 100.0 {s.set_field_invalid(page.ids[1], true) >>>
            << 0}
        s.set_number_value(id, value) >>>
        s.set_field_invalid(page.ids[1], false) >>> }
    << 0
}

fn tool_15_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(15)
    id := page.ids[0]
    if option == 0 {s.set_candidate_open(id, true) >>> }
    else {s.set_select_option_enabled(id, 2, false) >>> }
    << 0
}

fn tool_16_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(16)
    id := page.ids[0]
    if option < 2 {s.set_combo_custom_values(id, option == 0) >>> }
    else {s.set_value(id, "") >>> }
    << 0
}

fn tool_17_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(17)
    id := page.ids[0]
    s.select_all(id, option == 0) >>>
    << 0
}

fn tool_18_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(18)
    id := page.ids[0]
    if option == 0 {s.clear_calendar(id) >>> }
    else {value := time.date(2026, 10, 25, 0, 0, 0, 0)
        s.set_calendar_value(id, value) >>> }
    << 0
}

fn tool_19_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(19)
    id := page.ids[0]
    s.set_clock_format(id, option == 0, option != 2) >>>
    << 0
}

fn tool_20_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(20)
    id := page.ids[0]
    $value := "#B4392DFF"
    if option == 0 {value = s.text_value(page.ids[1]) >>> }
    if option == 2 {value = "#3279A080"}
    s.set_color_hex(id, value) >>> (err) {s.set_field_invalid(page.ids[1], true) >>>
        s.set_text(m.status, err.msg()) >>>
        << 0}
    s.set_field_invalid(page.ids[1], false) >>>
    s.remember_color(id) >>>
    s.set_value(page.ids[1], s.color_hex(id) >>>) >>>
    << 0
}

fn tool_24_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(24)
    id := page.ids[0]
    s.set_table_status(id,["", "Loading workspace… 正在加载", "Unable to load. Retry when ready."][option]) >>>
    << 0
}

fn tool_25_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(25)
    id := page.ids[0]
    sizes :=[10, 20, 50, 100]
    size := sizes[s.selected(page.ids[2]) >>>]
    data := s.pagination_data(id) >>>
    if option == 2 {if data.total == 0 {s.set_pagination_data(id, 20000, size) >>> }
        else {s.set_pagination_data(id, 0, size) >>> }}
    else {
        if data.pages == 0 {m.pagination_invalid(s) >>>
            << 0}
        current := s.pagination_data(id) >>>
        $number := current.pages
        if option == 0 {text := s.text_value(page.ids[1]) >>>
            number = strconv.atoi(&text) >>> (err) {m.pagination_invalid(s) >>>
                << 0}
            if number < 1 || number > current.pages{m.pagination_invalid(s) >>>
                << 0}}
        s.set_selected(id, number - 1) >>>
        s.set_field_invalid(page.ids[1], false) >>>
    }
    m.update_pagination(s, true) >>>
    << 0
}

fn tool_27_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(27)
    id := page.ids[0]
    s.set_number_value(id,[0.0, 0.42, 1.0][option]) >>>
    << 0
}

fn tool_28_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(28)
    id := page.ids[0]
    if option < 2 {s.set_running(id, option == 0) >>> }
    else {m.spinner_options_dirty = true}
    << 0
}

fn tool_30_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(30)
    id := page.ids[0]
    if option == 0 {m.unread += 1}
    if option == 1 {m.unread = 0}
    if option == 2 {m.unread = 123}
    m.update_badges(s) >>>
    << 0
}

fn tool_32_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(32)
    id := page.ids[0]
    if option == 0 {s.set_scroll_offset(id, 0.0) >>> }
    if option == 1 {s.set_scroll_offset(id, 100000.0) >>> }
    if option == 2 {s.focus(page.ids[page.ids.len - 1]) >>> }
    << 0
}

fn tool_33_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 33
    page := m.page_snapshot(33)
    id := page.ids[0]
    if option == 2 {m.variant =(m.variant + 1) % 4
        m.popover_options_dirty = true}
    if option == 0 {if index == 33 {m.open_preferences(s) >>> } else {s.open(page.ids[1]) >>> }}
    else if option == 1 {s.close_overlay(page.ids[1]) >>> }
    << 0
}

fn tool_34_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(34)
    id := page.ids[0]
    if option == 2 {m.tooltip_side =(m.tooltip_side + 1) % 4
        m.appearance_dirty = true
        s.focus(id) >>>
        s.set_text(m.status, "Tooltip · " +["Bottom 下", "Top 上", "Left 左", "Right 右"][m.tooltip_side] + " · flips at the viewport edge") >>> }
    if option == 0 {s.focus(id) >>> }
    else if option == 1 {s.focus(0) >>> }
    << 0
}

fn tool_35_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(35)
    id := page.ids[0]
    $notice := widgets.notice(&"Workspace saved · 工作区已保存")
    notice.action = "Undo"
    notice.key = "workspace-save"
    if option == 1 {notice.text = "Changes restored · 已撤销"
        notice.key = "workspace-undo"}
    $count := 1
    if option == 2 {count = 3}
    for i in 0:count{if count > 1 {notice.key = "notice-" + str(i)
            notice.text = "Queued notice " + str(i + 1)}
        _ := s.enqueue_notice(&notice) >>> }
    << 0
}

fn tool_36_update(m $&Workshop, s $&widgets.Screen, option int) int {
    index := 36
    page := m.page_snapshot(36)
    id := page.ids[0]
    if option == 0 {if index == 33 {m.open_preferences(s) >>> } else {s.open(page.ids[1]) >>> }}
    else if option == 1 {s.close_overlay(page.ids[1]) >>> }
    << 0
}

fn tool_37_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(37)
    id := page.ids[0]
    if option == 0 {native := s.webview_native(id) >>>
        client := webview.bind_native(&native) >>>
        client.command("reload") >>> }
    else {s.set_visible(id, option == 2) >>> }
    << 0
}

fn tool_38_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(38)
    id := page.ids[0]
    if option == 0 {m.group_disabled = !m.group_disabled
        m.controls_dirty = true}
    if option == 1 {m.group_vertical = !m.group_vertical}
    if option == 2 {m.group_joined = !m.group_joined}
    m.appearance_dirty = true
    << 0
}

fn tool_39_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(39)
    id := page.ids[0]
    if option == 0 {m.revision += 1
        english := (s.selected(m.language) >>>) == 1
        series := chart_data_locale(m.chart_kind, m.revision, english)
        s.set_chart_series(id, &series) >>>
        m.chart_paused = false}
    if option == 1 {s.reset_chart_viewport(id) >>> }
    if option == 2 {m.chart_paused = !m.chart_paused
        if m.chart_paused{s.pause_chart_animation(id) >>> }
        else {s.resume_chart_animation(id) >>> }}
    << 0
}

fn tool_40_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(40)
    id := page.ids[0]
    if option == 0 {m.range_push = !m.range_push}
    if option == 1 {m.range_commit = !m.range_commit}
    if option == 2 {m.range_vertical = !m.range_vertical}
    m.appearance_dirty = true
    m.range_summary(s, false) >>>
    << 0
}

fn tool_41_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(41)
    id := page.ids[0]
    if option == 0 {m.split_vertical = !m.split_vertical
        m.appearance_dirty = true}
    if option == 1 {m.split_collapsed =(s.split_layout(id) >>>).collapsed == 0
        s.set_split_collapsed(id, 0, m.split_collapsed) >>> }
    if option == 2 {m.split_collapsed = false
        s.set_split_ratio(id, 0.4) >>> }
    << 0
}

fn tool_42_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(42)
    id := page.ids[0]
    if option == 0 {s.focus(id) >>>
        s.set_menu_open(id, true) >>> }
    if option == 1 {m.menu_stays_open = !m.menu_stays_open
        m.appearance_dirty = true}
    if option == 2 {m.menu_export_disabled = !m.menu_export_disabled
        s.set_menu_item_enabled(id, 30, !m.menu_export_disabled) >>> }
    << 0
}

fn tool_43_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(43)
    id := page.ids[0]
    if option == 0 {s.focus(id) >>>
        s.set_menu_open(id, true) >>> }
    if option == 1 {m.menu_stays_open = !m.menu_stays_open
        m.appearance_dirty = true}
    if option == 2 {m.menu_export_disabled = !m.menu_export_disabled
        s.set_menu_item_enabled(id, 30, !m.menu_export_disabled) >>> }
    << 0
}

fn tool_44_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(44)
    id := page.ids[0]
    if option == 0 {s.focus(id) >>>
        s.set_menu_open(id, true) >>> }
    if option == 1 {m.menu_stays_open = !m.menu_stays_open
        m.appearance_dirty = true}
    if option == 2 {m.menu_export_disabled = !m.menu_export_disabled
        s.set_menu_item_enabled(id, 30, !m.menu_export_disabled) >>> }
    << 0
}

fn tool_45_update(m $&Workshop, s $&widgets.Screen, option int) int {
    page := m.page_snapshot(45)
    id := page.ids[0]
    if option == 0 {s.set_value(id, "") >>> }
    if option == 1 {s.set_value(id, "Xu-密码🙂") >>> }
    if option == 2 {s.focus(id) >>> }
    << 0
}

fn tool_none_update(m $&Workshop, s $&widgets.Screen, option int) int { << 0}

fn tool_clicked_update(m $&Workshop, s $&widgets.Screen, a &widgets.Action) int {
    handlers :=[tool_0, tool_1, tool_2, tool_3, tool_4, tool_5, tool_6, tool_7, tool_8, tool_9, tool_10, tool_11, tool_none, tool_13, tool_none, tool_15, tool_16, tool_17, tool_18, tool_19, tool_20, tool_none, tool_none, tool_none, tool_24, tool_25, tool_none, tool_27, tool_28, tool_none, tool_30, tool_none, tool_32, tool_33, tool_34, tool_35, tool_36, tool_37, tool_38, tool_39, tool_40, tool_41, tool_42, tool_43, tool_44, tool_45]
    for i in 0:3 {if a.id == m.tools[i] {handler := handlers[m.selected]
            message := handler(m, s, i)
            if message.len > 0 { >> errors.new(message)}
            << 0}}
    << 0
}

fn tool_clicked(m $&Workshop, s $&widgets.Screen, a &widgets.Action) string {
    tool_clicked_update(m, s, a) >>> (err) { << err.msg()}
    << ""
}

fn tool_0(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_0_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_1(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_1_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_2(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_2_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_3(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_3_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_4(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_4_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_5(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_5_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_6(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_6_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_7(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_7_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_8(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_8_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_9(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_9_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_10(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_10_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_11(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_11_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_13(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_13_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_15(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_15_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_16(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_16_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_17(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_17_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_18(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_18_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_19(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_19_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_20(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_20_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_24(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_24_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_25(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_25_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_27(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_27_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_28(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_28_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_30(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_30_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_32(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_32_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_33(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_33_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_34(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_34_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_35(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_35_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_36(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_36_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_37(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_37_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_38(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_38_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_39(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_39_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_40(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_40_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_41(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_41_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_42(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_42_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_43(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_43_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_44(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_44_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_45(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_45_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}

fn tool_none(m $&Workshop, s $&widgets.Screen, option int) string {
    tool_none_update(m, s, option) >>> (err) { << err.msg()}
    << ""
}
Xumodel.xu
@main

#ui/widgets
#time
#strconv

struct Page{group int
    ids []int}
// A state transition may reconfigure Workshop while these control IDs are in
// use. Copy the IDs; Screen keeps ownership of the actual controls.

fn (m &Workshop) page_snapshot(index int) Page{
    group := m.pages[index].group
    $ids :=[]int {}
    for id in m.pages[index].ids{ids.push(id)}
    << Page{group: group, ids: ids}
}

struct Workshop{
    controls_dirty bool
    field_options_page int
    appearance_dirty bool
    tree_options_dirty bool
    field_options_dirty bool
    spinner_options_dirty bool
    popover_options_dirty bool
    tooltip_side int
    range_push bool
    range_vertical bool
    range_commit bool
    split_vertical bool
    split_collapsed bool
    menu_stays_open bool
    menu_export_disabled bool
    chart_kind int
    chart_paused bool
    group_vertical bool
    group_joined bool
    group_disabled bool
    pages []Page
    names []string
    navigation int
    language int
    preview int
    title int
    caption int
    status int
    theme int
    density int
    scale int
    state int
    custom int
    reduced int
    motion int
    focus int
    tools []int
    selected int
    customized bool
    guided bool
    slow bool
    variant int
    revision int
    completed int
    running bool
    failure bool
    pending_before bool
    pagination_error bool
    preferences_name string
    preferences_history bool
    unread int
    online bool
    checksum int
    deadline time.Instant
}

fn create(s $&widgets.Screen) Workshop{
    labels := names()
    $pages :=[]Page{}
    preview := s.scroll_area(widgets.AXIS_VERTICAL, 760.0) >>>
    for index in 0:labels.len{page := new_page(s, index) >>>
        s.set_parent(page.group, preview) >>>
        pages.push(page)}
    title := s.label("", 25.0, false) >>>
    caption := s.label("", 13.0, true) >>>
    s.set_label_wrapped(caption, true) >>>
    status := s.label("Hover · press · Tab · arrows · Escape / 使用真实鼠标与键盘状态", 12.0, true) >>>
    s.set_label_wrapped(status, true) >>>
    navigation := s.list_box(&labels, 5) >>>
    language := s.select_box(&["中文", "English", "Custom"], 1) >>>
    _ := s.tooltip(language, "Component language · 组件默认文案与格式") >>>
    tools :=[s.button("Option 1") >>>, s.button("Option 2") >>>, s.button("Option 3") >>>]
    for tool in tools{s.set_parent(tool, preview) >>> }
    << Workshop{controls_dirty: true, field_options_page: 5, appearance_dirty: true, tree_options_dirty: false, field_options_dirty: false, spinner_options_dirty: false, popover_options_dirty: false, tooltip_side: widgets.OVERLAY_TOP, range_push: false, range_vertical: false, range_commit: false, split_vertical: false, split_collapsed: false, menu_stays_open: false, menu_export_disabled: false, chart_kind: 0, chart_paused: false, group_vertical: false, group_joined: true, group_disabled: false, language: language, pages: pages, names: labels, navigation: navigation, preview: preview, title: title, caption: caption, status: status,
        theme: s.select_box(&["Light 亮色", "Dark 暗色", "Contrast 对比"], 1) >>>,
        density: s.select_box(&["Normal 常规", "Compact 紧凑", "Touch 触控"], 0) >>>,
        scale: s.select_box(&["Text 100%", "Text 140%", "Text 200%"], 0) >>>,
        state: s.select_box(&["Normal 普通", "Disabled 禁用", "Read only 只读", "Invalid 错误"], 0) >>>,
        custom: s.button("Default 默认") >>>, reduced: s.checkbox("Reduce 减少动效", false) >>>,
        motion: s.button("Motion 160ms") >>>, focus: s.button("Focus sample") >>>,
        tools: tools, selected: 5, customized: false, guided: true, slow: false,
        variant: 0, revision: 0, completed: 0, running: false, failure: false, pending_before: false, pagination_error: false, preferences_name: "XU workspace", preferences_history: true, unread: 12, online: true, checksum: 0, deadline: time.zero_instant()}
}

fn (m $&Workshop) apply_locale(s $&widgets.Screen) int {
    index := s.selected(m.language) >>>
    $locale := widgets.locale_en_us()
    if index != 1 {locale = widgets.locale_zh_cn()}
    if index == 2 {
        locale.set_text(widgets.TEXT_TODAY, "回到今天") >>>
        locale.set_text(widgets.TEXT_CLEAR, "移除日期") >>>
        locale.set_text(widgets.TEXT_DATE_PLACEHOLDER, "预约日期") >>>
        locale.set_text(widgets.TEXT_DATE_PATTERN, "{day}.{month}.{year}") >>>
        locale.set_text(widgets.TEXT_DECIMAL_SEPARATOR, ",") >>>
        locale.set_text(widgets.TEXT_GROUP_SEPARATOR, " ") >>>
        locale.set_weekdays(&["周日", "周一", "周二", "周三", "周四", "周五", "周六"]) >>>
    }
    s.set_locale(&locale) >>>
    pages := &m.pages
    pagination := &pages[25]
    sizes :=[10, 20, 50, 100]
    for i in 0:4 {s.set_option_text(pagination.ids[2], i, locale.count_text(widgets.TEXT_ITEMS_PER_PAGE, sizes[i]) >>>) >>> }
    m.update_pagination(s, false) >>>
    // Business copy is supplied by the app, never silently translated by the widget.
    english := index == 1
    m.names = names_locale(english)
    for i in 0:m.names.len {s.set_option_text(m.navigation, i, m.names[i]) >>> }
    $themes :=["亮色", "暗色", "高对比"]
    $densities :=["常规", "紧凑", "触控"]
    $scales :=["字号 100%", "字号 140%", "字号 200%"]
    $states :=["普通", "禁用", "只读", "错误"]
    $choices :=["工作区", "历史", "输出"]
    $segments :=["日", "周", "月"]
    $group_titles :=["独立操作", "单选", "多选"]
    $btn_samples :=["构建项目", "打开工作区 · 右侧图标", "保存修改 · 上方图标", "下载 SDK · 下方图标", "次要按钮", "描边按钮", "幽灵按钮", "危险操作", "轻量按钮"]
    $overlay_triggers :=["打开浮层", "聚焦或悬停查看帮助", "发送通知", "打开确认弹窗"]
    if english{
        themes =["Light", "Dark", "Contrast"]
        densities =["Normal", "Compact", "Touch"]
        scales =["Text 100%", "Text 140%", "Text 200%"]
        states =["Normal", "Disabled", "Read only", "Invalid"]
        choices =["Workspace", "History", "Output"]
        segments =["Day", "Week", "Month"]
        group_titles =["Actions", "Single", "Multiple"]
        btn_samples =["Build project", "Open workspace · trailing", "Save changes · above", "Download SDK · below", "Secondary", "Outline", "Ghost", "Danger", "Subtle"]
        overlay_triggers =["Open popover", "Focus or hover for help", "Enqueue notification", "Open confirmation"]
    }
    for i in 0:9 {s.set_text(m.pages[1].ids[i], btn_samples[i]) >>> }
    for i in 0:4 {s.set_text(m.pages[33 + i].ids[0], overlay_triggers[i]) >>> }
    for i in 0:3 {
        s.set_option_text(m.theme, i, themes[i]) >>>
        s.set_option_text(m.density, i, densities[i]) >>>
        s.set_option_text(m.scale, i, scales[i]) >>>
        s.set_option_text(m.pages[21].ids[0], i, choices[i]) >>>
        s.set_option_text(m.pages[22].ids[0], i, segments[i]) >>>
        s.set_text(m.pages[38].ids[3 + i], group_titles[i]) >>>
    }
    for i in 0:4 {s.set_option_text(m.state, i, states[i]) >>> }
    page := &m.pages[8]
    if english {
        s.set_text(page.ids[0], "Build project") >>>
        s.set_task_labels(page.ids[0], "Building", "Built · run again", "Retry build", "Build again") >>>
        s.set_text(m.reduced, "Reduce motion") >>>
        s.set_text(m.focus, "Focus sample") >>>
        s.set_text(m.pages[9].ids[0], "Keep generated files") >>>
        s.set_text(m.pages[10].ids[0], "Live preview") >>>
        s.set_text(m.pages[11].ids[0], "Use workspace settings") >>>
        s.set_text(m.pages[25].ids[4], "Go") >>>
        s.set_text(m.pages[28].ids[1], "Loading assets…") >>>
        s.set_text(m.pages[29].ids[1], "SECTION") >>>
        s.set_text(m.pages[30].ids[1], "Inbox") >>>
        s.set_text(m.pages[30].ids[4], "Synced") >>>
        s.set_text(m.pages[30].ids[9], "Workspace") >>>
        s.set_text(m.pages[31].ids[4], "Workspace configuration") >>>
        s.set_text(m.pages[31].ids[6], "Open workspace") >>>
        s.set_text(m.pages[33].ids[2], "Workspace preferences") >>>
        s.set_text(m.pages[33].ids[5], "Keep history") >>>
        s.set_text(m.pages[33].ids[6], "Cancel") >>>
        s.set_text(m.pages[33].ids[7], "Apply") >>>
        s.set_text(m.pages[36].ids[2], "Cancel") >>>
        s.set_text(m.pages[36].ids[3], "Apply changes") >>>
        s.set_text(m.pages[41].ids[5], "Open workspace") >>>
        s.set_text(m.pages[41].ids[6], "Refresh preview") >>>
        s.set_text(m.pages[45].ids[1], "Show password") >>>
        s.set_text(m.pages[45].ids[2], "Password") >>>
    } else {
        s.set_text(page.ids[0], "构建项目") >>>
        s.set_task_labels(page.ids[0], "正在构建", "已构建 · 再次运行", "重试构建", "再次构建") >>>
        s.set_text(m.reduced, "减少动效") >>>
        s.set_text(m.focus, "聚焦示例") >>>
        s.set_text(m.pages[9].ids[0], "保留生成产物") >>>
        s.set_text(m.pages[10].ids[0], "实时预览") >>>
        s.set_text(m.pages[11].ids[0], "使用工作区设置") >>>
        s.set_text(m.pages[25].ids[4], "跳转") >>>
        s.set_text(m.pages[28].ids[1], "正在加载资源…") >>>
        s.set_text(m.pages[29].ids[1], "章节分隔") >>>
        s.set_text(m.pages[30].ids[1], "收件箱") >>>
        s.set_text(m.pages[30].ids[4], "已同步") >>>
        s.set_text(m.pages[30].ids[9], "工作区") >>>
        s.set_text(m.pages[31].ids[4], "工作区配置") >>>
        s.set_text(m.pages[31].ids[6], "打开工作区") >>>
        s.set_text(m.pages[33].ids[2], "工作区偏好设置") >>>
        s.set_text(m.pages[33].ids[5], "保留历史记录") >>>
        s.set_text(m.pages[33].ids[6], "取消") >>>
        s.set_text(m.pages[33].ids[7], "应用") >>>
        s.set_text(m.pages[36].ids[2], "取消") >>>
        s.set_text(m.pages[36].ids[3], "应用更改") >>>
        s.set_text(m.pages[41].ids[5], "打开工作区") >>>
        s.set_text(m.pages[41].ids[6], "刷新预览") >>>
        s.set_text(m.pages[45].ids[1], "显示密码") >>>
        s.set_text(m.pages[45].ids[2], "密码") >>>
    }
    m.update_badges(s) >>>
    m.range_summary(s, false) >>>
    m.appearance_dirty = true
    $group_labels :=[["Undo", "Redo", "Save"],["Day", "Week", "Month"],["Bold", "Italic", "Underline"]]
    $type_labels :=["Line", "Area", "Bar", "Scatter", "Donut",
        "Step", "StackArea", "StackBar", "H-Bar", "Pie",
        "Rose", "Radar", "Gauge", "Bubble", "Histogram",
        "Boxplot", "Heatmap", "Candle", "Waterfall", "Funnel",
        "Spline", "SplineArea", "PolarBar", "Treemap", "Sankey",
        "Violin", "Bullet", "Lollipop", "Dumbbell", "Pareto"]
    if !english {
        group_labels =[["撤销", "重做", "保存"],["日", "周", "月"],["加粗", "斜体", "下划线"]]
        type_labels =["折线", "面积", "柱状", "散点", "环形",
            "阶梯", "堆叠面积", "堆叠柱状", "水平条形", "饼图",
            "玫瑰", "雷达", "仪表盘", "气泡", "直方图",
            "箱线", "热力", "K 线", "瀑布", "漏斗",
            "样条线", "样条面积", "极坐标环", "树图", "桑基",
            "小提琴", "子弹图", "棒棒糖", "哑铃", "帕累托"]
    }
    for mode in 0:3 {for i in 0:3 {s.set_button_group_label(m.pages[38].ids[mode],(i + 1) * 10, group_labels[mode][i]) >>> }}
    for r in 0:6 {
        gid := m.pages[39].ids[1 + r]
        for k in 0:5 {
            key := r * 5 + k + 1
            s.set_button_group_label(gid, key, type_labels[key - 1]) >>>
        }
    }
    series := chart_data_locale(m.chart_kind, m.revision, english)
    s.set_chart_series(m.pages[39].ids[0], &series) >>>
    translated := workshop_menu_items(english)
    for page_index in 42:45 {for item in translated{s.set_menu_item_label(m.pages[page_index].ids[0], item.key, item.label) >>> }}
    $menu_labels :=["Actions", "Right click here", "Save"]
    if !english {menu_labels =["操作", "右键打开菜单", "保存"]}
    for page_index in 42:45 {s.set_menu_label(m.pages[page_index].ids[0], menu_labels[page_index - 42]) >>> }
    m.controls_dirty = true

    << 0
}

fn max_int_local(a int, b int) int {if a > b{ << a}
    << b}

fn (m &Workshop) open_preferences(s $&widgets.Screen) int {
    pages := &m.pages
    page := &pages[33]
    s.set_value(page.ids[4], m.preferences_name) >>>
    s.set_toggle_value(page.ids[5], m.preferences_history) >>>
    s.set_field_invalid(page.ids[4], false) >>>
    s.open(page.ids[1]) >>>
    << 0
}

fn (m $&Workshop) pagination_invalid(s $&widgets.Screen) int {
    m.pagination_error = true
    page := m.page_snapshot(25)
    data := s.pagination_data(page.ids[0]) >>>
    s.set_field_invalid(page.ids[1], true) >>>
    $message := "Enter a page from 1 to " + str(data.pages) + "."
    if(s.selected(m.language) >>>) != 1 {message = "请输入 1 至 " + str(data.pages) + " 之间的页码。"}
    if data.pages == 0 {message = "No pages / 暂无数据,无法跳页。"}
    s.set_text(page.ids[5], message) >>>
    << 0
}

fn (m $&Workshop) update_pagination(s $&widgets.Screen, sync_input bool) int {
    page := m.page_snapshot(25)
    data := s.pagination_data(page.ids[0]) >>>
    s.set_text(page.ids[3], s.pagination_summary(page.ids[0]) >>>) >>>
    s.set_enabled(page.ids[1], data.pages > 0) >>>
    s.set_enabled(page.ids[4], data.pages > 0) >>>
    if sync_input{
        m.pagination_error = false
        s.set_field_invalid(page.ids[1], false) >>>
        $text := ""
        if data.current >= 0 {text = str(data.current + 1)}
        s.set_value(page.ids[1], text) >>> }
    if m.pagination_error{m.pagination_invalid(s) >>> }
    else {$hint := "Enter ↵ · 1–" + str(data.pages)
        if data.pages == 0 {hint = "No data / 暂无数据"}
        s.set_text(page.ids[5], hint) >>> }
    << 0
}

fn (m &Workshop) update_badges(s $&widgets.Screen) int {
    english := (s.selected(m.language) >>>) == 1
    pages := &m.pages
    page := &pages[30]
    s.set_badge_count(page.ids[0], m.unread, 99, false) >>>
    $summary := str(m.unread) + " 条未读 · 点击收件箱清零,徽标随数字收紧。"
    if english {summary = str(m.unread) + " unread · Click Inbox to clear; badge tightens with digits."}
    s.set_text(page.ids[6], summary) >>>
    s.set_visible(page.ids[3], m.online) >>>
    $name := "林文 · 离线"
    if m.online{name = "林文 · 在线"}
    if english {
        name = "Lin Wen · Away"
        if m.online{name = "Lin Wen · Available"}
    }
    s.set_text(page.ids[7], name) >>>
    << 0
}

fn (m $&Workshop) work(s $&widgets.Screen, now time.Instant) int {
    if !m.running || now.before(m.deadline) { << 0}
    for i in 0:5000 {m.checksum =(m.checksum + i *(m.completed + 1)) % 1000003}
    m.completed += 1
    id := m.pages[8].ids[0]
    _ := s.set_task_progress(id, m.revision, f64(m.completed) / 48.0) >>>
    m.deadline = now.add(time.milliseconds(35) >>>) >>>
    if m.failure && m.completed == 18 {_ := s.complete_task(id, m.revision, widgets.TASK_ERROR) >>>
        m.running = false
        m.failure = false
        if m.selected == 8 {s.set_selected_button(m.tools[1], false) >>> }}
    else if m.completed == 48 {_ := s.complete_task(id, m.revision, widgets.TASK_SUCCESS) >>>
        m.running = false}
    << 0
}

fn (m &Workshop) range_summary(s $&widgets.Screen, committed bool) int {
    english := (s.selected(m.language) >>>) == 1
    pages := &m.pages
    page := &pages[40]
    values := s.range_values(page.ids[0]) >>>
    $prefix := "预览"
    if committed{prefix = "已提交"}
    if english {
        prefix = "Preview"
        if committed{prefix = "Committed"}
    }
    text := strconv.format_float(values.lower, 102, 0) >>>
    upper := strconv.format_float(values.upper, 102, 0) >>>
    s.set_text(page.ids[1], prefix + " · " + text + " – " + upper) >>>
    << 0
}

fn (m $&Workshop) configure_controls(s $&widgets.Screen) int {
    position := m.selected
    page := m.page_snapshot(position)
    s.set_text(m.title, "XUUI  /  " + m.names[position]) >>>
    s.set_selected(m.navigation, position) >>>
    id := page.ids[0]
    state := s.selected(m.state) >>>
    s.set_enabled(id, state != 1) >>>
    if position == 45 {s.set_enabled(page.ids[1], state != 1) >>> }
    if position == 25 {s.set_enabled(page.group, state != 1) >>> }
    if read_only(m.selected) {s.set_read_only(id, state == 2) >>> }
    if invalid(m.selected) || m.selected == 31 || m.selected == 32 {s.set_invalid(id, state == 3) >>> }
    if position == 1 {for i in 0:9 {s.set_enabled(page.ids[i], state != 1) >>> }}
    if position == 38 {for i in 0:3 {
            s.set_button_group_item_enabled(page.ids[i], 20, !m.group_disabled) >>>
            s.set_enabled(page.ids[i], state != 1) >>>
            s.set_read_only(page.ids[i], state == 2) >>>
        }}
    if position == 39 {
        for r in 0:6 {
            gid := page.ids[1 + r]
            for k in 0:5 {
                key := r * 5 + k + 1
                s.set_button_group_checked(gid, key, m.chart_kind + 1 == key) >>>
            }
            s.set_enabled(gid, state != 1) >>>
            s.set_read_only(gid, state == 2) >>>
        }
    }
    english := (s.selected(m.language) >>>) == 1
    s.set_enabled(m.motion, animated(m.selected) ||(m.selected >= 33 && m.selected <= 36)) >>>
    s.set_selected_button(m.custom, m.customized) >>>
    $custom_text := "默认样式"
    if m.customized{custom_text = "自定义样式"}
    if english {
        custom_text = "Default"
        if m.customized{custom_text = "Custom"}
    }
    s.set_text(m.custom, custom_text) >>>
    $motion_text := "动效 160ms"
    if m.slow{motion_text = "动效 420ms"}
    if english {
        motion_text = "Motion 160ms"
        if m.slow{motion_text = "Motion 420ms"}
    }
    s.set_text(m.motion, motion_text) >>>
    caption := description_locale(m.selected, english)
    s.set_text(m.caption, caption) >>>
    $state_text := "悬停 · 按下 · Tab · 方向键 · Escape(使用真实鼠标与键盘状态)"
    if english {state_text = "Hover · press · Tab · arrows · Escape"}
    if(state == 2 && !read_only(m.selected)) ||(state == 3 && !invalid(m.selected) && m.selected != 31 && m.selected != 32) {
        state_text = "此组件没有该状态。预览保持其实际语义。"
        if english {state_text = "This component does not have that state; preview keeps its actual semantics."}
    }
    s.set_text(m.status, state_text) >>>
    labels := tool_labels_locale(m.selected, english)
    for i in 0:3 {s.set_text(m.tools[i], labels[i]) >>>
        s.set_selected_button(m.tools[i], false) >>>
        s.set_enabled(m.tools[i], labels[i].len > 0) >>>
        s.set_visible(m.tools[i], labels[i].len > 0) >>> }
    if m.selected == 8 {s.set_selected_button(m.tools[1], m.failure) >>> }
    s.set_visible(m.focus, focusable_sample(m.selected)) >>>
    << 0
}

API declarations

atoi

XuAPI
pub fn atoi(s &string) int

Purpose

Convert decimal numeric text to an integer, for configuration or user input.

Parameters

s &string
Complete numeric text; whitespace and base prefixes are not removed.

Returns

The parsed integer, within the range of int.

Usage notes

  • Accepts digits with an optional leading minus; no leading +, whitespace or 0x prefix.

Error handling

  • An unsupported base throws strconv: unsupported base; invalid or out-of-range input also throws and must be handled or propagated.

See usage example

itoa

XuAPI
pub fn itoa(n int) string

Purpose

Format an integer as decimal text for output or messages.

Parameters

n int
Integer to format.

Returns

Numeric string; hexadecimal uses lowercase letters without an automatic base prefix.

format_bool

XuAPI
pub fn format_bool(b bool) string

Purpose

Format a boolean as predictable lowercase text for configuration or messages.

Parameters

b bool
Boolean to format.

Returns

The string true or false.

parse_bool

XuAPI
pub fn parse_bool(s &string) bool

Purpose

Parse common boolean text for environment variables and configuration switches.

Parameters

s &string
Complete input text; whitespace is not trimmed.

Returns

1, t, T, true, TRUE and True yield true; 0, f, F, false, FALSE and False yield false.

Error handling

  • Other spellings, including mixed case or surrounding whitespace, throw strconv: invalid bool.

format_int

XuAPI
pub fn format_int(n int, base int) string

Purpose

Format an integer as explicit-base text for output or messages.

Parameters

n int
Integer to format.
base int
Base: only 2, 8, 10 and 16 are supported; prefixes such as 0x do not select it automatically.

Returns

Numeric string; hexadecimal uses lowercase letters without an automatic base prefix.

Error handling

  • An unsupported base throws.

parse

XuAPI
pub fn parse(s &string, base int) int

Purpose

Convert explicit-base numeric text to an integer, for configuration or user input.

Parameters

s &string
Complete numeric text; whitespace and base prefixes are not removed.
base int
Base: only 2, 8, 10 and 16 are supported; prefixes such as 0x do not select it automatically.

Returns

The parsed integer, within the range of int.

Usage notes

  • Accepts digits with an optional leading minus; no leading +, whitespace or 0x prefix.

Error handling

  • An unsupported base throws strconv: unsupported base; invalid or out-of-range input also throws and must be handled or propagated.

Call example

Choose a base and handle a parse error

XuLocal SDK example
@main
#strconv

fn main() {
    text := "ff"
    value := strconv.parse(&text, 16) >>> (err) {
        println(err.msg())
        <<
    }
    println(value)
    bad := "0xff"
    _ := strconv.parse(&bad, 16) >>> (err) {
        println(err.msg())
        <<
    }
}

Expected output:

255
parse_int: invalid integer

format_uint

XuAPI
pub fn format_uint(n int, base int) string

Purpose

Format an integer as explicit-base text for output or messages.

Parameters

n int
Integer to format.
base int
Base: only 2, 8, 10 and 16 are supported; prefixes such as 0x do not select it automatically.

Returns

Numeric string; hexadecimal uses lowercase letters without an automatic base prefix.

Error handling

  • Negative input throws strconv: unsigned of negative; an unsupported base also throws.

parse_uint

XuAPI
pub fn parse_uint(s &string, base int) int

Purpose

Convert explicit-base numeric text to an integer, for configuration or user input.

Parameters

s &string
Complete numeric text; whitespace and base prefixes are not removed.
base int
Base: only 2, 8, 10 and 16 are supported; prefixes such as 0x do not select it automatically.

Returns

The parsed integer, within the range of int.

Usage notes

  • Accepts nonnegative integers only; the int result cannot represent values above the signed integer maximum.

Error handling

  • An unsupported base throws strconv: unsupported base; invalid or out-of-range input also throws and must be handled or propagated.

quote

XuAPI
pub fn quote(s &string) string

Purpose

Quote byte text with double quotes so logs can show newlines, quotes and invisible bytes.

Parameters

s &string
Original text, passed as a read-only borrow; this call does not modify it.

Returns

Double-quoted text preserving printable UTF-8 runes while escaping backslashes, quotes, control/non-printable runes (\a, \b, \t, \n, \v, \f, \r, \xHH, \uXXXX, \UXXXXXXXX), and invalid UTF-8 bytes (\xHH).

Usage notes

  • Printable Unicode characters remain UTF-8; use quote_to_ascii when ASCII-only \u/\U escaping is required.

Call example

Round-trip quoted text

XuLocal SDK example
@main
#strconv

fn main() {
    text := "甲"
    quoted := strconv.quote(&text)
    println(quoted)
    decoded := strconv.unquote(&quoted) >>> (err) {
        println(err.msg())
        <<
    }
    println(decoded)
}

Expected output:

"甲"
甲

unquote

XuAPI
pub fn unquote(s &string) string

Purpose

Remove double quotes or backticks and decode escapes, for example text produced by quote or quote_to_ascii.

Parameters

s &string
Complete text enclosed in matching double quotes or backticks.

Returns

The decoded string without surrounding quotes (backtick raw strings strip \r).

Usage notes

  • Double-quoted mode supports \a, \b, \f, \n, \r, \t, \v, \\, \", \xHH, \uXXXX, \UXXXXXXXX, and 3-digit octal \ooo, and rejects unescaped newlines.

Error handling

  • Missing matching quotes, unescaped newlines, or nested backticks throw strconv: invalid quoted string; unsupported, invalid, or incomplete escapes throw strconv: invalid escape.

quote_rune

XuAPI
pub fn quote_rune(r int) string

Purpose

Represent a Unicode code point as a single-quoted character literal.

Parameters

r int
Unicode code point; an invalid scalar is replaced with U+FFFD.

Returns

Single-quoted character text; printable non-ASCII characters remain UTF-8 and control characters use escapes.

is_print

XuAPI
pub fn is_print(r int) bool

Purpose

Whether it is print.

Parameters

r int
rune (Unicode code point) (int)

Returns

bool — true or false

is_graphic

XuAPI
pub fn is_graphic(r int) bool

Purpose

Whether it is graphic.

Parameters

r int
rune (Unicode code point) (int)

Returns

bool — true or false

quote_rune_to_ascii

XuAPI
pub fn quote_rune_to_ascii(r int) string

Purpose

Represent a Unicode code point as a single-quoted character literal.

Parameters

r int
Unicode code point; an invalid scalar is replaced with U+FFFD.

Returns

Single-quoted character text; non-ASCII characters use escapes.

quote_to_ascii

XuAPI
pub fn quote_to_ascii(s &string) string

Purpose

Represent Unicode text as an ASCII-only double-quoted string.

Parameters

s &string
Original text, passed as a read-only borrow; this call does not modify it.

Returns

Quoted ASCII text, using \u or \U for non-ASCII characters and \xHH for invalid UTF-8 bytes.

Usage notes

  • unquote decodes the \uXXXX, \UXXXXXXXX, and \xHH escapes produced by this function for full round trips within this package.

append_quote_to_ascii

XuAPI
pub fn append_quote_to_ascii(dst string, s &string) string

Purpose

Append quote to ascii.

Parameters

dst string
destination (string)
s &string
source string (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

append_quote_rune_to_ascii

XuAPI
pub fn append_quote_rune_to_ascii(dst string, r int) string

Purpose

Append quote rune to ascii.

Parameters

dst string
destination (string)
r int
rune (Unicode code point) (int)

Returns

string — result string

quoted_prefix

XuAPI
pub fn quoted_prefix(s &string) string

Purpose

Extract a complete quoted prefix, retaining its quotes for processing text that follows.

Parameters

s &string
Text beginning with a double quote, single quote, or backtick.

Returns

Prefix through the first unescaped matching closing quote, including both quotes.

Usage notes

  • This finds quote boundaries without decoding or fully validating escapes.

Error handling

  • Missing opening or closing quotes, unescaped newlines in quoted strings, or an incomplete trailing backslash throw strconv: invalid quoted prefix.

can_backquote

XuAPI
pub fn can_backquote(s &string) bool

Purpose

Check whether text can be placed directly inside backquotes.

Parameters

s &string
Original text, passed as a read-only borrow; this call does not modify it.

Returns

true when there are no backquotes, non-tab ASCII control bytes, DEL, BOM (U+FEFF), or invalid UTF-8 bytes; empty text returns true.

Usage notes

  • Horizontal tabs (\t) are allowed; newlines (\n/\r) and other ASCII control bytes are rejected.

append_int

XuAPI
pub fn append_int(dst string, n int, base int) string

Purpose

Append int. Throws on failure; handle the error or propagate it to the caller.

Parameters

dst string
destination (string)
n int
count, length, or limit (int)
base int
numeric base (int)

Returns

string — result string

append_uint

XuAPI
pub fn append_uint(dst string, n int, base int) string

Purpose

Append uint. Throws on failure; handle the error or propagate it to the caller.

Parameters

dst string
destination (string)
n int
count, length, or limit (int)
base int
numeric base (int)

Returns

string — result string

append_bool

XuAPI
pub fn append_bool(dst string, b bool) string

Purpose

Append boolean string representation ('true' or 'false') to dst.

Parameters

dst string
destination (string)
b bool
b (bool)

Returns

string — result string

append_quote

XuAPI
pub fn append_quote(dst string, s &string) string

Purpose

Append quote.

Parameters

dst string
destination (string)
s &string
source string (read-only borrow; caller keeps the original; type &string)

Returns

string — result string

append_quote_rune

XuAPI
pub fn append_quote_rune(dst string, r int) string

Purpose

Append quote rune.

Parameters

dst string
destination (string)
r int
rune (Unicode code point) (int)

Returns

string — result string

append_float

XuAPI
pub fn append_float(dst string, f f64, verb int, prec int) string

Purpose

Append float. Throws on failure; handle the error or propagate it to the caller.

Parameters

dst string
destination (string)
f f64
floating-point value
verb int
verb (int)
prec int
precision (int)

Returns

string — result string

format_float

XuAPI
pub fn format_float(f f64, verb int, prec int) string

Purpose

Format a floating-point number with an explicit style and precision for display.

Parameters

f f64
Floating-point value to format.
verb int
Format character code: 101 (e) for scientific, 102 (f) for fixed-point, or 103 (g) for a compact choice.
prec int
Decimal places for e/f or significant digits for g; negative selects the shortest round-trippable representation, and g treats 0 as 1.

Returns

Formatted text; special values are written as NaN, +Inf and -Inf.

Error handling

  • A verb other than 101, 102 or 103 throws strconv: unknown format verb.

See usage example

parse_float

XuAPI
pub fn parse_float(s &string) f64

Purpose

Parse decimal floating-point text with optional fractions and exponents.

Parameters

s &string
Complete numeric text with an optional sign; surrounding whitespace is not trimmed.

Returns

Parsed f64; case-insensitive NaN, Inf and Infinity forms are also accepted.

Error handling

  • Empty input, invalid syntax, trailing characters or finite numeric overflow throws.

See usage example