cranpose_ui/
background_draw.rs1use cranpose_ui_graphics::{
4 BlendMode, CornerRadii, DrawScope, Rect, RoundedCornerShape, ShapeRecorder, Size,
5};
6
7use crate::modifier::{Brush, Color, CoordinatorRect};
8
9#[derive(Clone)]
13pub struct BackgroundDraw {
14 color: Color,
15 shape: Option<RoundedCornerShape>,
16 coordinator: CoordinatorRect,
17}
18
19impl BackgroundDraw {
20 pub(crate) fn new(
21 color: Color,
22 shape: Option<RoundedCornerShape>,
23 coordinator: CoordinatorRect,
24 ) -> Self {
25 Self {
26 color,
27 shape,
28 coordinator,
29 }
30 }
31
32 pub fn color(&self) -> Color {
34 self.color
35 }
36
37 pub fn shape_in(&self, size: Size) -> (Rect, Option<CornerRadii>) {
40 let rect = self.coordinator.rect(size);
41 let radii = self
42 .shape
43 .map(|shape| shape.resolve(rect.width, rect.height));
44 (rect, radii)
45 }
46
47 pub fn draw(&self, scope: &mut dyn DrawScope) {
49 self.fill(scope, self.color);
50 }
51
52 pub(crate) fn fill(&self, scope: &mut dyn DrawScope, color: Color) {
54 let brush = Brush::solid(color);
55 match self.shape_in(scope.size()) {
56 (rect, Some(radii)) => scope.draw_round_rect_at(rect, brush, radii),
57 (rect, None) => scope.draw_rect_at(rect, brush),
58 }
59 }
60
61 pub fn record(&self, shape: (Rect, Option<CornerRadii>), recorder: &mut ShapeRecorder) {
65 let brush = Brush::solid(self.color);
66 match shape {
67 (rect, Some(radii)) => {
68 recorder.push_round_rect(rect, &brush, radii, None, BlendMode::SrcOver);
69 }
70 (rect, None) => {
71 recorder.push_rect(rect, &brush, None, BlendMode::SrcOver);
72 }
73 }
74 }
75
76 pub(crate) fn same_as(&self, other: &Self) -> bool {
78 self.color == other.color
79 && self.shape == other.shape
80 && self.coordinator.same_as(&other.coordinator)
81 }
82}