Struct slint_interpreter::ComponentInstance

source ·
#[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§

source§

impl ComponentInstance

source

pub fn definition(&self) -> ComponentDefinition

Return the ComponentDefinition that was used to create this instance.

source

pub fn get_property(&self, name: &str) -> Result<Value, GetPropertyError>

Return the value for a public property of this component.

§Examples
use slint_interpreter::{ComponentDefinition, Compiler, Value, SharedString};
let code = r#"
    export component MyWin inherits Window {
        in-out property <int> my_property: 42;
    }
"#;
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, "{:?}", result.diagnostics().collect::<Vec<_>>());
let instance = result.component("MyWin").unwrap().create().unwrap();
assert_eq!(instance.get_property("my_property").unwrap(), Value::from(42));
source

pub fn set_property( &self, name: &str, value: Value, ) -> Result<(), SetPropertyError>

Set the value for a public property of this component.

source

pub fn set_callback( &self, name: &str, callback: impl Fn(&[Value]) -> Value + 'static, ) -> Result<(), SetCallbackError>

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::{Compiler, Value, SharedString, ComponentHandle};
use core::convert::TryInto;
let code = r#"
    export component MyWin inherits Window {
        callback foo(int) -> int;
        in-out property <int> my_prop: 12;
    }
"#;
let result = spin_on::spin_on(
    Compiler::default().build_from_source(code.into(), Default::default()));
assert_eq!(result.diagnostics().count(), 0, "{:?}", result.diagnostics().collect::<Vec<_>>());
let instance = result.component("MyWin").unwrap().create().unwrap();
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("foo", &[Value::from(500)]).unwrap();
assert_eq!(res, Value::from(500+12));
source

pub fn invoke(&self, name: &str, args: &[Value]) -> Result<Value, InvokeError>

Call the given callback or function with the arguments

§Examples

See the documentation of Self::set_callback for an example

source

pub fn get_global_property( &self, global: &str, property: &str, ) -> Result<Value, GetPropertyError>

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::{Compiler, Value, SharedString};
let code = r#"
    global Glob {
        in-out property <int> my_property: 42;
    }
    export { Glob as TheGlobal }
    export component MyWin inherits Window {
    }
"#;
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, "{:?}", result.diagnostics().collect::<Vec<_>>());
let instance = result.component("MyWin").unwrap().create().unwrap();
assert_eq!(instance.get_global_property("TheGlobal", "my_property").unwrap(), Value::from(42));
source

pub fn set_global_property( &self, global: &str, property: &str, value: Value, ) -> Result<(), SetPropertyError>

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

source

pub fn set_global_callback( &self, global: &str, name: &str, callback: impl Fn(&[Value]) -> Value + 'static, ) -> Result<(), SetCallbackError>

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::{Compiler, Value, SharedString};
use core::convert::TryInto;
let code = r#"
    export global Logic {
        pure callback to_uppercase(string) -> string;
    }
    export component MyWin inherits Window {
        out property <string> hello: Logic.to_uppercase("world");
    }
"#;
let result = spin_on::spin_on(
    Compiler::default().build_from_source(code.into(), Default::default()));
let instance = result.component("MyWin").unwrap().create().unwrap();
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("Logic", "to_uppercase", &[
    SharedString::from("abc").into()
]).unwrap();
assert_eq!(abc, Value::from(SharedString::from("ABC")));
source

pub fn invoke_global( &self, global: &str, callable_name: &str, args: &[Value], ) -> Result<Value, InvokeError>

Call the given callback or function within a global singleton with the arguments

§Examples

See the documentation of Self::set_global_callback for an example

source

pub fn component_positions(&self, path: &Path, offset: u32) -> Vec<LogicalRect>

Find all positions of the components which are pointed by a given source location.

WARNING: this is not part of the public API

source

pub fn element_positions(&self, element: &ElementRc) -> Vec<LogicalRect>

Find the position of the element.

WARNING: this is not part of the public API

source

pub fn element_node_at_source_code_position( &self, path: &Path, offset: u32, ) -> Vec<(ElementRc, usize)>

Find the the element that was defined at the text position.

WARNING: this is not part of the public API

Trait Implementations§

source§

impl ComponentHandle for ComponentInstance

source§

fn as_weak(&self) -> Weak<Self>
where Self: Sized,

Returns a new weak pointer.
source§

fn clone_strong(&self) -> Self

Returns a clone of this handle that’s a strong reference.
source§

fn show(&self) -> Result<(), PlatformError>

Convenience function for crate::Window::show(). This shows the window on the screen and maintains an extra strong reference while the window is visible. 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.
source§

fn hide(&self) -> Result<(), PlatformError>

Convenience function for crate::Window::hide(). Hides the window, so that it is not visible anymore. The additional strong reference on the associated component, that was created when show() was called, is dropped.
source§

fn run(&self) -> Result<(), PlatformError>

This is a convenience function that first calls Self::show, followed by crate::run_event_loop() and Self::hide.
source§

fn window(&self) -> &Window

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

fn global<'a, T: Global<'a, Self>>(&'a self) -> T
where Self: Sized,

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

impl From<ComponentInstance> for VRc<ItemTreeVTable, ErasedItemTreeBox>

source§

fn from(value: ComponentInstance) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> Downcast for T
where T: Any,

§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

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

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
§

impl<T> Pointable for T

§

const ALIGN: usize = _

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more