Skip to content

Expressions and animations

A property doesn’t have to be a fixed value: it can be an expression that depends on other properties. Slint recomputes it by itself every time something changes. In the properties panel, the fx button lets you write it.

Expression What it does
parent.width - 40px 40 pixels less than the container’s width
root.total > 100 ? #e74c3c : #2ecc71 Red if the total is over 100, green otherwise
"Total: \{root.total}" A text with the value inside
input.text != "" True if the text box isn’t empty (useful in enabled)
round(slider.value) Round (also floor, ceil, abs, min, max, sqrt…)
area.has-hover True while the mouse is over a Clickable area

animate makes a property change gradually instead of jumping:

ui/app-window.slint
export component AppWindow inherits Window {
title: "Animated switch";
width: 320px;
height: 220px;
in-out property <bool> on: false;
property <int> seconds: 0;
track := Rectangle {
x: 20px;
y: 20px;
width: 280px;
height: 80px;
border-radius: 40px;
background: root.on ? #0dbc79 : #c8ccd4;
animate background { duration: 300ms; }
knob := Rectangle {
y: 10px;
width: 60px;
height: 60px;
border-radius: 30px;
background: white;
x: root.on ? parent.width - self.width - 10px : 10px;
animate x { duration: 300ms; easing: ease-in-out; }
}
area := TouchArea {
mouse-cursor: pointer;
clicked => {
root.on = !root.on;
}
}
}
status := Text {
x: 20px;
y: 120px;
text: "Off";
font-size: 20px;
color: #5b6070;
}
Text {
x: 20px;
y: 160px;
text: "On for \{root.seconds} s";
visible: root.on;
}
clock := Timer {
interval: 1s;
running: root.on;
triggered => {
root.seconds += 1;
}
}
states [
active when root.on: {
status.text: "On";
status.color: #0dbc79;
}
]
}
src/main.rs
slint::include_modules!();
fn main() -> Result<(), slint::PlatformError> {
AppWindow::new()?.run()
}

This whole example works without Rust code:

  • animate background { duration: 300ms; }: when the color changes, it goes from one to the other in 300 ms. easing chooses the curve: linear, ease, ease-in, ease-out, ease-in-out, ease-in-out-back, ease-out-bounce…
  • x: root.on ? … : … together with animate x: the knob slides.
  • states [ active when root.on: { … } ]: a state changes several properties at once when the condition holds, and they go back to their previous value by themselves when it stops holding.
  • Timer (in the designer: Components → Timer): runs triggered every interval, only while running is true.

Next: Styles and themes.