use std::f32::INFINITY;
use egui::{vec2, Align, Id, InnerResponse, Layout, Margin, Ui};
use super::Container;
pub struct Column {
pub id: Option<Id>,
pub halign: Align,
pub padding: Margin,
pub bottom_up: bool,
pub max_width: f32,
pub min_width: f32,
}
impl Column {
#[inline]
pub fn new(halign: Align) -> Self {
Self {
id: None,
halign,
padding: Margin::ZERO,
bottom_up: false,
max_width: INFINITY,
min_width: 0.0,
}
}
#[inline]
pub fn id(mut self, id: Id) -> Self {
self.id = Some(id);
self
}
#[inline]
pub fn halign(mut self, align: Align) -> Self {
self.halign = align;
self
}
#[inline]
pub fn padding(mut self, padding: impl Into<Margin>) -> Self {
self.padding = padding.into();
self
}
#[inline]
pub fn bottom_up(mut self, bottom_up: bool) -> Self {
self.bottom_up = bottom_up;
self
}
#[inline]
pub fn width(mut self, width: f32) -> Self {
self.min_width = width;
self.max_width = width;
self
}
#[inline]
pub fn max_width(mut self, width: f32) -> Self {
self.max_width = width;
self
}
#[inline]
pub fn min_width(mut self, width: f32) -> Self {
self.min_width = width;
self
}
}
impl Default for Column {
fn default() -> Self {
Self::new(Align::Min)
}
}
impl Column {
pub fn show<R>(
&self,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
let Self {
id,
halign,
padding,
max_width,
min_width,
..
} = *self;
let layout = if self.bottom_up {
Layout::bottom_up(halign)
} else {
Layout::top_down(halign)
};
Container {
id,
layout,
padding,
max_size: vec2(max_width, INFINITY),
min_size: vec2(min_width, 0.0),
}
.show(ui, add_contents)
}
}
#[inline]
pub fn column<R>(
ui: &mut Ui,
halign: Align,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
Column::new(halign).show(ui, add_contents)
}