cranpose-ui-graphics 0.30.0

Geometry, units, paths and render data for Cranpose UI
Documentation
//! Whether a blur clips its output to a shape, and how it reads past the
//! edge of its source.

use crate::{LayerShape, tile_mode::TileMode};

/// Controls blur behavior outside source bounds.
///
/// This mirrors Compose's `BlurredEdgeTreatment`:
/// - bounded treatment (`shape != None`) clips blur output and uses `TileMode::Clamp`
/// - unbounded treatment (`shape == None`) does not clip and uses `TileMode::Decal`
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct BlurredEdgeTreatment {
    shape: Option<LayerShape>,
}

impl BlurredEdgeTreatment {
    /// Bounded treatment that clips to a rectangle.
    pub const RECTANGLE: Self = Self {
        shape: Some(LayerShape::Rectangle),
    };

    /// Unbounded treatment that does not clip blurred output.
    pub const UNBOUNDED: Self = Self { shape: None };

    /// Bounded treatment with a specific clip shape.
    pub const fn with_shape(shape: LayerShape) -> Self {
        Self { shape: Some(shape) }
    }

    /// The shape the blurred output is clipped to, or `None` for the unbounded treatment.
    pub fn shape(self) -> Option<LayerShape> {
        self.shape
    }

    /// Whether the blurred output is clipped, which is the case when a clip shape is set.
    pub fn clip(self) -> bool {
        self.shape.is_some()
    }

    /// The tile mode blur uses at the edge: `Clamp` when bounded and `Decal` when unbounded.
    pub fn tile_mode(self) -> TileMode {
        if self.clip() {
            TileMode::Clamp
        } else {
            TileMode::Decal
        }
    }
}

impl Default for BlurredEdgeTreatment {
    fn default() -> Self {
        Self::RECTANGLE
    }
}