1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use crate;
use Any;
/// A state trait of a widget (the `state`) comes in handy to provide
/// interactivity. It is not required to define a `state` for the
/// widget. But if you don't, you cut of the possibility to adapt
/// properties during runtime. The `view` of the widget will stay
/// static.
///
/// When defining a `state` of a widget, it inherits the values of its
/// associated properties (`current values`), as well as the
/// implemented system. To gain access, each state has to derive or
/// implement the [`Default`] and the [`AsAny`] traits. You are free
/// to implement associated functions to the `state`, that react on
/// triggered events or adapt current values. The `properties` are
/// stored via ECM. They are organized in a tree (parent, children or
/// level entities).
///
/// # Example
///
/// The following code will define a widget called `MyWidget` (the
/// `view`) with an associcated state called `MyState`. The `MyState`
/// structure defines a propery `count` (a level entity), that will
/// store values of type usize. Inside the `state` trait, the method
/// `init` will manipulate the value of the count property (42). The
/// `update` method will increment the `count` property each time the
/// `view` got dirty and initiates a new render cycle.
///
/// ```rust
/// use orbtk::prelude::*;
///
/// #[derive(Default, AsAny)]
/// struct MyState {
/// count: usize
/// }
///
/// impl State for MyState {
/// fn init(&mut self, _registry: &mut Registry, _ctx: &mut Context) {
/// self.count = 42;
/// println!("MyState initialized.");
/// }
///
/// fn update(&mut self, _registry: &mut Registry, _ctx: &mut Context) {
/// self.count += 1;
/// println("MyState updated.");
/// }
///
/// fn update_post_layout(&mut self, _registry: &mut Registry, _ctx: &mut Context) {
/// println("MyState updated after layout is calculated.");
/// }
/// }
///
/// widget!(MyWidget<MyState>)
/// ```
///
/// [`widget`]: ./trait.Widget.html
/// [`Default`]: (https://doc.rust-lang.org/std/default/trait.Default.html)
/// [`AsAny`]: ./trait.AsAny.html