CAShapeLayerBuilder

Struct CAShapeLayerBuilder 

Source
pub struct CAShapeLayerBuilder { /* private fields */ }
Expand description

Builder for CAShapeLayer.

§Basic Usage

let shape = CAShapeLayerBuilder::new()
    .path(circle_path)
    .fill_color(Color::RED)
    .stroke_color(Color::WHITE)
    .line_width(2.0)
    .build();

§With Animations

Animations can be added inline using the .animate() method:

let shape = CAShapeLayerBuilder::new()
    .path(circle_path)
    .fill_color(Color::RED)
    .animate("pulse", KeyPath::TransformScale, |a| {
        a.values(0.85, 1.15)
            .duration(800.millis())
            .easing(Easing::InOut)
            .autoreverses()
            .repeat(Repeat::Forever)
    })
    .build();

Multiple animations can be added:

CAShapeLayerBuilder::new()
    .fill_color(Color::RED)
    .animate("pulse", KeyPath::TransformScale, |a| {
        a.values(0.9, 1.1).duration(500.millis()).repeat(Repeat::Forever)
    })
    .animate("fade", KeyPath::Opacity, |a| {
        a.values(1.0, 0.7).duration(1.seconds()).repeat(Repeat::Forever)
    })
    .build()

Implementations§

Source§

impl CAShapeLayerBuilder

Source

pub fn new() -> Self

Creates a new builder with default values.

Source

pub fn bounds(self, bounds: CGRect) -> Self

Sets the bounds rectangle.

Source

pub fn position(self, position: CGPoint) -> Self

Sets the position in superlayer coordinates.

Source

pub fn path(self, path: CFRetained<CGPath>) -> Self

Sets the path to render.

Source

pub fn circle(self, diameter: CGFloat) -> Self

Creates a perfect circle path and sets appropriate bounds.

This is a convenience method that wraps CGPath::with_ellipse_in_rect and automatically sets the layer’s bounds to match the circle size.

§Arguments
  • diameter - The diameter of the circle
§Examples
// Create a circle with 80pt diameter
let circle = CAShapeLayerBuilder::new()
    .circle(80.0)
    .position(CGPoint::new(100.0, 100.0))
    .fill_color(Color::CYAN)
    .build();
§Notes

This method sets both the path and bounds. If you call .bounds() or .path() after .circle(), those values will override what was set by .circle().

§See Also

Use ellipse for non-circular ellipses.

Source

pub fn ellipse(self, width: CGFloat, height: CGFloat) -> Self

Creates an ellipse path and sets appropriate bounds.

This is a convenience method that wraps CGPath::with_ellipse_in_rect and automatically sets the layer’s bounds to match the ellipse size.

§Arguments
  • width - The width of the ellipse bounding box
  • height - The height of the ellipse bounding box
§Examples
// Create an ellipse (oval shape)
let ellipse = CAShapeLayerBuilder::new()
    .ellipse(100.0, 60.0)  // wider than tall
    .position(CGPoint::new(100.0, 100.0))
    .fill_color(Color::RED)
    .build();
§Notes

This method sets both the path and bounds. If you call .bounds() or .path() after .ellipse(), those values will override what was set by .ellipse().

§See Also

Use circle for perfect circles (same width and height).

Source

pub fn fill_color(self, color: impl Into<CFRetained<CGColor>>) -> Self

Sets the fill color.

Accepts any type that implements Into<CFRetained<CGColor>>, including:

  • Color::RED, Color::rgb(1.0, 0.0, 0.0)
  • CFRetained<CGColor> directly
Source

pub fn fill_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self

Sets the fill color from RGBA values (0.0–1.0).

Source

pub fn stroke_color(self, color: impl Into<CFRetained<CGColor>>) -> Self

Sets the stroke color.

Accepts any type that implements Into<CFRetained<CGColor>>, including:

  • Color::WHITE, Color::rgb(1.0, 1.0, 1.0)
  • CFRetained<CGColor> directly
Source

pub fn stroke_rgba(self, r: CGFloat, g: CGFloat, b: CGFloat, a: CGFloat) -> Self

Sets the stroke color from RGBA values (0.0–1.0).

Source

pub fn line_width(self, width: CGFloat) -> Self

Sets the stroke line width.

Source

pub fn transform(self, transform: CATransform3D) -> Self

Sets the 3D transform.

Source

pub fn hidden(self, hidden: bool) -> Self

Sets whether the layer is hidden.

Source

pub fn opacity(self, opacity: f32) -> Self

Sets the opacity (0.0-1.0).

Source

pub fn shadow_color(self, color: impl Into<CFRetained<CGColor>>) -> Self

Sets the shadow color.

§Examples
CAShapeLayerBuilder::new()
    .shadow_color(Color::BLACK)
    .shadow_radius(10.0)
    .shadow_opacity(0.5)
    .build();
Source

pub fn shadow_offset(self, dx: f64, dy: f64) -> Self

Sets the shadow offset (dx, dy).

Positive dx moves the shadow right, positive dy moves it down.

§Examples
CAShapeLayerBuilder::new()
    .shadow_offset(0.0, 4.0)  // Shadow below
    .shadow_radius(8.0)
    .shadow_opacity(0.3)
    .build();
Source

pub fn shadow_radius(self, radius: f64) -> Self

Sets the shadow blur radius.

Larger values create a softer, more diffuse shadow.

§Examples
CAShapeLayerBuilder::new()
    .shadow_radius(15.0)  // Soft glow effect
    .shadow_opacity(0.7)
    .build();
Source

pub fn shadow_opacity(self, opacity: f32) -> Self

Sets the shadow opacity (0.0 to 1.0).

§Examples
CAShapeLayerBuilder::new()
    .shadow_color(Color::CYAN)
    .shadow_radius(10.0)
    .shadow_opacity(0.8)  // Bright glow
    .build();
Source

pub fn scale(self, scale: f64) -> Self

Sets a uniform scale transform.

This is applied using CATransform3D internally.

§Examples
// Scale to 80% of original size
CAShapeLayerBuilder::new()
    .scale(0.8)
    .build();
§Notes

When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.

If you also call .transform(), the explicit transform takes precedence and scale/rotation/translate are ignored.

Source

pub fn rotation(self, radians: f64) -> Self

Sets a z-axis rotation transform (in radians).

This is applied using CATransform3D internally. For degrees, use: .rotation(45.0_f64.to_radians())

§Examples
use std::f64::consts::PI;

// Rotate 45 degrees
CAShapeLayerBuilder::new()
    .rotation(PI / 4.0)
    .build();

// Or using to_radians()
CAShapeLayerBuilder::new()
    .rotation(45.0_f64.to_radians())
    .build();
§Notes

When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.

If you also call .transform(), the explicit transform takes precedence and scale/rotation/translate are ignored.

Source

pub fn translate(self, dx: f64, dy: f64) -> Self

Sets a translation transform (dx, dy).

This is applied using CATransform3D internally.

§Examples
// Move 10 points right and 20 points up
CAShapeLayerBuilder::new()
    .translate(10.0, 20.0)
    .build();
§Notes

When multiple transform shortcuts are set, they are composed in order: scale → rotation → translation.

If you also call .transform(), the explicit transform takes precedence and scale/rotation/translate are ignored.

Source

pub fn animate<F>( self, name: impl Into<String>, key_path: KeyPath, configure: F, ) -> Self

Adds an animation to be applied when the layer is built.

The animation is configured using a closure that receives a CABasicAnimationBuilder and returns the configured builder.

§Arguments
  • name - A unique identifier for this animation (used as the animation key)
  • key_path - The property to animate (e.g., KeyPath::TransformScale)
  • configure - A closure that configures the animation builder
§Examples
// Simple pulse animation
CAShapeLayerBuilder::new()
    .path(circle_path)
    .fill_color(Color::RED)
    .animate("pulse", KeyPath::TransformScale, |a| {
        a.values(0.85, 1.15)
            .duration(800.millis())
            .easing(Easing::InOut)
            .autoreverses()
            .repeat(Repeat::Forever)
    })
    .build();

// Multiple animations on the same layer
CAShapeLayerBuilder::new()
    .fill_color(Color::BLUE)
    .animate("scale", KeyPath::TransformScale, |a| {
        a.values(0.9, 1.1).duration(500.millis()).repeat(Repeat::Forever)
    })
    .animate("fade", KeyPath::Opacity, |a| {
        a.values(1.0, 0.5).duration(1.seconds()).repeat(Repeat::Forever)
    })
    .build();
Source

pub fn build(self) -> Retained<CAShapeLayer>

Builds and returns the configured CAShapeLayer.

All pending animations added via .animate() are applied to the layer.

Trait Implementations§

Source§

impl Default for CAShapeLayerBuilder

Source§

fn default() -> CAShapeLayerBuilder

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> AutoreleaseSafe for T
where T: ?Sized,