use crate::internal::InternalLower;
use crate::lowering::{InternalIrBuilder, InternalLoweringCx};
use crate::Widget;
use fission_ir::op::{AlignItems, FlexWrap, JustifyContent};
use fission_ir::{FlexDirection, LayoutOp, Op, Semantics, WidgetId};
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Row {
pub id: Option<WidgetId>,
pub children: Vec<Widget>,
pub semantics: Option<Semantics>,
pub flex_grow: f32,
pub flex_shrink: f32,
pub gap: Option<f32>,
pub wrap: FlexWrap,
pub align_items: AlignItems,
pub justify_content: JustifyContent,
}
impl Default for Row {
fn default() -> Self {
Self {
id: None,
children: Vec::new(),
semantics: None,
flex_grow: 0.0,
flex_shrink: 1.0,
gap: None,
wrap: FlexWrap::NoWrap,
align_items: AlignItems::Center,
justify_content: JustifyContent::Start,
}
}
}
impl Row {
pub fn children(mut self, children: Vec<Widget>) -> Self {
self.children = children;
self
}
pub fn flex_grow(mut self, flex_grow: f32) -> Self {
self.flex_grow = flex_grow;
self
}
pub fn gap(mut self, gap: f32) -> Self {
self.gap = Some(gap);
self
}
pub fn align_items(mut self, align: AlignItems) -> Self {
self.align_items = align;
self
}
pub fn justify_content(mut self, justify: JustifyContent) -> Self {
self.justify_content = justify;
self
}
}
impl InternalLower for Row {
fn lower(&self, cx: &mut InternalLoweringCx) -> WidgetId {
let layout_id = self.id.map(Into::into).unwrap_or_else(|| cx.next_node_id());
cx.push_scope(layout_id);
let mut builder = InternalIrBuilder::new(
layout_id,
Op::Layout(LayoutOp::Flex {
direction: FlexDirection::Row,
wrap: self.wrap,
flex_grow: self.flex_grow,
flex_shrink: self.flex_shrink,
padding: [0.0; 4],
gap: self.gap,
align_items: self.align_items,
justify_content: self.justify_content,
}),
);
for child in &self.children {
builder.add_child(child.lower(cx));
}
cx.pop_scope();
let layout_id = builder.build(cx);
if let Some(s) = &self.semantics {
let mut semantics_builder =
InternalIrBuilder::new(cx.next_node_id(), Op::Semantics(s.clone()));
semantics_builder.add_child(layout_id);
return semantics_builder.build(cx);
}
layout_id
}
}