use serde_json::{json, Value};
use super::{children_refs, Component, IntoBox, Role};
use crate::app::RowBuilder;
pub struct Row {
pub(crate) id: String,
pub(crate) gap: f64,
pub(crate) wrap: bool,
pub(crate) align: Option<String>,
pub(crate) justify: Option<String>,
pub(crate) children: Vec<Box<dyn Component>>,
}
impl Row {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
gap: 16.0,
wrap: true,
align: None,
justify: None,
children: Vec::new(),
}
}
pub fn gap(mut self, g: f64) -> Self {
self.gap = g;
self
}
pub fn wrap(mut self, w: bool) -> Self {
self.wrap = w;
self
}
pub fn align(mut self, a: impl Into<String>) -> Self {
self.align = Some(a.into());
self
}
pub fn justify(mut self, j: impl Into<String>) -> Self {
self.justify = Some(j.into());
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
}
impl Component for Row {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"row"
}
fn props(&self) -> Value {
let mut obj = serde_json::Map::new();
obj.insert("gap".into(), json!(self.gap));
obj.insert("wrap".into(), json!(self.wrap));
if let Some(ref a) = self.align {
obj.insert("align".into(), json!(a));
}
if let Some(ref j) = self.justify {
obj.insert("justify".into(), json!(j));
}
Value::Object(obj)
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct Column {
pub(crate) id: String,
pub(crate) gap: f64,
pub(crate) align: Option<String>,
pub(crate) justify: Option<String>,
pub(crate) children: Vec<Box<dyn Component>>,
}
impl Column {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
gap: 16.0,
align: None,
justify: None,
children: Vec::new(),
}
}
pub fn gap(mut self, g: f64) -> Self {
self.gap = g;
self
}
pub fn align(mut self, a: impl Into<String>) -> Self {
self.align = Some(a.into());
self
}
pub fn justify(mut self, j: impl Into<String>) -> Self {
self.justify = Some(j.into());
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
pub(crate) fn push(&mut self, c: Box<dyn Component>) {
self.children.push(c);
}
}
impl Component for Column {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"column"
}
fn props(&self) -> Value {
let mut obj = serde_json::Map::new();
obj.insert("gap".into(), json!(self.gap));
if let Some(ref a) = self.align {
obj.insert("align".into(), json!(a));
}
if let Some(ref j) = self.justify {
obj.insert("justify".into(), json!(j));
}
Value::Object(obj)
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct Grid {
pub(crate) id: String,
pub(crate) columns: usize,
pub(crate) gap: f64,
pub(crate) gap_x: Option<f64>,
pub(crate) gap_y: Option<f64>,
pub(crate) children: Vec<Box<dyn Component>>,
}
impl Grid {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
columns: 2,
gap: 16.0,
gap_x: None,
gap_y: None,
children: Vec::new(),
}
}
pub fn columns(mut self, cols: usize) -> Self {
self.columns = cols.max(1);
self
}
pub fn gap(mut self, g: f64) -> Self {
self.gap = g;
self
}
pub fn gap_x(mut self, gx: f64) -> Self {
self.gap_x = Some(gx);
self
}
pub fn gap_y(mut self, gy: f64) -> Self {
self.gap_y = Some(gy);
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
}
impl Component for Grid {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"grid"
}
fn props(&self) -> Value {
let mut obj = serde_json::Map::new();
obj.insert("columns".into(), json!(self.columns));
obj.insert("gap".into(), json!(self.gap));
if let Some(gx) = self.gap_x {
obj.insert("gap_x".into(), json!(gx));
}
if let Some(gy) = self.gap_y {
obj.insert("gap_y".into(), json!(gy));
}
Value::Object(obj)
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct Panel {
pub(crate) id: String,
pub(crate) label: String,
pub(crate) gap: f64,
pub(crate) children: Vec<Box<dyn Component>>,
}
impl Panel {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
label: String::new(),
gap: 16.0,
children: Vec::new(),
}
}
pub fn label(mut self, l: impl Into<String>) -> Self {
self.label = l.into();
self
}
pub fn gap(mut self, g: f64) -> Self {
self.gap = g;
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
}
impl Component for Panel {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"panel"
}
fn props(&self) -> Value {
json!({ "label": self.label, "gap": self.gap })
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct SectionBuilder {
pub(crate) children: Vec<Box<dyn Component>>,
pub(crate) gap: f64,
}
impl Default for SectionBuilder {
fn default() -> Self {
Self {
children: Vec::new(),
gap: 14.0,
}
}
}
impl SectionBuilder {
pub fn item(&mut self, c: impl IntoBox) {
self.children.push(c.into_box());
}
pub fn row(&mut self, task: impl FnOnce(&mut RowBuilder)) {
let mut b = RowBuilder::default();
task(&mut b);
let mut r = Row::new(format!("row-{}", self.children.len())).gap(b.gap);
for c in b.children {
r.children.push(c);
}
self.children.push(Box::new(r));
}
pub fn column(&mut self, task: impl FnOnce(&mut RowBuilder)) {
let mut b = RowBuilder::default();
task(&mut b);
let mut col = Column::new(format!("col-{}", self.children.len())).gap(b.gap);
for c in b.children {
col.children.push(c);
}
self.children.push(Box::new(col));
}
pub fn grid(&mut self, columns: usize, task: impl FnOnce(&mut RowBuilder)) {
let mut b = RowBuilder::default();
task(&mut b);
let mut g = Grid::new(format!("grid-{}", self.children.len()))
.columns(columns)
.gap(b.gap);
for c in b.children {
g.children.push(c);
}
self.children.push(Box::new(g));
}
pub fn panel(&mut self, label: impl Into<String>, task: impl FnOnce(&mut RowBuilder)) {
let label = label.into();
let mut b = RowBuilder::default();
task(&mut b);
let mut p = Panel::new(format!("panel-{}", label))
.label(label)
.gap(b.gap);
for c in b.children {
p.children.push(c);
}
self.children.push(Box::new(p));
}
pub fn gap(&mut self, g: f64) {
self.gap = g;
}
}
pub struct Tabs {
id: String,
labels: Vec<String>,
children: Vec<Box<dyn Component>>,
selected: usize,
}
impl Tabs {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
labels: Vec::new(),
children: Vec::new(),
selected: 0,
}
}
pub fn selected(mut self, idx: usize) -> Self {
self.selected = idx;
self
}
pub fn tab(mut self, label: impl Into<String>, task: impl FnOnce(&mut SectionBuilder)) -> Self {
let mut b = SectionBuilder::default();
task(&mut b);
let mut col = Column::new(format!("{}-{}", self.id, self.labels.len())).gap(b.gap);
for c in b.children {
col.push(c);
}
self.labels.push(label.into());
self.children.push(Box::new(col));
self
}
}
impl Component for Tabs {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"tabs"
}
fn props(&self) -> Value {
json!({ "labels": self.labels, "selected": self.selected })
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct Accordion {
id: String,
labels: Vec<String>,
children: Vec<Box<dyn Component>>,
open: bool,
}
impl Accordion {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
labels: Vec::new(),
children: Vec::new(),
open: false,
}
}
pub fn open(mut self, open: bool) -> Self {
self.open = open;
self
}
pub fn section(
mut self,
label: impl Into<String>,
task: impl FnOnce(&mut SectionBuilder),
) -> Self {
let mut b = SectionBuilder::default();
task(&mut b);
let mut col = Column::new(format!("{}-{}", self.id, self.labels.len())).gap(b.gap);
for c in b.children {
col.push(c);
}
self.labels.push(label.into());
self.children.push(Box::new(col));
self
}
}
impl Component for Accordion {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"accordion"
}
fn props(&self) -> Value {
json!({ "labels": self.labels, "open": self.open })
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct Drawer {
pub(crate) id: String,
pub(crate) title: String,
pub(crate) placement: String,
pub(crate) size: u32,
pub(crate) open: bool,
pub(crate) backdrop: bool,
pub(crate) gap: f64,
pub(crate) children: Vec<Box<dyn Component>>,
}
impl Drawer {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
title: String::new(),
placement: "right".into(),
size: 380,
open: false,
backdrop: true,
gap: 16.0,
children: Vec::new(),
}
}
pub fn title(mut self, t: impl Into<String>) -> Self {
self.title = t.into();
self
}
pub fn placement(mut self, p: impl Into<String>) -> Self {
self.placement = p.into();
self
}
pub fn size(mut self, px: u32) -> Self {
self.size = px;
self
}
pub fn open(mut self, on: bool) -> Self {
self.open = on;
self
}
pub fn backdrop(mut self, on: bool) -> Self {
self.backdrop = on;
self
}
pub fn gap(mut self, g: f64) -> Self {
self.gap = g;
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
pub fn content(mut self, task: impl FnOnce(&mut SectionBuilder)) -> Self {
let mut b = SectionBuilder::default();
task(&mut b);
for c in b.children {
self.children.push(c);
}
self
}
}
impl Component for Drawer {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"drawer"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"title": self.title,
"placement": self.placement,
"size": self.size,
"open": self.open,
"backdrop": self.backdrop,
"gap": self.gap,
})
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}
pub struct DynamicContainer {
id: String,
children: Vec<Box<dyn Component>>,
direction: String,
gap: f32,
}
impl DynamicContainer {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
children: Vec::new(),
direction: "column".into(),
gap: 12.0,
}
}
pub fn gap(mut self, g: f32) -> Self {
self.gap = g;
self
}
pub fn direction(mut self, d: impl Into<String>) -> Self {
self.direction = d.into();
self
}
pub fn item(mut self, c: impl IntoBox) -> Self {
self.children.push(c.into_box());
self
}
pub fn content(mut self, task: impl FnOnce(&mut SectionBuilder)) -> Self {
let mut b = SectionBuilder::default();
task(&mut b);
for c in b.children {
self.children.push(c);
}
self
}
}
impl Component for DynamicContainer {
fn id(&self) -> &str {
&self.id
}
fn kind(&self) -> &'static str {
"dynamic_container"
}
fn role(&self) -> Role {
Role::Output
}
fn props(&self) -> Value {
json!({
"direction": self.direction,
"gap": self.gap,
})
}
fn children(&self) -> Vec<&dyn Component> {
children_refs(&self.children)
}
}