diff --git a/egui_dynamic_grid/src/grid/horizontal.rs b/egui_dynamic_grid/src/grid/horizontal.rs index 78818b92..4682f1d8 100644 --- a/egui_dynamic_grid/src/grid/horizontal.rs +++ b/egui_dynamic_grid/src/grid/horizontal.rs @@ -51,7 +51,6 @@ impl<'a> HorizontalGridBuilder<'a> { widths, }; horizontal_grid(grid); - layout.done(); } } diff --git a/egui_dynamic_grid/src/grid/vertical.rs b/egui_dynamic_grid/src/grid/vertical.rs index 06a7c272..fbac1fe6 100644 --- a/egui_dynamic_grid/src/grid/vertical.rs +++ b/egui_dynamic_grid/src/grid/vertical.rs @@ -51,7 +51,6 @@ impl<'a> VerticalGridBuilder<'a> { heights, }; vertical_grid(grid); - layout.done(); } } diff --git a/egui_dynamic_grid/src/layout.rs b/egui_dynamic_grid/src/layout.rs index cee89556..16a1b5b1 100644 --- a/egui_dynamic_grid/src/layout.rs +++ b/egui_dynamic_grid/src/layout.rs @@ -15,8 +15,9 @@ pub(crate) enum LineDirection { TopToBottom, } -pub struct Layout<'a> { - ui: &'a mut Ui, +/// Positions cells in [LineDirection] and starts a new line on [Layout::end_line] +pub struct Layout<'l> { + ui: &'l mut Ui, padding: Padding, direction: LineDirection, rect: Rect, @@ -24,8 +25,8 @@ pub struct Layout<'a> { max: Pos2, } -impl<'a> Layout<'a> { - pub(crate) fn new(ui: &'a mut Ui, padding: Padding, direction: LineDirection) -> Self { +impl<'l> Layout<'l> { + pub(crate) fn new(ui: &'l mut Ui, padding: Padding, direction: LineDirection) -> Self { let mut rect = ui.available_rect_before_wrap(); rect.set_left(rect.left() + padding.outer + padding.inner); rect.set_top(rect.top() + padding.outer + padding.inner); @@ -128,6 +129,7 @@ impl<'a> Layout<'a> { } } + /// Set the rect so that the scrollview knows about our size fn set_rect(&mut self) { let mut rect = self.rect; rect.set_right(self.max.x); @@ -137,15 +139,6 @@ impl<'a> Layout<'a> { .allocate_rect(rect, Sense::focusable_noninteractive()); } - pub fn done(&mut self) { - self.set_rect(); - } - - pub fn done_ui(mut self) -> &'a mut Ui { - self.set_rect(); - self.ui - } - fn cell(&mut self, rect: Rect, clip: bool, add_contents: impl FnOnce(&mut Ui)) { let mut child_ui = self.ui.child_ui(rect, *self.ui.layout()); @@ -163,3 +156,9 @@ impl<'a> Layout<'a> { add_contents(&mut child_ui) } } + +impl<'a> Drop for Layout<'a> { + fn drop(&mut self) { + self.set_rect() + } +} diff --git a/egui_dynamic_grid/src/padding.rs b/egui_dynamic_grid/src/padding.rs index 2cd78525..23de3592 100644 --- a/egui_dynamic_grid/src/padding.rs +++ b/egui_dynamic_grid/src/padding.rs @@ -1,5 +1,5 @@ /// Configure padding of grid or table -/// TODO: Use padding settings of egui? +/// TODO: Use padding settings of egui/should we extend egui padding settings for table? #[derive(Clone, Debug)] pub struct Padding { pub(crate) inner: f32, diff --git a/egui_dynamic_grid/src/sizing.rs b/egui_dynamic_grid/src/sizing.rs index d04f8dec..8077483f 100644 --- a/egui_dynamic_grid/src/sizing.rs +++ b/egui_dynamic_grid/src/sizing.rs @@ -1,3 +1,4 @@ +/// Size hint for table column/grid cell #[derive(Clone, Debug)] pub enum Size { /// Absolute size in points diff --git a/egui_dynamic_grid/src/table.rs b/egui_dynamic_grid/src/table.rs index dcd3020c..746ff018 100644 --- a/egui_dynamic_grid/src/table.rs +++ b/egui_dynamic_grid/src/table.rs @@ -1,3 +1,6 @@ +/// Table view with (optional) fixed header and scrolling body. +/// Cell widths are precalculated with given size hints so we can have tables like this: +/// | fixed size | all available space/minimum | 30% of available width | fixed size | use crate::{ layout::{CellSize, LineDirection}, sizing::Sizing, @@ -53,23 +56,22 @@ impl<'a> TableBuilder<'a> { self } - pub fn header(self, height: f32, header: F) -> Table<'a> - where - F: for<'b> FnOnce(TableRow<'a, 'b>), - { + pub fn header(self, height: f32, header: impl FnOnce(TableRow<'_, '_>)) -> Table<'a> { let widths = self.sizing.into_lengths(); - let mut layout = Layout::new(self.ui, self.padding.clone(), LineDirection::TopToBottom); + let ui = self.ui; { - let row = TableRow { - layout: &mut layout, - widths: widths.clone(), - striped: false, - height, - clicked: false, - }; - header(row); + let mut layout = Layout::new(ui, self.padding.clone(), LineDirection::TopToBottom); + { + let row = TableRow { + layout: &mut layout, + widths: widths.clone(), + striped: false, + height, + clicked: false, + }; + header(row); + } } - let ui = layout.done_ui(); Table { ui, @@ -132,8 +134,8 @@ impl<'a> Table<'a> { } } -pub struct TableBody<'b> { - layout: Layout<'b>, +pub struct TableBody<'a> { + layout: Layout<'a>, widths: Vec, striped: bool, odd: bool, @@ -196,7 +198,7 @@ impl<'a> TableBody<'a> { } } - pub fn row<'b>(&'b mut self, height: f32, row: impl FnOnce(TableRow<'a, 'b>)) { + pub fn row(&mut self, height: f32, row: impl FnOnce(TableRow<'_, 'a>)) { row(TableRow { layout: &mut self.layout, widths: self.widths.clone(), @@ -209,14 +211,8 @@ impl<'a> TableBody<'a> { } } -impl<'a> Drop for TableBody<'a> { - fn drop(&mut self) { - self.layout.done(); - } -} - pub struct TableRow<'a, 'b> { - layout: &'b mut Layout<'a>, + layout: &'a mut Layout<'b>, widths: Vec, striped: bool, height: f32,