Skip to content

Commit 57c4533

Browse files
bu5hm4nnemilk
authored andcommitted
TextEdit: Change margin property to egui::Margin type (emilk#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 <[email protected]>
1 parent 2b2123f commit 57c4533

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

crates/egui/src/widgets/text_edit/builder.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ pub struct TextEdit<'t> {
6969
layouter: Option<&'t mut dyn FnMut(&Ui, &str, f32) -> Arc<Galley>>,
7070
password: bool,
7171
frame: bool,
72-
margin: Vec2,
72+
margin: Margin,
7373
multiline: bool,
7474
interactive: bool,
7575
desired_width: Option<f32>,
@@ -119,7 +119,7 @@ impl<'t> TextEdit<'t> {
119119
layouter: None,
120120
password: false,
121121
frame: true,
122-
margin: vec2(4.0, 2.0),
122+
margin: Margin::symmetric(4.0, 2.0),
123123
multiline: true,
124124
interactive: true,
125125
desired_width: None,
@@ -261,10 +261,10 @@ impl<'t> TextEdit<'t> {
261261
self
262262
}
263263

264-
/// Set margin of text. Default is [4.0,2.0]
264+
/// Set margin of text. Default is `Margin::symmetric(4.0, 2.0)`
265265
#[inline]
266-
pub fn margin(mut self, margin: Vec2) -> Self {
267-
self.margin = margin;
266+
pub fn margin(mut self, margin: impl Into<Margin>) -> Self {
267+
self.margin = margin.into();
268268
self
269269
}
270270

@@ -381,13 +381,14 @@ impl<'t> TextEdit<'t> {
381381
let where_to_put_background = ui.painter().add(Shape::Noop);
382382

383383
let margin = self.margin;
384-
let max_rect = ui.available_rect_before_wrap().shrink2(margin);
384+
let available = ui.available_rect_before_wrap();
385+
let max_rect = margin.shrink_rect(available);
385386
let mut content_ui = ui.child_ui(max_rect, *ui.layout());
386387

387388
let mut output = self.show_content(&mut content_ui);
388389

389390
let id = output.response.id;
390-
let frame_rect = output.response.rect.expand2(margin);
391+
let frame_rect = margin.expand_rect(output.response.rect);
391392
ui.allocate_space(frame_rect.size());
392393
if interactive {
393394
output.response |= ui.interact(frame_rect, id, Sense::click());
@@ -493,8 +494,9 @@ impl<'t> TextEdit<'t> {
493494
galley.size().x.max(wrap_width)
494495
};
495496
let desired_height = (desired_height_rows.at_least(1) as f32) * row_height;
496-
let desired_size = vec2(desired_width, galley.size().y.max(desired_height))
497-
.at_least(min_size - margin * 2.0);
497+
let at_least = min_size - margin.sum();
498+
let desired_size =
499+
vec2(desired_width, galley.size().y.max(desired_height)).at_least(at_least);
498500

499501
let (auto_id, rect) = ui.allocate_space(desired_size);
500502

0 commit comments

Comments
 (0)