summaryrefslogtreecommitdiffstats
path: root/tiny_skia/src/primitive.rs
blob: 22daaedc57286575b1b46b2c79cc91671bda8461 (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
use crate::{Rectangle, Vector};

use std::sync::Arc;

#[derive(Debug, Clone)]
pub enum Primitive {
    /// A group of primitives
    Group {
        /// The primitives of the group
        primitives: Vec<Primitive>,
    },
    /// A clip primitive
    Clip {
        /// The bounds of the clip
        bounds: Rectangle,
        /// The content of the clip
        content: Box<Primitive>,
    },
    /// A primitive that applies a translation
    Translate {
        /// The translation vector
        translation: Vector,

        /// The primitive to translate
        content: Box<Primitive>,
    },
    /// A cached primitive.
    ///
    /// This can be useful if you are implementing a widget where primitive
    /// generation is expensive.
    Cached {
        /// The cached primitive
        cache: Arc<Primitive>,
    },
    /// A basic primitive.
    Basic(iced_graphics::Primitive),
}

impl iced_graphics::backend::Primitive for Primitive {
    fn translate(self, translation: Vector) -> Self {
        Self::Translate {
            translation,
            content: Box::new(self),
        }
    }

    fn clip(self, bounds: Rectangle) -> Self {
        Self::Clip {
            bounds,
            content: Box::new(self),
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct Recording(pub(crate) Vec<Primitive>);

impl iced_graphics::backend::Recording for Recording {
    type Primitive = Primitive;

    fn push(&mut self, primitive: Primitive) {
        self.0.push(primitive);
    }

    fn push_basic(&mut self, basic: iced_graphics::Primitive) {
        self.0.push(Primitive::Basic(basic));
    }

    fn group(self) -> Self::Primitive {
        Primitive::Group { primitives: self.0 }
    }

    fn clear(&mut self) {
        self.0.clear();
    }
}

impl Recording {
    pub fn primitives(&self) -> &[Primitive] {
        &self.0
    }
}