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
|
use std::f64::consts::SQRT_2;
use super::prelude::*;
use super::{PadNode, SizedNode};
use crate::util::RcExt;
/// `rect`: A rectangle with optional content.
pub fn rect(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
let fill = args.named("fill")?;
let body = args.find();
Ok(shape_impl(ShapeKind::Rect, width, height, fill, body))
}
/// `square`: A square with optional content.
pub fn square(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let size = args.named::<Length>("size")?.map(Linear::from);
let width = match size {
None => args.named("width")?,
size => size,
};
let height = match size {
None => args.named("height")?,
size => size,
};
let fill = args.named("fill")?;
let body = args.find();
Ok(shape_impl(ShapeKind::Square, width, height, fill, body))
}
/// `ellipse`: An ellipse with optional content.
pub fn ellipse(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let width = args.named("width")?;
let height = args.named("height")?;
let fill = args.named("fill")?;
let body = args.find();
Ok(shape_impl(ShapeKind::Ellipse, width, height, fill, body))
}
/// `circle`: A circle with optional content.
pub fn circle(_: &mut EvalContext, args: &mut Args) -> TypResult<Value> {
let diameter = args.named("radius")?.map(|r: Length| 2.0 * Linear::from(r));
let width = match diameter {
None => args.named("width")?,
diameter => diameter,
};
let height = match diameter {
None => args.named("height")?,
diameter => diameter,
};
let fill = args.named("fill")?;
let body = args.find();
Ok(shape_impl(ShapeKind::Circle, width, height, fill, body))
}
fn shape_impl(
kind: ShapeKind,
width: Option<Linear>,
height: Option<Linear>,
fill: Option<Color>,
body: Option<Template>,
) -> Value {
// Set default fill if there's no fill.
let fill = fill.unwrap_or(Color::Rgba(RgbaColor::gray(175)));
Value::Template(Template::from_inline(move |style| {
let shape = Layout::pack(ShapeNode {
kind,
fill: Some(Paint::Color(fill)),
child: body.as_ref().map(|body| body.pack(style)),
});
if width.is_some() || height.is_some() {
Layout::pack(SizedNode {
sizing: Spec::new(width, height),
child: shape,
})
} else {
shape
}
}))
}
/// Places its child into a sizable and fillable shape.
#[derive(Debug, Hash)]
pub struct ShapeNode {
/// Which shape to place the child into.
pub kind: ShapeKind,
/// How to fill the shape, if at all.
pub fill: Option<Paint>,
/// The child node to place into the shape, if any.
pub child: Option<PackedNode>,
}
/// The type of a shape.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum ShapeKind {
/// A rectangle with equal side lengths.
Square,
/// A quadrilateral with four right angles.
Rect,
/// An ellipse with coinciding foci.
Circle,
/// A curve around two focal points.
Ellipse,
}
impl Layout for ShapeNode {
fn layout(
&self,
ctx: &mut LayoutContext,
regions: &Regions,
) -> Vec<Constrained<Rc<Frame>>> {
// Layout.
let mut frame = if let Some(child) = &self.child {
let mut node: &dyn Layout = child;
let storage;
if matches!(self.kind, ShapeKind::Circle | ShapeKind::Ellipse) {
// Padding with this ratio ensures that a rectangular child fits
// perfectly into a circle / an ellipse.
storage = PadNode {
padding: Sides::splat(Relative::new(0.5 - SQRT_2 / 4.0).into()),
child: child.clone(),
};
node = &storage;
}
// Now, layout the child.
let mut frames = node.layout(ctx, regions);
if matches!(self.kind, ShapeKind::Square | ShapeKind::Circle) {
// Relayout with full expansion into square region to make sure
// the result is really a square or circle.
let size = frames[0].item.size;
let mut pod = regions.clone();
pod.current.w = size.w.max(size.h).min(pod.current.w);
pod.current.h = pod.current.w;
pod.expand = Spec::splat(true);
frames = node.layout(ctx, &pod);
}
// Validate and set constraints.
assert_eq!(frames.len(), 1);
Rc::take(frames.into_iter().next().unwrap().item)
} else {
let default = Length::pt(30.0);
let size = Size::new(
if regions.expand.x && regions.current.w.is_finite() {
regions.current.w
} else {
match self.kind {
ShapeKind::Square | ShapeKind::Circle => default,
ShapeKind::Rect | ShapeKind::Ellipse => 1.5 * default,
}
},
if regions.expand.y && regions.current.h.is_finite() {
regions.current.h
} else {
default
},
);
Frame::new(size, size.h)
};
// Add background shape if desired.
if let Some(fill) = self.fill {
let (pos, geometry) = match self.kind {
ShapeKind::Square | ShapeKind::Rect => {
(Point::zero(), Geometry::Rect(frame.size))
}
ShapeKind::Circle | ShapeKind::Ellipse => {
(frame.size.to_point() / 2.0, Geometry::Ellipse(frame.size))
}
};
frame.prepend(pos, Element::Geometry(geometry, fill));
}
// Generate tight constraints for now.
let mut cts = Constraints::new(regions.expand);
cts.exact = regions.current.to_spec().map(Some);
cts.base = regions.base.to_spec().map(Some);
vec![frame.constrain(cts)]
}
}
|