summaryrefslogtreecommitdiff
path: root/src/layout/node.rs
blob: c945ee19a114c0ecc7063f04eb4306c816f1c95d (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
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
use std::any::Any;
use std::fmt::{self, Debug, Formatter};

use super::*;

/// A self-contained layout node.
#[derive(Clone, PartialEq)]
pub enum Node {
    /// A text node.
    Text(NodeText),
    /// A spacing node.
    Spacing(NodeSpacing),
    /// A dynamic node that can implement custom layouting behaviour.
    Any(NodeAny),
}

impl Node {
    /// Create a new dynamic node.
    pub fn any<T>(any: T) -> Self
    where
        T: Layout + Debug + Clone + PartialEq + 'static,
    {
        Self::Any(NodeAny::new(any))
    }
}

impl Layout for Node {
    fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
        match self {
            Self::Spacing(spacing) => spacing.layout(ctx, areas),
            Self::Text(text) => text.layout(ctx, areas),
            Self::Any(any) => any.layout(ctx, areas),
        }
    }
}

impl Debug for Node {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        match self {
            Self::Spacing(spacing) => spacing.fmt(f),
            Self::Text(text) => text.fmt(f),
            Self::Any(any) => any.fmt(f),
        }
    }
}

/// A wrapper around a dynamic layouting node.
pub struct NodeAny(Box<dyn Bounds>);

impl NodeAny {
    /// Create a new instance from any node that satisifies the required bounds.
    pub fn new<T>(any: T) -> Self
    where
        T: Layout + Debug + Clone + PartialEq + 'static,
    {
        Self(Box::new(any))
    }
}

impl Layout for NodeAny {
    fn layout(&self, ctx: &mut LayoutContext, areas: &Areas) -> Layouted {
        self.0.layout(ctx, areas)
    }
}

impl Clone for NodeAny {
    fn clone(&self) -> Self {
        Self(self.0.dyn_clone())
    }
}

impl PartialEq for NodeAny {
    fn eq(&self, other: &Self) -> bool {
        self.0.dyn_eq(other.0.as_ref())
    }
}

impl Debug for NodeAny {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        self.0.fmt(f)
    }
}

impl From<NodeAny> for Node {
    fn from(dynamic: NodeAny) -> Self {
        Self::Any(dynamic)
    }
}

trait Bounds: Layout + Debug + 'static {
    fn as_any(&self) -> &dyn Any;
    fn dyn_eq(&self, other: &dyn Bounds) -> bool;
    fn dyn_clone(&self) -> Box<dyn Bounds>;
}

impl<T> Bounds for T
where
    T: Layout + Debug + PartialEq + Clone + 'static,
{
    fn as_any(&self) -> &dyn Any {
        self
    }

    fn dyn_eq(&self, other: &dyn Bounds) -> bool {
        if let Some(other) = other.as_any().downcast_ref::<Self>() {
            self == other
        } else {
            false
        }
    }

    fn dyn_clone(&self) -> Box<dyn Bounds> {
        Box::new(self.clone())
    }
}