summaryrefslogtreecommitdiff
path: root/src/layout/background.rs
blob: 17280a86a1f0d86c2098ebe7c3866d56e8c31d15 (plain) (blame)
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
use super::*;

/// A node that places a rectangular filled background behind its child.
#[derive(Debug, Clone, PartialEq)]
pub struct BackgroundNode {
    /// The kind of shape to use as a background.
    pub shape: BackgroundShape,
    /// The background fill.
    pub fill: Fill,
    /// The child node to be filled.
    pub child: Node,
}

/// The kind of shape to use as a background.
#[derive(Debug, Clone, PartialEq)]
pub enum BackgroundShape {
    Rect,
    Ellipse,
}

impl Layout for BackgroundNode {
    fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Fragment {
        let mut fragment = self.child.layout(ctx, areas);

        for frame in fragment.frames_mut() {
            let (point, shape) = match self.shape {
                BackgroundShape::Rect => (Point::ZERO, Shape::Rect(frame.size)),
                BackgroundShape::Ellipse => {
                    (frame.size.to_point() / 2.0, Shape::Ellipse(frame.size))
                }
            };

            let element = Element::Geometry(Geometry { shape, fill: self.fill });
            frame.elements.insert(0, (point, element));
        }

        fragment
    }
}

impl From<BackgroundNode> for AnyNode {
    fn from(background: BackgroundNode) -> Self {
        Self::new(background)
    }
}