core_animation/
layer_builder.rs1use crate::color::Color;
4use objc2::rc::Retained;
5use objc2_core_foundation::{CFRetained, CGFloat, CGPoint, CGRect};
6use objc2_core_graphics::CGColor;
7use objc2_quartz_core::{CALayer, CATransform3D};
8
9#[derive(Default)]
20pub struct CALayerBuilder {
21 bounds: Option<CGRect>,
22 position: Option<CGPoint>,
23 background_color: Option<CFRetained<CGColor>>,
24 corner_radius: Option<CGFloat>,
25 hidden: Option<bool>,
26 transform: Option<CATransform3D>,
27 opacity: Option<f32>,
28}
29
30impl CALayerBuilder {
31 pub fn new() -> Self {
33 Self::default()
34 }
35
36 pub fn bounds(mut self, bounds: CGRect) -> Self {
38 self.bounds = Some(bounds);
39 self
40 }
41
42 pub fn position(mut self, position: CGPoint) -> Self {
44 self.position = Some(position);
45 self
46 }
47
48 pub fn background_color(mut self, color: impl Into<CFRetained<CGColor>>) -> Self {
62 self.background_color = Some(color.into());
63 self
64 }
65
66 pub fn background_rgba(mut self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self {
68 self.background_color = Some(Color::rgba(r, g, b, a).into());
69 self
70 }
71
72 pub fn corner_radius(mut self, radius: CGFloat) -> Self {
74 self.corner_radius = Some(radius);
75 self
76 }
77
78 pub fn hidden(mut self, hidden: bool) -> Self {
80 self.hidden = Some(hidden);
81 self
82 }
83
84 pub fn transform(mut self, transform: CATransform3D) -> Self {
86 self.transform = Some(transform);
87 self
88 }
89
90 pub fn opacity(mut self, opacity: f32) -> Self {
92 self.opacity = Some(opacity);
93 self
94 }
95
96 pub fn build(self) -> Retained<CALayer> {
98 let layer = CALayer::new();
99
100 if let Some(bounds) = self.bounds {
101 layer.setBounds(bounds);
102 }
103 if let Some(position) = self.position {
104 layer.setPosition(position);
105 }
106 if let Some(ref color) = self.background_color {
107 layer.setBackgroundColor(Some(&**color));
108 }
109 if let Some(radius) = self.corner_radius {
110 layer.setCornerRadius(radius);
111 }
112 if let Some(hidden) = self.hidden {
113 layer.setHidden(hidden);
114 }
115 if let Some(transform) = self.transform {
116 layer.setTransform(transform);
117 }
118 if let Some(opacity) = self.opacity {
119 layer.setOpacity(opacity);
120 }
121
122 layer
123 }
124}