2022-03-31 19:13:25 +00:00
|
|
|
use crate::{
|
|
|
|
layout::{CellDirection, CellSize, StripLayout},
|
|
|
|
sizing::Sizing,
|
|
|
|
Size,
|
|
|
|
};
|
|
|
|
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!
|
|
|
|
///
|
2022-04-11 07:54:44 +00:00
|
|
|
/// First use [`Self::size`] and [`Self::sizes`] to allocate space for the rows or columns will follow.
|
2022-05-23 15:25:31 +00:00
|
|
|
/// Then build the strip with [`Self::horizontal`]/[`Self::vertical`], and add 'cells'
|
2022-04-11 07:54:44 +00:00
|
|
|
/// to it using [`Strip::cell`]. The number of cells MUST match the number of pre-allocated sizes.
|
2022-03-31 19:13:25 +00:00
|
|
|
///
|
|
|
|
/// ### Example
|
|
|
|
/// ```
|
|
|
|
/// # egui::__run_test_ui(|ui| {
|
|
|
|
/// use egui_extras::{StripBuilder, Size};
|
|
|
|
/// StripBuilder::new(ui)
|
2022-04-11 07:54:44 +00:00
|
|
|
/// .size(Size::remainder().at_least(100.0)) // top cell
|
|
|
|
/// .size(Size::exact(40.0)) // bottom cell
|
2022-03-31 19:13:25 +00:00
|
|
|
/// .vertical(|mut strip| {
|
2022-04-11 07:54:44 +00:00
|
|
|
/// // Add the top 'cell'
|
|
|
|
/// strip.cell(|ui| {
|
|
|
|
/// ui.label("Fixed");
|
|
|
|
/// });
|
|
|
|
/// // We add a nested strip in the bottom cell:
|
2022-03-31 19:13:25 +00:00
|
|
|
/// strip.strip(|builder| {
|
2022-04-01 10:01:00 +00:00
|
|
|
/// builder.sizes(Size::remainder(), 2).horizontal(|mut strip| {
|
2022-03-31 19:13:25 +00:00
|
|
|
/// strip.cell(|ui| {
|
|
|
|
/// ui.label("Top Left");
|
|
|
|
/// });
|
|
|
|
/// strip.cell(|ui| {
|
|
|
|
/// ui.label("Top Right");
|
|
|
|
/// });
|
|
|
|
/// });
|
|
|
|
/// });
|
|
|
|
/// });
|
|
|
|
/// # });
|
|
|
|
/// ```
|
|
|
|
pub struct StripBuilder<'a> {
|
|
|
|
ui: &'a mut Ui,
|
|
|
|
sizing: Sizing,
|
2022-04-01 13:27:42 +00:00
|
|
|
clip: bool,
|
2022-04-11 08:29:34 +00:00
|
|
|
cell_layout: egui::Layout,
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StripBuilder<'a> {
|
|
|
|
/// Create new strip builder.
|
|
|
|
pub fn new(ui: &'a mut Ui) -> Self {
|
2022-04-11 08:29:34 +00:00
|
|
|
let cell_layout = *ui.layout();
|
2022-04-01 13:27:42 +00:00
|
|
|
Self {
|
|
|
|
ui,
|
2022-04-11 07:54:44 +00:00
|
|
|
sizing: Default::default(),
|
2022-04-11 08:29:34 +00:00
|
|
|
cell_layout,
|
2022-11-30 18:56:06 +00:00
|
|
|
clip: false,
|
2022-04-01 13:27:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-30 18:56:06 +00:00
|
|
|
/// Should we clip the contents of each cell? Default: `false`.
|
2022-04-01 13:27:42 +00:00
|
|
|
pub fn clip(mut self, clip: bool) -> Self {
|
|
|
|
self.clip = clip;
|
|
|
|
self
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 08:29:34 +00:00
|
|
|
/// What layout should we use for the individual cells?
|
|
|
|
pub fn cell_layout(mut self, cell_layout: egui::Layout) -> Self {
|
|
|
|
self.cell_layout = cell_layout;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:54:44 +00:00
|
|
|
/// Allocate space for for one column/row.
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn size(mut self, size: Size) -> Self {
|
|
|
|
self.sizing.add(size);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2022-04-11 07:54:44 +00:00
|
|
|
/// Allocate space for for several columns/rows at once.
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn sizes(mut self, size: Size, count: usize) -> Self {
|
|
|
|
for _ in 0..count {
|
|
|
|
self.sizing.add(size);
|
|
|
|
}
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build horizontal strip: Cells are positions from left to right.
|
|
|
|
/// Takes the available horizontal width, so there can't be anything right of the strip or the container will grow slowly!
|
|
|
|
///
|
2022-05-23 15:25:31 +00:00
|
|
|
/// Returns a [`egui::Response`] for hover events.
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn horizontal<F>(self, strip: F) -> Response
|
|
|
|
where
|
|
|
|
F: for<'b> FnOnce(Strip<'a, 'b>),
|
|
|
|
{
|
2022-04-01 10:01:00 +00:00
|
|
|
let widths = self.sizing.to_lengths(
|
2022-04-11 08:19:07 +00:00
|
|
|
self.ui.available_rect_before_wrap().width(),
|
2022-03-31 19:13:25 +00:00
|
|
|
self.ui.spacing().item_spacing.x,
|
|
|
|
);
|
2022-11-30 18:56:06 +00:00
|
|
|
let mut layout = StripLayout::new(self.ui, CellDirection::Horizontal, self.cell_layout);
|
2022-03-31 19:13:25 +00:00
|
|
|
strip(Strip {
|
|
|
|
layout: &mut layout,
|
|
|
|
direction: CellDirection::Horizontal,
|
2022-11-30 18:56:06 +00:00
|
|
|
clip: self.clip,
|
2022-04-11 12:27:32 +00:00
|
|
|
sizes: widths,
|
|
|
|
size_index: 0,
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
|
|
|
layout.allocate_rect()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Build vertical strip: Cells are positions from top to bottom.
|
|
|
|
/// Takes the full available vertical height, so there can't be anything below of the strip or the container will grow slowly!
|
|
|
|
///
|
2022-05-23 15:25:31 +00:00
|
|
|
/// Returns a [`egui::Response`] for hover events.
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn vertical<F>(self, strip: F) -> Response
|
|
|
|
where
|
|
|
|
F: for<'b> FnOnce(Strip<'a, 'b>),
|
|
|
|
{
|
2022-04-01 10:01:00 +00:00
|
|
|
let heights = self.sizing.to_lengths(
|
2022-04-11 08:19:07 +00:00
|
|
|
self.ui.available_rect_before_wrap().height(),
|
2022-03-31 19:13:25 +00:00
|
|
|
self.ui.spacing().item_spacing.y,
|
|
|
|
);
|
2022-11-30 18:56:06 +00:00
|
|
|
let mut layout = StripLayout::new(self.ui, CellDirection::Vertical, self.cell_layout);
|
2022-03-31 19:13:25 +00:00
|
|
|
strip(Strip {
|
|
|
|
layout: &mut layout,
|
|
|
|
direction: CellDirection::Vertical,
|
2022-11-30 18:56:06 +00:00
|
|
|
clip: self.clip,
|
2022-04-11 12:27:32 +00:00
|
|
|
sizes: heights,
|
|
|
|
size_index: 0,
|
2022-03-31 19:13:25 +00:00
|
|
|
});
|
|
|
|
layout.allocate_rect()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A Strip of cells which go in one direction. Each cell has a fixed size.
|
|
|
|
/// In contrast to normal egui behavior, strip cells do *not* grow with its children!
|
|
|
|
pub struct Strip<'a, 'b> {
|
|
|
|
layout: &'b mut StripLayout<'a>,
|
|
|
|
direction: CellDirection,
|
2022-11-30 18:56:06 +00:00
|
|
|
clip: bool,
|
2022-04-11 12:27:32 +00:00
|
|
|
sizes: Vec<f32>,
|
|
|
|
size_index: usize,
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Strip<'a, 'b> {
|
2023-01-07 19:27:19 +00:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2022-03-31 19:13:25 +00:00
|
|
|
fn next_cell_size(&mut self) -> (CellSize, CellSize) {
|
2022-04-11 12:27:32 +00:00
|
|
|
let size = if let Some(size) = self.sizes.get(self.size_index) {
|
|
|
|
self.size_index += 1;
|
|
|
|
*size
|
2022-04-11 07:54:44 +00:00
|
|
|
} else {
|
2022-04-11 12:27:32 +00:00
|
|
|
crate::log_or_panic!(
|
|
|
|
"Added more `Strip` cells than were pre-allocated ({} pre-allocated)",
|
|
|
|
self.sizes.len()
|
|
|
|
);
|
|
|
|
8.0 // anything will look wrong, so pick something that is obviously wrong
|
2022-04-11 07:54:44 +00:00
|
|
|
};
|
2022-03-31 19:13:25 +00:00
|
|
|
|
|
|
|
match self.direction {
|
|
|
|
CellDirection::Horizontal => (CellSize::Absolute(size), CellSize::Remainder),
|
|
|
|
CellDirection::Vertical => (CellSize::Remainder, CellSize::Absolute(size)),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-01 13:27:42 +00:00
|
|
|
/// Add cell contents.
|
2023-01-07 19:27:19 +00:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn cell(&mut self, add_contents: impl FnOnce(&mut Ui)) {
|
2022-04-01 13:27:42 +00:00
|
|
|
let (width, height) = self.next_cell_size();
|
2022-11-30 18:56:06 +00:00
|
|
|
let striped = false;
|
|
|
|
self.layout
|
|
|
|
.add(self.clip, striped, width, height, add_contents);
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
|
2022-04-11 07:54:44 +00:00
|
|
|
/// Add an empty cell.
|
2023-01-07 19:27:19 +00:00
|
|
|
#[cfg_attr(debug_assertions, track_caller)]
|
2022-04-11 07:54:44 +00:00
|
|
|
pub fn empty(&mut self) {
|
|
|
|
let (width, height) = self.next_cell_size();
|
|
|
|
self.layout.empty(width, height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Add a strip as cell.
|
2022-03-31 19:13:25 +00:00
|
|
|
pub fn strip(&mut self, strip_builder: impl FnOnce(StripBuilder<'_>)) {
|
2022-11-30 18:56:06 +00:00
|
|
|
let clip = self.clip;
|
2022-04-01 13:27:42 +00:00
|
|
|
self.cell(|ui| {
|
|
|
|
strip_builder(StripBuilder::new(ui).clip(clip));
|
|
|
|
});
|
2022-03-31 19:13:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'b> Drop for Strip<'a, 'b> {
|
|
|
|
fn drop(&mut self) {
|
2022-04-11 12:27:32 +00:00
|
|
|
while self.size_index < self.sizes.len() {
|
2022-03-31 19:13:25 +00:00
|
|
|
self.empty();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|