From d72cfe535926cf5992e19373611c3de9a080b423 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Thu, 31 Mar 2022 14:02:35 +0200 Subject: [PATCH] Improve docs and naming --- egui_extras/src/datepicker/popup.rs | 2 +- egui_extras/src/layout.rs | 8 +- egui_extras/src/strip.rs | 85 +++++++++---------- egui_extras/src/table.rs | 121 ++++++++++++++-------------- 4 files changed, 108 insertions(+), 108 deletions(-) diff --git a/egui_extras/src/datepicker/popup.rs b/egui_extras/src/datepicker/popup.rs index 86ed2a15..74cb8b8e 100644 --- a/egui_extras/src/datepicker/popup.rs +++ b/egui_extras/src/datepicker/popup.rs @@ -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") diff --git a/egui_extras/src/layout.rs b/egui_extras/src/layout.rs index 8b933d07..47952aa7 100644 --- a/egui_extras/src/layout.rs +++ b/egui_extras/src/layout.rs @@ -15,12 +15,12 @@ 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 Horizontal, - /// Cells go fromtop to bottom + /// Cells go from top to bottom Vertical, } @@ -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); diff --git a/egui_extras/src/strip.rs b/egui_extras/src/strip.rs index b05cb51b..fa946275 100644 --- a/egui_extras/src/strip.rs +++ b/egui_extras/src/strip.rs @@ -6,6 +6,37 @@ use crate::{ use egui::{Response, Ui}; /// Builder for creating a new [`Strip`]. +/// +/// This can be used to do dynamic layouts. +/// +/// In contrast to normal egui behavior, strip cells do *not* grow with its children! +/// +/// After adding size hints with `[Self::column]`/`[Self::columns]` the strip can be build with `[Self::horizontal]`/`[Self::vertical]`. +/// +/// ### Example +/// ``` +/// # egui::__run_test_ui(|ui| { +/// use egui_extras::{StripBuilder, Size}; +/// StripBuilder::new(ui) +/// .size(Size::RemainderMinimum(100.0)) +/// .size(Size::Absolute(40.0)) +/// .vertical(|mut strip| { +/// strip.strip(|builder| { +/// builder.sizes(Size::Remainder, 2).horizontal(|mut strip| { +/// strip.cell(|ui| { +/// ui.label("Top Left"); +/// }); +/// strip.cell(|ui| { +/// ui.label("Top Right"); +/// }); +/// }); +/// }); +/// strip.cell(|ui| { +/// ui.label("Fixed"); +/// }); +/// }); +/// # }); +/// ``` pub struct StripBuilder<'a> { ui: &'a mut Ui, sizing: Sizing, @@ -13,48 +44,19 @@ pub struct StripBuilder<'a> { impl<'a> StripBuilder<'a> { /// Create new strip builder. - /// - /// In contrast to normal egui behavior, strip cells do *not* grow with its children! - /// - /// After adding size hints with `[Self::column]`/`[Self::columns]` the strip can be build with `[Self::horizontal]`/`[Self::vertical]`. - /// - /// ### Example - /// ``` - /// # egui::__run_test_ui(|ui| { - /// use egui_extras::{StripBuilder, Size}; - /// StripBuilder::new(ui) - /// .size(Size::RemainderMinimum(100.0)) - /// .size(Size::Absolute(40.0)) - /// .vertical(|mut strip| { - /// strip.strip(|builder| { - /// builder.sizes(Size::Remainder, 2).horizontal(|mut strip| { - /// strip.cell(|ui| { - /// ui.label("Top Left"); - /// }); - /// strip.cell(|ui| { - /// ui.label("Top Right"); - /// }); - /// }); - /// }); - /// strip.cell(|ui| { - /// ui.label("Fixed"); - /// }); - /// }); - /// # }); - /// ``` 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); } } diff --git a/egui_extras/src/table.rs b/egui_extras/src/table.rs index d3706264..b4ab8a32 100644 --- a/egui_extras/src/table.rs +++ b/egui_extras/src/table.rs @@ -12,7 +12,42 @@ use crate::{ use egui::{Response, Ui}; use std::cmp; -/// Builder for creating a new [`Table`]. +/// 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: +/// +/// | fixed size | all available space/minimum | 30% of available width | fixed size | +/// +/// In contrast to normal egui behavior, columns/rows do *not* grow with its children! +/// Takes all available height, so if you want something below the table, put it in a strip. +/// +/// ### Example +/// ``` +/// # egui::__run_test_ui(|ui| { +/// use egui_extras::{TableBuilder, Size}; +/// TableBuilder::new(ui) +/// .column(Size::RemainderMinimum(100.0)) +/// .column(Size::Absolute(40.0)) +/// .header(20.0, |mut header| { +/// header.col(|ui| { +/// ui.heading("Growing"); +/// }); +/// header.col(|ui| { +/// ui.heading("Fixed"); +/// }); +/// }) +/// .body(|mut body| { +/// body.row(30.0, |mut row| { +/// row.col(|ui| { +/// ui.label("first row growing cell"); +/// }); +/// row.col_clip(|ui| { +/// ui.button("action"); +/// }); +/// }); +/// }); +/// # }); +/// ``` pub struct TableBuilder<'a> { ui: &'a mut Ui, sizing: Sizing, @@ -21,42 +56,6 @@ pub struct TableBuilder<'a> { } impl<'a> TableBuilder<'a> { - /// Build a table 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 | - /// - /// In contrast to normal egui behavior, columns/rows do *not* grow with its children! - /// Takes all available height, so if you want something below the table, put it in a strip. - /// - /// ### Example - /// ``` - /// # egui::__run_test_ui(|ui| { - /// use egui_extras::{TableBuilder, Size}; - /// TableBuilder::new(ui) - /// .column(Size::RemainderMinimum(100.0)) - /// .column(Size::Absolute(40.0)) - /// .header(20.0, |mut header| { - /// header.col(|ui| { - /// ui.heading("Growing"); - /// }); - /// header.col(|ui| { - /// ui.heading("Fixed"); - /// }); - /// }) - /// .body(|mut body| { - /// body.row(30.0, |mut row| { - /// row.col(|ui| { - /// ui.label("first row growing cell"); - /// }); - /// row.col_clip(|ui| { - /// ui.button("action"); - /// }); - /// }); - /// }); - /// # }); - /// ``` 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 { - layout: &mut layout, - widths: widths.clone(), - striped: false, - height, - clicked: false, - }; - header(row); - } - layout.set_rect(); + header(TableRow { + layout: &mut layout, + widths: widths.clone(), + striped: false, + height, + clicked: false, + }); + 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, @@ -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> {