-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathmod.rs
More file actions
307 lines (254 loc) · 7.82 KB
/
mod.rs
File metadata and controls
307 lines (254 loc) · 7.82 KB
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
use std::any::Any;
use std::fmt::{self, Debug};
use std::ops::ControlFlow;
use anathema_geometry::{Pos, Region, Size};
use anathema_state::StateId;
use anathema_store::slab::SecondaryMap;
use anathema_store::tree::{Tree, TreeView};
use anathema_templates::ComponentBlueprintId;
use anathema_value_resolver::AttributeStorage;
pub use self::factory::Factory;
pub use self::style::{Attributes, Style};
use crate::WidgetContainer;
use crate::error::Result;
use crate::layout::{Constraints, LayoutCtx, PositionCtx, PositionFilter};
use crate::paint::{PaintCtx, PaintFilter, SizePos};
pub use crate::tree::{Filter, ForEach, LayoutForEach};
mod factory;
mod style;
pub type WidgetTreeView<'a, 'bp> = TreeView<'a, WidgetContainer<'bp>>;
pub type WidgetTree<'a> = Tree<WidgetContainer<'a>>;
pub type LayoutChildren<'a, 'bp> = LayoutForEach<'a, 'bp>;
pub type PositionChildren<'a, 'bp> = ForEach<'a, 'bp, PositionFilter>;
pub type PaintChildren<'a, 'bp> = ForEach<'a, 'bp, PaintFilter>;
pub type WidgetId = anathema_store::slab::Key;
#[derive(Debug)]
pub struct CompEntry {
/// The state owned by this component
pub state_id: StateId,
/// The components id in the widget tree
pub widget_id: WidgetId,
/// Does the component accept tick events
pub accept_ticks: bool,
component_id: ComponentBlueprintId,
}
/// Store a list of components currently in the tree
pub struct Components {
inner: Vec<CompEntry>,
}
impl Components {
pub fn new() -> Self {
Self { inner: vec![] }
}
pub fn push(
&mut self,
component_id: ComponentBlueprintId,
widget_id: WidgetId,
state_id: StateId,
accept_ticks: bool,
) {
let entry = CompEntry {
component_id,
widget_id,
state_id,
accept_ticks,
};
self.inner.push(entry)
}
pub fn try_remove(&mut self, widget_id: WidgetId) {
self.inner.retain(|entry| entry.widget_id != widget_id);
}
/// Get the component by its index
pub fn get(&mut self, index: usize) -> Option<(WidgetId, StateId)> {
self.inner.get(index).map(|e| (e.widget_id, e.state_id))
}
/// This is used to send messages to components.
/// The `ComponentBlueprintId` is only available to components that were added
/// as a singular component, not prototypes
pub fn get_by_component_id(&mut self, id: ComponentBlueprintId) -> Option<&CompEntry> {
self.inner.iter().find(|e| e.component_id == id)
}
/// Get the component by its widget id
pub fn get_by_widget_id(&mut self, id: WidgetId) -> Option<(WidgetId, StateId)> {
self.inner
.iter()
.find(|entry| entry.widget_id == id)
.map(|e| (e.widget_id, e.state_id))
}
/// Get widget id and state id for a component that accepts tick events
pub fn get_ticking(&self, index: usize) -> Option<(WidgetId, StateId)> {
self.inner
.get(index)
.and_then(|e| e.accept_ticks.then_some((e.widget_id, e.state_id)))
}
pub fn iter(&self) -> impl Iterator<Item = &CompEntry> {
self.inner.iter()
}
pub fn len(&self) -> usize {
self.inner.len()
}
}
#[derive(Debug)]
pub struct FloatingWidgets(SecondaryMap<WidgetId, WidgetId>);
impl FloatingWidgets {
pub fn empty() -> Self {
Self(SecondaryMap::empty())
}
pub fn try_remove(&mut self, key: WidgetId) {
self.0.try_remove(key);
}
pub(crate) fn insert(&mut self, widget_id: WidgetId) {
self.0.insert(widget_id, widget_id);
}
pub fn iter(&self) -> impl Iterator<Item = &WidgetId> {
self.0.iter()
}
}
/// Parent in a component relationship
#[derive(Debug, Copy, Clone)]
pub struct Parent(pub WidgetId);
impl From<Parent> for WidgetId {
fn from(value: Parent) -> Self {
value.0
}
}
impl From<WidgetId> for Parent {
fn from(value: WidgetId) -> Self {
Self(value)
}
}
/// Component relationships, tracking the parent component of each component
pub struct ComponentParents(SecondaryMap<ComponentBlueprintId, Parent>);
impl ComponentParents {
pub fn empty() -> Self {
Self(SecondaryMap::empty())
}
pub fn try_remove(&mut self, key: ComponentBlueprintId) {
self.0.try_remove(key);
}
pub fn get_parent(&self, child: ComponentBlueprintId) -> Option<Parent> {
self.0.get(child).copied()
}
}
/// Any widget should never be implemented directly
/// as it's implemented for any type that implements `Widget`
pub trait AnyWidget {
fn to_any_ref(&self) -> &dyn Any;
fn to_any_mut(&mut self) -> &mut dyn Any;
fn any_layout<'bp>(
&mut self,
children: LayoutForEach<'_, 'bp>,
constraints: Constraints,
id: WidgetId,
ctx: &mut LayoutCtx<'_, 'bp>,
) -> Result<Size>;
fn any_position<'bp>(
&mut self,
children: ForEach<'_, 'bp, PositionFilter>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PositionCtx,
);
fn any_paint<'bp>(
&mut self,
children: ForEach<'_, 'bp, PaintFilter>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PaintCtx<'_, SizePos>,
);
fn any_floats(&self) -> bool;
fn any_inner_bounds(&self, pos: Pos, size: Size) -> Region;
fn any_needs_reflow(&mut self) -> bool;
}
impl<T: 'static + Widget> AnyWidget for T {
fn to_any_ref(&self) -> &dyn Any {
self
}
fn to_any_mut(&mut self) -> &mut dyn Any {
self
}
fn any_layout<'bp>(
&mut self,
children: LayoutForEach<'_, 'bp>,
constraints: Constraints,
id: WidgetId,
ctx: &mut LayoutCtx<'_, 'bp>,
) -> Result<Size> {
self.layout(children, constraints, id, ctx)
}
fn any_position<'bp>(
&mut self,
children: ForEach<'_, 'bp, PositionFilter>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PositionCtx,
) {
self.position(children, id, attribute_storage, ctx)
}
fn any_paint<'bp>(
&mut self,
children: ForEach<'_, 'bp, PaintFilter>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PaintCtx<'_, SizePos>,
) {
self.paint(children, id, attribute_storage, ctx)
}
fn any_inner_bounds(&self, pos: Pos, size: Size) -> Region {
self.inner_bounds(pos, size)
}
fn any_floats(&self) -> bool {
self.floats()
}
fn any_needs_reflow(&mut self) -> bool {
self.needs_reflow()
}
}
impl Debug for dyn AnyWidget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<dyn AnyWidget>")
}
}
pub trait Widget {
fn layout<'bp>(
&mut self,
children: LayoutForEach<'_, 'bp>,
constraints: Constraints,
id: WidgetId,
ctx: &mut LayoutCtx<'_, 'bp>,
) -> Result<Size>;
fn paint<'bp>(
&mut self,
mut children: ForEach<'_, 'bp, PaintFilter>,
_id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
mut ctx: PaintCtx<'_, SizePos>,
) {
_ = children.each(|child, children| {
let ctx = ctx.to_unsized();
child.paint(children, ctx, attribute_storage);
ControlFlow::Continue(())
});
}
fn position<'bp>(
&mut self,
children: ForEach<'_, 'bp, PositionFilter>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PositionCtx,
);
fn floats(&self) -> bool {
false
}
fn inner_bounds(&self, pos: Pos, size: Size) -> Region {
Region::from((pos, size))
}
fn needs_reflow(&mut self) -> bool {
false
}
}
impl Debug for dyn Widget {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "<dyn Widget>")
}
}