use std::ops::RangeInclusive;
use std::rc::Rc;
use serge_style::{Align, Color, Layout, Length, StyleProp};
use crate::builder::{Build, IntoProp};
use crate::element::{Attr, AttrValue, Cursor, Element, Handler, Kind, Node, Prop, Widget};
use crate::paint::{PaintCtx, Rect};
pub trait Styled: Sized {
fn widget(&mut self) -> &mut Widget;
#[doc(hidden)]
fn __style<T: Clone + 'static>(mut self, v: impl IntoProp<T>, f: fn(T) -> StyleProp) -> Self {
self.widget().style.push(v.into_prop().map(f));
self
}
fn width(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::Width)
}
fn height(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::Height)
}
fn min_width(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::MinWidth)
}
fn max_width(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::MaxWidth)
}
fn padding(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::Padding)
}
fn padding_x(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::PaddingX)
}
fn padding_y(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::PaddingY)
}
fn gap(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::Gap)
}
fn layout(self, v: impl IntoProp<Layout>) -> Self {
self.__style(v, StyleProp::Layout)
}
fn align(self, v: impl IntoProp<Align>) -> Self {
self.__style(v, StyleProp::Align)
}
fn justify(self, v: impl IntoProp<Align>) -> Self {
self.__style(v, StyleProp::Justify)
}
fn background(self, v: impl IntoProp<Color>) -> Self {
self.__style(v, StyleProp::Background)
}
fn color(self, v: impl IntoProp<Color>) -> Self {
self.__style(v, StyleProp::Color)
}
fn radius(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::Radius)
}
fn border_width(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::BorderWidth)
}
fn border_color(self, v: impl IntoProp<Color>) -> Self {
self.__style(v, StyleProp::BorderColor)
}
fn font_size(self, v: impl IntoProp<Length>) -> Self {
self.__style(v, StyleProp::FontSize)
}
fn font_weight(self, v: impl IntoProp<u16>) -> Self {
self.__style(v, StyleProp::FontWeight)
}
fn grow(self, v: impl IntoProp<f32>) -> Self {
self.__style(v, StyleProp::Grow)
}
#[doc(hidden)]
fn __web(mut self, attr: crate::web::WebAttr) -> Self {
self.widget().web.push((attr.0, attr.1));
self
}
#[doc(hidden)]
fn __native(mut self, attr: crate::native::NativeAttr) -> Self {
match attr {
crate::native::NativeAttr::Cursor(c) => self.widget().cursor = Some(c),
}
self
}
}
macro_rules! widgets {
($($(#[$doc:meta])* $name:ident => $kind:ident),* $(,)?) => {$(
$(#[$doc])*
pub struct $name(Widget);
impl $name {
#[doc(hidden)]
pub fn __new() -> Self {
$name(Widget::new(Kind::$kind))
}
}
impl Styled for $name {
fn widget(&mut self) -> &mut Widget {
&mut self.0
}
}
impl crate::builder::Sited for $name {
fn __site(mut self, site: &'static crate::element::Site) -> Self {
self.0.site = Some(site);
self
}
}
impl Build for $name {
const REGION: bool = false;
fn build(self) -> Element {
Element(Node::Widget(Box::new(self.0)))
}
}
)*};
}
widgets! {
View => View,
Stack => Stack,
Card => Card,
Text => Text,
Button => Button,
Slider => Slider,
Input => Input,
Checkbox => Checkbox,
Canvas => Canvas,
ProgressBar => ProgressBar,
Link => Link,
}
macro_rules! containers {
($($name:ident),*) => {$(
impl crate::builder::Children for $name {
fn __child(mut self, child: Element) -> Self {
self.0.children.push(child.0);
self
}
}
)*};
}
containers!(View, Stack, Card);
fn attr<T: Clone + 'static>(w: &mut Widget, a: Attr, v: impl IntoProp<T>, f: fn(T) -> AttrValue) {
w.attrs.push((a, v.into_prop().map(f)));
}
impl Text {
#[doc(hidden)]
pub fn __text(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Text, v, AttrValue::Str);
self
}
}
impl Link {
#[doc(hidden)]
pub fn __text(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Text, v, AttrValue::Str);
self
}
pub fn to(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Href, v, AttrValue::Str);
self
}
}
impl Button {
pub fn text(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Text, v, AttrValue::Str);
self
}
pub fn disabled(mut self, v: impl IntoProp<bool>) -> Self {
attr(&mut self.0, Attr::Disabled, v, AttrValue::Bool);
self
}
pub fn clicked(mut self, f: impl FnOnce() + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::Clicked(crate::builder::reuse0(f)));
self
}
}
impl Slider {
pub fn range(mut self, v: impl IntoProp<RangeInclusive<f32>>) -> Self {
let p = v.into_prop();
let lo = p.clone().map(|r| AttrValue::Num(*r.start()));
let hi = p.map(|r| AttrValue::Num(*r.end()));
self.0.attrs.push((Attr::Min, lo));
self.0.attrs.push((Attr::Max, hi));
self
}
pub fn value(mut self, v: impl IntoProp<f32>) -> Self {
attr(&mut self.0, Attr::Value, v, AttrValue::Num);
self
}
pub fn step(mut self, v: impl IntoProp<f32>) -> Self {
attr(&mut self.0, Attr::Step, v, AttrValue::Num);
self
}
pub fn changed(mut self, f: impl FnOnce(f32) + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::ChangedNum(crate::builder::reuse1(f)));
self
}
}
impl Input {
pub fn value(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::InputText, v, AttrValue::Str);
self
}
pub fn placeholder(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Placeholder, v, AttrValue::Str);
self
}
pub fn changed(mut self, f: impl FnOnce(String) + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::ChangedText(crate::builder::reuse1(f)));
self
}
pub fn submitted(mut self, f: impl FnOnce() + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::Submitted(crate::builder::reuse0(f)));
self
}
}
impl Checkbox {
pub fn text(mut self, v: impl IntoProp<String>) -> Self {
attr(&mut self.0, Attr::Text, v, AttrValue::Str);
self
}
pub fn checked(mut self, v: impl IntoProp<bool>) -> Self {
attr(&mut self.0, Attr::Checked, v, AttrValue::Bool);
self
}
pub fn changed(mut self, f: impl FnOnce(bool) + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::ChangedBool(crate::builder::reuse1(f)));
self
}
}
impl Canvas {
pub fn paint(mut self, f: impl FnOnce(Rect, &mut PaintCtx) + Clone + 'static) -> Self {
self.0
.handlers
.push(Handler::Paint(Rc::new(move |b, ctx| (f.clone())(b, ctx))));
self
}
}
impl ProgressBar {
pub fn value(mut self, v: impl IntoProp<f32>) -> Self {
attr(&mut self.0, Attr::Value, v, AttrValue::Num);
self
}
}
#[allow(non_upper_case_globals)]
pub const v_stack: Layout = Layout::Column;
#[allow(non_upper_case_globals)]
pub const h_stack: Layout = Layout::Row;
#[allow(non_upper_case_globals)]
pub const start: Align = Align::Start;
#[allow(non_upper_case_globals)]
pub const center: Align = Align::Center;
#[allow(non_upper_case_globals)]
pub const end: Align = Align::End;
#[allow(non_upper_case_globals)]
pub const stretch: Align = Align::Stretch;
#[allow(non_upper_case_globals)]
pub const space_between: Align = Align::SpaceBetween;
#[doc(hidden)]
pub type __Prop<T> = Prop<T>;
pub use crate::element::Cursor as CursorKind;
#[doc(hidden)]
pub fn __cursor(c: Cursor) -> Cursor {
c
}