diff --git a/src/ui/widgets/editbox.rs b/src/ui/widgets/editbox.rs index c0d01f73..2ed979cc 100644 --- a/src/ui/widgets/editbox.rs +++ b/src/ui/widgets/editbox.rs @@ -13,6 +13,7 @@ pub struct Editbox<'a> { filter: Option<&'a dyn Fn(char) -> bool>, pos: Option, password: bool, + margin: Option, } mod text_editor; @@ -31,6 +32,7 @@ impl<'a> Editbox<'a> { multiline: true, pos: None, password: false, + margin: None, } } @@ -65,6 +67,14 @@ impl<'a> Editbox<'a> { size: self.size, password: self.password, filter: Some(filter), + margin: self.margin, + } + } + + pub fn margin(self, margin: Vec2) -> Self { + Editbox { + margin: Some(margin), + ..self } } @@ -363,8 +373,8 @@ impl<'a> Editbox<'a> { line_height * text_vec.iter().filter(|c| **c == '\n').count() as f32, ); - // TODO: this is very weird hardcoded text_vec margin - let pos = context.window.cursor.fit(size, Layout::Free(vec2(2., 2.))); + let margin = self.margin.unwrap_or(vec2(2., 2.)); + let pos = context.window.cursor.fit(size, Layout::Free(margin)); context.window.painter.clip(parent_rect); diff --git a/src/ui/widgets/input.rs b/src/ui/widgets/input.rs index de31bb1a..6dc09b1b 100644 --- a/src/ui/widgets/input.rs +++ b/src/ui/widgets/input.rs @@ -11,6 +11,7 @@ pub struct InputText<'a> { numbers: bool, ratio: f32, pos: Option, + margin: Option, } impl<'a> InputText<'a> { @@ -23,6 +24,7 @@ impl<'a> InputText<'a> { password: false, ratio: 0.5, pos: None, + margin: None, } } @@ -35,6 +37,7 @@ impl<'a> InputText<'a> { password: self.password, ratio: self.ratio, pos: self.pos, + margin: self.margin, } } @@ -67,6 +70,13 @@ impl<'a> InputText<'a> { } } + pub fn margin(self, margin: Vec2) -> Self { + Self { + margin: Some(margin), + ..self + } + } + pub fn ui(self, ui: &mut Ui, data: &mut String) { let context = ui.get_active_window_context(); @@ -94,6 +104,10 @@ impl<'a> InputText<'a> { .position(pos) .multiline(false); + if let Some(margin) = self.margin { + editbox = editbox.margin(margin); + } + if self.numbers { editbox = editbox .filter(&|character| character.is_digit(10) || character == '.' || character == '-')