Skip to main content

densa_widgets/
panel.rs

1use densa_core::Color;
2use densa_draw::Canvas;
3use densa_ui::{PaintCx, Widget};
4
5use crate::paint::outline;
6
7/// A container that paints a background (the theme's surface color by default) and,
8/// optionally, a border. Lay out its children with its style.
9#[derive(Debug, Default)]
10pub struct Panel {
11    color: Option<Color>,
12    transparent: bool,
13    border: bool,
14}
15
16impl Panel {
17    /// A panel in the theme's surface color.
18    pub fn new() -> Self {
19        Self::default()
20    }
21
22    /// A panel that paints nothing: only groups and lays out its children.
23    pub fn transparent() -> Self {
24        Self {
25            transparent: true,
26            ..Self::default()
27        }
28    }
29
30    /// The panel in `color` instead of the theme's.
31    #[must_use]
32    pub fn with_color(mut self, color: Color) -> Self {
33        self.color = Some(color);
34        self.transparent = false;
35        self
36    }
37
38    /// The panel with a border in the theme's border color.
39    #[must_use]
40    pub fn with_border(mut self) -> Self {
41        self.border = true;
42        self
43    }
44}
45
46impl Widget for Panel {
47    fn paint(&mut self, cx: &PaintCx<'_>, canvas: &mut Canvas<'_>) {
48        if !self.transparent {
49            let color = self.color.unwrap_or(cx.theme().surface);
50            canvas.fill_rect(cx.rect(), color);
51        }
52        if self.border {
53            outline(canvas, cx.rect(), cx.px(1.0).round(), cx.theme().border);
54        }
55    }
56}