diff --git a/egui/src/containers/combo_box.rs b/egui/src/containers/combo_box.rs index 92b967b9..e840f20c 100644 --- a/egui/src/containers/combo_box.rs +++ b/egui/src/containers/combo_box.rs @@ -58,8 +58,7 @@ pub fn combo_box( let frame_margin = frame.margin; frame.show(ui, |ui| { ui.set_min_width(button_response.rect.width() - 2.0 * frame_margin.x); - ui.set_layout(Layout::justified(Direction::Vertical)); - menu_contents(ui); + ui.with_layout(Layout::justified(Direction::Vertical), menu_contents); }) }); @@ -83,7 +82,7 @@ fn button_frame( let outer_rect_bounds = ui.available(); let inner_rect = outer_rect_bounds.shrink2(margin); let where_to_put_background = ui.painter().add(PaintCmd::Noop); - let mut content_ui = ui.child_ui(inner_rect); + let mut content_ui = ui.child_ui(inner_rect, *ui.layout()); add_contents(&mut content_ui); let outer_rect = Rect::from_min_max( diff --git a/egui/src/containers/frame.rs b/egui/src/containers/frame.rs index f0c006da..699b4fcb 100644 --- a/egui/src/containers/frame.rs +++ b/egui/src/containers/frame.rs @@ -81,7 +81,7 @@ impl Frame { let outer_rect_bounds = ui.available(); let inner_rect = outer_rect_bounds.shrink2(self.margin); let where_to_put_background = ui.painter().add(PaintCmd::Noop); - let content_ui = ui.child_ui(inner_rect); + let content_ui = ui.child_ui(inner_rect, *ui.layout()); Prepared { frame: self, outer_rect_bounds, diff --git a/egui/src/containers/resize.rs b/egui/src/containers/resize.rs index 08b4bc90..d9c6020a 100644 --- a/egui/src/containers/resize.rs +++ b/egui/src/containers/resize.rs @@ -203,7 +203,7 @@ impl Resize { content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region - let mut content_ui = ui.child_ui(inner_rect); + let mut content_ui = ui.child_ui(inner_rect, *ui.layout()); content_ui.set_clip_rect(content_clip_rect); Prepared { diff --git a/egui/src/containers/scroll_area.rs b/egui/src/containers/scroll_area.rs index a3caeb28..5eb8d67f 100644 --- a/egui/src/containers/scroll_area.rs +++ b/egui/src/containers/scroll_area.rs @@ -100,10 +100,13 @@ impl ScrollArea { let inner_size = outer_size - vec2(current_scroll_bar_width, 0.0); let inner_rect = Rect::from_min_size(ui.available().min, inner_size); - let mut content_ui = ui.child_ui(Rect::from_min_size( - inner_rect.min - state.offset, - vec2(inner_size.x, f32::INFINITY), - )); + let mut content_ui = ui.child_ui( + Rect::from_min_size( + inner_rect.min - state.offset, + vec2(inner_size.x, f32::INFINITY), + ), + *ui.layout(), + ); let mut content_clip_rect = inner_rect.expand(ui.style().visuals.clip_rect_margin); content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); content_clip_rect.max.x = ui.clip_rect().max.x - current_scroll_bar_width; // Nice handling of forced resizing beyond the possible diff --git a/egui/src/demos/app.rs b/egui/src/demos/app.rs index a4a9db8b..b0b9eb28 100644 --- a/egui/src/demos/app.rs +++ b/egui/src/demos/app.rs @@ -389,13 +389,15 @@ fn show_menu_bar(ui: &mut Ui, windows: &mut OpenWindows, env: &DemoEnvironment) (time.rem_euclid(60.0)).floor(), (time.rem_euclid(1.0) * 100.0).floor() ); - ui.set_layout(Layout::horizontal(Align::Max).reverse()); - if ui - .add(Button::new(time).text_style(TextStyle::Monospace)) - .clicked - { - windows.fractal_clock = !windows.fractal_clock; - } + + ui.with_layout(Layout::horizontal(Align::Max).reverse(), |ui| { + if ui + .add(Button::new(time).text_style(TextStyle::Monospace)) + .clicked + { + windows.fractal_clock = !windows.fractal_clock; + } + }); } }); } diff --git a/egui/src/demos/demo_window.rs b/egui/src/demos/demo_window.rs index 84f08560..7f151192 100644 --- a/egui/src/demos/demo_window.rs +++ b/egui/src/demos/demo_window.rs @@ -360,20 +360,24 @@ impl Default for LayoutDemo { } impl LayoutDemo { + fn layout(&self) -> Layout { + let layout = Layout::from_dir_align(self.dir, self.align); + if self.reversed { + layout.reverse() + } else { + layout + } + } + pub fn ui(&mut self, ui: &mut Ui) { Resize::default() .default_size([200.0, 100.0]) - .show(ui, |ui| self.content_ui(ui)); + .show(ui, |ui| { + ui.with_layout(self.layout(), |ui| self.content_ui(ui)) + }); } pub fn content_ui(&mut self, ui: &mut Ui) { - let layout = Layout::from_dir_align(self.dir, self.align); - if self.reversed { - ui.set_layout(layout.reverse()); - } else { - ui.set_layout(layout); - } - // ui.add(label!("Available space: {:?}", ui.available().size())); if ui.add(Button::new("Reset")).clicked { *self = Default::default(); diff --git a/egui/src/menu.rs b/egui/src/menu.rs index 9be946d8..aaf971fb 100644 --- a/egui/src/menu.rs +++ b/egui/src/menu.rs @@ -111,8 +111,7 @@ fn menu_impl<'c>( style.visuals.widgets.inactive.bg_fill = TRANSPARENT; style.visuals.widgets.inactive.bg_stroke = Stroke::none(); ui.set_style(style); - ui.set_layout(Layout::justified(Direction::Vertical)); - add_contents(ui) + ui.with_layout(Layout::justified(Direction::Vertical), add_contents); }) }); diff --git a/egui/src/ui.rs b/egui/src/ui.rs index 5285d8b6..62fdf1e1 100644 --- a/egui/src/ui.rs +++ b/egui/src/ui.rs @@ -67,17 +67,24 @@ impl Ui { } } - pub fn child_ui(&mut self, child_rect: Rect) -> Self { + pub fn child_ui(&mut self, child_rect: Rect, layout: Layout) -> Self { let id = self.make_position_id(); // TODO: is this a good idea? self.child_count += 1; + + let cursor = if layout.is_reversed() { + child_rect.max + } else { + child_rect.min + }; + Ui { id, painter: self.painter.clone(), desired_rect: child_rect, child_bounds: Rect::from_min_size(child_rect.min, Vec2::zero()), // TODO: Rect::nothing() ? style: self.style().clone(), - layout: self.layout, - cursor: child_rect.min, + layout, + cursor, child_count: 0, } } @@ -275,16 +282,6 @@ impl Ui { &self.layout } - // TODO: remove - pub fn set_layout(&mut self, layout: Layout) { - self.layout = layout; - - // TODO: remove this HACK: - if layout.is_reversed() { - self.cursor = self.rect_finite().max; - } - } - // ------------------------------------------------------------------------ pub fn contains_mouse(&self, rect: Rect) -> bool { @@ -613,7 +610,7 @@ impl Ui { pub fn add_custom_contents(&mut self, size: Vec2, add_contents: impl FnOnce(&mut Ui)) -> Rect { let size = size.at_most(self.available().size()); let child_rect = Rect::from_min_size(self.cursor, size); - let mut child_ui = self.child_ui(child_rect); + let mut child_ui = self.child_ui(child_rect, self.layout); add_contents(&mut child_ui); self.allocate_space(child_ui.bounding_size()) } @@ -621,7 +618,7 @@ impl Ui { /// Create a child ui. You can use this to temporarily change the Style of a sub-region, for instance. pub fn add_custom(&mut self, add_contents: impl FnOnce(&mut Ui) -> R) -> (R, Rect) { let child_rect = self.available(); - let mut child_ui = self.child_ui(child_rect); + let mut child_ui = self.child_ui(child_rect, self.layout); let r = add_contents(&mut child_ui); let size = child_ui.bounding_size(); (r, self.allocate_space(size)) @@ -641,7 +638,7 @@ impl Ui { let child_rect = Rect::from_min_max(self.cursor + indent, self.bottom_right()); let mut child_ui = Ui { id: self.id.with(id_source), - ..self.child_ui(child_rect) + ..self.child_ui(child_rect, self.layout) }; let ret = add_contents(&mut child_ui); let size = child_ui.bounding_size(); @@ -677,10 +674,13 @@ impl Ui { Align::Center => self.available().width() / 2.0 - width / 2.0, Align::Max => self.available().width() - width, }; - self.child_ui(Rect::from_min_size( - self.cursor + vec2(x, 0.0), - vec2(width, self.available().height()), - )) + self.child_ui( + Rect::from_min_size( + self.cursor + vec2(x, 0.0), + vec2(width, self.available().height()), + ), + self.layout, + ) } /// Start a ui with horizontal layout. @@ -716,8 +716,19 @@ impl Ui { add_contents: impl FnOnce(&mut Self) -> R, ) -> (R, Rect) { let child_rect = Rect::from_min_size(self.cursor, initial_size); - let mut child_ui = self.child_ui(child_rect); - child_ui.set_layout(layout); // HACK: need a separate call right now + let mut child_ui = self.child_ui(child_rect, layout); + let ret = add_contents(&mut child_ui); + let size = child_ui.bounding_size(); + let rect = self.allocate_space(size); + (ret, rect) + } + + pub fn with_layout( + &mut self, + layout: Layout, + add_contents: impl FnOnce(&mut Self) -> R, + ) -> (R, Rect) { + let mut child_ui = self.child_ui(self.rect(), layout); let ret = add_contents(&mut child_ui); let size = child_ui.bounding_size(); let rect = self.allocate_space(size); @@ -749,7 +760,7 @@ impl Ui { Self { id: self.make_child_id(&("column", col_idx)), - ..self.child_ui(child_rect) + ..self.child_ui(child_rect, self.layout) } }) .collect();