In this first example, you see the basics of the Slint language:
We import the VerticalBox layout and the Button widget from the standard library
using the import statement. This statement can import widgets or your own components
declared in different files. You don’t need to import built-in element such as Window or Rectangle.
We declare the Recipe component using the component keyword. Recipe inherits from Window
and has elements: A layout (VerticalBox) with one button.
You instantiate elements using their name followed by a pair of braces (with optional contents.
You can assign a name to a specific element using :=
Elements have properties. Use : to set property values. Here we assign a
binding that computes a string by concatenating some string literals, and the
counter property to the Button’s text property.
You can declare custom properties for any element with property <...>. A
property needs to have a type, and can have a default value and an access
specifier. Access specifiers like private, in, out or in-out defines
how outside elements can interact with the property. Private is the default
value and stops any outside element from accessing the property.
The counter property is custom in this example.
Elements can also have callback. In this case we assign a callback
handler to the clicked callback of the button with => { ... }.
Property bindings are automatically re-evaluated if any of the properties the
binding depends on changes. The text binding of the button is
automatically re-computed whenever the counter changes.
React to a Button Click in Native Code
This example increments the counter using native code:
The <=> syntax binds two callbacks together. Here the new button-pressed
callback binds to button.clicked.
The root element of the main component exposes all non-private properties and
callbacks to native code.
In Slint, - and _ are equivalent and interchangable in all identifiers.
This is different in native code: Most programming languages forbid - in
identifiers, so - is replaced with _.
Rust code
For technical reasons, this example uses export {Recipe} in the slint! macro.
In real code, you can put the whole Slint code in the slint! macro, or use
an external .slint file together with a build script.
The Slint compiler generates a struct Recipe with a getter (get_counter) and
a setter (set_counter) for each accessible property of the root element of the
Recipe component. It also generates a function for each accessible callback,
like in this case on_button_pressed.
The Recipe struct implements the [slint::ComponentHandle] trait. A component
manages a strong and a weak reference count, similar to an Rc.
We call the as_weak function to get a weak handle to the component, which we
can move into the callback.
We can’t use a strong handle here, because that would form a cycle: The component
handle has ownership of the callback, which itself has ownership of the
closure’s captured variables.
C++ code
In C++ you can write
The CMake integration handles the Slint compiler invocations as needed,
which will parse the .slint file and generate the button_native.h header.
This header file contains a Recipe class with a getter and setter for each
accessible property, as well as a function to set up a callback
for each accessible callback in Recipe. In this case we will have get_counter,
set_counter to access the counter property and on_button_pressed to
set up the callback.
Use Property Bindings to Synchronize Controls
This example introduces the Slider widget.
It also introduces interpolation in string literals: Use \{...} to render
the result of code between the curly braces as a string.
Animation Examples
Animate the Position of an Element
Layouts position elements automatically. In this example we manually position
elements instead, using the x, y, width, height properties.
Notice the animate x block that specifies an animation. It’s run whenever the
property changes: Either because a callback sets the property, or because
its binding value changes.
Animation Sequence
This example uses the delay property to make one animation run after another.
States Examples
Associate Property Values With States
Transitions
Layout Examples
Vertical
Horizontal
Grid
Global Callbacks
Invoke a Globally Registered Native Callback from Slint
This example uses a global singleton to implement common logic in native code.
This singleton may also store properties that are accessible to native code.
Note: The preview visualize the Slint code only. It’s not connected to the native code.
Rust code
In Rust you can set the callback like this:
C++ code
In C++ you can set the callback like this:
JavaScript code
In JavaScript you can set the callback like this:
Custom Widgets
Custom Button
ToggleSwitch
CustomSlider
The TouchArea is covering the entire widget, so you can drag this slider from
any point within itself.
This example show another implementation that has a drag-able handle:
The handle only moves when we click on that handle.
The TouchArea is within the handle and moves with the handle.
Custom Tabs
Use this recipe as a basis to when you want to create your own custom tab widget.
Custom Table View
Slint provides a table widget, but you can also do something custom based on a
ListView.
Breakpoints for Responsive User Interfaces
Use recipe implements a responsive SideBar that collapses when the parent
width is smaller than the given break-point. When clicking the Button, the
SideBar expands again. Use the blue Splitter to resize the container and
test the responsive behavior.