use std::f32::INFINITY;
use egui::{vec2, Align, Id, InnerResponse, Layout, Margin, Ui};
use super::Container;
pub struct Row {
pub id: Option<Id>,
pub valign: Align,
pub padding: Margin,
pub right_to_left: Option<bool>,
pub wrapping: bool,
pub max_height: f32,
pub min_height: f32,
}
impl Row {
#[inline]
pub fn new(valign: Align) -> Self {
Self {
id: None,
valign,
padding: Margin::ZERO,
right_to_left: None,
wrapping: false,
max_height: INFINITY,
min_height: 0.0,
}
}
#[inline]
pub fn id(mut self, id: Id) -> Self {
self.id = Some(id);
self
}
#[inline]
pub fn valign(mut self, align: Align) -> Self {
self.valign = align;
self
}
#[inline]
pub fn padding(mut self, padding: impl Into<Margin>) -> Self {
self.padding = padding.into();
self
}
#[inline]
pub fn right_to_left(mut self, right_to_left: bool) -> Self {
self.right_to_left = Some(right_to_left);
self
}
#[inline]
pub fn wrapping(mut self, wrapping: bool) -> Self {
self.wrapping = wrapping;
self
}
#[inline]
pub fn max_height(mut self, max_height: f32) -> Self {
self.max_height = max_height;
self
}
#[inline]
pub fn min_height(mut self, min_height: f32) -> Self {
self.min_height = min_height;
self
}
}
impl Default for Row {
fn default() -> Self {
Self::new(Align::Min)
}
}
impl Row {
pub fn show<R>(
&self,
ui: &mut Ui,
add_contents: impl FnOnce(&mut Ui) -> R,
) -> InnerResponse<R> {
let Self {
id,
valign,
padding,
max_height,
min_height,
..
} = *self;
let right_to_left = self
.right_to_left
.unwrap_or(ui.layout().prefer_right_to_left());
let valign = if self.wrapping { Align::Min } else { valign };
let layout = if right_to_left {
Layout::right_to_left(valign)
} else {
Layout::left_to_right(valign)
}
.with_main_wrap(self.wrapping);
Container {
id,
layout,
padding,
max_size: vec2(INFINITY, max_height),
min_size: vec2(0.0, min_height),
}
.show(ui, add_contents)
}
}
#[inline]
pub fn row(ui: &mut Ui, valign: Align, add_contents: impl FnOnce(&mut Ui)) -> InnerResponse<()> {
Row::new(valign).show(ui, add_contents)
}