Skip to main content

bhoo/systems/material/
divider.rs

1use crate::*;
2
3impl Frame<'_> {
4    pub fn divider(&mut self) {
5        let b = self.allocate(1.0);
6        self.divider_at(b, DividerOrientation::Horizontal, 0.0, 0.0);
7    }
8    /// Draw a 1dp decorative divider centered on its short axis. Insets are
9    /// physical distances along the long axis; callers handle RTL placement.
10    pub fn divider_at(
11        &mut self,
12        bounds: Rect,
13        orientation: DividerOrientation,
14        inset_start: f32,
15        inset_end: f32,
16    ) {
17        self.divider_styled_at(bounds, DividerOptions {orientation,inset_start,inset_end,..DividerOptions::default()});
18    }
19    /// Draw a divider with logical horizontal insets and an explicit thickness.
20    /// Vertical insets run from top to bottom regardless of layout direction.
21    pub fn divider_styled_at(&mut self, bounds: Rect, options: DividerOptions) {
22        let DividerOptions {orientation,inset_start,inset_end,thickness,rtl,color}=options;
23        if !bounds.x.is_finite()
24            || !bounds.y.is_finite()
25            || !bounds.width.is_finite()
26            || !bounds.height.is_finite()
27            || bounds.width <= 0.0
28            || bounds.height <= 0.0
29            || !thickness.is_finite()
30            || thickness <= 0.0
31        {
32            return;
33        }
34        let long = if orientation == DividerOrientation::Horizontal {
35            bounds.width
36        } else {
37            bounds.height
38        };
39        let (inset_start,inset_end)=if rtl && orientation==DividerOrientation::Horizontal {(inset_end,inset_start)} else {(inset_start,inset_end)};
40        let start = if inset_start.is_finite() {
41            inset_start.clamp(0.0, long)
42        } else {
43            0.0
44        };
45        let end = if inset_end.is_finite() {
46            inset_end.clamp(0.0, long)
47        } else {
48            0.0
49        };
50        let length = (long - start - end).max(0.0);
51        if length == 0.0 {
52            return;
53        }
54        let line = match orientation {
55            DividerOrientation::Horizontal => Rect {
56                x: bounds.x + start,
57                y: bounds.y + (bounds.height - bounds.height.min(thickness)) / 2.0,
58                width: length,
59                height: bounds.height.min(thickness),
60            },
61            DividerOrientation::Vertical => Rect {
62                x: bounds.x + (bounds.width - bounds.width.min(thickness)) / 2.0,
63                y: bounds.y + start,
64                width: bounds.width.min(thickness),
65                height: length,
66            },
67        };
68        self.rect(line, 0.0, color.unwrap_or(self.ui.theme.outline_variant));
69    }
70}
71
72#[cfg(test)]
73#[path = "divider.spec.rs"]
74mod tests;