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
- The Slint Language Documentation
- Type mappings between .slint and Rust
- Feature flags and backend selection
- Slint on Microcontrollers
§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:
- The
.slint
code is inline in a macro. - The
.slint
code in external files compiled withbuild.rs
- The
.slint
code is loaded dynamically at run-time from the file system, by using the interpreter API.
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 inherits Window {
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
:
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 callbackfn 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
Android and ( backend-android-activity-05
orbackend-android-activity-06
) - Android backend.
- docs
- This is a pseudo module which only exist for documentation purposes as a way to show the Slint documentation as part of rustdoc.
- platform
- 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§
- format
- This macro is the same as
std::format!
, but it returns aSharedString
instead. - include_
modules - Include the code generated with the slint-build crate from the build script. After calling
slint_build::compile
in yourbuild.rs
build script, the use of this macro includes the generated Rust code and makes the exported types available for you to instantiate. - init_
translations gettext
- Initialize translations when using the
gettext
feature. - slint
- 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§
- Backend
Selector - Use the BackendSelector to configure one of Slint’s built-in backends with a renderer
to accommodate specific needs of your application. This is a programmatic substitute for
the
SLINT_BACKEND
environment variable. - Borrowed
OpenGL Texture Builder Non-WebAssembly - Factory to create
slint::Image
from an existing OpenGL texture. - Color
- 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_
andfrom_
(a)rgb helper functions: - Filter
Model - Provides a filtered subset of rows by another
Model
. - Image
- 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
. - Join
Handle - The return value of the
spawn_local()
function - Load
Image Error - Error generated if an image cannot be loaded for any reasons.
- Logical
Position - A position represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
- Logical
Size - A size represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
- MapModel
- Provides rows that are generated by a map function based on the rows of another Model
- Model
Notify - Dispatch notifications from a
Model
to one or severalModelPeer
. Typically, you would want to put this in the implementation of the Model - Model
Peer - Represent a handle to a view that listens to changes to a model.
- ModelRc
- ModelRc is a type wrapper for a reference counted implementation of the
Model
trait. - Physical
Position - 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.
- Physical
Size - 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.
- Reverse
Model - Provides a reversed view of another
Model
. - Rgba
Color - 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
. - Shared
Pixel Buffer - SharedPixelBuffer is a container for storing image data as pixels. It is internally reference counted and cheap to clone.
- Shared
String - A string type used by the Slint run-time.
- Shared
Vector - SharedVector holds a reference-counted read-only copy of
[T]
. - Sort
Model - Provides a sorted view of rows by another
Model
. - Standard
List View Item - Represents an item in a StandardListView and a StandardTableView.
- Table
Column - This is used to define the column and the column header of a TableView
- Timer
- Timer is a handle to the timer system that triggers a callback after a specified period of time.
- VecModel
- A
Model
backed by aVec<T>
, using interior mutability. - Weak
- Struct that’s used to hold weak references of a Slint component
- Window
- 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.
- Window
Handle raw-window-handle-06
- This struct represents a persistent handle to a window and implements the
raw_window_handle_06::HasWindowHandle
andraw_window_handle_06::HasDisplayHandle
traits for accessing exposing raw window and display handles. Obtain an instance of this by callingWindow::window_handle()
.
Enums§
- Borrowed
OpenGL Texture Origin Non-WebAssembly - This enum describes the origin to use when rendering a borrowed OpenGL texture.
Use this with
BorrowedOpenGLTextureBuilder::origin
. - Brush
- A brush is a data structure that is used to describe how a shape, such as a rectangle, path or even text, shall be filled. A brush can also be applied to the outline of a shape, that means the fill of the outline itself.
- Close
Request Response - This enum describes whether a Window is allowed to be hidden when the user tries to close the window. It is the return type of the callback provided to Window::on_close_requested.
- Event
Loop Error - Error returned from the
invoke_from_event_loop()
andquit_event_loop()
function - GraphicsAPI
- This enum describes a low-level access to specific graphics APIs used by the renderer.
- Platform
Error - The platform encountered a fatal error.
- Rendering
State - This enum describes the different rendering states, that will be provided
to the parameter of the callback for
set_rendering_notifier
on theslint::Window
. - Select
Bundled Translation Error - Error type returned from the
select_bundled_translation
function. - SetRendering
Notifier Error - This enum describes the different error scenarios that may occur when the application
registers a rendering notifier on a
slint::Window
. - Timer
Mode - The TimerMode specifies what should happen after the timer fired.
- Window
Position - The position of the window in either physical or logical pixels. This is used
with
Window::set_position
. - Window
Size - The size of a window represented in either physical or logical pixels. This is used
with
Window::set_size
.
Traits§
- Component
Handle - 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.
- Global
- This trait is used to obtain references to global singletons exported in
.slint
markup. Alternatively, you can useComponentHandle::global
to obtain access. - Model
- A Model is providing Data for the repeated elements with
for
in the.slint
language - Model
Ext - Extension trait with extra methods implemented on types that implement
Model
- Model
Tracker - 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 ofModelNotify
. - ToShared
String - A trait for converting a value to a
SharedString
.
Functions§
- invoke_
from_ event_ loop - 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.
- quit_
event_ loop - 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. - run_
event_ loop - 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. - run_
event_ loop_ until_ quit - Similar to
run_event_loop()
, but this function enters the main event loop and continues to run even when the last window is closed, untilquit_event_loop()
is called. - select_
bundled_ translation - Select the current translation language when using bundled translations.
This function requires that the application’s
.slint
file was compiled with bundled translations.. It must be called after creating the first component. ReturnsOk
if the language was selected;SelectBundledTranslationError
otherwise. - set_
xdg_ app_ id - Sets the application id for use on Wayland or X11 with xdg compliant window managers. This must be set before the window is shown, and has only an effect on Wayland or X11.
- spawn_
local target_has_atomic="ptr"
- Spawns a
Future
to execute in the Slint event loop.
Type Aliases§
- Rgb8
Pixel - Convenience alias for a pixel with three color channels (red, green and blue), each encoded as u8.
- Rgba8
Pixel - Convenience alias for a pixel with four color channels (red, green, blue and alpha), each encoded as u8.