Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create a UiBuilder for building Uis #4969

Merged
merged 15 commits into from
Aug 26, 2024
Prev Previous commit
Next Next commit
Deprecate ui.child_ui
  • Loading branch information
emilk committed Aug 15, 2024

Verified

This commit was signed with the committer’s verified signature.
csainty Chris Sainty
commit 02b11ff204e50a0350e53ab579f9e2f7993ee793
2 changes: 1 addition & 1 deletion crates/egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
@@ -425,7 +425,7 @@ fn button_frame(
outer_rect.set_height(outer_rect.height().at_least(interact_size.y));

let inner_rect = outer_rect.shrink2(margin);
let mut content_ui = ui.child_ui(inner_rect, *ui.layout(), None);
let mut content_ui = ui.new_child(UiBuilder::new().max_rect(inner_rect));
add_contents(&mut content_ui);

let mut outer_rect = content_ui.min_rect().expand2(margin);
8 changes: 4 additions & 4 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
@@ -250,10 +250,10 @@ impl Frame {
inner_rect.max.x = inner_rect.max.x.max(inner_rect.min.x);
inner_rect.max.y = inner_rect.max.y.max(inner_rect.min.y);

let content_ui = ui.child_ui(
inner_rect,
*ui.layout(),
Some(UiStackInfo::new(UiKind::Frame).with_frame(self)),
let content_ui = ui.new_child(
UiBuilder::new()
.ui_stack_info(UiStackInfo::new(UiKind::Frame).with_frame(self))
.max_rect(inner_rect),
);

// content_ui.set_clip_rect(outer_rect_bounds.shrink(self.stroke.width * 0.5)); // Can't do this since we don't know final size yet
13 changes: 7 additions & 6 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
@@ -261,7 +261,7 @@ impl SidePanel {
}
}

let mut panel_ui = ui.child_from_builder(
let mut panel_ui = ui.new_child(
UiBuilder::new()
.id_source(id)
.ui_stack_info(UiStackInfo::new(match side {
@@ -751,7 +751,7 @@ impl TopBottomPanel {
}
}

let mut panel_ui = ui.child_from_builder(
let mut panel_ui = ui.new_child(
UiBuilder::new()
.id_source(id)
.ui_stack_info(UiStackInfo::new(match side {
@@ -1093,10 +1093,11 @@ impl CentralPanel {
let Self { frame } = self;

let panel_rect = ui.available_rect_before_wrap();
let mut panel_ui = ui.child_ui(
panel_rect,
Layout::top_down(Align::Min),
Some(UiStackInfo::new(UiKind::CentralPanel)),
let mut panel_ui = ui.new_child(
UiBuilder::new()
.ui_stack_info(UiStackInfo::new(UiKind::CentralPanel))
.max_rect(panel_rect)
.layout(Layout::top_down(Align::Min)),
);
panel_ui.set_clip_rect(panel_rect); // If we overflow, don't do so visibly (#4475)

8 changes: 4 additions & 4 deletions crates/egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
@@ -270,10 +270,10 @@ impl Resize {

content_clip_rect = content_clip_rect.intersect(ui.clip_rect()); // Respect parent region

let mut content_ui = ui.child_ui(
inner_rect,
*ui.layout(),
Some(UiStackInfo::new(UiKind::Resize)),
let mut content_ui = ui.new_child(
UiBuilder::new()
.ui_stack_info(UiStackInfo::new(UiKind::Resize))
.max_rect(inner_rect),
);
content_ui.set_clip_rect(content_clip_rect);

8 changes: 4 additions & 4 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
@@ -560,10 +560,10 @@ impl ScrollArea {
}

let content_max_rect = Rect::from_min_size(inner_rect.min - state.offset, content_max_size);
let mut content_ui = ui.child_ui(
content_max_rect,
*ui.layout(),
Some(UiStackInfo::new(UiKind::ScrollArea)),
let mut content_ui = ui.new_child(
UiBuilder::new()
.ui_stack_info(UiStackInfo::new(UiKind::ScrollArea))
.max_rect(content_max_rect),
);

{
31 changes: 19 additions & 12 deletions crates/egui/src/ui.rs
Original file line number Diff line number Diff line change
@@ -130,13 +130,14 @@ impl Ui {
/// [`Self::scope`] if needed.
///
/// When in doubt, use `None` for the `UiStackInfo` argument.
#[deprecated = "Use ui.child_from_builder instead"]
pub fn child_ui(
&mut self,
max_rect: Rect,
layout: Layout,
ui_stack_info: Option<UiStackInfo>,
) -> Self {
self.child_from_builder(
self.new_child(
UiBuilder::new()
.max_rect(max_rect)
.layout(layout)
@@ -155,7 +156,7 @@ impl Ui {
id_source: impl Hash,
ui_stack_info: Option<UiStackInfo>,
) -> Self {
self.child_from_builder(
self.new_child(
UiBuilder::new()
.id_source(id_source)
.max_rect(max_rect)
@@ -165,7 +166,7 @@ impl Ui {
}

/// Create a child `Ui` with the properties of the given builder.
pub fn child_from_builder(&mut self, ui_builder: UiBuilder) -> Self {
pub fn new_child(&mut self, ui_builder: UiBuilder) -> Self {
let UiBuilder {
id_source,
ui_stack_info,
@@ -1171,7 +1172,7 @@ impl Ui {
let frame_rect = self.placer.next_space(desired_size, item_spacing);
let child_rect = self.placer.justify_and_align(frame_rect, desired_size);

let mut child_ui = self.child_ui(child_rect, layout, None);
let mut child_ui = self.new_child(UiBuilder::new().max_rect(child_rect).layout(layout));
let ret = add_contents(&mut child_ui);
let final_child_rect = child_ui.min_rect();

@@ -1193,7 +1194,7 @@ impl Ui {
add_contents: impl FnOnce(&mut Self) -> R,
) -> InnerResponse<R> {
debug_assert!(max_rect.is_finite());
let mut child_ui = self.child_ui(max_rect, *self.layout(), None);
let mut child_ui = self.new_child(UiBuilder::new().max_rect(max_rect));
let ret = add_contents(&mut child_ui);
let final_child_rect = child_ui.min_rect();

@@ -2069,7 +2070,7 @@ impl Ui {
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<R> {
let next_auto_id_source = self.next_auto_id_source;
let mut child_ui = self.child_from_builder(ui_builder);
let mut child_ui = self.new_child(ui_builder);
self.next_auto_id_source = next_auto_id_source; // HACK: we want `scope` to only increment this once, so that `ui.scope` is equivalent to `ui.allocate_space`.
let ret = add_contents(&mut child_ui);
let response = self.allocate_rect(child_ui.min_rect(), Sense::hover());
@@ -2129,7 +2130,7 @@ impl Ui {
child_rect.min.x += indent;

let mut child_ui =
self.child_from_builder(UiBuilder::new().id_source(id_source).max_rect(child_rect));
self.new_child(UiBuilder::new().id_source(id_source).max_rect(child_rect));
let ret = add_contents(&mut child_ui);

let left_vline = self.visuals().indent_has_left_vline;
@@ -2351,7 +2352,7 @@ impl Ui {
layout: Layout,
add_contents: Box<dyn FnOnce(&mut Self) -> R + 'c>,
) -> InnerResponse<R> {
let mut child_ui = self.child_ui(self.available_rect_before_wrap(), layout, None);
let mut child_ui = self.new_child(UiBuilder::new().layout(layout));
let inner = add_contents(&mut child_ui);
let rect = child_ui.min_rect();
let item_spacing = self.spacing().item_spacing;
@@ -2434,8 +2435,11 @@ impl Ui {
pos,
pos2(pos.x + column_width, self.max_rect().right_bottom().y),
);
let mut column_ui =
self.child_ui(child_rect, Layout::top_down_justified(Align::LEFT), None);
let mut column_ui = self.new_child(
UiBuilder::new()
.max_rect(child_rect)
.layout(Layout::top_down_justified(Align::LEFT)),
);
column_ui.set_width(column_width);
column_ui
})
@@ -2488,8 +2492,11 @@ impl Ui {
pos,
pos2(pos.x + column_width, self.max_rect().right_bottom().y),
);
let mut column_ui =
self.child_ui(child_rect, Layout::top_down_justified(Align::LEFT), None);
let mut column_ui = self.new_child(
UiBuilder::new()
.max_rect(child_rect)
.layout(Layout::top_down_justified(Align::LEFT)),
);
column_ui.set_width(column_width);
column_ui
});
2 changes: 1 addition & 1 deletion crates/egui_extras/src/layout.rs
Original file line number Diff line number Diff line change
@@ -197,7 +197,7 @@ impl<'l> StripLayout<'l> {
child_ui_id_source: egui::Id,
add_cell_contents: impl FnOnce(&mut Ui),
) -> Ui {
let mut child_ui = self.ui.child_from_builder(
let mut child_ui = self.ui.new_child(
UiBuilder::new()
.id_source(child_ui_id_source)
.ui_stack_info(egui::UiStackInfo::new(egui::UiKind::TableCell))
2 changes: 1 addition & 1 deletion examples/custom_window_frame/src/main.rs
Original file line number Diff line number Diff line change
@@ -71,7 +71,7 @@ fn custom_window_frame(ctx: &egui::Context, title: &str, add_contents: impl FnOn
rect
}
.shrink(4.0);
let mut content_ui = ui.child_ui(content_rect, *ui.layout(), None);
let mut content_ui = ui.new_child(UiBuilder::new().max_rect(content_rect));
add_contents(&mut content_ui);
});
}
4 changes: 2 additions & 2 deletions tests/test_viewports/src/main.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@
use std::sync::Arc;

use eframe::egui;
use egui::{mutex::RwLock, Id, InnerResponse, ViewportBuilder, ViewportId};
use egui::{mutex::RwLock, Id, InnerResponse, UiBuilder, ViewportBuilder, ViewportId};

// Drag-and-drop between windows is not yet implemented, but if you wanna work on it, enable this:
pub const DRAG_AND_DROP_TEST: bool = false;
@@ -457,7 +457,7 @@ fn drop_target<R>(

let available_rect = ui.available_rect_before_wrap();
let inner_rect = available_rect.shrink2(margin);
let mut content_ui = ui.child_ui(inner_rect, *ui.layout(), None);
let mut content_ui = ui.new_child(UiBuilder::new().max_rect(inner_rect));
let ret = body(&mut content_ui);

let outer_rect =