use std::rc::Rc;
use taffy::AlignItems;
use super::{Label, Styled, column, icon_button, on_back, row, text};
use crate::icons;
use crate::text::Font;
use crate::widget::{Element, EventCx};
type Action = Rc<dyn Fn(&mut EventCx)>;
enum Lead {
Back(Action),
Menu(Action),
}
#[doc = example!("page")]
pub fn page(title: impl Into<Element>) -> Page {
Page {
title: title.into(),
lead: None,
body: Vec::new(),
}
}
pub fn heading(s: impl Into<String>) -> Label {
text(s).font(Font::BOLD).size(22.0)
}
pub struct Page {
title: Element,
lead: Option<Lead>,
body: Vec<Element>,
}
impl Page {
pub fn back(mut self, f: impl Fn(&mut EventCx) + 'static) -> Self {
self.lead = Some(Lead::Back(Rc::new(f)));
self
}
pub fn menu(mut self, f: impl Fn(&mut EventCx) + 'static) -> Self {
self.lead = Some(Lead::Menu(Rc::new(f)));
self
}
pub fn child(mut self, e: impl Into<Element>) -> Self {
self.body.push(e.into());
self
}
pub fn children<E: Into<Element>>(mut self, items: impl IntoIterator<Item = E>) -> Self {
self.body.extend(items.into_iter().map(Into::into));
self
}
}
impl From<Page> for Element {
fn from(p: Page) -> Element {
let mut items: Vec<Element> = Vec::new();
let mut head: Vec<Element> = Vec::new();
let lead = match p.lead {
Some(Lead::Back(f)) => {
let g = f.clone();
items.push(on_back(move |cx| g(cx)).into());
Some((icons::back(), f))
}
Some(Lead::Menu(f)) => Some((icons::menu(), f)),
None => None,
};
if let Some((shape, f)) = lead {
head.push(icon_button(&shape).on_click(move |cx| f(cx)).into());
}
head.push(p.title);
items.push(row(head).align(AlignItems::CENTER).gap(4.0).into());
items.extend(p.body);
column(items).gap(12.0).padding(16.0).fill().into()
}
}