use bevy_ecs::prelude::*;
use taffy::LengthPercentage;
use taffy::LengthPercentageAuto;
use taffy::Overflow;
use taffy::Position;
use taffy::style::AlignItems;
use taffy::style::JustifyContent;
use taffy::style_helpers::auto;
use taffy::style_helpers::length;
use crate::color::Color;
use crate::layout::AlignItemsNode;
use crate::layout::BottomNode;
use crate::layout::FlexBasis;
use crate::layout::FlexGrow;
use crate::layout::Height;
use crate::layout::JustifyContentNode;
use crate::layout::LeftNode;
use crate::layout::OverflowNode;
use crate::layout::PaddingNode;
use crate::layout::PositionNode;
use crate::layout::RightNode;
use crate::layout::ScrollNode;
use crate::layout::TopNode;
use crate::layout::Width;
use crate::point::Point;
use crate::style::BackgroundColor;
use crate::widget::Widget;
#[derive(Debug, Clone, Copy)]
pub struct ContainerStyle {
pub position: Position,
pub top: LengthPercentageAuto,
pub right: LengthPercentageAuto,
pub bottom: LengthPercentageAuto,
pub left: LengthPercentageAuto,
pub width: LengthPercentageAuto,
pub height: LengthPercentageAuto,
pub flex_grow: f32,
pub flex_basis: LengthPercentageAuto,
pub align_items: Option<AlignItems>,
pub justify_content: Option<JustifyContent>,
pub overflow: taffy::Point<Overflow>,
pub background_color: Option<Color>,
pub scroll: Point,
pub padding: taffy::Rect<LengthPercentage>,
}
impl Default for ContainerStyle {
fn default() -> Self {
ContainerStyle {
position: Position::Relative,
top: auto(),
right: auto(),
bottom: auto(),
left: auto(),
width: auto(),
height: auto(),
flex_grow: 0.0,
flex_basis: auto(),
align_items: None,
justify_content: None,
overflow: taffy::Point {
x: Overflow::Visible,
y: Overflow::Visible,
},
background_color: None,
scroll: Point { x: 0.0, y: 0.0 },
padding: taffy::Rect::zero(),
}
}
}
#[derive(Bundle)]
pub struct ContainerStyleBundle {
position: PositionNode,
top: TopNode,
right: RightNode,
bottom: BottomNode,
left: LeftNode,
width: Width,
height: Height,
flex_grow: FlexGrow,
flex_basis: FlexBasis,
align_items: AlignItemsNode,
justify_content: JustifyContentNode,
background_color: BackgroundColor,
overflow: OverflowNode,
scroll: ScrollNode,
padding: PaddingNode,
}
impl ContainerStyle {
pub fn position_relative(mut self) -> Self {
self.position = Position::Relative;
self
}
pub fn position_absolute(mut self) -> Self {
self.position = Position::Absolute;
self
}
pub fn width(mut self, width: f32) -> Self {
self.width = length(width);
self
}
pub fn width_auto(mut self) -> Self {
self.width = auto();
self
}
pub fn top(mut self, top: f32) -> Self {
self.top = length(top);
self
}
pub fn top_auto(mut self) -> Self {
self.top = auto();
self
}
pub fn right(mut self, right: f32) -> Self {
self.right = length(right);
self
}
pub fn right_auto(mut self) -> Self {
self.right = auto();
self
}
pub fn bottom(mut self, bottom: f32) -> Self {
self.bottom = length(bottom);
self
}
pub fn bottom_auto(mut self) -> Self {
self.bottom = auto();
self
}
pub fn left(mut self, left: f32) -> Self {
self.left = length(left);
self
}
pub fn left_auto(mut self) -> Self {
self.left = auto();
self
}
pub fn height(mut self, height: f32) -> Self {
self.height = length(height);
self
}
pub fn height_auto(mut self) -> Self {
self.height = auto();
self
}
pub fn flex_grow(mut self, flex_grow: f32) -> Self {
self.flex_grow = flex_grow;
self
}
pub fn flex_basis(mut self, flex_basis: f32) -> Self {
self.flex_basis = length(flex_basis);
self
}
pub fn align_items_start(mut self) -> Self {
self.align_items = Some(AlignItems::Start);
self
}
pub fn align_items_center(mut self) -> Self {
self.align_items = Some(AlignItems::Center);
self
}
pub fn align_items_end(mut self) -> Self {
self.align_items = Some(AlignItems::End);
self
}
pub fn align_items_flex_start(mut self) -> Self {
self.align_items = Some(AlignItems::FlexStart);
self
}
pub fn align_items_flex_end(mut self) -> Self {
self.align_items = Some(AlignItems::FlexEnd);
self
}
pub fn align_items_baseline(mut self) -> Self {
self.align_items = Some(AlignItems::Baseline);
self
}
pub fn align_items_stretch(mut self) -> Self {
self.align_items = Some(AlignItems::Stretch);
self
}
pub fn justify_content_start(mut self) -> Self {
self.justify_content = Some(JustifyContent::Start);
self
}
pub fn justify_content_center(mut self) -> Self {
self.justify_content = Some(JustifyContent::Center);
self
}
pub fn justify_content_end(mut self) -> Self {
self.justify_content = Some(JustifyContent::End);
self
}
pub fn justify_content_flex_start(mut self) -> Self {
self.justify_content = Some(JustifyContent::FlexStart);
self
}
pub fn justify_content_flex_end(mut self) -> Self {
self.justify_content = Some(JustifyContent::FlexEnd);
self
}
pub fn justify_content_stretch(mut self) -> Self {
self.justify_content = Some(JustifyContent::Stretch);
self
}
pub fn justify_content_space_between(mut self) -> Self {
self.justify_content = Some(JustifyContent::SpaceBetween);
self
}
pub fn justify_content_space_around(mut self) -> Self {
self.justify_content = Some(JustifyContent::SpaceAround);
self
}
pub fn justify_content_space_evenly(mut self) -> Self {
self.justify_content = Some(JustifyContent::SpaceEvenly);
self
}
pub fn overflow_x_visible(mut self) -> Self {
self.overflow.x = Overflow::Visible;
self
}
pub fn overflow_x_hidden(mut self) -> Self {
self.overflow.x = Overflow::Hidden;
self
}
pub fn overflow_x_scroll(mut self) -> Self {
self.overflow.x = Overflow::Scroll;
self
}
pub fn overflow_y_visible(mut self) -> Self {
self.overflow.y = Overflow::Visible;
self
}
pub fn overflow_y_hidden(mut self) -> Self {
self.overflow.y = Overflow::Hidden;
self
}
pub fn overflow_y_scroll(mut self) -> Self {
self.overflow.y = Overflow::Scroll;
self
}
pub fn scroll(mut self, scroll: impl Into<Point>) -> Self {
self.scroll = scroll.into();
self
}
pub fn scroll_x(mut self, scroll_x: impl Into<Point>) -> Self {
let scroll_x = scroll_x.into();
self.scroll.x = scroll_x.x;
self
}
pub fn scroll_y(mut self, scroll_y: impl Into<Point>) -> Self {
let scroll_y = scroll_y.into();
self.scroll.y = scroll_y.y;
self
}
pub fn background_color(mut self, color: impl Into<Color>) -> Self {
self.background_color = Some(color.into());
self
}
pub fn padding(mut self, padding: f32) -> Self {
self.padding = taffy::Rect {
left: length(padding),
right: length(padding),
top: length(padding),
bottom: length(padding),
};
self
}
pub fn bundle(&self) -> impl Bundle {
ContainerStyleBundle {
position: PositionNode(self.position),
top: TopNode(self.top),
right: RightNode(self.right),
bottom: BottomNode(self.bottom),
left: LeftNode(self.left),
width: Width(self.width),
height: Height(self.height),
flex_grow: FlexGrow(self.flex_grow),
flex_basis: FlexBasis(self.flex_basis),
align_items: AlignItemsNode(self.align_items),
justify_content: JustifyContentNode(self.justify_content),
background_color: BackgroundColor(self.background_color),
overflow: OverflowNode(self.overflow),
scroll: ScrollNode(self.scroll),
padding: PaddingNode(self.padding),
}
}
}
pub trait ContainerStyleExt<'widget, M: Clone + Send + Sync + 'static>:
Widget<'widget, M> + Sized
{
fn style(&self) -> ContainerStyle;
fn set_style(self, style: ContainerStyle) -> Self {
let _ = style;
self
}
fn position_relative(self) -> Self {
let style = self.style().position_relative();
self.set_style(style)
}
fn position_absolute(self) -> Self {
let style = self.style().position_absolute();
self.set_style(style)
}
fn top(self, top: f32) -> Self {
let style = self.style().top(top);
self.set_style(style)
}
fn top_auto(self) -> Self {
let style = self.style().top_auto();
self.set_style(style)
}
fn right(self, right: f32) -> Self {
let style = self.style().right(right);
self.set_style(style)
}
fn right_auto(self) -> Self {
let style = self.style().right_auto();
self.set_style(style)
}
fn bottom(self, bottom: f32) -> Self {
let style = self.style().bottom(bottom);
self.set_style(style)
}
fn bottom_auto(self) -> Self {
let style = self.style().bottom_auto();
self.set_style(style)
}
fn left(self, left: f32) -> Self {
let style = self.style().left(left);
self.set_style(style)
}
fn left_auto(self) -> Self {
let style = self.style().left_auto();
self.set_style(style)
}
fn width(self, width: f32) -> Self {
let style = self.style().width(width);
self.set_style(style)
}
fn width_auto(self) -> Self {
let style = self.style().width_auto();
self.set_style(style)
}
fn height(self, height: f32) -> Self {
let style = self.style().height(height);
self.set_style(style)
}
fn height_auto(self) -> Self {
let style = self.style().height_auto();
self.set_style(style)
}
fn flex_grow(self, flex_grow: f32) -> Self {
let style = self.style().flex_grow(flex_grow);
self.set_style(style)
}
fn flex_basis(self, flex_basis: f32) -> Self {
let style = self.style().flex_basis(flex_basis);
self.set_style(style)
}
fn align_items_start(self) -> Self {
let style = self.style().align_items_start();
self.set_style(style)
}
fn align_items_center(self) -> Self {
let style = self.style().align_items_center();
self.set_style(style)
}
fn align_items_end(self) -> Self {
let style = self.style().align_items_end();
self.set_style(style)
}
fn align_items_flex_start(self) -> Self {
let style = self.style().align_items_flex_start();
self.set_style(style)
}
fn align_items_flex_end(self) -> Self {
let style = self.style().align_items_flex_end();
self.set_style(style)
}
fn align_items_baseline(self) -> Self {
let style = self.style().align_items_baseline();
self.set_style(style)
}
fn align_items_stretch(self) -> Self {
let style = self.style().align_items_stretch();
self.set_style(style)
}
fn justify_content_start(self) -> Self {
let style = self.style().justify_content_start();
self.set_style(style)
}
fn justify_content_center(self) -> Self {
let style = self.style().justify_content_center();
self.set_style(style)
}
fn justify_content_end(self) -> Self {
let style = self.style().justify_content_end();
self.set_style(style)
}
fn justify_content_flex_start(self) -> Self {
let style = self.style().justify_content_flex_start();
self.set_style(style)
}
fn justify_content_flex_end(self) -> Self {
let style = self.style().justify_content_flex_end();
self.set_style(style)
}
fn justify_content_stretch(self) -> Self {
let style = self.style().justify_content_stretch();
self.set_style(style)
}
fn justify_content_space_between(self) -> Self {
let style = self.style().justify_content_space_between();
self.set_style(style)
}
fn justify_content_space_around(self) -> Self {
let style = self.style().justify_content_space_around();
self.set_style(style)
}
fn justify_content_space_evenly(self) -> Self {
let style = self.style().justify_content_space_evenly();
self.set_style(style)
}
fn overflow_x_visible(self) -> Self {
let style = self.style().overflow_x_visible();
self.set_style(style)
}
fn overflow_x_hidden(self) -> Self {
let style = self.style().overflow_x_hidden();
self.set_style(style)
}
fn overflow_x_scroll(self) -> Self {
let style = self.style().overflow_x_scroll();
self.set_style(style)
}
fn overflow_y_visible(self) -> Self {
let style = self.style().overflow_y_visible();
self.set_style(style)
}
fn overflow_y_hidden(self) -> Self {
let style = self.style().overflow_y_hidden();
self.set_style(style)
}
fn overflow_y_scroll(self) -> Self {
let style = self.style().overflow_y_scroll();
self.set_style(style)
}
fn scroll(self, scroll: impl Into<Point>) -> Self {
let style = self.style().scroll(scroll);
self.set_style(style)
}
fn scroll_x(self, scroll_x: impl Into<Point>) -> Self {
let style = self.style().scroll_x(scroll_x);
self.set_style(style)
}
fn scroll_y(self, scroll_y: impl Into<Point>) -> Self {
let style = self.style().scroll_y(scroll_y);
self.set_style(style)
}
fn background_color(self, color: impl Into<Color>) -> Self {
let style = self.style().background_color(color);
self.set_style(style)
}
fn padding(self, padding: f32) -> Self {
let style = self.style().padding(padding);
self.set_style(style)
}
}