use crate::core::{PaintCtx, PropertiesRef, PropertyCache};
use crate::imaging::Painter;
use crate::kurbo::{Affine, Join, Rect, Stroke};
use crate::properties::{Background, BorderColor, BorderWidth, BoxShadow, CornerRadius};
#[derive(Debug)]
pub struct PrePaintProps<'a> {
pub box_shadow: &'a BoxShadow,
pub background: &'a Background,
pub border_width: &'a BorderWidth,
pub border_color: &'a BorderColor,
pub corner_radius: &'a CornerRadius,
}
impl<'a> PrePaintProps<'a> {
pub fn fetch(props: &'a PropertiesRef<'_>, cache: &mut PropertyCache) -> Self {
let box_shadow = props.get::<BoxShadow>(cache);
let background = props.get::<Background>(cache);
let border_color = props.get::<BorderColor>(cache);
let border_width = props.get::<BorderWidth>(cache);
let corner_radius = props.get::<CornerRadius>(cache);
Self {
box_shadow,
background,
border_width,
border_color,
corner_radius,
}
}
}
pub fn pre_paint(ctx: &mut PaintCtx<'_>, props: &PropertiesRef<'_>, painter: &mut Painter<'_>) {
let bbox = ctx.border_box();
let cache = ctx.property_cache();
let p = PrePaintProps::fetch(props, cache);
paint_box_shadow(painter, bbox, p.box_shadow, p.corner_radius);
paint_background(painter, bbox, p.background, p.border_width, p.corner_radius);
paint_border(
painter,
bbox,
p.border_color,
p.border_width,
p.corner_radius,
);
}
pub fn paint_box_shadow(
painter: &mut Painter<'_>,
border_box: Rect,
box_shadow: &BoxShadow,
corner_radius: &CornerRadius,
) {
if !box_shadow.is_visible() {
return;
}
let box_shadow_rect = border_box.to_rounded_rect(corner_radius.radius.get());
box_shadow.paint(painter, Affine::IDENTITY, box_shadow_rect);
}
pub fn paint_background(
painter: &mut Painter<'_>,
border_box: Rect,
background: &Background,
border_width: &BorderWidth,
corner_radius: &CornerRadius,
) {
if !background.is_visible() {
return;
}
let bg_rect = border_width.bg_rect(border_box, corner_radius);
let bg_brush = background.get_peniko_brush_for_rect(bg_rect.rect());
painter.fill(bg_rect, &bg_brush).draw();
}
pub fn paint_border(
painter: &mut Painter<'_>,
border_box: Rect,
border_color: &BorderColor,
border_width: &BorderWidth,
corner_radius: &CornerRadius,
) {
let border_width_value = border_width.width.get();
if border_width_value == 0. || !border_color.is_visible() {
return;
}
let border_rect = border_width.border_rect(border_box, corner_radius);
let border_style = Stroke {
width: border_width_value,
join: Join::Miter,
..Default::default()
};
painter
.stroke(border_rect, &border_style, border_color.color)
.draw();
}