use crate::core::{Property, UsesProperty, Widget};
use crate::kurbo::Rect;
use crate::peniko::color::{AlphaColor, Srgb};
use crate::properties::types::Gradient;
impl<W: Widget> UsesProperty<Background> for W {}
#[expect(missing_docs, reason = "field names are self-descriptive")]
#[derive(Clone, Debug, PartialEq)]
pub enum Background {
Color(AlphaColor<Srgb>),
Gradient(Gradient),
}
impl Property for Background {
fn static_default() -> &'static Self {
static DEFAULT: Background = Background::Color(AlphaColor::TRANSPARENT);
&DEFAULT
}
}
impl Default for Background {
fn default() -> Self {
Self::static_default().clone()
}
}
impl Background {
pub fn get_peniko_brush_for_rect(&self, rect: Rect) -> peniko::Brush {
match self {
Self::Color(color) => (*color).into(),
Self::Gradient(gradient) => gradient.get_peniko_gradient_for_rect(rect).into(),
}
}
pub const fn is_visible(&self) -> bool {
match self {
Self::Color(color) => {
let alpha = color.components[3];
alpha != 0.0
}
Self::Gradient(_) => true,
}
}
}
impl From<AlphaColor<Srgb>> for Background {
fn from(color: AlphaColor<Srgb>) -> Self {
Self::Color(color)
}
}
impl From<Gradient> for Background {
fn from(gradient: Gradient) -> Self {
Self::Gradient(gradient)
}
}