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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#[cfg(not(feature = "layout"))] mod layout_extern;
#[cfg(feature = "cassowary")]
#[macro_use] mod layout_cw;
mod class;
mod data;
#[cfg(not(feature = "layout"))]
pub use self::layout_extern::{Layout, ChildLayout, GridPos};
#[cfg(feature = "cassowary")]
pub use self::layout_cw::{Layout};
pub use self::data::*;
pub use self::class::Class;
use crate::toolkit::TkData;
pub trait Core {
fn number(&self) -> u32;
fn set_number(&mut self, number: u32);
fn tkd(&self) -> TkData;
fn set_tkd(&mut self, tkd: TkData);
fn rect(&self) -> &Rect;
fn rect_mut(&mut self) -> &mut Rect;
}
#[derive(Clone, Default, Debug)]
pub struct CoreData {
number: u32,
tkd: TkData,
rect: Rect,
}
impl Core for CoreData {
#[inline]
fn number(&self) -> u32 {
self.number
}
#[inline]
fn set_number(&mut self, number: u32) {
if self.number != 0 {
panic!("widget number has been set twice");
}
self.number = number;
}
#[inline]
fn tkd(&self) -> TkData {
self.tkd.clone()
}
#[inline]
fn set_tkd(&mut self, tkd: TkData) {
if !self.tkd.is_null() && !tkd.is_null() {
panic!("widget's toolkit data has been set twice");
}
self.tkd = tkd;
}
#[inline]
fn rect(&self) -> &Rect {
&self.rect
}
#[inline]
fn rect_mut(&mut self) -> &mut Rect {
&mut self.rect
}
}
pub trait Widget: Layout {
fn class(&self) -> Class;
fn label(&self) -> Option<&str>;
fn is_editable(&self) -> bool {
false
}
fn len(&self) -> usize;
fn get(&self, index: usize) -> Option<&Widget>;
fn get_mut(&mut self, index: usize) -> Option<&mut Widget>;
fn enumerate(&mut self, mut n: u32) -> u32 {
for i in 0..self.len() {
self.get_mut(i).map(|w| n = w.enumerate(n));
}
self.set_number(n);
n + 1
}
}
pub struct ChildIter<'a, W: 'a + Widget + ?Sized> {
w: &'a W,
i: ::std::ops::Range<usize>,
}
impl<'a, W: 'a + Widget + ?Sized> ChildIter<'a, W> {
pub fn new(widget: &'a W) -> Self {
ChildIter { w: widget, i: (0..widget.len()).into_iter() }
}
}
impl<'a, W: 'a + Widget + ?Sized> Iterator for ChildIter<'a, W> {
type Item = &'a Widget;
fn next(&mut self) -> Option<Self::Item> {
self.i.next().and_then(|i| self.w.get(i))
}
}