-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathlayout.rs
More file actions
591 lines (520 loc) · 16.3 KB
/
layout.rs
File metadata and controls
591 lines (520 loc) · 16.3 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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
use std::{fmt::Display, num::ParseFloatError, str::FromStr};
use taffy::{
NodeId, Style, TaffyTree, TrackSizingFunction,
prelude::{auto, fr, length, line, percent, span},
};
use crate::{HAlign, Margin, Orient, Point, Rect, Size, VAlign};
/// Trait for a widget to set visibility.
pub trait Visible {
/// If the widget is visible.
fn is_visible(&self) -> bool;
/// Set the visibility.
fn set_visible(&mut self, v: bool);
/// Show the widget.
fn show(&mut self) {
self.set_visible(true);
}
/// Hide the widget.
fn hide(&mut self) {
self.set_visible(false);
}
}
/// Trait for a widget to enable or disable.
pub trait Enable {
/// If the widget is enabled.
fn is_enabled(&self) -> bool;
/// Set if the widget is enabled.
fn set_enabled(&mut self, v: bool);
/// Enable the widget.
fn enable(&mut self) {
self.set_enabled(true);
}
/// Disable the widget.
fn disable(&mut self) {
self.set_enabled(false);
}
}
/// Trait for a layoutable widget.
pub trait Layoutable {
/// The left top location.
fn loc(&self) -> Point;
/// Move the location.
fn set_loc(&mut self, p: Point);
/// The size.
fn size(&self) -> Size;
/// Resize.
fn set_size(&mut self, s: Size);
/// The bounding rectangle.
fn rect(&self) -> Rect {
Rect::new(self.loc(), self.size())
}
/// Set the location and size.
fn set_rect(&mut self, r: Rect) {
self.set_loc(r.origin);
self.set_size(r.size);
}
/// The preferred size.
fn preferred_size(&self) -> Size {
Size::zero()
}
}
fn rect_t2e(rect: &taffy::Layout, margin: Margin) -> Rect {
Rect::new(
Point::new(
rect.location.x as f64 + margin.left,
rect.location.y as f64 + margin.top,
),
Size::new(
rect.size.width as f64 - margin.horizontal(),
rect.size.height as f64 - margin.vertical(),
),
)
}
fn offset(mut a: Rect, offset: Point) -> Rect {
a.origin += offset.to_vector();
a
}
struct LayoutChild<'a> {
widget: &'a mut dyn Layoutable,
width: Option<f64>,
height: Option<f64>,
margin: Margin,
grow: bool,
halign: HAlign,
valign: VAlign,
column: usize,
row: usize,
column_span: usize,
row_span: usize,
}
impl<'a> LayoutChild<'a> {
pub fn new(widget: &'a mut dyn Layoutable) -> Self {
Self {
widget,
width: None,
height: None,
margin: Margin::zero(),
grow: false,
halign: HAlign::Stretch,
valign: VAlign::Stretch,
column: 0,
row: 0,
column_span: 1,
row_span: 1,
}
}
}
/// Builder of a layoutable child.
pub struct LayoutChildBuilder<'a, 'b> {
child: LayoutChild<'a>,
children: &'b mut Vec<LayoutChild<'a>>,
}
impl LayoutChildBuilder<'_, '_> {
/// Specify the child width.
pub fn width(mut self, v: f64) -> Self {
self.child.width = Some(v);
self
}
/// Specify the child height.
pub fn height(mut self, v: f64) -> Self {
self.child.height = Some(v);
self
}
/// Specify the child size.
pub fn size(self, s: Size) -> Self {
self.width(s.width).height(s.height)
}
/// Margin of the child.
pub fn margin(mut self, m: Margin) -> Self {
self.child.margin = m;
self
}
/// Whether to fill the remain space of the container.
pub fn grow(mut self, v: bool) -> Self {
self.child.grow = v;
self
}
/// Horizontal alignment in the available area.
pub fn halign(mut self, v: HAlign) -> Self {
self.child.halign = v;
self
}
/// Vertical alignment in the available area.
pub fn valign(mut self, v: VAlign) -> Self {
self.child.valign = v;
self
}
/// The column index in the grid.
pub fn column(mut self, c: usize) -> Self {
self.child.column = c;
self
}
/// The row index in the grid.
pub fn row(mut self, r: usize) -> Self {
self.child.row = r;
self
}
/// The column span in the grid.
pub fn column_span(mut self, s: usize) -> Self {
self.child.column_span = s;
self
}
/// The row span in the grid.
pub fn row_span(mut self, s: usize) -> Self {
self.child.row_span = s;
self
}
/// Add the child to the container.
pub fn finish(self) {
self.children.push(self.child);
}
}
/// A stacked layout container.
pub struct StackPanel<'a> {
children: Vec<LayoutChild<'a>>,
orient: Orient,
loc: Point,
size: Size,
}
impl<'a> StackPanel<'a> {
/// Create [`StackPanel`] with orientation.
pub fn new(orient: Orient) -> Self {
Self {
children: vec![],
orient,
loc: Point::zero(),
size: Size::zero(),
}
}
/// Push a child into the panel.
pub fn push<'b>(&'b mut self, widget: &'a mut dyn Layoutable) -> LayoutChildBuilder<'a, 'b> {
LayoutChildBuilder {
child: LayoutChild::new(widget),
children: &mut self.children,
}
}
fn tree(&self) -> (TaffyTree, NodeId, Vec<NodeId>) {
let mut tree: TaffyTree<()> = TaffyTree::new();
let mut nodes = vec![];
for child in &self.children {
let mut preferred_size = child.widget.preferred_size();
preferred_size.width += child.margin.horizontal();
preferred_size.height += child.margin.vertical();
let mut style = Style::default();
style.size.width = match child.width {
Some(w) => length(w as f32),
None => match (self.orient, child.halign, child.grow) {
(Orient::Vertical, HAlign::Stretch, _) => percent(1.0),
(Orient::Horizontal, _, true) => auto(),
_ => length(preferred_size.width as f32),
},
};
style.size.height = match child.height {
Some(h) => length(h as f32),
None => match (self.orient, child.valign, child.grow) {
(Orient::Horizontal, VAlign::Stretch, _) => percent(1.0),
(Orient::Vertical, _, true) => auto(),
_ => length(preferred_size.height as f32),
},
};
style.min_size = taffy::Size {
width: length(preferred_size.width as f32),
height: length(preferred_size.height as f32),
};
match self.orient {
Orient::Horizontal => {
if matches!(child.valign, VAlign::Top | VAlign::Center) {
style.margin.bottom = auto();
}
if matches!(child.valign, VAlign::Bottom | VAlign::Center) {
style.margin.top = auto();
}
}
Orient::Vertical => {
if matches!(child.halign, HAlign::Left | HAlign::Center) {
style.margin.right = auto();
}
if matches!(child.halign, HAlign::Right | HAlign::Center) {
style.margin.left = auto();
}
}
}
if child.grow {
style.flex_grow = 1.0
}
let node = tree.new_leaf(style).unwrap();
nodes.push(node);
}
let root = tree
.new_with_children(
Style {
size: taffy::Size::from_percent(1.0, 1.0),
flex_direction: match self.orient {
Orient::Horizontal => taffy::FlexDirection::Row,
Orient::Vertical => taffy::FlexDirection::Column,
},
..Default::default()
},
&nodes,
)
.unwrap();
(tree, root, nodes)
}
fn render(&mut self) {
let (mut tree, root, nodes) = self.tree();
tree.compute_layout(
root,
taffy::Size {
width: taffy::AvailableSpace::Definite(self.size.width as _),
height: taffy::AvailableSpace::Definite(self.size.height as _),
},
)
.unwrap();
for (id, child) in nodes.iter().zip(&mut self.children) {
let layout = tree.layout(*id).unwrap();
child
.widget
.set_rect(offset(rect_t2e(layout, child.margin), self.loc));
}
}
}
impl Layoutable for StackPanel<'_> {
fn loc(&self) -> Point {
self.loc
}
fn set_loc(&mut self, p: Point) {
self.loc = p;
self.render();
}
fn size(&self) -> Size {
self.size
}
fn set_size(&mut self, s: Size) {
self.size = s;
self.render();
}
fn set_rect(&mut self, r: Rect) {
self.loc = r.origin;
self.size = r.size;
self.render();
}
fn preferred_size(&self) -> Size {
let (mut tree, root, _) = self.tree();
tree.compute_layout(root, taffy::Size::max_content())
.unwrap();
rect_t2e(tree.layout(root).unwrap(), Margin::zero()).size
}
}
/// Error can be returned when parsing [`GridLength`].
#[derive(Debug)]
#[non_exhaustive]
pub enum ParseGridLengthError {
/// Invalid length value.
InvalidLength(ParseFloatError),
}
impl Display for ParseGridLengthError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::InvalidLength(e) => write!(f, "invalid length value: {e}"),
}
}
}
impl std::error::Error for ParseGridLengthError {}
/// The width or height of a grid cell.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum GridLength {
/// The length is determined automatically.
Auto,
/// Represents a relative ratio.
Stretch(f64),
/// Fixed length.
Length(f64),
}
impl FromStr for GridLength {
type Err = ParseGridLengthError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.eq_ignore_ascii_case("auto") {
Ok(Self::Auto)
} else if let Some(s) = s.strip_suffix('*') {
s.parse::<f64>()
.map(Self::Stretch)
.map_err(ParseGridLengthError::InvalidLength)
} else {
s.parse::<f64>()
.map(Self::Length)
.map_err(ParseGridLengthError::InvalidLength)
}
}
}
impl From<GridLength> for TrackSizingFunction {
fn from(value: GridLength) -> Self {
match value {
GridLength::Auto => auto(),
GridLength::Length(v) => length(v as f32),
GridLength::Stretch(v) => fr(v as f32),
}
}
}
impl From<&GridLength> for TrackSizingFunction {
fn from(value: &GridLength) -> Self {
TrackSizingFunction::from(*value)
}
}
/// A grid layout container.
pub struct Grid<'a> {
children: Vec<LayoutChild<'a>>,
columns: Vec<GridLength>,
rows: Vec<GridLength>,
loc: Point,
size: Size,
}
impl<'a> Grid<'a> {
/// Create [`Grid`].
pub fn new(columns: Vec<GridLength>, rows: Vec<GridLength>) -> Self {
Self {
children: vec![],
columns,
rows,
loc: Point::zero(),
size: Size::zero(),
}
}
/// Create [`Grid`] with string-representative of grid lengths.
pub fn from_str(
columns: impl AsRef<str>,
rows: impl AsRef<str>,
) -> Result<Self, ParseGridLengthError> {
Ok(Self::new(
Self::parse_grid_lengths(columns.as_ref())?,
Self::parse_grid_lengths(rows.as_ref())?,
))
}
fn parse_grid_lengths(s: &str) -> Result<Vec<GridLength>, ParseGridLengthError> {
let mut lengths = vec![];
for s in s.split(',') {
let s = s.trim();
lengths.push(s.parse()?);
}
Ok(lengths)
}
/// Push a child into the panel.
pub fn push<'b>(&'b mut self, widget: &'a mut dyn Layoutable) -> LayoutChildBuilder<'a, 'b> {
LayoutChildBuilder {
child: LayoutChild::new(widget),
children: &mut self.children,
}
}
fn tree(&self) -> (TaffyTree, NodeId, Vec<NodeId>) {
let mut tree: TaffyTree<()> = TaffyTree::new();
let mut nodes = vec![];
for child in &self.children {
let mut preferred_size = child.widget.preferred_size();
preferred_size.width += child.margin.horizontal();
preferred_size.height += child.margin.vertical();
let mut style = Style::default();
style.size.width = match child.width {
Some(w) => length(w as f32),
None => match child.halign {
HAlign::Stretch => auto(),
_ => length(preferred_size.width as f32),
},
};
style.size.height = match child.height {
Some(h) => length(h as f32),
None => match child.valign {
VAlign::Stretch => auto(),
_ => length(preferred_size.height as f32),
},
};
style.min_size = taffy::Size {
width: length(preferred_size.width as f32),
height: length(preferred_size.height as f32),
};
if matches!(child.valign, VAlign::Top | VAlign::Center) {
style.margin.bottom = auto();
}
if matches!(child.valign, VAlign::Bottom | VAlign::Center) {
style.margin.top = auto();
}
if matches!(child.halign, HAlign::Left | HAlign::Center) {
style.margin.right = auto();
}
if matches!(child.halign, HAlign::Right | HAlign::Center) {
style.margin.left = auto();
}
style.grid_column.start = line(child.column as i16 + 1);
style.grid_row.start = line(child.row as i16 + 1);
let cspan = child.column_span;
if cspan > 1 {
style.grid_column.end = span(cspan as u16);
}
let rspan = child.row_span;
if rspan > 1 {
style.grid_row.end = span(rspan as u16);
}
let node = tree.new_leaf(style).unwrap();
nodes.push(node);
}
let root = tree
.new_with_children(
Style {
display: taffy::Display::Grid,
size: taffy::Size::from_percent(1.0, 1.0),
grid_template_columns: self
.columns
.iter()
.map(TrackSizingFunction::from)
.collect(),
grid_template_rows: self.rows.iter().map(TrackSizingFunction::from).collect(),
..Default::default()
},
&nodes,
)
.unwrap();
(tree, root, nodes)
}
fn render(&mut self) {
let (mut tree, root, nodes) = self.tree();
tree.compute_layout(
root,
taffy::Size {
width: taffy::AvailableSpace::Definite(self.size.width as _),
height: taffy::AvailableSpace::Definite(self.size.height as _),
},
)
.unwrap();
for (id, child) in nodes.iter().zip(&mut self.children) {
let layout = tree.layout(*id).unwrap();
child
.widget
.set_rect(offset(rect_t2e(layout, child.margin), self.loc));
}
}
}
impl Layoutable for Grid<'_> {
fn loc(&self) -> Point {
self.loc
}
fn set_loc(&mut self, p: Point) {
self.loc = p;
self.render();
}
fn size(&self) -> Size {
self.size
}
fn set_size(&mut self, s: Size) {
self.size = s;
self.render();
}
fn set_rect(&mut self, r: Rect) {
self.loc = r.origin;
self.size = r.size;
self.render();
}
fn preferred_size(&self) -> Size {
let (mut tree, root, _) = self.tree();
tree.compute_layout(root, taffy::Size::max_content())
.unwrap();
rect_t2e(tree.layout(root).unwrap(), Margin::zero()).size
}
}