Skip to content
beetlebugorgPublic

About

Native GUI apps in Rust: your crate compiles to WebAssembly and runs in a ztk window. The Rust binding of the ztk wasm interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

2 Commits

Folders and files

Repository files navigation

ztk-rust

Native GUI apps in Rust. Your crate compiles to a WebAssembly module, and ztk runs it in a native window on macOS, Windows, and Linux. The widgets are the widgets of Tk: labels, buttons, entries, a canvas, a text widget, and the rest.

The gallery example

pub use tk::app_event;
use tk::{Button, Entry, Label, Pack};

#[no_mangle]
pub extern "C" fn app_init() {
    let root = tk::root().title("Hello from Rust");
    let msg = Label::new(root).text("Type a name.");
    let name = Entry::new(root).width(24);
    let greet = Button::new(root).text("Greet").on_click(move || {
        msg.text(&format!("Hello, {}, from Rust!", name.get()));
    });
    let pad = Pack { padx: 16, pady: 8, ..Pack::default() };
    msg.pack(pad);
    name.pack(pad);
    greet.pack(pad);
}
cargo build --release --target wasm32-unknown-unknown
ztk target/wasm32-unknown-unknown/release/hello.wasm

The module of this app has 21,037 bytes.

How it works

ztk has a WebAssembly interface with the name tk: 91 typed functions for the widgets, the options, the geometry, the canvas, the events, and the timers. A widget is a handle, an option is a case of an enum, and a number goes as a number. No Tcl text crosses the boundary. This crate puts these functions in the form of Rust: one Copy type for each widget class, setters that chain, and closures as callbacks. The crate has no dependency.

What you need

  • Rust with the target wasm32-unknown-unknown: rustup target add wasm32-unknown-unknown.
  • ztk with ztk::wasm::run (the command of ztk#429).

Use the crate

The crate is not on crates.io yet. Use the repository:

[lib]
crate-type = ["cdylib"]

[dependencies]
tk = { package = "ztk", git = "https://github.com/beetlebugorg/ztk-rust" }

[profile.release]
opt-level = "s"
lto = true
panic = "abort"
codegen-units = 1
strip = true

An app exports app_init, which builds the widgets, and exports app_event of the crate again with pub use tk::app_event;. That export runs your closures.

The examples

Example What it shows Module
hello A label, an entry, and a button 21,037 bytes
sheet A spreadsheet with formulas, on a canvas with an entry 72,844 bytes
vector A drawing app: shapes, a selection, a drag 25,694 bytes
gallery Each widget class, the variables, the fonts, the timers, a second window 53,833 bytes
escape Text with each special character of Tcl, as plain text 20,539 bytes
make                 # builds dist/hello.wasm and the others
make run APP=sheet   # runs one in a window
make test            # runs each example headless with its checks

make test runs the check scripts of tests/: each one drives its app with pointer and key events and reads the widgets. The five examples have 78 checks.

sheet vector

The generated file

src/raw.rs has the imports, the enums, and one safe function for each import. The tool tools/wasm-tk-gen.py of ztk writes it from tk.wit, so the file changes only with the interface. src/lib.rs is the part written by hand.

License

MIT. See LICENSE.

About

Native GUI apps in Rust: your crate compiles to WebAssembly and runs in a ztk window. The Rust binding of the ztk wasm interface.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages