summaryrefslogtreecommitdiff
path: root/src/layout/pad.rs
diff options
context:
space:
mode:
authorLaurenz <laurmaedje@gmail.com>2021-10-31 15:52:16 +0100
committerLaurenz <laurmaedje@gmail.com>2021-10-31 15:52:35 +0100
commit5b344b663a3d224134923eea0d67ebf44c069b07 (patch)
tree34a5fb464a38b9d4cb11294379b3ddf351dfce21 /src/layout/pad.rs
parentfeff013abb17f31bc5305fe77fe67cf615c19ff2 (diff)
Reorganize modules
Instead of separating functionality into layout and library, everything lives in the library now. This way, related things live side by side and there are no duplicate file names in the two directories.
Diffstat (limited to 'src/layout/pad.rs')
-rw-r--r--src/layout/pad.rs77
1 files changed, 0 insertions, 77 deletions
diff --git a/src/layout/pad.rs b/src/layout/pad.rs
deleted file mode 100644
index 52766dfa..00000000
--- a/src/layout/pad.rs
+++ /dev/null
@@ -1,77 +0,0 @@
-use super::*;
-
-/// A node that adds padding to its child.
-#[derive(Debug, Hash)]
-pub struct PadNode {
- /// The amount of padding.
- pub padding: Sides<Linear>,
- /// The child node whose sides to pad.
- pub child: BlockNode,
-}
-
-impl BlockLevel for PadNode {
- fn layout(
- &self,
- ctx: &mut LayoutContext,
- regions: &Regions,
- ) -> Vec<Constrained<Rc<Frame>>> {
- // Layout child into padded regions.
- let mut frames = self.child.layout(
- ctx,
- &regions.map(|size| size - self.padding.resolve(size).size()),
- );
-
- for (Constrained { item: frame, cts }, (current, base)) in
- frames.iter_mut().zip(regions.iter())
- {
- fn solve_axis(length: Length, padding: Linear) -> Length {
- (length + padding.abs)
- .div_finite(1.0 - padding.rel.get())
- .unwrap_or_default()
- }
-
- // Solve for the size `padded` that satisfies (approximately):
- // `padded - padding.resolve(padded).size() == size`
- let padded = Size::new(
- solve_axis(frame.size.w, self.padding.left + self.padding.right),
- solve_axis(frame.size.h, self.padding.top + self.padding.bottom),
- );
-
- let padding = self.padding.resolve(padded);
- let origin = Point::new(padding.left, padding.top);
-
- // Create a new larger frame and place the child's frame inside it.
- let empty = Frame::new(padded, frame.baseline + origin.y);
- let prev = std::mem::replace(frame, Rc::new(empty));
- let new = Rc::make_mut(frame);
- new.push_frame(origin, prev);
-
- // Inflate min and max contraints by the padding.
- for spec in [&mut cts.min, &mut cts.max] {
- if let Some(x) = spec.x.as_mut() {
- *x += padding.size().w;
- }
- if let Some(y) = spec.y.as_mut() {
- *y += padding.size().h;
- }
- }
-
- // Set exact and base constraints if the child had them.
- cts.exact.x.and_set(Some(current.w));
- cts.exact.y.and_set(Some(current.h));
- cts.base.x.and_set(Some(base.w));
- cts.base.y.and_set(Some(base.h));
-
- // Also set base constraints if the padding is relative.
- if self.padding.left.is_relative() || self.padding.right.is_relative() {
- cts.base.x = Some(base.w);
- }
-
- if self.padding.top.is_relative() || self.padding.bottom.is_relative() {
- cts.base.y = Some(base.h);
- }
- }
-
- frames
- }
-}