@@ -12,8 +12,7 @@ use std::sync::Arc;
1212
1313/// A rendering primitive.
1414#[ derive( Debug , Clone , PartialEq ) ]
15- #[ non_exhaustive]
16- pub enum Primitive {
15+ pub enum Primitive < T > {
1716 /// A text primitive
1817 Text {
1918 /// The contents of the text
@@ -90,61 +89,39 @@ pub enum Primitive {
9089 /// Any geometry that falls out of this region will be clipped.
9190 size : Size ,
9291 } ,
93- /// A [`tiny_skia`] path filled with some paint.
94- #[ cfg( feature = "tiny-skia" ) ]
95- Fill {
96- /// The path to fill.
97- path : tiny_skia:: Path ,
98- /// The paint to use.
99- paint : tiny_skia:: Paint < ' static > ,
100- /// The fill rule to follow.
101- rule : tiny_skia:: FillRule ,
102- /// The transform to apply to the path.
103- transform : tiny_skia:: Transform ,
104- } ,
105- /// A [`tiny_skia`] path stroked with some paint.
106- #[ cfg( feature = "tiny-skia" ) ]
107- Stroke {
108- /// The path to stroke.
109- path : tiny_skia:: Path ,
110- /// The paint to use.
111- paint : tiny_skia:: Paint < ' static > ,
112- /// The stroke settings.
113- stroke : tiny_skia:: Stroke ,
114- /// The transform to apply to the path.
115- transform : tiny_skia:: Transform ,
116- } ,
11792 /// A group of primitives
11893 Group {
11994 /// The primitives of the group
120- primitives : Vec < Primitive > ,
95+ primitives : Vec < Primitive < T > > ,
12196 } ,
12297 /// A clip primitive
12398 Clip {
12499 /// The bounds of the clip
125100 bounds : Rectangle ,
126101 /// The content of the clip
127- content : Box < Primitive > ,
102+ content : Box < Primitive < T > > ,
128103 } ,
129104 /// A primitive that applies a translation
130105 Translate {
131106 /// The translation vector
132107 translation : Vector ,
133108
134109 /// The primitive to translate
135- content : Box < Primitive > ,
110+ content : Box < Primitive < T > > ,
136111 } ,
137112 /// A cached primitive.
138113 ///
139114 /// This can be useful if you are implementing a widget where primitive
140115 /// generation is expensive.
141116 Cache {
142117 /// The cached primitive
143- content : Arc < Primitive > ,
118+ content : Arc < Primitive < T > > ,
144119 } ,
120+ /// A backend-specific primitive.
121+ Custom ( T ) ,
145122}
146123
147- impl Primitive {
124+ impl < T > Primitive < T > {
148125 /// Creates a [`Primitive::Group`].
149126 pub fn group ( primitives : Vec < Self > ) -> Self {
150127 Self :: Group { primitives }
@@ -165,68 +142,6 @@ impl Primitive {
165142 content : Box :: new ( self ) ,
166143 }
167144 }
168-
169- /// Returns the bounds of the [`Primitive`].
170- pub fn bounds ( & self ) -> Rectangle {
171- match self {
172- Self :: Text {
173- bounds,
174- horizontal_alignment,
175- vertical_alignment,
176- ..
177- } => {
178- let mut bounds = * bounds;
179-
180- bounds. x = match horizontal_alignment {
181- alignment:: Horizontal :: Left => bounds. x ,
182- alignment:: Horizontal :: Center => {
183- bounds. x - bounds. width / 2.0
184- }
185- alignment:: Horizontal :: Right => bounds. x - bounds. width ,
186- } ;
187-
188- bounds. y = match vertical_alignment {
189- alignment:: Vertical :: Top => bounds. y ,
190- alignment:: Vertical :: Center => {
191- bounds. y - bounds. height / 2.0
192- }
193- alignment:: Vertical :: Bottom => bounds. y - bounds. height ,
194- } ;
195-
196- bounds. expand ( 1.5 )
197- }
198- Self :: Quad { bounds, .. }
199- | Self :: Image { bounds, .. }
200- | Self :: Svg { bounds, .. } => bounds. expand ( 1.0 ) ,
201- Self :: Clip { bounds, .. } => bounds. expand ( 1.0 ) ,
202- Self :: SolidMesh { size, .. } | Self :: GradientMesh { size, .. } => {
203- Rectangle :: with_size ( * size)
204- }
205- #[ cfg( feature = "tiny-skia" ) ]
206- Self :: Fill { path, .. } | Self :: Stroke { path, .. } => {
207- let bounds = path. bounds ( ) ;
208-
209- Rectangle {
210- x : bounds. x ( ) ,
211- y : bounds. y ( ) ,
212- width : bounds. width ( ) ,
213- height : bounds. height ( ) ,
214- }
215- . expand ( 1.0 )
216- }
217- Self :: Group { primitives } => primitives
218- . iter ( )
219- . map ( Self :: bounds)
220- . fold ( Rectangle :: with_size ( Size :: ZERO ) , |a, b| {
221- Rectangle :: union ( & a, & b)
222- } ) ,
223- Self :: Translate {
224- translation,
225- content,
226- } => content. bounds ( ) + * translation,
227- Self :: Cache { content } => content. bounds ( ) ,
228- }
229- }
230145}
231146
232147/// A set of [`Vertex2D`] and indices representing a list of triangles.
@@ -268,9 +183,3 @@ unsafe impl Zeroable for GradientVertex2D {}
268183
269184#[ allow( unsafe_code) ]
270185unsafe impl Pod for GradientVertex2D { }
271-
272- impl From < ( ) > for Primitive {
273- fn from ( _: ( ) ) -> Self {
274- Self :: Group { primitives : vec ! [ ] }
275- }
276- }
0 commit comments