From 6c8ff5f3da3a2f644aef1614e654d92721c57c54 Mon Sep 17 00:00:00 2001 From: Leo dev Date: Fri, 27 Jun 2025 15:06:45 -0500 Subject: [PATCH] margin --- src/main.rs | 2 +- src/style.rs | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index e3630a2..87f47e8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -22,7 +22,7 @@ fn main() { screen .draw(format!("Type quit to quit")) - .component(Transform::new().bottom()); + .component(Transform::new().bottom().margin(0, -1)); screen .draw(Input::new()) diff --git a/src/style.rs b/src/style.rs index 2e8d030..4c2b81c 100644 --- a/src/style.rs +++ b/src/style.rs @@ -23,6 +23,8 @@ pub enum Dimension { component!(Transform { pub x: Position, pub y: Position, + pub mx: i32, + pub my: i32, pub width: Dimension, pub height: Dimension, }); @@ -32,6 +34,8 @@ impl Transform { Transform { x: Position::Const(0), y: Position::Const(0), + mx: 0, + my: 0, width: Dimension::Auto, height: Dimension::Auto, } @@ -41,6 +45,8 @@ impl Transform { Transform { x: Position::Center, y: Position::Center, + mx: 0, + my: 0, width: Dimension::Auto, height: Dimension::Auto, } @@ -51,6 +57,12 @@ impl Transform { self } + pub fn margin(mut self, x: i32, y: i32) -> Self { + self.mx = x; + self.my = y; + self + } + pub fn dimensions(mut self, width: u16, height: u16) -> Self { self.width = Dimension::Const(width); self.height = Dimension::Const(height); @@ -63,8 +75,10 @@ impl Transform { } pub fn use_position(&self, parent_width: u16, parent_height: u16, raw: &mut RawTransform) { - self.x.use_position(raw.width, parent_width, &mut raw.x); - self.y.use_position(raw.height, parent_height, &mut raw.y); + self.x + .use_position(raw.width, parent_width, self.mx, &mut raw.x); + self.y + .use_position(raw.height, parent_height, self.my, &mut raw.y); } } @@ -89,11 +103,17 @@ impl Dimension { } impl Position { - pub fn use_position(&self, size: u16, parent: u16, r: &mut u16) { + pub fn use_position(&self, size: u16, parent: u16, m: i32, r: &mut u16) { match self { Self::Center => *r = (parent - size) / 2, Self::Const(n) => *r = *n, Self::End => *r = parent - size, } + + if m > 0 { + *r += m as u16; + } else { + *r -= -m as u16; + } } }