egui_extras: add cell_layout
option to set the layout of all cells
This commit is contained in:
parent
0e0eedfdda
commit
426b933d2f
4 changed files with 60 additions and 30 deletions
|
@ -81,25 +81,20 @@ 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");
|
||||
});
|
||||
ui.heading("Row");
|
||||
});
|
||||
header.col(|ui| {
|
||||
ui.centered_and_justified(|ui| {
|
||||
ui.heading("Clock");
|
||||
});
|
||||
ui.heading("Clock");
|
||||
});
|
||||
header.col(|ui| {
|
||||
ui.centered_and_justified(|ui| {
|
||||
ui.heading("Content");
|
||||
});
|
||||
ui.heading("Content");
|
||||
});
|
||||
})
|
||||
.body(|mut body| {
|
||||
|
@ -129,24 +124,18 @@ 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());
|
||||
});
|
||||
ui.label(row_index.to_string());
|
||||
});
|
||||
row.col(|ui| {
|
||||
ui.centered_and_justified(|ui| {
|
||||
ui.label(clock_emoji(row_index));
|
||||
});
|
||||
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");
|
||||
} else {
|
||||
ui.label("Normal row");
|
||||
}
|
||||
});
|
||||
ui.style_mut().wrap = Some(false);
|
||||
if thick {
|
||||
ui.heading("Extra thick row");
|
||||
} else {
|
||||
ui.label("Normal row");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in a new issue