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,25 +81,20 @@ impl TableDemo {
TableBuilder::new(ui) TableBuilder::new(ui)
.striped(true) .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::initial(60.0).at_least(40.0)) .column(Size::initial(60.0).at_least(40.0))
.column(Size::remainder().at_least(60.0)) .column(Size::remainder().at_least(60.0))
.resizable(self.resizable) .resizable(self.resizable)
.header(20.0, |mut header| { .header(20.0, |mut header| {
header.col(|ui| { header.col(|ui| {
ui.centered_and_justified(|ui| { ui.heading("Row");
ui.heading("Row");
});
}); });
header.col(|ui| { header.col(|ui| {
ui.centered_and_justified(|ui| { ui.heading("Clock");
ui.heading("Clock");
});
}); });
header.col(|ui| { header.col(|ui| {
ui.centered_and_justified(|ui| { ui.heading("Content");
ui.heading("Content");
});
}); });
}) })
.body(|mut body| { .body(|mut body| {
@ -129,24 +124,18 @@ impl TableDemo {
let row_height = if thick { 30.0 } else { 18.0 }; let row_height = if thick { 30.0 } else { 18.0 };
body.row(row_height, |mut row| { body.row(row_height, |mut row| {
row.col(|ui| { row.col(|ui| {
ui.centered_and_justified(|ui| { ui.label(row_index.to_string());
ui.label(row_index.to_string());
});
}); });
row.col(|ui| { row.col(|ui| {
ui.centered_and_justified(|ui| { ui.label(clock_emoji(row_index));
ui.label(clock_emoji(row_index));
});
}); });
row.col(|ui| { row.col(|ui| {
ui.centered_and_justified(|ui| { ui.style_mut().wrap = Some(false);
ui.style_mut().wrap = Some(false); if thick {
if thick { ui.heading("Extra thick row");
ui.heading("Extra thick row"); } else {
} else { ui.label("Normal row");
ui.label("Normal row"); }
}
});
}); });
}); });
} }

View file

@ -32,20 +32,27 @@ pub struct StripLayout<'l> {
cursor: Pos2, cursor: Pos2,
max: Pos2, max: Pos2,
pub(crate) clip: bool, pub(crate) clip: bool,
cell_layout: egui::Layout,
} }
impl<'l> StripLayout<'l> { 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 rect = ui.available_rect_before_wrap();
let pos = rect.left_top(); let pos = rect.left_top();
Self { Self {
ui, ui,
direction,
rect, rect,
cursor: pos, cursor: pos,
max: pos, max: pos,
direction,
clip, clip,
cell_layout,
} }
} }
@ -132,7 +139,7 @@ impl<'l> StripLayout<'l> {
} }
fn cell(&mut self, rect: Rect, add_contents: impl FnOnce(&mut Ui)) -> Rect { 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 { if self.clip {
child_ui.set_clip_rect(child_ui.clip_rect().intersect(rect)); 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, ui: &'a mut Ui,
sizing: Sizing, sizing: Sizing,
clip: bool, clip: bool,
cell_layout: egui::Layout,
} }
impl<'a> StripBuilder<'a> { impl<'a> StripBuilder<'a> {
/// Create new strip builder. /// Create new strip builder.
pub fn new(ui: &'a mut Ui) -> Self { pub fn new(ui: &'a mut Ui) -> Self {
let cell_layout = *ui.layout();
Self { Self {
ui, ui,
sizing: Default::default(), sizing: Default::default(),
cell_layout,
clip: true, clip: true,
} }
} }
@ -63,6 +66,12 @@ impl<'a> StripBuilder<'a> {
self 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. /// Allocate space for for one column/row.
pub fn size(mut self, size: Size) -> Self { pub fn size(mut self, size: Size) -> Self {
self.sizing.add(size); self.sizing.add(size);
@ -89,7 +98,12 @@ impl<'a> StripBuilder<'a> {
self.ui.available_rect_before_wrap().width(), self.ui.available_rect_before_wrap().width(),
self.ui.spacing().item_spacing.x, 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 { strip(Strip {
layout: &mut layout, layout: &mut layout,
direction: CellDirection::Horizontal, direction: CellDirection::Horizontal,
@ -110,7 +124,12 @@ impl<'a> StripBuilder<'a> {
self.ui.available_rect_before_wrap().height(), self.ui.available_rect_before_wrap().height(),
self.ui.spacing().item_spacing.y, 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 { strip(Strip {
layout: &mut layout, layout: &mut layout,
direction: CellDirection::Vertical, direction: CellDirection::Vertical,

View file

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