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 ComponentCompiler
type, which you can
use to create ComponentDefinition
with the ComponentCompiler::build_from_source
or ComponentCompiler::build_from_path
functions.
Note about async
functions
Compiling a component is async
but in practice, this is only asynchronous if ComponentCompiler::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, ComponentCompiler, ComponentHandle};
let mut compiler = ComponentCompiler::default();
let definition =
spin_on::spin_on(compiler.build_from_path("hello.slint"));
slint_interpreter::print_diagnostics(&compiler.diagnostics());
if let Some(definition) = definition {
let instance = definition.create().unwrap();
instance.run().unwrap();
}
This example load a .slint
from a string and set some properties:
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString, ComponentHandle};
let code = r#"
export component MyWin inherits Window {
in property <string> my_name;
Text {
text: "Hello, " + my_name;
}
}
"#;
let mut compiler = ComponentCompiler::default();
let definition =
spin_on::spin_on(compiler.build_from_source(code.into(), Default::default()));
assert!(compiler.diagnostics().is_empty());
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 theprint_diagnostics
function to show diagnostic in the console output -
std
(enabled by default) — Enable use of the Rust standard library.
Backends
See the documentation of the slint
crate
-
backend-qt
(enabled by default) — The Qt backend feature uses Qt for the windowing system integration and rendering. This backend also provides thenative
style. It requires Qt 5.15 or later to be installed. If Qt is not installed, the backend will not be operational -
backend-winit
(enabled by default) — 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 specificbackend-winit-XX
features. -
backend-winit-x11
— Simliar tobackend-winit
this enables the winit based event loop but only with support for the X Window System on Unix. -
backend-winit-wayland
— Simliar tobackend-winit
this enables the winit based event loop but only with support for the Wayland window system on Unix. -
backend-linuxkms
— Vulkan/EGL and libinput on Linux are used to render the application in full screen mode, without any windowing system. -
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 withbackend-winit
,backend-winit-x11
, orbackend-winit-wayland
. -
renderer-skia-opengl
— Same asrenderer-skia
, but Skia will always use OpenGL. -
renderer-skia-vulkan
— Same asrenderer-skia
, but Skia will always use Vulkan. -
renderer-software
— 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.
Modules
- 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
- (Re-export from corelib.) 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: - ComponentCompiler is the entry point to the Slint interpreter that can be used to load .slint files or compile them on-the-fly from a string.
- 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.
- The return value of the
spawn_local()
function - 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.
- (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.
Enums
- (Re-export from corelib.) 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.
- 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.
- This enum describes the level or severity of a diagnostic message produced by the compiler.
- Error returned from the
invoke_from_event_loop()
andquit_event_loop()
function - Error returned by
ComponentInstance::get_property
- This enum describes a low-level access to specific graphics APIs used by the renderer.
- Error returned by
ComponentInstance::invoke
- The platform encountered a fatal error.
- This enum describes the different rendering states, that will be provided to the parameter of the callback for
set_rendering_notifier
on theslint::Window
. - Error returned by
ComponentInstance::set_callback
- Error returned by
ComponentInstance::set_property
- This enum describes the different error scenarios that may occur when the application registers a rendering notifier on a
slint::Window
. - This enum represents the different public variants of the
Value
enum, without the contained values. - The position of the window in either physical or logical pixels. This is used with
Window::set_position
. - The size of a window represented in either physical or logical pixels. This is used with
Window::set_size
.
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 useComponentHandle::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.
- Spawn a Future to run in the Slint event loop