Connecting to Rust
The interface and the code talk in two ways: callbacks (the interface tells Rust something happened) and properties (Rust reads or changes what the interface shows). The designer creates both for you.
Events: from the interface to Rust
Section titled “Events: from the interface to Rust”- Give the control a name (for example
greet). - In the panel, Events tab, click Create next to On click.
- Raxorin Studio adds to the
.slintacallback greet_clicked();that’s called from the event, and inmain.rsthe functionui.on_greet_clicked(…)where you write what happens. Go to code takes you there.
Properties: from Rust to the interface
Section titled “Properties: from Rust to the interface”In the panel of a named control, Use from Rust → Read and change text from Rust adds:
in-out property <string> greeting_text <=> greeting.text;<=> binds the two properties: changing one changes the other. From Rust they’re used with get_ and set_:
let text = ui.get_greeting_text(); // SharedStringui.set_greeting_text("Hello".into());| Type in Slint | Type in Rust |
|---|---|
string |
slint::SharedString (converted with .into() and .to_string()) |
int |
i32 |
float |
f32 |
bool |
bool |
color |
slint::Color |
[T] (list) |
slint::ModelRc<T> (see Lists) |
A complete example
Section titled “A complete example”import { Button, LineEdit, VerticalBox } from "std-widgets.slint";
export component AppWindow inherits Window { title: "Greeting"; width: 320px; height: 220px;
in-out property <string> name_text <=> name.text; in-out property <string> greeting_text <=> greeting.text; in-out property <bool> busy: false; callback greet_clicked(); callback count_clicked();
VerticalBox { name := LineEdit { placeholder-text: "Your name"; }
greet := Button { text: "Greet"; clicked => { root.greet_clicked(); } }
count := Button { text: root.busy ? "Counting…" : "Count to 3 in the background"; enabled: !root.busy; clicked => { root.count_clicked(); } }
greeting := Text { text: ""; font-size: 16px; } }}#![cfg_attr(not(debug_assertions), windows_subsystem = "windows")]
use std::time::Duration;
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> { let ui = AppWindow::new()?;
ui.on_greet_clicked({ // A "weak" handle to the window: it doesn't keep it alive (avoids reference cycles). let ui = ui.as_weak(); move || { let ui = ui.unwrap(); let name = ui.get_name_text(); let greeting = if name.trim().is_empty() { "What's your name?".to_string() } else { format!("Hello, {name}!") }; ui.set_greeting_text(greeting.into()); } });
ui.on_count_clicked({ let ui = ui.as_weak(); move || { ui.unwrap().set_busy(true); let ui = ui.clone(); // Long work goes in another thread so the window doesn't freeze... std::thread::spawn(move || { for n in 1..=3 { std::thread::sleep(Duration::from_secs(1)); // ...and to touch the interface you go back to the window's thread. let _ = ui.upgrade_in_event_loop(move |ui| ui.set_greeting_text(format!("{n}…").into())); } let _ = ui.upgrade_in_event_loop(|ui| { ui.set_greeting_text("Done!".into()); ui.set_busy(false); }); }); } });
ui.run()}Sharing data between events
Section titled “Sharing data between events”Each move || takes its own copy of what it uses. For several events to share the same data, use Rc<RefCell<…>>:
use std::{cell::RefCell, rc::Rc};
let total = Rc::new(RefCell::new(0));ui.on_add_clicked({ let total = total.clone(); move || *total.borrow_mut() += 1});The Calculator sample uses it to keep the state of the calculation.
Next: Lists and models.