-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathoverflow.rs
More file actions
303 lines (253 loc) · 8.43 KB
/
overflow.rs
File metadata and controls
303 lines (253 loc) · 8.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
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
use std::ops::ControlFlow;
use anathema_geometry::{Pos, Size};
use anathema_value_resolver::AttributeStorage;
use anathema_widgets::error::Result;
use anathema_widgets::layout::{Constraints, LayoutCtx, PositionCtx};
use anathema_widgets::paint::{PaintCtx, SizePos};
use anathema_widgets::{LayoutForEach, PaintChildren, PositionChildren, Widget, WidgetId};
use crate::layout::many::Many;
use crate::layout::{AXIS, Axis, DIRECTION, Direction};
use crate::{HEIGHT, WIDTH};
const UNCONSTRAINED: &str = "unconstrained";
const CLAMP: &str = "clamp";
#[derive(Debug, Default)]
pub struct Overflow {
offset: Pos,
// The size of the children since the last layout call
inner_size: Size,
direction: Direction,
is_dirty: bool,
}
impl Overflow {
pub fn scroll(&mut self, direction: Direction, amount: Pos) {
self.is_dirty = true;
match (self.direction, direction) {
(Direction::Forward, Direction::Forward) => self.offset += amount,
(Direction::Forward, Direction::Backward) => self.offset -= amount,
(Direction::Backward, Direction::Backward) => self.offset += amount,
(Direction::Backward, Direction::Forward) => self.offset -= amount,
}
}
pub fn scroll_up(&mut self) {
self.scroll(Direction::Backward, Pos { x: 0, y: 1 });
}
pub fn scroll_up_by(&mut self, amount: i32) {
self.scroll(Direction::Backward, Pos { x: 0, y: amount });
}
pub fn scroll_down(&mut self) {
self.scroll(Direction::Forward, Pos { x: 0, y: 1 });
}
pub fn scroll_down_by(&mut self, amount: i32) {
self.scroll(Direction::Forward, Pos { x: 0, y: amount });
}
pub fn scroll_right(&mut self) {
self.scroll(Direction::Forward, Pos { x: 1, y: 0 });
}
pub fn scroll_right_by(&mut self, amount: i32) {
self.scroll(Direction::Forward, Pos { x: amount, y: 0 });
}
pub fn scroll_left(&mut self) {
self.scroll(Direction::Backward, Pos { x: 1, y: 0 });
}
pub fn scroll_left_by(&mut self, amount: i32) {
self.scroll(Direction::Backward, Pos { x: amount, y: 0 });
}
pub fn scroll_to(&mut self, pos: Pos) {
self.offset = pos;
}
pub fn offset(&self) -> Pos {
self.offset
}
fn clamp(&mut self, children: Size, parent: Size) {
if self.offset.x < 0 {
self.offset.x = 0;
}
if self.offset.y < 0 {
self.offset.y = 0;
}
if children.height <= parent.height {
self.offset.y = 0;
} else {
let max_y = children.height as i32 - parent.height as i32;
if self.offset.y > max_y {
self.offset.y = max_y;
}
}
if children.width <= parent.width {
self.offset.x = 0;
} else {
let max_x = children.width as i32 - parent.width as i32;
if self.offset.x > max_x {
self.offset.x = max_x
}
}
}
}
impl Widget for Overflow {
fn layout<'bp>(
&mut self,
children: LayoutForEach<'_, 'bp>,
mut constraints: Constraints,
id: WidgetId,
ctx: &mut LayoutCtx<'_, 'bp>,
) -> Result<Size> {
let attributes = ctx.attribute_storage.get(id);
let axis = attributes.get_as(AXIS).unwrap_or(Axis::Vertical);
let output_size: Size = (constraints.max_width(), constraints.max_height()).into();
match axis {
Axis::Horizontal => constraints.unbound_width(),
Axis::Vertical => constraints.unbound_height(),
}
if attributes.get_as::<bool>(UNCONSTRAINED).unwrap_or_default() {
constraints.unbound_width();
constraints.unbound_height();
}
if let Some(width) = attributes.get_as::<u16>(WIDTH) {
constraints.make_width_tight(width);
}
if let Some(height) = attributes.get_as::<u16>(HEIGHT) {
constraints.make_height_tight(height);
}
self.direction = attributes.get_as(DIRECTION).unwrap_or_default();
// Make `unconstrained` an enum instead of a `bool`
let unconstrained = true;
let mut many = Many::new(self.direction, axis, unconstrained);
// NOTE: we use the inner size here from many.layout
_ = many.layout(children, constraints, ctx)?;
self.inner_size = many.used_size.inner_size();
Ok(output_size)
}
fn position<'bp>(
&mut self,
mut children: PositionChildren<'_, 'bp>,
id: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
ctx: PositionCtx,
) {
let attributes = attribute_storage.get(id);
let direction = attributes.get_as(DIRECTION).unwrap_or_default();
let axis = attributes.get_as(AXIS).unwrap_or(Axis::Vertical);
let mut pos = ctx.pos;
// If the value is clamped, update the offset
match attributes.get_as::<bool>(CLAMP).unwrap_or(true) {
false => (),
true => self.clamp(self.inner_size, ctx.inner_size),
}
if let Direction::Backward = direction {
match axis {
Axis::Horizontal => pos.x += ctx.inner_size.width as i32,
Axis::Vertical => pos.y += ctx.inner_size.height as i32,
}
}
let mut pos = match direction {
Direction::Forward => pos - self.offset,
Direction::Backward => pos + self.offset,
};
let mut count = 0;
_ = children.each(|node, children| {
match direction {
Direction::Forward => {
node.position(children, pos, attribute_storage, ctx.viewport);
match axis {
Axis::Horizontal => pos.x += node.size().width as i32,
Axis::Vertical => pos.y += node.size().height as i32,
}
}
Direction::Backward => {
match axis {
Axis::Horizontal => pos.x -= node.size().width as i32,
Axis::Vertical => pos.y -= node.size().height as i32,
}
node.position(children, pos, attribute_storage, ctx.viewport);
}
}
count += 1;
ControlFlow::Continue(())
});
}
fn paint<'bp>(
&mut self,
mut children: PaintChildren<'_, 'bp>,
_: WidgetId,
attribute_storage: &AttributeStorage<'bp>,
mut ctx: PaintCtx<'_, SizePos>,
) {
let region = ctx.create_region();
_ = children.each(|widget, children| {
ctx.set_clip_region(region);
let ctx = ctx.to_unsized();
widget.paint(children, ctx, attribute_storage);
ControlFlow::Continue(())
});
}
fn needs_reflow(&mut self) -> bool {
let needs_reflow = self.is_dirty;
self.is_dirty = false;
needs_reflow
}
}
#[cfg(test)]
mod test {
use crate::Overflow;
use crate::testing::TestRunner;
#[test]
fn overflow() {
let tpl = "
overflow
for i in [0, 1, 2]
border
text i
";
let expected_first = "
╔═══╗
║┌─┐║
║│0│║
║└─┘║
║┌─┐║
║│1│║
║└─┘║
╚═══╝
";
let expected_second = "
╔═══╗
║│0│║
║└─┘║
║┌─┐║
║│1│║
║└─┘║
║┌─┐║
╚═══╝
";
TestRunner::new(tpl, (3, 6))
.instance()
.render_assert(expected_first)
.with_widget(|mut query| {
query.by_tag("overflow").first(|el, _| {
let overflow = el.to::<Overflow>();
overflow.scroll_down();
});
})
.render_assert(expected_second);
}
#[test]
fn clamp_prevents_scrolling() {
let tpl = "
overflow
text '0'";
let expected_first = "
╔═══╗
║0 ║
║ ║
╚═══╝
";
TestRunner::new(tpl, (3, 2))
.instance()
.with_widget(|mut query| {
query.by_tag("overflow").first(|el, _| {
let overflow = el.to::<Overflow>();
overflow.scroll_down();
});
})
.render_assert(expected_first);
}
}