egui_extras: add cell_layout option to set the layout of all cells

This commit is contained in:
Emil Ernerfeldt 2022-04-11 10:29:34 +02:00
parent 0e0eedfdda
commit 426b933d2f
4 changed files with 60 additions and 30 deletions

View file

@ -81,26 +81,21 @@ impl TableDemo {
TableBuilder::new(ui)
.striped(true)
.cell_layout(egui::Layout::left_to_right().with_cross_align(egui::Align::Center))
.column(Size::initial(60.0).at_least(40.0))
.column(Size::initial(60.0).at_least(40.0))
.column(Size::remainder().at_least(60.0))
.resizable(self.resizable)
.header(20.0, |mut header| {
header.col(|ui| {
ui.centered_and_justified(|ui| {
ui.heading("Row");
});
});
header.col(|ui| {
ui.centered_and_justified(|ui| {
ui.heading("Clock");
});
});
header.col(|ui| {
ui.centered_and_justified(|ui| {
ui.heading("Content");
});
});
})
.body(|mut body| {
if self.virtual_scroll {
@ -129,17 +124,12 @@ impl TableDemo {
let row_height = if thick { 30.0 } else { 18.0 };
body.row(row_height, |mut row| {
row.col(|ui| {
ui.centered_and_justified(|ui| {
ui.label(row_index.to_string());
});
});
row.col(|ui| {
ui.centered_and_justified(|ui| {
ui.label(clock_emoji(row_index));
});
});
row.col(|ui| {
ui.centered_and_justified(|ui| {
ui.style_mut().wrap = Some(false);
if thick {
ui.heading("Extra thick row");
@ -148,7 +138,6 @@ impl TableDemo {
}
});
});
});
}
}
});

View file

@ -32,20 +32,27 @@ pub struct StripLayout<'l> {
cursor: Pos2,
max: Pos2,
pub(crate) clip: bool,
cell_layout: egui::Layout,
}
impl<'l> StripLayout<'l> {
pub(crate) fn new(ui: &'l mut Ui, direction: CellDirection, clip: bool) -> Self {
pub(crate) fn new(
ui: &'l mut Ui,
direction: CellDirection,
clip: bool,
cell_layout: egui::Layout,
) -> Self {
let rect = ui.available_rect_before_wrap();
let pos = rect.left_top();
Self {
ui,
direction,
rect,
cursor: pos,
max: pos,
direction,
clip,
cell_layout,
}
}
@ -132,7 +139,7 @@ impl<'l> StripLayout<'l> {
}
fn cell(&mut self, rect: Rect, add_contents: impl FnOnce(&mut Ui)) -> Rect {
let mut child_ui = self.ui.child_ui(rect, *self.ui.layout());
let mut child_ui = self.ui.child_ui(rect, self.cell_layout);
if self.clip {
child_ui.set_clip_rect(child_ui.clip_rect().intersect(rect));

View file

@ -45,14 +45,17 @@ pub struct StripBuilder<'a> {
ui: &'a mut Ui,
sizing: Sizing,
clip: bool,
cell_layout: egui::Layout,
}
impl<'a> StripBuilder<'a> {
/// Create new strip builder.
pub fn new(ui: &'a mut Ui) -> Self {
let cell_layout = *ui.layout();
Self {
ui,
sizing: Default::default(),
cell_layout,
clip: true,
}
}
@ -63,6 +66,12 @@ impl<'a> StripBuilder<'a> {
self
}
/// What layout should we use for the individual cells?
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
self.cell_layout = cell_layout;
self
}
/// Allocate space for for one column/row.
pub fn size(mut self, size: Size) -> Self {
self.sizing.add(size);
@ -89,7 +98,12 @@ impl<'a> StripBuilder<'a> {
self.ui.available_rect_before_wrap().width(),
self.ui.spacing().item_spacing.x,
);
let mut layout = StripLayout::new(self.ui, CellDirection::Horizontal, self.clip);
let mut layout = StripLayout::new(
self.ui,
CellDirection::Horizontal,
self.clip,
self.cell_layout,
);
strip(Strip {
layout: &mut layout,
direction: CellDirection::Horizontal,
@ -110,7 +124,12 @@ impl<'a> StripBuilder<'a> {
self.ui.available_rect_before_wrap().height(),
self.ui.spacing().item_spacing.y,
);
let mut layout = StripLayout::new(self.ui, CellDirection::Vertical, self.clip);
let mut layout = StripLayout::new(
self.ui,
CellDirection::Vertical,
self.clip,
self.cell_layout,
);
strip(Strip {
layout: &mut layout,
direction: CellDirection::Vertical,

View file

@ -56,10 +56,12 @@ pub struct TableBuilder<'a> {
striped: bool,
resizable: bool,
clip: bool,
cell_layout: egui::Layout,
}
impl<'a> TableBuilder<'a> {
pub fn new(ui: &'a mut Ui) -> Self {
let cell_layout = *ui.layout();
Self {
ui,
sizing: Default::default(),
@ -67,6 +69,7 @@ impl<'a> TableBuilder<'a> {
striped: false,
resizable: false,
clip: true,
cell_layout,
}
}
@ -102,6 +105,12 @@ impl<'a> TableBuilder<'a> {
self
}
/// What layout should we use for the individual cells?
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
self.cell_layout = cell_layout;
self
}
/// Allocate space for one column.
pub fn column(mut self, width: Size) -> Self {
self.sizing.add(width);
@ -136,6 +145,7 @@ impl<'a> TableBuilder<'a> {
striped,
resizable,
clip,
cell_layout,
} = self;
let resize_id = resizable.then(|| ui.id().with("__table_resize"));
@ -146,7 +156,7 @@ impl<'a> TableBuilder<'a> {
let table_top = ui.cursor().top();
{
let mut layout = StripLayout::new(ui, CellDirection::Horizontal, clip);
let mut layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);
header(TableRow {
layout: &mut layout,
widths: &widths,
@ -166,6 +176,7 @@ impl<'a> TableBuilder<'a> {
scroll,
striped,
clip,
cell_layout,
}
}
@ -183,6 +194,7 @@ impl<'a> TableBuilder<'a> {
striped,
resizable,
clip,
cell_layout,
} = self;
let resize_id = resizable.then(|| ui.id().with("__table_resize"));
@ -202,6 +214,7 @@ impl<'a> TableBuilder<'a> {
scroll,
striped,
clip,
cell_layout,
}
.body(body);
}
@ -230,6 +243,7 @@ pub struct Table<'a> {
scroll: bool,
striped: bool,
clip: bool,
cell_layout: egui::Layout,
}
impl<'a> Table<'a> {
@ -248,6 +262,7 @@ impl<'a> Table<'a> {
scroll,
striped,
clip,
cell_layout,
} = self;
let avail_rect = ui.available_rect_before_wrap();
@ -257,7 +272,7 @@ impl<'a> Table<'a> {
egui::ScrollArea::new([false, scroll])
.auto_shrink([true; 2])
.show(ui, move |ui| {
let layout = StripLayout::new(ui, CellDirection::Horizontal, clip);
let layout = StripLayout::new(ui, CellDirection::Horizontal, clip, cell_layout);
body(TableBody {
layout,