@@ -27,8 +27,61 @@ impl Default for CursorKind {
2727
2828#[derive(Debug, Clone, PartialEq, Eq, Hash)]
2929pub struct Margin {
30− pub vertical: u16,
31− pub horizontal: u16,
30+ pub left: u16,
31+ pub right: u16,
32+ pub top: u16,
33+ pub bottom: u16,
34+}
35+
36+impl Margin {
37+ pub fn none() -> Self {
38+ Self {
39+ left: 0,
40+ right: 0,
41+ top: 0,
42+ bottom: 0,
43+ }
44+ }
45+
46+ /// Set uniform margin for all sides.
47+ pub fn all(value: u16) -> Self {
48+ Self {
49+ left: value,
50+ right: value,
51+ top: value,
52+ bottom: value,
53+ }
54+ }
55+
56+ /// Set the margin of left and right sides to specified value.
57+ pub fn horizontal(value: u16) -> Self {
58+ Self {
59+ left: value,
60+ right: value,
61+ top: 0,
62+ bottom: 0,
63+ }
64+ }
65+
66+ /// Set the margin of top and bottom sides to specified value.
67+ pub fn vertical(value: u16) -> Self {
68+ Self {
69+ left: 0,
70+ right: 0,
71+ top: value,
72+ bottom: value,
73+ }
74+ }
75+
76+ /// Get the total width of the margin (left + right)
77+ pub fn width(&self) -> u16 {
78+ self.left + self.right
79+ }
80+
81+ /// Get the total height of the margin (top + bottom)
82+ pub fn height(&self) -> u16 {
83+ self.top + self.bottom
84+ }
3285}
3386
3487/// A simple rectangle used in the computation of the layout and to give widgets an hint about the
@@ -141,14 +194,14 @@ impl Rect {
141194 }
142195
143196 pub fn inner(self, margin: &Margin) -> Rect {
144− if self.width < 2 * margin.horizontal || self.height < 2 * margin.vertical {
197+ if self.width < margin.width() || self.height < margin.height() {
145198 Rect::default()
146199 } else {
147200 Rect {
148− x: self.x + margin.horizontal,
149− y: self.y + margin.vertical,
150− width: self.width - 2 * margin.horizontal,
151− height: self.height - 2 * margin.vertical,
201+ x: self.x + margin.left,
202+ y: self.y + margin.top,
203+ width: self.width - margin.width(),
204+ height: self.height - margin.height(),
152205 }
153206 }
154207 }