`TextEdit`: Change `margin` property to `egui::Margin` type (#3993)

Despite their being an actual `egui::Margin` struct, Textedit has a
`margin()` builder function that supports only `Vec2` types and thereby
only symmetric margins. This PR changes the function to accept
`egui::Margin` type instead making it more congruent with overall egui
logic as well as supporting asymmetric margins.

P.S: I tried to run all checks but I had to modify `./rust-toolchain` to
1.67.0 to get the checks to run on macOS.

---------

Co-authored-by: Emil Ernerfeldt <emil.ernerfeldt@gmail.com>
This commit is contained in:
bu5hm4nn 2024-02-13 03:44:55 -06:00 committed by GitHub
parent 7a4ab666b4
commit 4875b01de3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 11 additions and 9 deletions

View File

@ -69,7 +69,7 @@ pub struct TextEdit<'t> {
layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>>, layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>>,
password: bool, password: bool,
frame: bool, frame: bool,
margin: Vec2, margin: Margin,
multiline: bool, multiline: bool,
interactive: bool, interactive: bool,
desired_width: Option<f32>, desired_width: Option<f32>,
@ -119,7 +119,7 @@ impl<'t> TextEdit<'t> {
layouter: None, layouter: None,
password: false, password: false,
frame: true, frame: true,
margin: vec2(4.0, 2.0), margin: Margin::symmetric(4.0, 2.0),
multiline: true, multiline: true,
interactive: true, interactive: true,
desired_width: None, desired_width: None,
@ -261,10 +261,10 @@ impl<'t> TextEdit<'t> {
self self
} }
/// Set margin of text. Default is [4.0,2.0] /// Set margin of text. Default is `Margin::symmetric(4.0, 2.0)`
#[inline] #[inline]
pub fn margin(mut self, margin: Vec2) -> Self { pub fn margin(mut self, margin: impl Into<Margin>) -> Self {
self.margin = margin; self.margin = margin.into();
self self
} }
@ -381,13 +381,14 @@ impl<'t> TextEdit<'t> {
let where_to_put_background = ui.painter().add(Shape::Noop); let where_to_put_background = ui.painter().add(Shape::Noop);
let margin = self.margin; let margin = self.margin;
let max_rect = ui.available_rect_before_wrap().shrink2(margin); let available = ui.available_rect_before_wrap();
let max_rect = margin.shrink_rect(available);
let mut content_ui = ui.child_ui(max_rect, *ui.layout()); let mut content_ui = ui.child_ui(max_rect, *ui.layout());
let mut output = self.show_content(&mut content_ui); let mut output = self.show_content(&mut content_ui);
let id = output.response.id; let id = output.response.id;
let frame_rect = output.response.rect.expand2(margin); let frame_rect = margin.expand_rect(output.response.rect);
ui.allocate_space(frame_rect.size()); ui.allocate_space(frame_rect.size());
if interactive { if interactive {
output.response |= ui.interact(frame_rect, id, Sense::click()); output.response |= ui.interact(frame_rect, id, Sense::click());
@ -493,8 +494,9 @@ impl<'t> TextEdit<'t> {
galley.size().x.max(wrap_width) galley.size().x.max(wrap_width)
}; };
let desired_height = (desired_height_rows.at_least(1) as f32) * row_height; let desired_height = (desired_height_rows.at_least(1) as f32) * row_height;
let desired_size = vec2(desired_width, galley.size().y.max(desired_height)) let at_least = min_size - margin.sum();
.at_least(min_size - margin * 2.0); let desired_size =
vec2(desired_width, galley.size().y.max(desired_height)).at_least(at_least);
let (auto_id, rect) = ui.allocate_space(desired_size); let (auto_id, rect) = ui.allocate_space(desired_size);