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,
};
horizontal_grid(grid);
layout.done();
}
}

View file

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

View file

@ -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()
}
}

View file

@ -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,

View file

@ -1,3 +1,4 @@
/// Size hint for table column/grid cell
#[derive(Clone, Debug)]
pub enum Size {
/// 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::{
layout::{CellSize, LineDirection},
sizing::Sizing,
@ -53,23 +56,22 @@ impl<'a> TableBuilder<'a> {
self
}
pub fn header<F>(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<f32>,
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<f32>,
striped: bool,
height: f32,