Improve docs and naming

This commit is contained in:
Emil Ernerfeldt 2022-03-31 14:02:35 +02:00
parent 153e2cf54f
commit d72cfe5359
4 changed files with 108 additions and 108 deletions

View file

@ -70,7 +70,7 @@ impl<'a> DatePickerPopup<'a> {
.size(Size::Absolute(height))
.vertical(|mut strip| {
if self.combo_boxes {
strip.strip_noclip(|builder| {
strip.strip_clip(|builder| {
builder.sizes(Size::Remainder, 3).horizontal(|mut strip| {
strip.cell(|ui| {
ComboBox::from_id_source("date_picker_year")

View file

@ -15,7 +15,7 @@ pub(crate) enum CellSize {
/// In a horizontal strip, a `[StripLayout]` with horizontal `[CellDirection]` is used.
/// Its cells go from left to right inside this `[StripLayout]`.
///
/// In a table there's a `[StripLayout]` for each table row with a horizonal `[CellDirection]`.
/// In a table there's a `[StripLayout]` for each table row with a horizontal `[CellDirection]`.
/// Its cells go from left to right. And the lines go from top to bottom.
pub(crate) enum CellDirection {
/// Cells go from left to right
@ -151,8 +151,8 @@ impl<'l> StripLayout<'l> {
add_contents(&mut child_ui);
}
/// Set the rect so that the scrollview knows about our size
pub fn set_rect(&mut self) -> Response {
/// Allocate the rect in [`Self::ui`] so that the scrollview knows about our size
pub fn allocate_rect(&mut self) -> Response {
let mut rect = self.rect;
rect.set_right(self.max.x);
rect.set_bottom(self.max.y);

View file

@ -6,13 +6,8 @@ use crate::{
use egui::{Response, Ui};
/// Builder for creating a new [`Strip`].
pub struct StripBuilder<'a> {
ui: &'a mut Ui,
sizing: Sizing,
}
impl<'a> StripBuilder<'a> {
/// Create new strip builder.
///
/// This can be used to do dynamic layouts.
///
/// In contrast to normal egui behavior, strip cells do *not* grow with its children!
///
@ -42,19 +37,26 @@ impl<'a> StripBuilder<'a> {
/// });
/// # });
/// ```
pub struct StripBuilder<'a> {
ui: &'a mut Ui,
sizing: Sizing,
}
impl<'a> StripBuilder<'a> {
/// Create new strip builder.
pub fn new(ui: &'a mut Ui) -> Self {
let sizing = Sizing::new();
Self { ui, sizing }
}
/// Add size hint for column/row
/// Add size hint for one column/row.
pub fn size(mut self, size: Size) -> Self {
self.sizing.add(size);
self
}
/// Add size hint for columns/rows `count` times
/// Add size hint for several columns/rows at once.
pub fn sizes(mut self, size: Size, count: usize) -> Self {
for _ in 0..count {
self.sizing.add(size);
@ -80,7 +82,7 @@ impl<'a> StripBuilder<'a> {
direction: CellDirection::Horizontal,
sizes: widths,
});
layout.set_rect()
layout.allocate_rect()
}
/// Build vertical strip: Cells are positions from top to bottom.
@ -101,7 +103,7 @@ impl<'a> StripBuilder<'a> {
direction: CellDirection::Vertical,
sizes: heights,
});
layout.set_rect()
layout.allocate_rect()
}
}
@ -138,7 +140,7 @@ impl<'a, 'b> Strip<'a, 'b> {
self.layout.empty(width, height);
}
fn _cell(&mut self, clip: bool, add_contents: impl FnOnce(&mut Ui)) {
fn cell_impl(&mut self, clip: bool, add_contents: impl FnOnce(&mut Ui)) {
assert!(
!self.sizes.is_empty(),
"Tried using more strip cells then available."
@ -150,27 +152,28 @@ impl<'a, 'b> Strip<'a, 'b> {
/// Add cell, content is wrapped
pub fn cell(&mut self, add_contents: impl FnOnce(&mut Ui)) {
self._cell(false, add_contents);
self.cell_impl(false, add_contents);
}
/// Add cell, content is clipped
pub fn cell_clip(&mut self, add_contents: impl FnOnce(&mut Ui)) {
self._cell(true, add_contents);
self.cell_impl(true, add_contents);
}
fn _strip(&mut self, clip: bool, strip_builder: impl FnOnce(StripBuilder<'_>)) {
self._cell(clip, |ui| {
fn strip_impl(&mut self, clip: bool, strip_builder: impl FnOnce(StripBuilder<'_>)) {
self.cell_impl(clip, |ui| {
strip_builder(StripBuilder::new(ui));
});
}
/// Add strip as cell
pub fn strip(&mut self, strip_builder: impl FnOnce(StripBuilder<'_>)) {
self._strip(false, strip_builder);
self.strip_impl(false, strip_builder);
}
/// Add strip as cell, content is clipped
pub fn strip_noclip(&mut self, strip_builder: impl FnOnce(StripBuilder<'_>)) {
self._strip(true, strip_builder);
pub fn strip_clip(&mut self, strip_builder: impl FnOnce(StripBuilder<'_>)) {
self.strip_impl(true, strip_builder);
}
}

View file

@ -12,16 +12,7 @@ use crate::{
use egui::{Response, Ui};
use std::cmp;
/// Builder for creating a new [`Table`].
pub struct TableBuilder<'a> {
ui: &'a mut Ui,
sizing: Sizing,
scroll: bool,
striped: bool,
}
impl<'a> TableBuilder<'a> {
/// Build a table with (optional) fixed header and scrolling body.
/// Builder for a [`Table`] with (optional) fixed header and scrolling body.
///
/// Cell widths are precalculated with given size hints so we can have tables like this:
///
@ -57,6 +48,14 @@ impl<'a> TableBuilder<'a> {
/// });
/// # });
/// ```
pub struct TableBuilder<'a> {
ui: &'a mut Ui,
sizing: Sizing,
scroll: bool,
striped: bool,
}
impl<'a> TableBuilder<'a> {
pub fn new(ui: &'a mut Ui) -> Self {
let sizing = Sizing::new();
@ -86,7 +85,7 @@ impl<'a> TableBuilder<'a> {
self
}
/// Add size hint for column `count` times
/// Add size hint for several columns at once.
pub fn columns(mut self, size: Size, count: usize) -> Self {
for _ in 0..count {
self.sizing.add(size);
@ -109,17 +108,14 @@ impl<'a> TableBuilder<'a> {
let ui = self.ui;
{
let mut layout = StripLayout::new(ui, CellDirection::Horizontal);
{
let row = TableRow {
header(TableRow {
layout: &mut layout,
widths: widths.clone(),
striped: false,
height,
clicked: false,
};
header(row);
}
layout.set_rect();
});
layout.allocate_rect();
}
Table {
@ -151,7 +147,8 @@ impl<'a> TableBuilder<'a> {
}
/// Table struct which can construct a [`TableBody`].
/// Is created by [`TableBuilder`] by either calling `body` or after creating a header row with `header`.
///
/// Is created by [`TableBuilder`] by either calling [`TableBuilder::body`] or after creating a header row with [`TableBuilder::header`].
pub struct Table<'a> {
ui: &'a mut Ui,
widths: Vec<f32>,
@ -266,7 +263,7 @@ impl<'a> TableBody<'a> {
impl<'a> Drop for TableBody<'a> {
fn drop(&mut self) {
self.layout.set_rect();
self.layout.allocate_rect();
}
}
@ -286,7 +283,17 @@ impl<'a, 'b> TableRow<'a, 'b> {
self.clicked
}
fn _col(&mut self, clip: bool, add_contents: impl FnOnce(&mut Ui)) -> Response {
/// Add column, content is wrapped
pub fn col(&mut self, add_contents: impl FnOnce(&mut Ui)) -> Response {
self.column(false, add_contents)
}
/// Add column, content is clipped
pub fn col_clip(&mut self, add_contents: impl FnOnce(&mut Ui)) -> Response {
self.column(true, add_contents)
}
fn column(&mut self, clip: bool, add_contents: impl FnOnce(&mut Ui)) -> Response {
assert!(
!self.widths.is_empty(),
"Tried using more table columns then available."
@ -309,16 +316,6 @@ impl<'a, 'b> TableRow<'a, 'b> {
response
}
/// Add column, content is wrapped
pub fn col(&mut self, add_contents: impl FnOnce(&mut Ui)) -> Response {
self._col(false, add_contents)
}
/// Add column, content is clipped
pub fn col_clip(&mut self, add_contents: impl FnOnce(&mut Ui)) -> Response {
self._col(true, add_contents)
}
}
impl<'a, 'b> Drop for TableRow<'a, 'b> {