Skip to main content

windows_composition/
brush.rs

1use super::*;
2
3/// The base type shared by every composition brush. A [`Brush`] can be turned
4/// into one via [`Brush::as_brush`] to paint a visual or fill a shape.
5#[derive(Clone)]
6pub struct CompositionBrush(pub(crate) bindings::CompositionBrush);
7
8/// A brush that can paint a [`SpriteVisual`](crate::SpriteVisual), fill a
9/// [`CompositionSpriteShape`](crate::CompositionSpriteShape), or serve as the
10/// source of a [`CompositionNineGridBrush`].
11///
12/// This trait is sealed: only the brush types in this crate implement it.
13pub trait Brush: Sealed {
14    /// Returns this brush as the shared [`CompositionBrush`] base type.
15    fn as_brush(&self) -> CompositionBrush;
16}
17
18/// A brush that paints with a single solid color.
19#[derive(Clone)]
20pub struct CompositionColorBrush(pub(crate) bindings::CompositionColorBrush);
21
22impl CompositionColorBrush {
23    /// Sets the brush's color.
24    pub fn set_color(&self, color: Color) {
25        self.0.SetColor(color.0).unwrap();
26    }
27
28    /// Returns the brush's color.
29    pub fn color(&self) -> Color {
30        Color(self.0.Color().unwrap())
31    }
32}
33
34impl Sealed for CompositionColorBrush {}
35
36impl Brush for CompositionColorBrush {
37    fn as_brush(&self) -> CompositionBrush {
38        CompositionBrush(self.0.cast().unwrap())
39    }
40}
41
42/// A brush that stretches its source as a nine-grid: the four corners keep their
43/// size, the edges stretch along one axis, and the center stretches (or is left
44/// [hollow](Self::set_center_hollow)) to fill the remaining space.
45#[derive(Clone)]
46pub struct CompositionNineGridBrush(pub(crate) bindings::CompositionNineGridBrush);
47
48impl CompositionNineGridBrush {
49    /// Sets the left, top, right, and bottom inset widths, in DIPs.
50    pub fn set_insets(&self, left: f32, top: f32, right: f32, bottom: f32) {
51        self.0
52            .SetInsetsWithValues(left, top, right, bottom)
53            .unwrap();
54    }
55
56    /// Sets whether the center of the grid is left unpainted (hollow).
57    pub fn set_center_hollow(&self, hollow: bool) {
58        self.0.SetIsCenterHollow(hollow).unwrap();
59    }
60
61    /// Sets the brush stretched across the nine-grid.
62    pub fn set_source(&self, brush: &impl Brush) {
63        self.0.SetSource(&brush.as_brush().0).unwrap();
64    }
65}
66
67impl Sealed for CompositionNineGridBrush {}
68
69impl Brush for CompositionNineGridBrush {
70    fn as_brush(&self) -> CompositionBrush {
71        CompositionBrush(self.0.cast().unwrap())
72    }
73}