Struct sixtyfps_interpreter::ComponentInstance [−][src]
#[repr(C)]pub struct ComponentInstance { /* fields omitted */ }
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
impl ComponentInstance
[src]
impl ComponentInstance
[src]pub fn definition(&self) -> ComponentDefinition
[src]
Return the ComponentDefinition
that was used to create this instance.
pub fn get_property(&self, name: &str) -> Result<Value, GetPropertyError>
[src]
Return the value for a public property of this component.
Examples
use sixtyfps_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString}; let code = r#" MyWin := Window { property <int> my_property: 42; } "#; let mut compiler = ComponentCompiler::new(); 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));
pub fn set_property(
&self,
name: &str,
value: Value
) -> Result<(), SetPropertyError>
[src]
&self,
name: &str,
value: Value
) -> Result<(), SetPropertyError>
Set the value for a public property of this component
pub fn set_callback(
&self,
name: &str,
callback: impl Fn(&[Value]) -> Value + 'static
) -> Result<(), SetCallbackError>
[src]
&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 sixtyfps_interpreter::{ComponentDefinition, ComponentCompiler, Value, SharedString}; 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::new().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));
pub fn invoke_callback(
&self,
name: &str,
args: &[Value]
) -> Result<Value, CallCallbackError>
[src]
&self,
name: &str,
args: &[Value]
) -> Result<Value, CallCallbackError>
Call the given callback with the arguments
Examples
See the documentation of Self::set_callback
for an example
pub fn show(&self)
[src]
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
.
pub fn hide(&self)
[src]
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.
pub fn run(&self)
[src]
This is a convenience function that first calls Self::show
, followed by crate::run_event_loop()
and Self::hide
.
pub fn clone_strong(&self) -> Self
[src]
Clone this ComponentInstance
.
A ComponentInstance
is in fact a handle to a reference counted instance.
This function is semanticallt the same as the one from Clone::clone
, but
Clone is not implemented because of the danger of circular reference:
If you want to use this instance in a callback, you should capture a weak
reference given by Self::as_weak
.
pub fn as_weak(&self) -> WeakComponentInstance
[src]
Create a weak pointer to this component
Auto Trait Implementations
impl !RefUnwindSafe for ComponentInstance
impl !RefUnwindSafe for ComponentInstance
impl !Send for ComponentInstance
impl !Send for ComponentInstance
impl !Sync for ComponentInstance
impl !Sync for ComponentInstance
impl Unpin for ComponentInstance
impl Unpin for ComponentInstance
impl !UnwindSafe for ComponentInstance
impl !UnwindSafe for ComponentInstance