-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathlib.rs
More file actions
210 lines (175 loc) · 7.43 KB
/
lib.rs
File metadata and controls
210 lines (175 loc) · 7.43 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
use std::ops::ControlFlow;
use std::time::Duration;
use anathema_geometry::{Pos, Size};
use anathema_value_resolver::{AttributeStorage, Scope};
use anathema_widgets::components::events::Event;
use anathema_widgets::error::Result;
use anathema_widgets::layout::{Constraints, LayoutCtx, LayoutFilter, PositionFilter, Viewport};
use anathema_widgets::paint::PaintFilter;
use anathema_widgets::{
DirtyWidgets, GlyphMap, Layout, LayoutForEach, PaintChildren, PositionChildren, WidgetTreeView,
};
pub mod testing;
pub mod tui;
pub trait Backend {
fn size(&self) -> Size;
fn next_event(&mut self, timeout: Duration) -> Option<Event>;
fn resize(&mut self, new_size: Size, glyph_map: &mut GlyphMap);
/// Paint the widgets
fn paint<'bp>(
&mut self,
glyph_map: &mut GlyphMap,
widgets: PaintChildren<'_, 'bp>,
attribute_storage: &AttributeStorage<'bp>,
);
/// Called by the runtime at the end of the frame.
fn render(&mut self, glyph_map: &mut GlyphMap);
/// Clear is called immediately after `render` is called.
fn clear(&mut self);
/// Finalizes the backend. This is called when the runtime starts.
fn finalize(&mut self) {}
}
// TODO: rename this.
// This does layout, position and paint and should have
// a less silly name
pub struct WidgetCycle<'rt, 'bp, T> {
backend: &'rt mut T,
tree: WidgetTreeView<'rt, 'bp>,
constraints: Constraints,
}
impl<'rt, 'bp, T: Backend> WidgetCycle<'rt, 'bp, T> {
pub fn new(backend: &'rt mut T, tree: WidgetTreeView<'rt, 'bp>, constraints: Constraints) -> Self {
Self {
backend,
tree,
constraints,
}
}
fn fixed(&mut self, ctx: &mut LayoutCtx<'_, 'bp>) -> Result<()> {
// -----------------------------------------------------------------------------
// - Position -
// -----------------------------------------------------------------------------
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::fixed());
// -----------------------------------------------------------------------------
// - Paint -
// -----------------------------------------------------------------------------
self.paint(ctx, PaintFilter::fixed());
Ok(())
}
fn floating(&mut self, ctx: &mut LayoutCtx<'_, 'bp>) -> Result<()> {
// -----------------------------------------------------------------------------
// - Position -
// -----------------------------------------------------------------------------
self.position(ctx.attribute_storage, *ctx.viewport, PositionFilter::floating());
// -----------------------------------------------------------------------------
// - Paint -
// -----------------------------------------------------------------------------
self.paint(ctx, PaintFilter::floating());
Ok(())
}
pub fn run(
&mut self,
ctx: &mut LayoutCtx<'_, 'bp>,
force_layout: bool,
dirty_widgets: &mut DirtyWidgets,
) -> Result<()> {
// -----------------------------------------------------------------------------
// - Layout -
// -----------------------------------------------------------------------------
self.layout(ctx, LayoutFilter, dirty_widgets, force_layout)?;
// -----------------------------------------------------------------------------
// - Position and paint -
// -----------------------------------------------------------------------------
self.fixed(ctx)?;
self.floating(ctx)?;
Ok(())
}
fn layout(
&mut self,
ctx: &mut LayoutCtx<'_, 'bp>,
filter: LayoutFilter,
dirty_widgets: &mut DirtyWidgets,
force_layout: bool,
) -> Result<()> {
#[cfg(feature = "profile")]
puffin::profile_function!();
let mut tree = self.tree.view();
if force_layout {
// Perform a layout across the entire tree
let scope = Scope::root();
let mut for_each = LayoutForEach::new(tree, &scope, filter);
let constraints = self.constraints;
_ = for_each.each(ctx, |ctx, widget, children| {
_ = widget.layout(children, constraints, ctx)?;
Ok(ControlFlow::Break(()))
})?;
return Ok(());
}
// If a widget has changed, mark the parent as dirty
// Layout only changed widgets.
// These are the parents of changed widgets.
//
// Investigate the possibility of attaching an offset as existing widgets don't need
// to reflow unless the constraint has changed.
//
// This means `additional_widgets` needs to store (key, offset) where offset can be None
//
// If this is going to work we need to consider `expand` and `spacer`
//
// Since widgets can be made by anyone and they are always guaranteed to give
// access to all their children this might not be a possibility.
//
// parent
// widget 0
// widget 1
// widget 2 | <- if this changes, only reflow this, three and four
// widget 3 |-- reflow
// widget 4 |
// TODO: make `additional_widgets` a scratch buffer part of `DirtyWidgets`.
// Also ensure that it tracks last id as well
// ... and removes it when done!
let mut additional_widgets = vec![];
loop {
for widget_id in dirty_widgets.drain() {
if !tree.contains(widget_id) {
continue;
}
tree.with_value_mut(widget_id, |_, widget, children| {
let scope = Scope::root();
let mut children = LayoutForEach::new(children, &scope, filter);
children.parent_element = Some(widget_id);
let parent_id = widget.parent_widget;
let anathema_widgets::WidgetKind::Element(widget) = &mut widget.kind else { return };
let constraints = widget.constraints();
if let Ok(Layout::Changed(_)) = widget.layout(children, constraints, ctx) {
// write into scratch buffer
if let Some(id) = parent_id {
additional_widgets.push(id);
}
}
});
}
// merge the scratch if it's not empty
if additional_widgets.is_empty() {
break;
}
dirty_widgets.inner.append(&mut additional_widgets);
}
Ok(())
}
fn position(&mut self, attributes: &AttributeStorage<'bp>, viewport: Viewport, filter: PositionFilter) {
#[cfg(feature = "profile")]
puffin::profile_function!();
let mut for_each = PositionChildren::new(self.tree.view(), attributes, filter);
_ = for_each.each(|widget, children| {
widget.position(children, Pos::ZERO, attributes, viewport);
ControlFlow::Break(())
});
}
fn paint(&mut self, ctx: &mut LayoutCtx<'_, 'bp>, filter: PaintFilter) {
#[cfg(feature = "profile")]
puffin::profile_function!();
let for_each = PaintChildren::new(self.tree.view(), ctx.attribute_storage, filter);
self.backend.paint(ctx.glyph_map, for_each, ctx.attribute_storage);
}
}