add some comments, rework lifetimes of table row

This commit is contained in:
René Rössler 2022-01-07 17:13:37 +01:00
parent e373961e21
commit a944208b19
6 changed files with 34 additions and 40 deletions

View file

@ -51,7 +51,6 @@ impl<'a> HorizontalGridBuilder<'a> {
widths, widths,
}; };
horizontal_grid(grid); horizontal_grid(grid);
layout.done();
} }
} }

View file

@ -51,7 +51,6 @@ impl<'a> VerticalGridBuilder<'a> {
heights, heights,
}; };
vertical_grid(grid); vertical_grid(grid);
layout.done();
} }
} }

View file

@ -15,8 +15,9 @@ pub(crate) enum LineDirection {
TopToBottom, TopToBottom,
} }
pub struct Layout<'a> { /// Positions cells in [LineDirection] and starts a new line on [Layout::end_line]
ui: &'a mut Ui, pub struct Layout<'l> {
ui: &'l mut Ui,
padding: Padding, padding: Padding,
direction: LineDirection, direction: LineDirection,
rect: Rect, rect: Rect,
@ -24,8 +25,8 @@ pub struct Layout<'a> {
max: Pos2, max: Pos2,
} }
impl<'a> Layout<'a> { impl<'l> Layout<'l> {
pub(crate) fn new(ui: &'a mut Ui, padding: Padding, direction: LineDirection) -> Self { pub(crate) fn new(ui: &'l mut Ui, padding: Padding, direction: LineDirection) -> Self {
let mut rect = ui.available_rect_before_wrap(); let mut rect = ui.available_rect_before_wrap();
rect.set_left(rect.left() + padding.outer + padding.inner); rect.set_left(rect.left() + padding.outer + padding.inner);
rect.set_top(rect.top() + 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) { fn set_rect(&mut self) {
let mut rect = self.rect; let mut rect = self.rect;
rect.set_right(self.max.x); rect.set_right(self.max.x);
@ -137,15 +139,6 @@ impl<'a> Layout<'a> {
.allocate_rect(rect, Sense::focusable_noninteractive()); .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)) { 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()); 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) add_contents(&mut child_ui)
} }
} }
impl<'a> Drop for Layout<'a> {
fn drop(&mut self) {
self.set_rect()
}
}

View file

@ -1,5 +1,5 @@
/// Configure padding of grid or table /// 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)] #[derive(Clone, Debug)]
pub struct Padding { pub struct Padding {
pub(crate) inner: f32, pub(crate) inner: f32,

View file

@ -1,3 +1,4 @@
/// Size hint for table column/grid cell
#[derive(Clone, Debug)] #[derive(Clone, Debug)]
pub enum Size { pub enum Size {
/// Absolute size in points /// Absolute size in points

View file

@ -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::{ use crate::{
layout::{CellSize, LineDirection}, layout::{CellSize, LineDirection},
sizing::Sizing, sizing::Sizing,
@ -53,23 +56,22 @@ impl<'a> TableBuilder<'a> {
self self
} }
pub fn header<F>(self, height: f32, header: F) -> Table<'a> pub fn header(self, height: f32, header: impl FnOnce(TableRow<'_, '_>)) -> Table<'a> {
where
F: for<'b> FnOnce(TableRow<'a, 'b>),
{
let widths = self.sizing.into_lengths(); 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 { let mut layout = Layout::new(ui, self.padding.clone(), LineDirection::TopToBottom);
layout: &mut layout, {
widths: widths.clone(), let row = TableRow {
striped: false, layout: &mut layout,
height, widths: widths.clone(),
clicked: false, striped: false,
}; height,
header(row); clicked: false,
};
header(row);
}
} }
let ui = layout.done_ui();
Table { Table {
ui, ui,
@ -132,8 +134,8 @@ impl<'a> Table<'a> {
} }
} }
pub struct TableBody<'b> { pub struct TableBody<'a> {
layout: Layout<'b>, layout: Layout<'a>,
widths: Vec<f32>, widths: Vec<f32>,
striped: bool, striped: bool,
odd: 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 { row(TableRow {
layout: &mut self.layout, layout: &mut self.layout,
widths: self.widths.clone(), 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> { pub struct TableRow<'a, 'b> {
layout: &'b mut Layout<'a>, layout: &'a mut Layout<'b>,
widths: Vec<f32>, widths: Vec<f32>,
striped: bool, striped: bool,
height: f32, height: f32,