Skip to main content

Your first app

A Taunus app is a Rust program with a Slint window. It uses the Taunus design system (taunus-theme), so it looks like the rest of the system, follows the wallpaper's colours, works by touch and by remote, and speaks the system's language.

This page builds "Hello", an app with one page and one button.

1. The crate​

apps/hello/Cargo.toml:

[package]
name = "taunus-hello"
version = "0.1.0"
edition = "2021"
license = "GPL-3.0-or-later"

[dependencies]
slint = { workspace = true, features = ["backend-winit", "backend-winit-wayland", "backend-winit-x11"] }
taunus-core = { path = "../../libs/core" }
taunus-i18n = { path = "../../libs/i18n" }
taunus-theme = { path = "../../libs/theme", features = ["winit"] }
taunus-devtools = { path = "../../libs/devtools" }

[build-dependencies]
taunus-theme = { path = "../../libs/theme", features = ["build"] }

Add "apps/hello" to the members of the root Cargo.toml.

apps/hello/build.rs compiles the UI with the theme library and the translations:

fn main() {
taunus_theme::build::compile("ui/app.slint").unwrap();
}

2. The window​

apps/hello/ui/app.slint:

import { Theme, Focus, FocusDir, FormFactor, Page, ListGroup, ListRow, Button } from "@taunus/index.slint";
export { Theme, Focus, FocusDir, FormFactor }

export global Hello {
in-out property <int> count;
callback greet();
}

export component HelloWindow inherits Window {
title: @tr("hello.title");
preferred-width: 1280px;
preferred-height: 800px;
default-font-family: Theme.font;
background: Theme.background;

Page {
title: @tr("hello.title");
ListGroup {
ListRow { title: @tr("hello.count"); trailing: value; value: Hello.count; }
}
Button { text: @tr("hello.greet"); clicked => { Hello.greet(); } }
}
}

The window must export Theme, Focus, FocusDir and FormFactor: the Rust macros below set them.

3. main.rs​

taunus_i18n::load!();
use slint::ComponentHandle;
use taunus_theme::Look;

slint::include_modules!();

const APP_ID: &str = "org.taunus.Hello";

fn main() -> Result<(), Box<dyn std::error::Error>> {
let ui = HelloWindow::new()?;
taunus_theme::follow_language(); // @tr follows the system's language
slint::set_xdg_app_id(APP_ID)?; // how the shell knows the window (and its .desktop file)
taunus_theme::attach!(ui); // form factor, focus engine, Back, on-screen keyboard

let weak = ui.as_weak();
ui.global::<Hello>().on_greet(move || {
let ui = weak.unwrap();
let h = ui.global::<Hello>();
h.set_count(h.get_count() + 1);
});

// the wallpaper's colours: computed off the UI thread, applied when ready
let weak = ui.as_weak();
std::thread::spawn(move || {
if let Ok(look) = Look::current((1280, 800)) {
let _ = weak.upgrade_in_event_loop(move |ui| taunus_theme::apply!(ui.global::<Theme>(), &look));
}
});

taunus_devtools::register("main", &ui); // `tdt layout` and the layout-bounds overlay
taunus_devtools::serve("taunus-hello");
ui.run()?;
Ok(())
}

//2bi: all of this boilerplate (language, app ID, attach!, the Look thread, devtools) becomes one call, taunus_theme::run(ui, "org.taunus.Hello"), and the Look takes the window's real size instead of (1280, 800).

4. Texts​

Every visible text is a translation key. Add them with the script, which writes every language file of the crate:

tools/add-translation.py apps/hello/lang hello.title "Hello"
tools/add-translation.py apps/hello/lang hello.count "Greetings"
tools/add-translation.py apps/hello/lang hello.greet "Say hello"

See Translations.

5. Run it on the PC​

cargo run --release -p taunus-hello
TAUNUS_FORM_FACTOR=tv cargo run --release -p taunus-hello # the TV look; try the arrow keys, Enter, Esc
TAUNUS_LANG=de cargo run --release -p taunus-hello

6. Put it on the device​

//2bi: an app declares itself in its own Cargo.toml, and the build finds it:

[package.metadata.taunus]
app-id = "org.taunus.Hello"
name = "Hello"
icon = "data/icon.svg"

Until then, three places name it:

  1. tools/xtask/src/programs.rs: add ("taunus-hello", "taunus-hello", "bin", 0o755) to PROGRAMS.

  2. image/rootfs/usr/local/share/applications/taunus-hello.desktop:

    [Desktop Entry]
    Type=Application
    Name=Hello
    Exec=taunus-hello
    Icon=taunus-hello
    Terminal=false
    StartupWMClass=org.taunus.Hello

    StartupWMClass must equal the app ID: that is how the shell matches the window to the app.

  3. The icon: image/rootfs/usr/share/icons/hicolor/scalable/apps/taunus-hello.svg.

Then cargo xtask sync copies it to the running device, and the app appears on the home screen.