use cranpose_ui_graphics::{
BlendMode, CornerRadii, DrawScope, Rect, RoundedCornerShape, ShapeRecorder, Size,
};
use crate::modifier::{Brush, Color, CoordinatorRect};
#[derive(Clone)]
pub struct BackgroundDraw {
color: Color,
shape: Option<RoundedCornerShape>,
coordinator: CoordinatorRect,
}
impl BackgroundDraw {
pub(crate) fn new(
color: Color,
shape: Option<RoundedCornerShape>,
coordinator: CoordinatorRect,
) -> Self {
Self {
color,
shape,
coordinator,
}
}
pub fn color(&self) -> Color {
self.color
}
pub fn shape_in(&self, size: Size) -> (Rect, Option<CornerRadii>) {
let rect = self.coordinator.rect(size);
let radii = self
.shape
.map(|shape| shape.resolve(rect.width, rect.height));
(rect, radii)
}
pub fn draw(&self, scope: &mut dyn DrawScope) {
self.fill(scope, self.color);
}
pub(crate) fn fill(&self, scope: &mut dyn DrawScope, color: Color) {
let brush = Brush::solid(color);
match self.shape_in(scope.size()) {
(rect, Some(radii)) => scope.draw_round_rect_at(rect, brush, radii),
(rect, None) => scope.draw_rect_at(rect, brush),
}
}
pub fn record(&self, shape: (Rect, Option<CornerRadii>), recorder: &mut ShapeRecorder) {
let brush = Brush::solid(self.color);
match shape {
(rect, Some(radii)) => {
recorder.push_round_rect(rect, &brush, radii, None, BlendMode::SrcOver);
}
(rect, None) => {
recorder.push_rect(rect, &brush, None, BlendMode::SrcOver);
}
}
}
pub(crate) fn same_as(&self, other: &Self) -> bool {
self.color == other.color
&& self.shape == other.shape
&& self.coordinator.same_as(&other.coordinator)
}
}