Controllers
Give an item behaviour by declaring a Controller in it, written
as a QML file or as a TypeScript module, and act on its host when
it is attached, detached and updated.
After reading this page, you will be able to write a
Controller as a QML file or as a TypeScript module, declare it inside the item it acts on, and use its lifecycle: attached, detached and updated.Some behaviour belongs to an item without being drawn: it follows the pointer, orbits a camera, keeps a flag in step with the window's size. A
Controller, from Ljos.Core, is a non-visual object that holds that behaviour. Declare one inside an item and that item is its host:host is the item it is declared in. A Controller declared inside another Controller shares that one's host. One declared in a popup acts on the popup's content item, where what is declared in a popup goes; if the popup's contentItem is replaced, it moves to the new one, with detached() and then attached(host).attached(host) runs once the host is complete, or when the Controller is put into a host that already is.detached() runs when the host goes (a Loader unloading it, a delegate removed), when the Controller itself is destroyed, or when it leaves a popup's content item for the one that replaces it.updated() runs once after attached, then once after each turn in which a property of the host changed, after the bindings have settled and before the frame is drawn.Everything else is what any object has: properties, functions, signals and children. A Controller's state is its properties, read and bound through its id, so there are no rules about the order of calls as React's hooks have.
A file whose root is
Controller is a type, named after the file. This one says whether its host is narrower than a width it is given:compact is a binding on the host's width, so it changes when the window does, or, here, when a tap makes the page narrower than 400. Where a property follows from the host, a binding says it best. updated is for what a binding cannot say.The gestures of
Ljos.Ui (Tap, Drag, Pinch, Hover, Wheel and Pointer) are Controllers too: their host is the item they act on. A gesture declared inside a Controller acts on the Controller's host, so a Controller can bundle the gestures of one behaviour. This one moves a viewpoint nearer or further with the wheel and back with a double tap. The view reads the clip planes from it, and they follow the distance through a conditional:The Controller has no size to keep equal to its host's, as an invisible
Item filling its parent would, and the clip planes are bindings the view reads, not values written from a handler that has to be called once by hand at the start.A
.ts module exports a Controller made by controller, which every module has without an import. It takes the Controller's typed properties, its bindings (each a function of the host and the Controller, evaluated again when what it reads changes, as a QML binding is) and its lifecycle as functions: attached, detached and updated. attached may return a function, which runs when the Controller is detached:The module form has no
class and no this: self is the Controller with its typed properties and bindings, and host is its host, an Item. A Controller from a module is named in QML through its import, Size.Breakpoint, and it is the same native object the QML form makes, so it is read and bound the same way. The checker types both: width: "wide" on a Size.Breakpoint is error: Invalid property assignment: number expected [invalid-assignment], and a key that is not one of the five is an error too. properties and bindings are written in the call, each member as name: value, since the Controller is made from the names written there: an object made elsewhere or a spread is an error.A Controller acts on an item, so it is declared in one, or in a Controller that is. One held in a property of a plain object has no host and is never attached, and
ljos check reports it as a `Controller` acts on the item it is declared in […] [controller-host].updated costs nothing unless a Controller handles it: the engine follows the host's properties only for Controllers with an onUpdated handler (or an updated function in a module). It runs once a turn however many properties changed, and what its handlers write does not run it again, so a Controller that handles it never keeps an idle app awake:Each tap changes
taps, a property of the host, so turns goes up by one. Writing turns changes the host as well, and does not run updated again.




