Native screens of a phone
The import module native gives a WebAssembly app the screens of iOS and Android: a navigation stack, screens of labels, buttons, switches, and text fields, list screens, tabs, alerts, and the photo picker. Each function goes to a class of ztk::ios or ztk::android, so a module gets the views of a Tcl script, in the same sandbox.


Use native for the screens of a phone app: lists, forms, and settings that look and feel as the system does. Use tk for a window that is the same on each platform, such as a canvas, a game, or an app for a desktop. One module can use both: it asks platform() at the start, and builds its window with tk when the answer is none.
native.wit defines the interface, with the core form of each type of tk.wit (the ABI of tk). tools/wasm-tk-gen.py writes this page, the glue of the host, and the import layer of Rust from that file.
Build and run the example
The app of the pictures is examples/wasm/native-rust of the ztk repository: a list of three plants in a navigation stack. A tap on a row pushes a screen with a switch and a button, and the button shows an alert. src/native.rs there is the import layer from tools/wasm-tk-gen.py.
-
Build the module in that folder:
rustup target add wasm32-unknown-unknown cargo build --release --target wasm32-unknown-unknown cp target/wasm32-unknown-unknown/release/plants.wasm . -
Bundle it. A phone app starts with a Tcl script, so the app has a
main.tclof one line that runs the module from the folder of the app:`ztk::wasm::run` [file join [file dirname [info script]] plants.wasm]ztk bundlemakes the app for a store from the two files (iOS, Android):ztk bundle -o Plants.apk --target aarch64-linux-android main.tcl plants.wasm ztk bundle -o Plants.app --target aarch64-ios main.tcl plants.wasm -
Run it in the iOS Simulator on a Mac, or on an Android emulator or device, with the log of the app in the terminal:
ztk --ios main.tcl plants.wasm ztk --android main.tcl plants.wasm
A tap on Basil and then on Save shows the alert:


On a desktop, platform() gives none: an app builds its window with tk there, and this example has no such part.
The code
The module exports app_init, which builds the first screen, and app_event, which gets each callback. This is the start of src/lib.rs; the whole file has the answers of the list and the screen of a plant.
mod native;
use native::*;
use std::cell::Cell;
// The callbacks of the app: numbers that the app chooses.
const LIST: i32 = 1;
const SAVE: i32 = 2;
const DONE: i32 = 3;
thread_local! {
static NAV: Cell<i32> = const { Cell::new(0) };
static LIST_VIEW: Cell<i32> = const { Cell::new(0) };
}
#[no_mangle]
pub extern "C" fn app_init() {
// A desktop has no native screens: an app there builds a window with tk.
if platform() == Platform::None {
return;
}
let list = list_screen("Plants", ListStyle::InsetGrouped, LIST);
set_id(list, "plants");
let nav = navigation(list);
LIST_VIEW.set(list);
NAV.set(nav);
show(nav);
}
The bindings of Go and JavaScript have no layer for native yet. A module in another language calls the core imports of the reference directly, with the rules of the ABI of tk.
How events work
A callback is a number that the module chooses. The host calls the export app_event with it, for a tap and for each question of a list. In app_event, event_kind gives the kind of the event, and event_count, event_int, and event_str give its words. The module answers a question with answer_int, answer_str, answer_action, or cell_set before app_event returns.
host (iOS or Android) module
--------------------- ------
the list shows -- app_event(LIST) --> event_kind() = rows
<-- answer_int(3) ---
each row shows -- app_event(LIST) --> event_kind() = cell, event_int(1) = row
<-- cell_set(...) ---
tap on "Basil" -- app_event(LIST) --> event_kind() = select: push a screen
tap on "Save" -- app_event(SAVE) --> event_kind() = action: alert_show(a, DONE)
tap on "OK" -- app_event(DONE) --> event_kind() = alert, event_str(1) = "OK"
The host never enters the module two times. A question that comes while the module runs, for example while app_init makes the list, gets the default answer, and the host asks again after the module returns.
| Event kind | Words | Answer |
|---|---|---|
action, changed, closed |
none | none |
alert |
the index of the button, its label | none |
picked |
the path of each file | none |
accessory |
the label of the button over the keyboard | none |
sections |
none | answer_int, default 1 |
rows |
the section | answer_int, default 0 |
title |
the section | answer_str, default empty |
cell |
the section, the row | cell_set for each field |
select |
the section, the row | none |
actions |
the section, the row | answer_action for each swipe action |
swiped |
the section, the row, the label of the action | none |
search |
the text of the search field | none |
movable |
the section, the row | answer_int: 1 or 0, default 0 |
move |
the section and the row of the start, then of the end | none |
The paths of picked are paths of the file commands of the app: in the root folder of the app, or the paths of the host in host mode.
Handles and errors
- A handle is a number from the host, and 0 is no view.
destroyfrees a view and the views in it. A screen that leaves its navigation stack, an alert after a tap, and a photo picker after a pick are free already. - A function that fails gives 0, false, or an empty value, and writes a line
native.NAME: MESSAGEto the standard error of the host.last_errorgives the message, and each other call clears it. - On a desktop and in the browser,
platform()givesnone,hasgives false, and each function of a view fails withno native screens on this platform. - A check script finds a view by the name of
set_idwithztk::wasm::view, and drives it as a Tcl app.
Platforms
| Kind | iOS | Android | Desktop and browser |
|---|---|---|---|
screen, list, navigation, tabs |
Yes | Yes | No |
label, button, switch, text-field, text-view, date-picker, stack |
Yes | Yes | No |
alert (and the action sheet) |
Yes | Yes | No |
photos |
Yes | Yes | No |
tk-screen |
Yes | No | No |
has(kind) gives the column of the platform. On Android, a tab symbol is a name of SF Symbols that ztk maps to an icon of Android.
Enums
The number of a case is its place in the list, from 0.
| Enum | Cases |
|---|---|
platform |
none, ios, android |
kind |
screen, list, navigation, tabs, tk-screen, alert, photos, label, button, switch, text-field, text-view, date-picker, stack |
opt |
text, title, hidden, accessibility-label, accessibility-hint, font, color, lines, align, style, enabled, on, date, mode, placeholder, secure, editable, axis, spacing, alignment, large-titles, search, editing, selected, accessory |
event-kind |
action, changed, closed, alert, picked, accessory, sections, rows, title, cell, select, actions, swiped, search, movable, move |
cell-field |
text, detail, indent, accessory, symbol |
list-style |
inset-grouped, grouped, plain |
button-role |
default, cancel, destructive |
Reference
The interface has 42 functions, in groups by task. Each function has its form in WIT, the core import of the module native, its errors, and a call in Rust. The Rust names are the core names, with the enums as types. Each function of a view also fails with no native screens on this platform on a desktop and in the browser.
The platform
platform
platform: func() -> platform
The system of the phone: ios or android, or none on a desktop and in the browser.
Core import: platform() -> i32.
if platform() == Platform::None { return; }
has
has: func(k: kind) -> bool
True when the platform has the kind of view. Each kind is false on a desktop, and tk-screen is false on Android.
Core import: has(k: i32) -> i32.
if has(Kind::TkScreen) { show(tk_screen("Map")); }
last-error
last-error: func() -> string
The message of the last call that failed, or empty. Each other call clears it.
Core import: last_error(out_ptr: i32, out_cap: i32) -> i32.
if v == 0 { eprintln!("{}", last_error()); }
result
result: func() -> string
The full text of the last string result, when the buffer of the call was too small. The bindings of Rust and Go call it for you.
Core import: result(out_ptr: i32, out_cap: i32) -> i32.
Screens
screen
screen: func(title: string) -> view
A screen with a column of views. Add views to it with add.
Core import: screen(title_ptr: i32, title_len: i32) -> i32.
Errors: no native screens on this platform.
let s = screen("Basil");
list-screen
list-screen: func(title: string, style: list-style, source: s32) -> view
A screen of rows in sections. The host asks the callback source for the sections, the rows, and the cells.
Core import: list_screen(title_ptr: i32, title_len: i32, style: i32, source: i32) -> i32.
Errors: no native screens on this platform.
let list = list_screen("Plants", ListStyle::InsetGrouped, LIST);
tk-screen
tk-screen: func(title: string) -> view
The Tk window of the app as a screen, to put a part of tk in a navigation stack or a tab. iOS only.
Core import: tk_screen(title_ptr: i32, title_len: i32) -> i32.
Errors: android has no view of the kind tk-screen.
let map = tk_screen("Map");
tabs
tabs: func() -> view
A tab screen with no tab. Add the tabs with tab-add.
Core import: tabs() -> i32.
Errors: no native screens on this platform.
let tabs = tabs();
tab-add
tab-add: func(tabs: view, child: view, title: string, symbol: string)
Adds a screen or a navigation stack as a tab. The symbol is a name of SF Symbols, and Android maps the common names to its own icons.
Core import: tab_add(tabs: i32, child: i32, title_ptr: i32, title_len: i32, symbol_ptr: i32, symbol_len: i32).
Errors: no view with the handle N, "OBJECT" is not a screen or a navigation stack.
tab_add(tabs, nav, "Plants", "leaf");
show
show: func(v: view)
Shows a screen, a navigation stack, or a tab screen in place of the Tk window.
Core import: show(v: i32).
Errors: no view with the handle N, "OBJECT" is not a screen, a navigation stack, or a tab screen.
show(nav);
show-tk
show-tk: func()
Shows the Tk window again.
Core import: show_tk().
Errors: no native screens on this platform.
show_tk();
destroy
destroy: func(v: view)
Frees a view and the views in it. A screen that leaves its navigation stack, an alert after a tap, and a photo picker after a pick are free already.
Core import: destroy(v: i32).
Errors: no view with the handle N.
destroy(screen);
set-id
set-id: func(v: view, id: string)
Gives a view a name, so a check script finds it with ztk::wasm::view.
Core import: set_id(v: i32, id_ptr: i32, id_len: i32).
Errors: no view with the handle N.
set_id(list, "plants");
Navigation
navigation
navigation: func(root: view) -> view
A navigation stack with its first screen.
Core import: navigation(root: i32) -> i32.
Errors: no view with the handle N, "OBJECT" is not a screen.
let nav = navigation(list);
push
push: func(nav: view, screen: view)
Puts a screen on the top of a navigation stack, with the animation of the platform.
Core import: push(nav: i32, screen: i32).
Errors: no view with the handle N, "OBJECT" is not a screen.
push(nav, detail);
pop
pop: func(nav: view)
Removes the top screen of a navigation stack. The screen gets the event closed of on-close, and the host frees it.
Core import: pop(nav: i32).
Errors: no view with the handle N.
pop(nav);
Controls
add
add: func(container: view, k: kind) -> view
Makes a view of a kind from label to stack at the end of a screen or a stack.
Core import: add(container: i32, k: i32) -> i32.
Errors: no view with the handle N, bad kind "K" for add: must be label, button, switch, text-field, text-view, date-picker, or stack.
let save = add(screen, Kind::Button);
set-str
set-str: func(v: view, o: opt, value: string)
Sets an option to a text. The accessory labels of a text field are one label on each line.
Core import: set_str(v: i32, o: i32, value_ptr: i32, value_len: i32).
Errors: no view with the handle N, unknown option "-NAME": must be ....
set_str(save, Opt::Text, "Save");
set-int
set-int: func(v: view, o: opt, value: s32)
Sets an option to a number, such as lines of a label or spacing of a stack.
Core import: set_int(v: i32, o: i32, value: i32).
Errors: no view with the handle N, unknown option "-NAME": must be ....
set_int(label, Opt::Lines, 2);
set-bool
set-bool: func(v: view, o: opt, value: bool)
Sets an option to true or false, such as on of a switch or enabled of a button.
Core import: set_bool(v: i32, o: i32, value: i32).
Errors: no view with the handle N, unknown option "-NAME": must be ....
set_bool(water, Opt::On, true);
get-str
get-str: func(v: view, o: opt) -> string
Gives an option as a text. The text of a text field and the date of a date picker are the live values.
Core import: get_str(v: i32, o: i32, out_ptr: i32, out_cap: i32) -> i32.
Errors: no view with the handle N, unknown option "-NAME": must be ....
let name = get_str(field, Opt::Text);
get-int
get-int: func(v: view, o: opt) -> s32
Gives an option as a number, or 0 for a value that is not a number.
Core import: get_int(v: i32, o: i32) -> i32.
Errors: no view with the handle N, unknown option "-NAME": must be ....
let tab = get_int(tabs, Opt::Selected);
get-bool
get-bool: func(v: view, o: opt) -> bool
Gives an option as true or false. The state of a switch is the live value.
Core import: get_bool(v: i32, o: i32) -> i32.
Errors: no view with the handle N, unknown option "-NAME": must be ....
let on = get_bool(water, Opt::On);
focus
focus: func(v: view)
Gives the focus to a text field or a text view, and shows the keyboard.
Core import: focus(v: i32).
Errors: no view with the handle N.
focus(field);
blur
blur: func(v: view)
Removes the focus from a text field or a text view, and hides the keyboard.
Core import: blur(v: i32).
Errors: no view with the handle N.
blur(field);
Lists
reload
reload: func(l: view)
Asks the source callback again for each section, row, and cell, after a change of the data.
Core import: reload(l: i32).
Errors: no view with the handle N.
reload(list);
cell-set
cell-set: func(l: view, f: cell-field, value: string)
Sets a field of the cell in the answer to a question cell. The field accessory is checkmark, detail, disclosure, or none.
Core import: cell_set(l: i32, f: i32, value_ptr: i32, value_len: i32).
Errors: no view with the handle N, cell-set is only for the answer of a question cell.
cell_set(list, CellField::Text, "Basil");
answer-int
answer-int: func(value: s32)
The answer to a question sections, rows, or movable.
Core import: answer_int(value: i32).
answer_int(3);
answer-str
answer-str: func(value: string)
The answer to a question title: the title of the section.
Core import: answer_str(value_ptr: i32, value_len: i32).
answer_str("Herbs");
answer-action
answer-action: func(label: string, destructive: bool)
Adds a swipe action to the answer of a question actions. A destructive action shows in red.
Core import: answer_action(label_ptr: i32, label_len: i32, destructive: i32).
answer_action("Delete", true);
Alerts
alert
alert: func(title: string, message: string, sheet: bool) -> view
Makes an alert, or an action sheet with sheet. Add its buttons with alert-button, then show it with alert-show.
Core import: alert(title_ptr: i32, title_len: i32, message_ptr: i32, message_len: i32, sheet: i32) -> i32.
Errors: no native screens on this platform.
let a = alert("Saved", "The plant is saved.", false);
alert-button
alert-button: func(a: view, label: string, role: button-role)
Adds a button to an alert before alert-show. A destructive button shows in red, and a cancel button stands apart.
Core import: alert_button(a: i32, label_ptr: i32, label_len: i32, role: i32).
Errors: no alert with the handle N before alert-show.
alert_button(a, "OK", ButtonRole::Default);
alert-show
alert-show: func(a: view, callback: s32)
Shows the alert. A tap on a button gives the event alert with the index and the label of the button, and the host frees the alert.
Core import: alert_show(a: i32, callback: i32).
Errors: no alert with the handle N before alert-show.
alert_show(a, DONE);
Pickers
photos
photos: func(limit: s32, filter: string, format: string, callback: s32) -> view
Shows the photo picker of the system, for at most limit files (0 is no limit). The filter is images, videos, or any, and the format is png or original. The event picked gives the paths of the files, and the host frees the picker.
Core import: photos(limit: i32, filter_ptr: i32, filter_len: i32, format_ptr: i32, format_len: i32, callback: i32) -> i32.
Errors: no native screens on this platform, bad filter "F": must be any, images, or videos, bad format "F": must be original or png, expected a count but got "N".
photos(3, "images", "png", PICKED);
Events
on-command
on-command: func(v: view, callback: s32)
Calls back with the event action at a tap on a button, a change of a switch or a date picker, Return in a text field, or a change of tab.
Core import: on_command(v: i32, callback: i32).
Errors: no view with the handle N.
on_command(save, SAVE);
on-change
on-change: func(v: view, callback: s32)
Calls back with the event changed at each change of the text of a text field or a text view.
Core import: on_change(v: i32, callback: i32).
Errors: no view with the handle N.
on_change(field, TYPED);
on-close
on-close: func(v: view, callback: s32)
Calls back with the event closed when the screen leaves its navigation stack. The host then frees the screen.
Core import: on_close(v: i32, callback: i32).
Errors: no view with the handle N, view N is not a screen.
on_close(detail, CLOSED);
on-accessory
on-accessory: func(v: view, callback: s32)
Calls back with the event accessory at a tap on a button over the keyboard of a text field or a text view.
Core import: on_accessory(v: i32, callback: i32).
Errors: no view with the handle N.
on_accessory(field, KEY);
event-kind
event-kind: func() -> event-kind
The kind of the event of the callback that runs.
Core import: event_kind() -> i32.
match event_kind() { EventKind::Rows => answer_int(3), _ => {} }
event-count
event-count: func() -> s32
The count of the words of the event.
Core import: event_count() -> i32.
let n = event_count();
event-int
event-int: func(index: s32) -> s32
A word of the event as a number, or 0 for a word that is not a number.
Core import: event_int(index: i32) -> i32.
let row = event_int(1);
event-str
event-str: func(index: s32) -> string
A word of the event as a text, such as the label of a button or the path of a picked file.
Core import: event_str(index: i32, out_ptr: i32, out_cap: i32) -> i32.
let path = event_str(0);