Skip to main content

rosace_widgets/tree/
padding.rs

1use rosace_core::types::{Point, Rect, Size};
2
3/// Inset amounts on each edge (logical pixels).
4#[derive(Debug, Clone, Copy, Default)]
5pub struct EdgeInsets {
6    pub top: f32,
7    pub right: f32,
8    pub bottom: f32,
9    pub left: f32,
10}
11
12impl EdgeInsets {
13    pub fn all(v: f32) -> Self { Self { top: v, right: v, bottom: v, left: v } }
14    pub fn symmetric(horizontal: f32, vertical: f32) -> Self {
15        Self { top: vertical, bottom: vertical, left: horizontal, right: horizontal }
16    }
17    pub fn horizontal(h: f32) -> Self { Self { left: h, right: h, ..Default::default() } }
18    pub fn vertical(v: f32) -> Self { Self { top: v, bottom: v, ..Default::default() } }
19    pub fn only(top: f32, right: f32, bottom: f32, left: f32) -> Self {
20        Self { top, right, bottom, left }
21    }
22
23    pub fn total_h(&self) -> f32 { self.left + self.right }
24    pub fn total_v(&self) -> f32 { self.top + self.bottom }
25
26    /// Shrink a rect by these insets.
27    pub fn shrink(&self, r: Rect) -> Rect {
28        Rect {
29            origin: Point { x: r.origin.x + self.left, y: r.origin.y + self.top },
30            size: Size {
31                width:  (r.size.width  - self.total_h()).max(0.0),
32                height: (r.size.height - self.total_v()).max(0.0),
33            },
34        }
35    }
36
37    /// Grow a size by these insets.
38    pub fn grow(&self, s: Size) -> Size {
39        Size { width: s.width + self.total_h(), height: s.height + self.total_v() }
40    }
41}
42