Skip to content

Commit

Permalink
Added a method to set Editbox and InputText margin
Browse files Browse the repository at this point in the history
  • Loading branch information
yui-915 authored and not-fl3 committed Sep 4, 2024
1 parent e21e5e7 commit fc56d5e
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/ui/widgets/editbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub struct Editbox<'a> {
filter: Option<&'a dyn Fn(char) -> bool>,
pos: Option<Vec2>,
password: bool,
margin: Option<Vec2>,
}

mod text_editor;
Expand All @@ -31,6 +32,7 @@ impl<'a> Editbox<'a> {
multiline: true,
pos: None,
password: false,
margin: None,
}
}

Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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);

Expand Down
14 changes: 14 additions & 0 deletions src/ui/widgets/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct InputText<'a> {
numbers: bool,
ratio: f32,
pos: Option<Vec2>,
margin: Option<Vec2>,
}

impl<'a> InputText<'a> {
Expand All @@ -23,6 +24,7 @@ impl<'a> InputText<'a> {
password: false,
ratio: 0.5,
pos: None,
margin: None,
}
}

Expand All @@ -35,6 +37,7 @@ impl<'a> InputText<'a> {
password: self.password,
ratio: self.ratio,
pos: self.pos,
margin: self.margin,
}
}

Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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 == '-')
Expand Down

0 comments on commit fc56d5e

Please sign in to comment.