summaryrefslogtreecommitdiff
path: root/src/library/layout/spacing.rs
blob: 3468af5ec82ddd2a68250b0b6e2ecaf3d1e2f95c (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
46
47
48
49
50
51
52
use crate::library::prelude::*;

/// Horizontal spacing.
pub struct HNode;

#[node]
impl HNode {
    fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> {
        Ok(Content::Horizontal(args.expect("spacing")?))
    }
}

/// Vertical spacing.
pub struct VNode;

#[node]
impl VNode {
    fn construct(_: &mut Context, args: &mut Args) -> TypResult<Content> {
        Ok(Content::Vertical(args.expect("spacing")?))
    }
}

/// Kinds of spacing.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Spacing {
    /// Spacing specified in absolute terms and relative to the parent's size.
    Relative(Relative<RawLength>),
    /// Spacing specified as a fraction of the remaining free space in the parent.
    Fractional(Fraction),
}

impl Spacing {
    /// Whether this is fractional spacing.
    pub fn is_fractional(self) -> bool {
        matches!(self, Self::Fractional(_))
    }
}

impl From<Length> for Spacing {
    fn from(length: Length) -> Self {
        Self::Relative(length.into())
    }
}

castable! {
    Spacing,
    Expected: "relative length or fraction",
    Value::Length(v) => Self::Relative(v.into()),
    Value::Ratio(v) => Self::Relative(v.into()),
    Value::Relative(v) => Self::Relative(v),
    Value::Fraction(v) => Self::Fractional(v),
}