Crate slint_interpreter

Source
Expand description

§Slint interpreter library

With this crate, you can load a .slint file at runtime and show its UI.

You only need to use this crate if you do not want to use pre-compiled .slint code, which is the normal way to use Slint, using the slint crate

The entry point for this crate is the Compiler type, which you can use to create CompilationResult with the Compiler::build_from_source or Compiler::build_from_path functions. CompilationResult provides access to all components declared for export. Obtain a ComponentDefinition for each and use ComponentDefinition::create() to instantiate a component. The returned ComponentInstance in turn provides access to properties, callbacks, functions, global singletons, as well as implementing ComponentHandle.

§Note about async functions

Compiling a component is async but in practice, this is only asynchronous if Compiler::set_file_loader is set and its future is actually asynchronous. If that is not used, then it is fine to use a very simple executor, such as the one provided by the spin_on crate

§Examples

This example loads a .slint dynamically from a path and show errors if any:

use slint_interpreter::{ComponentDefinition, Compiler, ComponentHandle};

let compiler = Compiler::default();
let result = spin_on::spin_on(compiler.build_from_path("hello.slint"));
let diagnostics : Vec<_> = result.diagnostics().collect();
diagnostics.print();
if let Some(definition) = result.component("Foo") {
    let instance = definition.create().unwrap();
    instance.run().unwrap();
}

This example load a .slint from a string and set some properties:

use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString, ComponentHandle};

let code = r#"
    export component MyWin inherits Window {
        in property <string> my_name;
        Text {
            text: "Hello, " + my_name;
        }
    }
"#;

let mut compiler = Compiler::default();
let result =
    spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
assert_eq!(result.diagnostics().count(), 0);
let definition = result.component("MyWin");
let instance = definition.unwrap().create().unwrap();
instance.set_property("my_name", Value::from(SharedString::from("World"))).unwrap();
instance.run().unwrap();

§Feature flags

  • compat-1-2 (enabled by default) — Mandatory feature: This feature is required to keep the compatibility with Slint 1.2 Newer patch version may put current functionality behind a new feature that would be enabled by default only if this feature was added
  • display-diagnostics — enable the print_diagnostics function to show diagnostic in the console output
  • image-default-formats — Enable the default image formats from the image crate, to support additional image formats in Image::load_from_path and @image-url. When this feature is disabled, only PNG and JPEG are supported. When enabled, the following image formats are supported: AVIF, BMP, DDS, Farbfeld, GIF, HDR, ICO, JPEG, EXR, PNG, PNM, QOI, TGA, TIFF, WebP.

§Backends

See the documentation of the slint crate

  • backend-qt — The Qt backend feature uses Qt for the windowing system integration and rendering. This backend also provides the native style. It requires Qt 5.15 or later to be installed. If Qt is not installed, the backend will not be operational

  • backend-winit — The winit crate is used for the event loop and windowing system integration. With this feature, both x11 and wayland windowing systems are supported. For a smaller build, omit this feature and select one of the other specific backend-winit-XX features.

  • backend-winit-x11 — Simliar to backend-winit this enables the winit based event loop but only with support for the X Window System on Unix.

  • backend-winit-wayland — Simliar to backend-winit this enables the winit based event loop but only with support for the Wayland window system on Unix.

  • backend-linuxkms — KMS with Vulkan or EGL and libinput on Linux are used to render the application in full screen mode, without any windowing system. Requires libseat. If you don’t have libseat, select backend-linuxkms-noseat instead. (Experimental)

  • backend-linuxkms-noseat — KMS with Vulkan or EGL and libinput on Linux are used to render the application in full screen mode, without any windowing system. Requires libseat. (Experimental)

  • backend-default (enabled by default) — Alias to a backend and renderer that depends on the platform. Will select the Qt backend on linux if present, and the winit otherwise

  • renderer-femtovg (enabled by default) — Make the winit backend capable of rendering using the femtovg crate.

  • renderer-skia — Make the winit backend capable of rendering using Skia. Must be used in combination with backend-winit, backend-winit-x11, or backend-winit-wayland.

  • renderer-skia-opengl — Same as renderer-skia, but Skia will always use OpenGL.

  • renderer-skia-vulkan — Same as renderer-skia, but Skia will always use Vulkan.

  • renderer-software (enabled by default) — Make the winit backend capable of rendering using the software renderer.

  • accessibility (enabled by default) — Enable integration with operating system provided accessibility APIs (default: enabled)

    Enabling this feature will try to expose the tree of UI elements to OS provided accessibility APIs to support screen readers and other assistive technologies.

  • raw-window-handle-06 — Enable integration with raw-window-handle version 0.6. This provides a Window::window_handle() function that returns a struct that implements HasWindowHandle and HasDisplayHandle implementation.

  • internal — Features used internally by Slint tooling that are not stable and come without any stability guarantees whatsoever.

Modules§

testing
This module contains a few functions used by the tests

Structs§

BackendSelector
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.
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_ and from_ (a)rgb helper functions:
CompilationResult
The result of a compilation
Compiler
This is the entry point of the crate, it can be used to load a .slint file and compile it into a CompilationResult.
ComponentCompilerDeprecated
ComponentCompiler is deprecated, use Compiler instead
ComponentDefinition
ComponentDefinition is a representation of a compiled component from .slint markup.
ComponentInstance
This represent an instance of a dynamic component
Diagnostic
This structure represent a diagnostic emitted while compiling .slint code.
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.
JoinHandle
The return value of the spawn_local() function
LoadImageError
Error generated if an image cannot be loaded for any reasons.
LogicalPosition
A position represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
LogicalSize
A size represented in the coordinate space of logical pixels. That is the space before applying a display device specific scale factor.
PhysicalPosition
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.
PhysicalSize
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.
RgbaColor
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
SharedPixelBuffer is a container for storing image data as pixels. It is internally reference counted and cheap to clone.
SharedString
(Re-export from corelib.) A string type used by the Slint run-time.
SharedVector
(Re-export from corelib.) SharedVector holds a reference-counted read-only copy of [T].
Struct
This type represents a runtime instance of structure in .slint.
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.
WindowHandle
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§

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.
CloseRequestResponse
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.
DiagnosticLevel
This enum describes the level or severity of a diagnostic message produced by the compiler.
EventLoopError
Error returned from the invoke_from_event_loop() and quit_event_loop() function
GetPropertyError
Error returned by ComponentInstance::get_property
GraphicsAPI
This enum describes a low-level access to specific graphics APIs used by the renderer.
InvokeError
Error returned by ComponentInstance::invoke
PlatformError
The platform encountered a fatal error.
RenderingState
This enum describes the different rendering states, that will be provided to the parameter of the callback for set_rendering_notifier on the slint::Window.
SetCallbackError
Error returned by ComponentInstance::set_callback
SetPropertyError
Error returned by ComponentInstance::set_property
SetRenderingNotifierError
This enum describes the different error scenarios that may occur when the application registers a rendering notifier on a slint::Window.
Value
This is a dynamically typed value used in the Slint interpreter. It can hold a value of different types, and you should use the From or TryFrom traits to access the value.
ValueType
This enum represents the different public variants of the Value enum, without the contained values.
WindowPosition
The position of the window in either physical or logical pixels. This is used with Window::set_position.
WindowSize
The size of a window represented in either physical or logical pixels. This is used with Window::set_size.

Traits§

ComponentHandle
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 use ComponentHandle::global to obtain access.

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.
print_diagnosticsdisplay-diagnostics
Print the diagnostics to stderr
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 in order to render to the screen and react to user input.
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
Spawns a Future to execute in the Slint event loop.

Type Aliases§

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