struct@ Error
struct@ Error {
fn msg() string
}用途
错误值的方法约束;实现类型须提供 msg()。
要求的方法
msgfn msg() stringError上的操作:返回错误或对象的文本。
#errors使用 errors 创建错误值,并为错误消息添加上下文。
wrap 将原因保存为消息文本;is_err 比较文本链,不判断对象身份或具体类型。
将完整程序保存为 main.xu,在本机运行 xu run main.xu。
@main
#errors
fn main() {
cause := errors.new("missing file")
wrapped := errors.wrap("load config", errors.Error(cause))
println(wrapped.msg())
target := errors.new("missing file")
println(errors.is_err(errors.Error(wrapped), errors.Error(target)))
}预期依次输出:load config: missing file 与 true。
以下源码来自官方 examples 仓库,可复制或下载。请在完整工程中使用本地 Xu SDK 运行;网络、数据库和桌面示例需要对应的服务或资源。
05_errors/main.xu@main
#errors
fn read_name(ok bool) string {
if !ok {
>> errors.new("empty name")
}
<< "ada"
}
fn load_or_fallback(ok bool) string {
<< read_name(ok) >>> (err) {
println(err.msg())
<< "fallback"
}
}
fn main() int {
s := load_or_fallback(false)
println(s)
t := read_name(true) >>> (err) {
println(err.msg())
<< 1
}
println(t)
<< 0
}16_ui_webview/xacp_screen/main.xu@main
#ui
#ui/widgets
#xacp
#errors
demo #"github.com/xulang-org/examples/ui_demo"
struct Model{
label_id int
count int
}
fn (m $&Model) layout(s $&widgets.Screen) int {
s.place(m.label_id, 32.0, 36.0, 540.0, 44.0) >>>
<< 0
}
fn inspect_screen(s $&widgets.Screen) int {
$session := xacp.open_session()
// The in-process adapter follows the current session lifecycle too.
// This token belongs only to this local demonstration; no listener is opened.
permissions := xacp.PERM_UI_READ | xacp.PERM_UI_MUTATE
limits := xacp.default_limits()
$nonces := xacp.new_nonce_registry(8, true)
hello := xacp.parse_frame("id: !i 1\nop: xacp.hello\nargs:\n ver: !ver 1.0.0\n nonce: local-screen-demo\n token: local-screen-demo\n permissions: !i " + str(permissions) + "\n") >>> (err) {
xacp.close_session($&session)
>> err
}
opened := xacp.dispatch_with_token($&session, &hello, &limits, "local-screen-demo", $&nonces)
if !opened.ok{
xacp.close_session($&session)
>> errors.new("xacp_screen: " + opened.err_msg)
}
tree := xacp.ui_tree(& * s, $&session, 2, false, 0, 32, &limits) >>> (err) {
xacp.close_session($&session)
>> err
}
println("XACP ui.tree: " + str(tree.nodes.len) + " visible nodes")
for node in tree.nodes{
println(" #" + str(node.id) + " kind=" + str(node.kind) + " " + node.text)
}
$snapshot := xacp.ui_screenshot(& * s, $&session, 3, 1.0, &limits) >>> (err) {
xacp.close_session($&session)
>> err
}
println("XACP ui.screenshot: " + str(snapshot.pixel_width) + "x" + str(snapshot.pixel_height) + " pixels")
snapshot.dispose() >>> (err) {
xacp.close_session($&session)
>> err
}
_ := xacp.ui_act(& * s, $&session, 2, 4, "focus", "", false, &limits) >>> (err) {
xacp.close_session($&session)
>> err
}
xacp.close_session($&session)
<< 0
}
fn scene(events chan ui.Event) int {
ready := demo.ready(events) >>>
if ready.kind == ui.EV_EXIT{ << 0}
$screen := widgets.screen(&ready) >>>
screen.resize(640.0, 180.0) >>>
label_id := screen.label("XACP Screen adapter", 22.0, false) >>>
button_id := screen.button("Activate locally or through ui.act") >>>
screen.place(label_id, 32.0, 36.0, 540.0, 44.0) >>>
screen.place(button_id, 32.0, 96.0, 360.0, 40.0) >>>
inspect_screen($&screen) >>> (err) {
screen.dispose()
>> err
}
$model := Model{label_id: label_id, count: 0}
$actions := widgets.action_dispatcher(&model, &screen)
actions.on(button_id, widgets.ACTION_ACTIVATE, activate) >>>
actions.set_fallback(global_key)
<< widgets.run_screen_actions($&screen, events, $&model, layout_action, &actions) >>>
}
fn worker(events chan ui.Event) {
scene(events) >>> (err) {demo.fail(err.msg())
<< }
}
fn launch() int {
$app := demo.app("Xu / XACP Screen adapter", 640, 180) >>>
xu worker(app.event_ch)
<< app.run() >>>
}
fn main() int {
<< launch() >>> (err) {eprintln(err.msg())
<< 1}
}16_ui_webview/interaction_gallery/main.xu@main
#ui
#ui/widgets
#ui/layout
#time
#math
#errors
demo #"github.com/xulang-org/examples/ui_demo"
struct Model{
tree int
name int
search int
task int
cancel int
table int
theme int
guides int
density int
effects int
failure int
message int
headings []int
controls_page int
work_group int
basic_group int
extras []int
show_controls bool
theme_index int
guided bool
compact bool
reduced bool
fail_next bool
appearance_dirty bool
task_id int
completed int
running bool
checksum int
next_work time.Instant
}
fn make_model(screen $&widgets.Screen) Model{
$name_decoration := widgets.field_decoration(&"Output name")
name_decoration.helper = "A persistent label, native text selection and IME"
name_decoration.error = "Enter an output name before building."
name_decoration.clearable = true
name := screen.field("", &name_decoration) >>>
$search_decoration := widgets.field_decoration(&"Find a component")
search_decoration.helper = "Matching nodes keep their ancestor path"
search_decoration.clearable = true
search := screen.field("", &search_decoration) >>>
tree := screen.tree() >>>
root := screen.tree_add(tree, - 1, "xu-workbench") >>>
src := screen.tree_add(tree, root, "src") >>>
entry := screen.tree_add(tree, src, "main.xu") >>>
controls := screen.tree_add(tree, src, "widgets") >>>
for label in["tree_view.xu", "task_button.xu", "field.xu", "table.xu"] {
node := screen.tree_add(tree, controls, label) >>>
screen.set_tree_action(tree, node, true) >>>
}
manifest := screen.tree_add(tree, root, "xu.xun") >>>
screen.set_tree_metadata(tree, entry, "entry") >>>
screen.set_tree_metadata(tree, manifest, "module") >>>
for node in[root, src, controls] {screen.set_expanded(tree, node, true) >>> }
task := screen.task_button("Build project") >>>
screen.set_task_labels(task, "Building", "Built · run again", "Retry build", "Build again") >>>
table := screen.table(&["Component", "Experience", "Status"], 18, 0) >>>
screen.set_table_column(table, 0, 144.0, 0.0) >>>
screen.set_table_column(table, 1, 340.0, 0.0) >>>
screen.set_table_column(table, 2, 128.0, 0.0) >>>
names :=["TreeView", "TaskButton", "Field", "Table", "Select", "Popover"]
details :=["Find and reveal", "Real task progress", "Floating label / native IME", "Resizable columns", "Keyboard choices", "Anchored feedback"]
for row in 0:18 {
index := row % 6
screen.set_cell(table, row, 0, names[index]) >>>
screen.set_cell(table, row, 1, details[index]) >>>
screen.set_cell(table, row, 2, "Ready") >>>
}
headings :=[screen.label("XUUI / INTERACTION LAB", 12.0, true) >>>,
screen.label("Precision in every interaction.", 25.0, false) >>>,
screen.label("WORKSPACE", 12.0, true) >>>, screen.label("BUILD CONFIGURATION", 12.0, true) >>>,
screen.label("COMPONENTS / scroll, select and sort", 12.0, true) >>>]
work_group := screen.group() >>>
basic_group := screen.group() >>>
for id in[tree, name, search, task, table, headings[2], headings[3], headings[4]] {screen.set_parent(id, work_group) >>> }
extras := extra_controls(screen) >>>
for id in extras{screen.set_parent(id, basic_group) >>> }
cancel := screen.button("Cancel task") >>>
screen.set_parent(cancel, work_group) >>>
controls_page := screen.button("All controls") >>>
styles := screen.styles()
primary_style := styles.button_variant(widgets.BUTTON_PRIMARY) >>>
screen.set_control_style(task, &primary_style) >>>
<< Model{tree: tree, name: name, search: search, task: task, cancel: cancel,
table: table, theme: screen.button("Theme") >>>, guides: screen.button("Tree guides") >>>,
density: screen.button("Density") >>>, effects: screen.button("Motion") >>>, failure: screen.button("Fail next build") >>>,
message: screen.label("Local work batches report completed items. No simulated percentage clock.", 13.0, true) >>>,
headings: headings, controls_page: controls_page, work_group: work_group, basic_group: basic_group, extras: extras, show_controls: false, theme_index: 1, guided: false, compact: false, reduced: false, fail_next: false, appearance_dirty: true,
task_id: 0, completed: 0, running: false, checksum: 0, next_work: time.zero_instant()}
}
fn (m &Model) layout(screen $&widgets.Screen) int {
width := screen.width()
height := screen.height()
left := 310.0
screen.place(m.headings[0], 28.0, 24.0, width - 56.0, 22.0) >>>
screen.place(m.headings[1], 28.0, 54.0, width - 56.0, 36.0) >>>
buttons :=[m.theme, m.guides, m.density, m.effects, m.failure, m.controls_page]
for i in 0:buttons.len{screen.place(buttons[i], 28.0 + f64(i) * 144.0, 111.0, 132.0, 36.0) >>> }
screen.set_visible(m.work_group, !m.show_controls) >>>
screen.set_visible(m.basic_group, m.show_controls) >>>
screen.set_selected_button(m.controls_page, m.show_controls) >>>
extra_layout(screen, &m.extras) >>>
screen.set_selected_button(m.guides, m.guided) >>>
screen.set_selected_button(m.effects, m.reduced) >>>
screen.set_selected_button(m.failure, m.fail_next) >>>
screen.place(m.headings[2], 28.0, 173.0, left - 24.0, 24.0) >>>
screen.place(m.search, 28.0, 208.0, left - 24.0, 80.0) >>>
screen.place(m.tree, 28.0, 302.0, left - 24.0, height - 352.0) >>>
x := left + 36.0
right_width := width - x - 28.0
screen.place(m.headings[3], x, 173.0, right_width, 24.0) >>>
screen.place(m.name, x, 208.0, right_width, 86.0) >>>
screen.place(m.task, x, 307.0, 230.0, 40.0) >>>
screen.place(m.cancel, x + 246.0, 307.0, 136.0, 40.0) >>>
screen.set_enabled(m.cancel, m.running) >>>
screen.place(m.headings[4], x, 377.0, right_width, 24.0) >>>
screen.place(m.table, x, 413.0, right_width, height - 493.0) >>>
screen.place(m.message, 28.0, height - 41.0, width - 56.0, 24.0) >>>
<< 0
}
fn (m $&Model) work(screen $&widgets.Screen, now time.Instant) int {
if !m.running || now.before(m.next_work) { << 0}
// Each progress update follows an actual bounded work batch. The pause
// represents an application work queue and never changes the percentage.
for i in 0:5000 {m.checksum =(m.checksum + i *(m.completed + 1)) % 1000003}
m.completed = m.completed + 1
_ := screen.set_task_progress(m.task, m.task_id, f64(m.completed) / 48.0) >>>
m.next_work = now.add(time.milliseconds(35) >>>) >>>
if m.fail_next && m.completed == 18 {
_ := screen.complete_task(m.task, m.task_id, widgets.TASK_ERROR) >>>
m.running = false
m.fail_next = false
screen.set_text(m.message, "Example failure. Retry starts a new task; stale progress is rejected.") >>>
} else if m.completed == 48 {
_ := screen.complete_task(m.task, m.task_id, widgets.TASK_SUCCESS) >>>
m.running = false
screen.set_text(m.message, "48/48 work batches completed · checksum " + str(m.checksum)) >>>
}
<< 0
}
fn extra_controls(screen $&widgets.Screen)[]int {
$ids :=[]int {}
ids.push(screen.card() >>>)
ids.push(screen.label("CONTROLS / keyboard, pointer and state", 12.0, true) >>>)
ids.push(screen.checkbox("Keep generated files", true) >>>)
ids.push(screen.switch_control("Live preview", true) >>>)
ids.push(screen.radio("Use workspace settings", 1, true) >>>)
ids.push(screen.slider(0.0, 100.0, 40.0) >>>)
ids.push(screen.stepper(1.0, 16.0, 1.0, 4.0) >>>)
ids.push(screen.tabs(&["Files", "History", "Output"], 0) >>>)
ids.push(screen.segmented(&["Desktop", "Tablet", "Phone"], 0) >>>)
ids.push(screen.select_box(&["Xu source", "Configuration", "Plain text"], 0) >>>)
ids.push(screen.list_box(&["Compiler", "Widgets", "Runtime", "Desktop", "Examples", "Tooling"], 0) >>>)
ids.push(screen.progress(0.62) >>>)
ids.push(screen.badge("Ready") >>>)
ids.push(screen.divider(widgets.AXIS_HORIZONTAL) >>>)
ids.push(screen.radio_group(&["Use local build", "Use cached build", "Build dependencies"], 0) >>>)
ids.push(screen.input("Native input · 中文") >>>)
ids.push(screen.text_area("A multi-line editor.\nSelection, clipboard and native IME remain available.") >>>)
ids.push(screen.date_picker(time.date(2026, 10, 25, 10, 30, 0, 0)) >>>)
ids.push(screen.time_picker(time.date(2026, 10, 25, 10, 30, 0, 0)) >>>)
ids.push(screen.button("Queue notification") >>>)
_ := screen.tooltip(ids[19], "Repeated notifications queue; hover or focus pauses expiry.") >>>
screen.set_slider_commit_only(ids[5], true) >>>
screen.set_checkbox_indeterminate(ids[2], true) >>>
screen.set_select_option_enabled(ids[9], 2, false) >>>
screen.set_badge_tone(ids[12], widgets.BADGE_SUCCESS) >>>
screen.set_progress_buffer(ids[11], 0.84) >>>
screen.set_date_options(ids[17], time.date(2026, 10, 25, 0, 0, 0, 0), true) >>>
screen.set_clock_format(ids[18], true, true) >>>
<< ids
}
fn extra_layout(screen $&widgets.Screen, ids &[]int) int {
gap := 28.0
width :=(screen.width() - 84.0) * 0.5
right := 56.0 + width
screen.place(ids[0], gap, 628.0, width, 112.0) >>>
screen.place(ids[1], gap, 173.0, screen.width() - 56.0, 24.0) >>>
for i in 2:5 {screen.place(ids[i], gap, 211.0 + f64(i - 2) * 46.0, width, 36.0) >>> }
screen.place(ids[5], gap, 357.0, width - 136.0, 36.0) >>>
screen.place(ids[6], gap + width - 124.0, 357.0, 124.0, 36.0) >>>
screen.place(ids[7], gap, 412.0, width, 36.0) >>>
screen.place(ids[8], gap, 466.0, width, 36.0) >>>
screen.place(ids[11], gap, 522.0, width - 120.0, 12.0) >>>
screen.place(ids[12], gap + width - 100.0, 510.0, 100.0, 32.0) >>>
screen.place(ids[13], gap, 607.0, width, 1.0) >>>
screen.place(ids[14], right, 211.0, width, 126.0) >>>
screen.place(ids[9], right, 357.0, width, 36.0) >>>
screen.place(ids[10], right, 412.0, width, 158.0) >>>
screen.place(ids[15], right, 590.0, width, 36.0) >>>
screen.place(ids[16], right, 644.0, width, 96.0) >>>
screen.place(ids[17], gap, 560.0,(width - 16.0) * 0.5, 36.0) >>>
screen.place(ids[18], gap +(width + 16.0) * 0.5, 560.0,(width - 16.0) * 0.5, 36.0) >>>
screen.place(ids[19], gap + 16.0, 665.0, width - 32.0, 38.0) >>>
<< 0
}
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 scene(events chan ui.Event) int {
ready := demo.ready(events) >>>
if ready.kind == ui.EV_EXIT{ << 0}
$screen := widgets.screen(&ready) >>>
screen.resize(1000.0, 820.0) >>>
screen.set_min_size(900, 720) >>>
$model := make_model($&screen) >>> (err) {screen.dispose()
>> err}
apply_appearance($&model, $&screen) >>>
model.layout($&screen) >>>
_ := screen.render() >>>
$dispatcher := register_actions(&model, &screen) >>>
while screen.is_open() {
now := time.instant()
apply_appearance($&model, $&screen) >>>
register_notices(&screen, $&dispatcher) >>>
model.work($&screen, now) >>>
model.layout($&screen) >>>
$deadline := screen.advance(now) >>> (err) { >> errors.new("gallery animation: " + err.msg())}
for action in screen.take_actions() {_ := dispatcher.dispatch(&action, $&model, $&screen) >>> }
apply_appearance($&model, $&screen) >>>
_ := screen.render() >>> (err) { >> errors.new("gallery render: " + err.msg())}
if model.running &&(deadline.is_zero() || model.next_work.before(deadline)) {deadline = model.next_work}
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{_ := dispatcher.dispatch(&action, $&model, $&screen) >>> }
}
screen.dispose()
<< 0
}
fn worker(events chan ui.Event) {scene(events) >>> (err) {demo.fail(err.msg())
<< }}
fn launch() int {
$app := demo.app("XUUI / Interaction Lab", 1000, 820) >>>
xu worker(app.event_ch)
<< app.run() >>>
}
fn main() int { << launch() >>> (err) {eprintln(err.msg())
<< 1}}16_ui_webview/component_workshop/main.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
}@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(¬ice) >>> }
<< 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()}
<< ""
}struct@ Errorstruct@ Error {
fn msg() string
}错误值的方法约束;实现类型须提供 msg()。
msg fn msg() stringError 上的操作:返回错误或对象的文本。(Error) msgfn msg() stringError 上的操作:返回错误或对象的文本。
self SelfSelf 实例;按值接收。string — 结果字符串
struct StrErrpub struct StrErr {
pub text string
}承载 text 等属性的结构体。
text string(StrErr) msgpub fn (e StrErr) msg() string读取供用户或日志查看的错误消息。本调用只返回保存的文本。
e StrErrStrErr 实例;按值接收。错误消息字符串。
struct CodeErrpub struct CodeErr {
pub code int
pub text string
}承载 code、text 等属性的结构体。
code inttext string(CodeErr) msgpub fn (e CodeErr) msg() string读取供用户或日志查看的错误消息。本调用只返回保存的文本。
e CodeErrCodeErr 实例;按值接收。错误消息字符串。数值错误码不会自动加入文本。
codepub fn code(code int, text string) CodeErr创建同时携带数值代码和消息的错误值,便于程序分类和用户阅读。
code inttext stringmsg() 返回它,不会自动拼接代码。包含 code 和 text 的 CodeErr。
newpub fn new(text string) StrErr用一段可读文字创建错误值,供调用方返回、包装或抛出。创建本身不会抛错。
text string保存该文本的 StrErr。
struct WrapErrpub struct WrapErr {
pub text string
pub cause string
}承载 text、cause 等属性的结构体。
text stringcause string(WrapErr) msgpub fn (w WrapErr) msg() string读取供用户或日志查看的错误消息。包装错误会用 : 连接上下文和原消息。
w WrapErrWrapErr 实例;按值接收。错误消息字符串。
wrappub fn wrap(text string, err Error) WrapErr在已有错误前补充当前操作的背景,例如说明是读取哪个文件时失败。
text stringerr ErrorError 值;保存的是调用时的消息文本。WrapErr;msg() 按 text + ": " + cause 生成消息。
为底层错误补充上下文
@main
#errors
fn main() {
missing := errors.new("not found")
wrapped := errors.wrap("read config", errors.Error(missing))
println(wrapped.msg())
println(wrapped.cause())
println(errors.is_err(errors.Error(wrapped), errors.Error(errors.new("not found"))))
}
预期输出:
read config: not found not found true
(WrapErr) causepub fn (w WrapErr) cause() string读取包装时保存的原错误消息,适合单独展示底层原因。
w WrapErrWrapErr 实例;按值接收。保存的原因字符串;类型为 string,不是 Error 对象。
is_errpub fn is_err(err Error, target Error) bool按消息匹配目标错误,也能识别以 : 包装的消息后缀。
err Errortarget Error消息完全相同,或原消息以 ": " + target.msg() 结尾时为 true。
joinpub fn join(a Error, b Error) StrErr把两个错误的消息合成一段,适合汇总两项操作的失败原因。
a Errorb Error包含合并消息的 StrErr;两段非空消息之间插入换行,空消息直接省略。
unwrappub fn unwrap(err Error) string从错误消息中取最后一个 : 后面的文本,适合读取最内层的消息片段。
err Error最后一个分隔符之后的字符串;没有 : 时返回空字符串。
: 时也会被拆分。