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

§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§

  • This module contains the code for the highlight of some elements
  • This module contains a few functions used by the tests

Macros§

  • Instantiate a static ModelAdaptorVTable for a given type and implements vtable::HasStaticVTable<ModelAdaptorVTable> for it.

Structs§

  • 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:
  • The result of a compilation
  • This is the entry point of the crate, it can be used to load a .slint file and compile it into a CompilationResult.
  • ComponentCompiler is deprecated, use Compiler instead
  • ComponentDefinition is a representation of a compiled component from .slint markup.
  • This represent an instance of a dynamic component
  • This structure represent a diagnostic emitted while compiling .slint code.
  • 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.
  • 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.
  • 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.
  • (Re-export from corelib.) A string type used by the Slint run-time.
  • (Re-export from corelib.) SharedVector holds a reference-counted read-only copy of [T].
  • This type represents a runtime instance of structure in .slint.
  • 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.
  • Internal trait that’s used to map rendering state callbacks to either a Rust-API provided impl FnMut or a struct that invokes a C callback and implements Drop to release the closure on the C++ side.

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.
  • Print the diagnostics to stderr
  • 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 in order to render to the screen and react to user input.
  • 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.