Class ComponentInstance

Inheritance Relationships

Base Type

  • private Dyn

Class Documentation

class sixtyfps::interpreter::ComponentInstance : private Dyn

The ComponentInstance represents a running instance of a 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::show() or the ComponentInstance::run()

Public Functions

inline void show() const

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 sixtyfps::run_event_loop().

inline void hide() const

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.

inline void run() const

This is a convenience function that first calls show(), followed by sixtyfps::run_event_loop() and hide().

inline QWidget *qwidget() const

Return a QWidget for this instance. This function is only available if the qt graphical backend was compiled in, and it may return nullptr if the Qt backend is not used at runtime.

inline bool set_property(std::string_view name, const Value &value) const

Set the value for a public property of this component

For example, if the component has a property <string> hello;, we can set this property

instance->set_property("hello", sixtyfps::SharedString("world"));

Returns true if the property was correctly set. Returns false if the property could not be set because it either do not exist (was not declared in .60) or if the value is not of the proper type for the property’s type.

inline std::optional<Value> get_property(std::string_view name) const

Returns the value behind a property declared in .60.

inline std::optional<Value> invoke_callback(std::string_view name, Slice<Value> args) const

Invoke the specified callback declared in .60 with the given arguments

Example: imagine the .60 file contains the given callback declaration:

callback foo(string, int) -> string;

Then one can call it with this function

sixtyfps::Value args[] = { SharedString("Hello"), 42. };
instance->invoke_callback("foo", { args, 2 });

Returns an null optional if the callback don’t exist or if the argument don’t match Otherwise return the returned value from the callback, which may be an empty Value if the callback did not return a value.

template<typename F>
inline bool set_callback(std::string_view name, F callback) const

Set a handler for the callback with the given name.

A callback with that name must be defined in the document otherwise the function returns false.

The callback parameter is a functor which takes as argument a slice of Value and must return a Value.

Example: imagine the .60 file contains the given callback declaration:

callback foo(string, int) -> string;

Then one can call it with this function

instance->set_callback("foo", [](auto args) {
   std::cout << "foo(" << *args[0].to_string() << ", " << *args[1].to_number() << ")\n";
});

Note: Since the ComponentInstance holds the handler, the handler itself should not capture a strong reference to the instance.