Module suzy::watch [−][src]
Suzy’s watch system provides the main way to define functionality within the framework. It enables you to describe the relationships between widgets in a declaritive way.
The watch system is based off an “automatic” observer pattern, inspired by Kivy’s “Kv Language”.
Inside a “watch” closure Suzy tracks which values are accessed at runtime and automatically binds to them. The closure is re-run when the bound values change.
The watch system is defined in terms of relationships between two API
surfaces: Watched
represents some data which will
be interesting to observe, and
WidgetInit::watch
is used to submit a closure which observes values.
A WatchedEvent
is similar to a Watched value.
Instead of representing a “current state” however, it provides a system
where each watch closure will be run exactly once with each value provided
to dispatch
.
Other utilities for less common situations are provided in this module.
Examples
Place a fixed sized button at the bottom-left of a custom widget, with
a small amount of padding. Because the layout instructions are included
in a closure submitted to watch
, whenever the position of the Widget
MyWidgetData
changes, the closure will be re-run and the position of the
button will be updated to match.
use suzy::widgets::Button; struct MyWidgetData { button: Button<ButtonContent>, } impl WidgetContent for MyWidgetData { fn init(mut init: impl WidgetInit<Self>) { init.watch(|this, rect| { this.button.set_width(200.0); this.button.set_height(100.0); this.button.set_left(rect.left() + 50.0); this.button.set_bottom(rect.bottom() + 50.0); }); } // ... }
Structs
Watched | This represents some value which will be interesting to watch. Watcher functions that reference this value will be re-run when this value changes. |
WatchedCell | A Watched value which provides interior mutability. This provides correct
behavior (triggering watch functions when changed) where |
WatchedEvent | A WatchedEvent uses the watch system provided by this crate to implement an event disptacher. This is different from a watched value (Watched) in that events will fire for each value passed to WatchedEvent::dispatch() and will not “store” the data. |
WatchedMeta | This provides the basic functionality behind watched values. You can use it to provide functionality using the watch system for cases where Watched and WatchedEvent are not appropriate. |
WatchedReceiver | The receiver half of a watched channel. |
WatchedSender | The sender half of a watched channel. |
Watcher | Watcher is a structure designed to hold some data along with associated functions which will run when watched data changes. |
WatcherId | A type representing a unique id for a particular instance of a watcher. This value may outlive the watcher, and will never compare equal to a value returned by the id method of a Watcher other than this one. |
WatcherMeta | This structure is used internally by Watcher |
Functions
watched_channel | Create a new asynchronous channel which is designed to work within the watch system. |