Skip to main content

rosace_widgets/tree/
divider.rs

1use rosace_core::types::Size;
2use rosace_render::Color;
3use super::{Widget, LayoutCtx, PaintCtx, avail_w, avail_h};
4
5/// A thin separator line — horizontal or vertical.
6pub struct Divider {
7    pub vertical: bool,
8    pub thickness: f32,
9    pub color: Color,
10    pub indent: f32,
11}
12
13impl Divider {
14    /// A horizontal divider — the common case (D093: `new()` must exist
15    /// wherever named constructors exist).
16    pub fn new() -> Self {
17        Self::horizontal()
18    }
19
20    pub fn horizontal() -> Self {
21        Self { vertical: false, thickness: 1.0, color: Color::rgba(0, 0, 0, 0), indent: 0.0 }
22    }
23    pub fn vertical() -> Self {
24        Self { vertical: true, thickness: 1.0, color: Color::rgba(0, 0, 0, 0), indent: 0.0 }
25    }
26    pub fn color(mut self, c: Color) -> Self { self.color = c; self }
27    pub fn thickness(mut self, t: f32) -> Self { self.thickness = t; self }
28    pub fn indent(mut self, i: f32) -> Self { self.indent = i; self }
29}
30
31impl Default for Divider {
32    fn default() -> Self {
33        Self::new()
34    }
35}
36
37impl Widget for Divider {
38    fn layout(&self, ctx: &LayoutCtx) -> Size {
39        let constraints = ctx.constraints;
40        if self.vertical {
41            Size { width: self.thickness, height: avail_h(constraints) }
42        } else {
43            Size { width: avail_w(constraints), height: self.thickness }
44        }
45    }
46
47    fn paint(&self, ctx: &mut PaintCtx) {
48        use rosace_core::types::{Point, Rect};
49        let color = if self.color.a == 0 { ctx.tc(ctx.theme.colors.outline) } else { self.color };
50        let r = ctx.rect;
51        let rect = if self.vertical {
52            Rect { origin: Point { x: r.origin.x, y: r.origin.y + self.indent }, size: Size { width: self.thickness, height: (r.size.height - self.indent).max(0.0) } }
53        } else {
54            Rect { origin: Point { x: r.origin.x + self.indent, y: r.origin.y }, size: Size { width: (r.size.width - self.indent).max(0.0), height: self.thickness } }
55        };
56        ctx.fill_rect(rect, color);
57    }
58}