Crate slint

source ·
Expand description

§Slint

This crate is the main entry point for embedding user interfaces designed with Slint in Rust programs. If you are new to Slint, start with the Walk-through tutorial If you are already familiar with Slint, the following topics provide related information.

§Topics

§How to use this crate:

Designs of user interfaces are described in the .slint design markup language. There are three ways of including them in Rust:

With the first two methods, the markup code is translated to Rust code and each component is turned into a Rust struct with functions. Use these functions to instantiate and show the component, and to access declared properties. Check out our sample component for more information about the generation functions and how to use them.

§The .slint code in a macro

This method combines your Rust code with the .slint design markup in one file, using a macro:

slint::slint!{
    export component HelloWorld {
        Text {
            text: "hello world";
            color: green;
        }
    }
}
fn main() {
    HelloWorld::new().unwrap().run().unwrap();
}

§The .slint code in external files is compiled with build.rs

When your design becomes bigger in terms of markup code, you may want move it to a dedicated .slint file. It’s also possible to split a .slint file into multiple files using modules. Use a build script to compile your main .slint file:

In your Cargo.toml add a build assignment and use the slint-build crate in build-dependencies:

[package]
...
build = "build.rs"
edition = "2021"

[dependencies]
slint = "1.8.0"
...

[build-dependencies]
slint-build = "1.8.0"

Use the API of the slint-build crate in the build.rs file:

fn main() {
    slint_build::compile("ui/hello.slint").unwrap();
}

Finally, use the include_modules! macro in your main.rs:

slint::include_modules!();
fn main() {
    HelloWorld::new().unwrap().run().unwrap();
}

Use our Template Repository to create a skeleton file hierarchy that uses this method:

  1. Download and extract the ZIP archive of the Rust Template.
  2. Rename the extracted directory and change into it:
mv slint-rust-template-main my-project
cd my-project

§Generated components

Exported component from the macro or the main file that inherit Window or Dialog is mapped to a Rust structure.

The components are generated and re-exported to the location of the include_modules! or slint! macro. It is represented as a struct with the same name as the component.

For example, if you have

export component MyComponent inherits Window { /*...*/ }

in the .slint file, it will create a

struct MyComponent{ /*...*/ }

See also our sample component for more information about the API of the generated struct.

A component is instantiated using the fn new() -> Self function. The following convenience functions are available through the ComponentHandle implementation:

For each top-level property

For each top-level callback

Note: All dashes (-) are replaced by underscores (_) in names of types or functions.

After instantiating the component, call ComponentHandle::run() on show it on the screen and spin the event loop to react to input events. To show multiple components simultaneously, call ComponentHandle::show() on each instance. Call run_event_loop() when you’re ready to enter the event loop.

The generated component struct acts as a handle holding a strong reference (similar to an Rc). The Clone trait is not implemented. Instead you need to make explicit ComponentHandle::clone_strong and ComponentHandle::as_weak calls. A strong reference should not be captured by the closures given to a callback, as this would produce a reference loop and leak the component. Instead, the callback function should capture a weak component.

§Threading and Event-loop

For platform-specific reasons, the event loop must run in the main thread, in most backends, and all the components must be created in the same thread as the thread the event loop is running or is going to run.

You should perform the minimum amount of work in the main thread and delegate the actual logic to another thread to avoid blocking animations. Use the invoke_from_event_loop function to communicate from your worker thread to the UI thread.

To run a function with a delay or with an interval use a Timer.

To run an async function or a future, use spawn_local().

§Exported Global singletons

When you export a global singleton from the main file, it is also generated with the exported name. Like the main component, the generated struct have inherent method to access the properties and callback:

For each property

  • A setter: fn set_<property_name>(&self, value: <PropertyType>)
  • A getter: fn get_<property_name>(&self) -> <PropertyType>

For each callback

  • fn invoke_<callback_name>(&self, <CallbackArgs>) -> <ReturnValue> to invoke the callback
  • fn on_<callback_name>(&self, callback: impl Fn(<CallbackArgs>) + 'static) to set the callback handler.

The global can be accessed with the ComponentHandle::global() function, or with Global::get()

See the documentation of the Global trait for an example.

Note: Global singletons are instantiated once per component. When declaring multiple components for export to Rust, each instance will have their own instance of associated globals singletons.

Modules§

  • Android backend.
  • This is a pseudo module which only exist for documentation purposes as a way to show the Slint documentation as part of rustdoc.
  • This module contains items that you need to use or implement if you want use Slint in an environment without one of the supplied platform backends such as qt or winit.

Macros§

  • This macro is the same as std::format!, but it returns a SharedString instead.
  • Include the code generated with the slint-build crate from the build script. After calling slint_build::compile in your build.rs build script, the use of this macro includes the generated Rust code and makes the exported types available for you to instantiate.
  • Initialize translations when using the gettext feature.
  • This macro allows you to use the Slint design markup language inline in Rust code. Within the braces of the macro you can use place Slint code and the named exported components will be available for instantiation.

Structs§

  • Factory to create slint::Image from an existing OpenGL texture.
  • Color represents a color in the Slint run-time, represented using 8-bit channels for red, green, blue and the alpha (opacity). It can be conveniently converted using the to_ and from_ (a)rgb helper functions:
  • Provides a filtered subset of rows by another Model.
  • An image type that can be displayed by the Image element. You can construct Image objects from a path to an image file on disk, using Self::load_from_path.
  • The return value of the spawn_local() function
  • Error generated if an image cannot be loaded for any reasons.
  • A position represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
  • A size represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
  • Provides rows that are generated by a map function based on the rows of another Model
  • Dispatch notifications from a Model to one or several ModelPeer. Typically, you would want to put this in the implementation of the Model
  • Represent a handle to a view that listens to changes to a model.
  • ModelRc is a type wrapper for a reference counted implementation of the Model trait.
  • A position represented in the coordinate space of physical device pixels. That is the space after applying a display device specific scale factor to pixels from the logical coordinate space.
  • A size represented in the coordinate space of physical device pixels. That is the space after applying a display device specific scale factor to pixels from the logical coordinate space.
  • Provides a reversed view of another Model.
  • RgbaColor stores the red, green, blue and alpha components of a color with the precision of the generic parameter T. For example if T is f32, the values are normalized between 0 and 1. If T is u8, they values range is 0 to 255. This is merely a helper class for use with Color.
  • SharedPixelBuffer is a container for storing image data as pixels. It is internally reference counted and cheap to clone.
  • A string type used by the Slint run-time.
  • SharedVector holds a reference-counted read-only copy of [T].
  • Provides a sorted view of rows by another Model.
  • Represents an item in a StandardListView and a StandardTableView.
  • This is used to define the column and the column header of a TableView
  • Timer is a handle to the timer system that allows triggering a callback to be called after a specified period of time.
  • A Model backed by a Vec<T>, using interior mutability.
  • Struct that’s used to hold weak references of a Slint component
  • This type represents a window towards the windowing system, that’s used to render the scene of a component. It provides API to control windowing system specific aspects such as the position on the screen.
  • This struct represents a persistent handle to a window and implements the raw_window_handle_06::HasWindowHandle and raw_window_handle_06::HasDisplayHandle traits for accessing exposing raw window and display handles. Obtain an instance of this by calling Window::window_handle().

Enums§

Traits§

  • This trait describes the common public API of a strongly referenced Slint component. It allows creating strongly-referenced clones, a conversion into/ a weak pointer as well as other convenience functions.
  • This trait is used to obtain references to global singletons exported in .slint markup. Alternatively, you can use ComponentHandle::global to obtain access.
  • A Model is providing Data for the repeated elements with for in the .slint language
  • Extension trait with extra methods implemented on types that implement Model
  • This trait defines the interface that users of a model can use to track changes to a model. It is supplied via Model::model_tracker and implementation usually return a reference to its field of ModelNotify.

Functions§

  • Adds the specified function to an internal queue, notifies the event loop to wake up. Once woken up, any queued up functors will be invoked.
  • Schedules the main event loop for termination. This function is meant to be called from callbacks triggered by the UI. After calling the function, it will return immediately and once control is passed back to the event loop, the initial call to slint::run_event_loop() will return.
  • Enters the main event loop. This is necessary in order to receive events from the windowing system for rendering to the screen and reacting to user input. This function will run until the last window is closed or until quit_event_loop() is called.
  • Similar to run_event_loop(), but this function enters the main event loop and continues to run even when the last window is closed, until quit_event_loop() is called.
  • Spawns a Future to execute in the Slint event loop.

Type Aliases§

  • Convenience alias for a pixel with three color channels (red, green and blue), each encoded as u8.
  • Convenience alias for a pixel with four color channels (red, green, blue and alpha), each encoded as u8.