#[repr(C)]
pub struct ComponentInstance { /* private fields */ }
Expand description

This represent an instance of a dynamic component

You can create an instance with the ComponentDefinition::create function.

Properties and callback can be accessed using the associated functions.

An instance can be put on screen with the ComponentInstance::run function.

Implementations§

Return the ComponentDefinition that was used to create this instance.

Return the value for a public property of this component.

Examples
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString};
let code = r#"
    MyWin := Window {
        property <int> my_property: 42;
    }
"#;
let mut compiler = ComponentCompiler::default();
let definition = spin_on::spin_on(
    compiler.build_from_source(code.into(), Default::default()));
assert!(compiler.diagnostics().is_empty(), "{:?}", compiler.diagnostics());
let instance = definition.unwrap().create();
assert_eq!(instance.get_property("my_property").unwrap(), Value::from(42));

Set the value for a public property of this component

Set a handler for the callback with the given name. A callback with that name must be defined in the document otherwise an error will be returned.

Note: Since the ComponentInstance holds the handler, the handler itself should not contain a strong reference to the instance. So if you need to capture the instance, you should use Self::as_weak to create a weak reference.

Examples
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString, ComponentHandle};
use core::convert::TryInto;
let code = r#"
    MyWin := Window {
        callback foo(int) -> int;
        property <int> my_prop: 12;
    }
"#;
let definition = spin_on::spin_on(
    ComponentCompiler::default().build_from_source(code.into(), Default::default()));
let instance = definition.unwrap().create();

let instance_weak = instance.as_weak();
instance.set_callback("foo", move |args: &[Value]| -> Value {
    let arg: u32 = args[0].clone().try_into().unwrap();
    let my_prop = instance_weak.unwrap().get_property("my_prop").unwrap();
    let my_prop : u32 = my_prop.try_into().unwrap();
    Value::from(arg + my_prop)
}).unwrap();

let res = instance.invoke_callback("foo", &[Value::from(500)]).unwrap();
assert_eq!(res, Value::from(500+12));

Call the given callback with the arguments

Examples

See the documentation of Self::set_callback for an example

Return the value for a property within an exported global singleton used by this component.

The global parameter is the exported name of the global singleton. The property argument is the name of the property

Examples
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString};
let code = r#"
    global Glob := {
        property <int> my_property: 42;
    }
    export { Glob as TheGlobal }
    MyWin := Window {
    }
"#;
let mut compiler = ComponentCompiler::default();
let definition = spin_on::spin_on(
    compiler.build_from_source(code.into(), Default::default()));
assert!(compiler.diagnostics().is_empty(), "{:?}", compiler.diagnostics());
let instance = definition.unwrap().create();
assert_eq!(instance.get_global_property("TheGlobal", "my_property").unwrap(), Value::from(42));

Set the value for a property within an exported global singleton used by this component.

Set a handler for the callback in the exported global singleton. A callback with that name must be defined in the specified global and the global must be exported from the main document otherwise an error will be returned.

Examples
use slint_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString};
use core::convert::TryInto;
let code = r#"
    export global Logic := {
        callback to_uppercase(string) -> string;
    }
    MyWin := Window {
        property <string> hello: Logic.to_uppercase("world");
    }
"#;
let definition = spin_on::spin_on(
    ComponentCompiler::default().build_from_source(code.into(), Default::default()));
let instance = definition.unwrap().create();
instance.set_global_callback("Logic", "to_uppercase", |args: &[Value]| -> Value {
    let arg: SharedString = args[0].clone().try_into().unwrap();
    Value::from(SharedString::from(arg.to_uppercase()))
}).unwrap();

let res = instance.get_property("hello").unwrap();
assert_eq!(res, Value::from(SharedString::from("WORLD")));

let abc = instance.invoke_global_callback("Logic", "to_uppercase", &[
    SharedString::from("abc").into()
]).unwrap();
assert_eq!(abc, Value::from(SharedString::from("ABC")));

Call the given callback within a global singleton with the arguments

Examples

See the documentation of Self::set_global_callback for an example

Highlight the elements which are pointed by a given source location.

WARNING: this is not part of the public API

Trait Implementations§

Returns a new weak pointer.
Returns a clone of this handle that’s a strong reference.
Marks the window of this component to be shown on the screen. This registers the window with the windowing system. In order to react to events from the windowing system, such as draw requests or mouse/touch input, it is still necessary to spin the event loop, using crate::run_event_loop. Read more
Marks the window of this component to be hidden on the screen. This de-registers the window from the windowing system and it will not receive any further events. Read more
This is a convenience function that first calls Self::show, followed by crate::run_event_loop() and Self::hide. Read more
Returns the Window associated with this component. The window API can be used to control different aspects of the integration into the windowing system, such as the position on the screen. Read more
This function provides access to instances of global singletons exported in .slint. See Global for an example how to export and access globals from .slint markup. Read more
Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.
The type for initializers.
Initializes a with the given initializer. Read more
Dereferences the given pointer. Read more
Mutably dereferences the given pointer. Read more
Drops the object pointed to by the given pointer. Read more
Should always be Self
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.
Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more