use std::hash::Hash;
use std::path::PathBuf;
use std::rc::Rc;
use std::time::Duration;
use crabshell_reactive::{Memo, Signal};
use crate::cx::Cx;
use crate::geometry::{Point, Rect, Size};
use crate::image::ImageTransition;
use crate::input::{CursorShape, Drag, DragValue, DropTarget, Handler, Handlers, Wheel};
use crate::keyboard::KeyEvent;
use crate::keyed::{self, KeyedList, KeyedMount};
use crate::prop::{IntoProp, Prop};
use crate::screencopy::ScreencopyProps;
use crate::scroll::ScrollProps;
use crate::style::{Color, Style};
use crate::text_input::InputProps;
pub trait Widget: 'static {
fn into_element(self: Box<Self>) -> Element;
}
pub struct Element {
pub(crate) name: &'static str,
pub(crate) common: Common,
pub(crate) kind: ElementKind,
pub(crate) children: Vec<ElementChild>,
}
pub(crate) enum ElementKind {
Flex(FlexProps),
Grid(GridProps),
Stack,
Spacer,
Text(TextProps),
Icon(IconProps),
Image(ImageProps),
Capture(ScreencopyProps),
Canvas(Box<dyn FnMut(&mut crate::canvas::Pen)>),
Button(Box<ButtonProps>),
Scroll(Box<ScrollProps>),
Input(Box<InputProps>),
}
#[derive(Default)]
pub(crate) struct Common {
pub(crate) style: Option<Prop<Style>>,
pub(crate) visible: Option<Prop<bool>>,
pub(crate) geometry: Option<Signal<Rect>>,
pub(crate) measure: Option<Measure>,
}
pub(crate) type Measure = Rc<dyn Fn(Size)>;
pub(crate) enum Child {
One(Box<dyn Widget>),
Keyed(KeyedList),
}
pub(crate) enum ElementChild {
One(Box<Element>),
Keyed(KeyedMount),
}
fn describe(children: Vec<Child>) -> Vec<ElementChild> {
children
.into_iter()
.map(|child| match child {
Child::One(widget) => ElementChild::One(Box::new(widget.into_element())),
Child::Keyed(list) => ElementChild::Keyed(list.into_mount()),
})
.collect()
}
pub(crate) struct FlexProps {
pub(crate) column: bool,
pub(crate) grow: bool,
pub(crate) gap: Option<Prop<u32>>,
pub(crate) padding_vertical: Option<Prop<u32>>,
pub(crate) padding_horizontal: Option<Prop<u32>>,
pub(crate) align: Option<Prop<Align>>,
pub(crate) justify: Option<Prop<Justify>>,
pub(crate) wrap: Option<Prop<bool>>,
pub(crate) reverse: Option<Prop<bool>>,
pub(crate) column_binding: Option<Prop<bool>>,
}
pub(crate) struct GridProps {
pub(crate) columns: Option<Prop<u32>>,
pub(crate) gap: Option<Prop<u32>>,
pub(crate) row_gap: Option<Prop<u32>>,
pub(crate) column_gap: Option<Prop<u32>>,
pub(crate) padding_vertical: Option<Prop<u32>>,
pub(crate) padding_horizontal: Option<Prop<u32>>,
}
pub(crate) struct TextProps {
pub(crate) text: Prop<String>,
pub(crate) ellipsis: Option<Prop<bool>>,
pub(crate) max_lines: Option<Prop<u32>>,
}
pub(crate) struct IconProps {
pub(crate) source: Prop<IconSource>,
pub(crate) size: Option<Prop<u32>>,
pub(crate) tint: Option<Prop<Option<Color>>>,
}
pub(crate) struct ImageProps {
pub(crate) source: Prop<PathBuf>,
pub(crate) width: Option<Prop<u32>>,
pub(crate) height: Option<Prop<u32>>,
pub(crate) fit: Option<Prop<Fit>>,
pub(crate) tint: Option<Prop<Option<Color>>>,
pub(crate) brightness: Option<Prop<f32>>,
pub(crate) contrast: Option<Prop<f32>>,
pub(crate) saturation: Option<Prop<f32>>,
pub(crate) blur: Option<Prop<f32>>,
pub(crate) playing: Option<Prop<bool>>,
pub(crate) transition: Option<Prop<ImageTransition>>,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct ImageFilters {
pub tint: Option<Color>,
pub brightness: f32,
pub contrast: f32,
pub saturation: f32,
pub blur: f32,
}
impl Default for ImageFilters {
fn default() -> ImageFilters {
ImageFilters {
tint: None,
brightness: 1.0,
contrast: 1.0,
saturation: 1.0,
blur: 0.0,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Fit {
#[default]
Fill,
Contain,
Cover,
}
pub(crate) struct ButtonProps {
pub(crate) hover_style: Option<Prop<Style>>,
pub(crate) pressed_style: Option<Prop<Style>>,
pub(crate) focus_style: Option<Prop<Style>>,
pub(crate) hovered: Option<Signal<bool>>,
pub(crate) position: Option<Signal<Option<Point>>>,
pub(crate) enabled: Option<Prop<bool>>,
pub(crate) focus: Option<Prop<bool>>,
pub(crate) cursor: Option<Prop<CursorShape>>,
pub(crate) drag_data: Option<Prop<Option<DragValue>>>,
pub(crate) drop_hovered: Option<Signal<bool>>,
pub(crate) handlers: Handlers,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Align {
Start,
Center,
End,
#[default]
Stretch,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum Justify {
#[default]
Start,
Center,
End,
SpaceBetween,
SpaceAround,
SpaceEvenly,
}
#[derive(Clone, Debug, PartialEq)]
pub enum IconSource {
Name(String),
File(PathBuf),
Pixels(IconPixels),
}
#[derive(Clone, Debug, PartialEq)]
pub struct IconPixels {
pub width: u32,
pub height: u32,
pub data: Rc<[u8]>,
}
macro_rules! container_methods {
() => {
pub fn child(mut self, child: impl Widget) -> Self {
self.children.push(Child::One(Box::new(child)));
self
}
pub fn children_keyed<T, K, W>(
mut self,
items: impl FnMut() -> Vec<T> + 'static,
key: impl FnMut(&T) -> K + 'static,
view: impl FnMut(&Cx, Memo<T>) -> W + 'static,
) -> Self
where
T: Clone + PartialEq + 'static,
K: Eq + Hash + 'static,
W: Widget,
{
self
.children
.push(Child::Keyed(keyed::mount(items, key, view)));
self
}
pub fn exit(mut self, exit: impl IntoProp<Duration>) -> Self {
let last = self
.children
.iter_mut()
.rev()
.find_map(|child| match child {
Child::Keyed(list) => Some(list),
Child::One(_) => None,
});
match last {
Some(list) => list.exit = Some(exit.into_prop()),
None => tracing::warn!("`exit` was called before any `children_keyed`; it does nothing"),
}
self
}
pub fn displaced(mut self, transition: impl IntoProp<crate::Transition>) -> Self {
let last = self
.children
.iter_mut()
.rev()
.find_map(|child| match child {
Child::Keyed(list) => Some(list),
Child::One(_) => None,
});
match last {
Some(list) => list.displaced = Some(transition.into_prop()),
None => {
tracing::warn!("`displaced` was called before any `children_keyed`; it does nothing")
}
}
self
}
};
}
macro_rules! visibility_methods {
() => {
pub fn visible(mut self, visible: impl $crate::IntoProp<bool>) -> Self {
self.common.visible = Some(visible.into_prop());
self
}
pub fn geometry(mut self, geometry: ::crabshell_reactive::Signal<$crate::Rect>) -> Self {
self.common.geometry = Some(geometry);
self
}
};
}
macro_rules! style_method {
() => {
pub fn style(mut self, style: impl $crate::IntoProp<$crate::Style>) -> Self {
self.common.style = Some(style.into_prop());
self
}
$crate::widgets::visibility_methods!();
};
}
pub(crate) use visibility_methods;
macro_rules! flex_widget {
($name:ident, $column:literal, $doc:literal) => {
#[doc = $doc]
pub struct $name {
flex: FlexProps,
common: Common,
children: Vec<Child>,
}
impl $name {
pub fn new() -> Self {
$name {
flex: FlexProps {
column: $column,
grow: false,
gap: None,
padding_vertical: None,
padding_horizontal: None,
align: None,
justify: None,
wrap: None,
reverse: None,
column_binding: None,
},
common: Common::default(),
children: Vec::new(),
}
}
pub fn gap(mut self, gap: impl IntoProp<u32>) -> Self {
self.flex.gap = Some(gap.into_prop());
self
}
pub fn padding(
mut self,
vertical: impl IntoProp<u32>,
horizontal: impl IntoProp<u32>,
) -> Self {
self.flex.padding_vertical = Some(vertical.into_prop());
self.flex.padding_horizontal = Some(horizontal.into_prop());
self
}
pub fn align(mut self, align: impl IntoProp<Align>) -> Self {
self.flex.align = Some(align.into_prop());
self
}
pub fn justify(mut self, justify: impl IntoProp<Justify>) -> Self {
self.flex.justify = Some(justify.into_prop());
self
}
pub fn wrap(mut self, wrap: impl IntoProp<bool>) -> Self {
self.flex.wrap = Some(wrap.into_prop());
self
}
pub fn reverse(mut self, reverse: impl IntoProp<bool>) -> Self {
self.flex.reverse = Some(reverse.into_prop());
self
}
container_methods!();
style_method!();
}
impl Default for $name {
fn default() -> Self {
Self::new()
}
}
impl Widget for $name {
fn into_element(self: Box<Self>) -> Element {
Element {
name: stringify!($name),
common: self.common,
kind: ElementKind::Flex(self.flex),
children: describe(self.children),
}
}
}
};
}
flex_widget!(
Row,
false,
"Lays its children out left to right, like a CSS flexbox row."
);
flex_widget!(
Column,
true,
"Lays its children out top to bottom, like a CSS flexbox column."
);
pub struct Grid {
grid: GridProps,
common: Common,
children: Vec<Child>,
}
impl Grid {
pub fn new() -> Grid {
Grid {
grid: GridProps {
columns: None,
gap: None,
row_gap: None,
column_gap: None,
padding_vertical: None,
padding_horizontal: None,
},
common: Common::default(),
children: Vec::new(),
}
}
pub fn columns(mut self, columns: impl IntoProp<u32>) -> Grid {
self.grid.columns = Some(columns.into_prop());
self
}
pub fn gap(mut self, gap: impl IntoProp<u32>) -> Grid {
self.grid.gap = Some(gap.into_prop());
self
}
pub fn row_gap(mut self, gap: impl IntoProp<u32>) -> Grid {
self.grid.row_gap = Some(gap.into_prop());
self
}
pub fn column_gap(mut self, gap: impl IntoProp<u32>) -> Grid {
self.grid.column_gap = Some(gap.into_prop());
self
}
pub fn padding(mut self, vertical: impl IntoProp<u32>, horizontal: impl IntoProp<u32>) -> Grid {
self.grid.padding_vertical = Some(vertical.into_prop());
self.grid.padding_horizontal = Some(horizontal.into_prop());
self
}
container_methods!();
style_method!();
}
impl Default for Grid {
fn default() -> Self {
Self::new()
}
}
impl Widget for Grid {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Grid",
common: self.common,
kind: ElementKind::Grid(self.grid),
children: describe(self.children),
}
}
}
pub struct Stack {
common: Common,
children: Vec<Child>,
}
impl Stack {
pub fn new() -> Stack {
Stack {
common: Common::default(),
children: Vec::new(),
}
}
container_methods!();
style_method!();
}
impl Default for Stack {
fn default() -> Self {
Self::new()
}
}
impl Widget for Stack {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Stack",
common: self.common,
kind: ElementKind::Stack,
children: describe(self.children),
}
}
}
pub struct Spacer {
common: Common,
}
impl Spacer {
pub fn new() -> Spacer {
Spacer {
common: Common::default(),
}
}
style_method!();
}
impl Default for Spacer {
fn default() -> Self {
Self::new()
}
}
impl Widget for Spacer {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Spacer",
common: self.common,
kind: ElementKind::Spacer,
children: Vec::new(),
}
}
}
pub struct Text {
text: TextProps,
common: Common,
}
impl Text {
pub fn new(text: impl IntoProp<String>) -> Text {
Text {
text: TextProps {
text: text.into_prop(),
ellipsis: None,
max_lines: None,
},
common: Common::default(),
}
}
pub fn ellipsis(mut self, ellipsis: impl IntoProp<bool>) -> Text {
self.text.ellipsis = Some(ellipsis.into_prop());
self
}
pub fn max_lines(mut self, max_lines: impl IntoProp<u32>) -> Text {
self.text.max_lines = Some(max_lines.into_prop());
self
}
style_method!();
}
impl Widget for Text {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Text",
common: self.common,
kind: ElementKind::Text(self.text),
children: Vec::new(),
}
}
}
pub struct Icon {
icon: IconProps,
common: Common,
}
impl Icon {
pub const DEFAULT_SIZE: u32 = 16;
pub fn new(source: impl IntoProp<IconSource>) -> Icon {
Icon {
icon: IconProps {
source: source.into_prop(),
size: None,
tint: None,
},
common: Common::default(),
}
}
pub fn size(mut self, size: impl IntoProp<u32>) -> Icon {
self.icon.size = Some(size.into_prop());
self
}
pub fn tint(mut self, tint: impl IntoProp<Option<Color>>) -> Icon {
self.icon.tint = Some(tint.into_prop());
self
}
style_method!();
}
pub struct Image {
image: ImageProps,
common: Common,
}
impl Image {
pub const DEFAULT_SIZE: u32 = 64;
pub fn new(source: impl IntoProp<PathBuf>) -> Image {
Image {
image: ImageProps {
source: source.into_prop(),
width: None,
height: None,
fit: None,
tint: None,
brightness: None,
contrast: None,
saturation: None,
blur: None,
playing: None,
transition: None,
},
common: Common::default(),
}
}
pub fn width(mut self, width: impl IntoProp<u32>) -> Image {
self.image.width = Some(width.into_prop());
self
}
pub fn height(mut self, height: impl IntoProp<u32>) -> Image {
self.image.height = Some(height.into_prop());
self
}
pub fn fit(mut self, fit: impl IntoProp<Fit>) -> Image {
self.image.fit = Some(fit.into_prop());
self
}
pub fn tint(mut self, tint: impl IntoProp<Option<Color>>) -> Image {
self.image.tint = Some(tint.into_prop());
self
}
pub fn brightness(mut self, brightness: impl IntoProp<f32>) -> Image {
self.image.brightness = Some(brightness.into_prop());
self
}
pub fn contrast(mut self, contrast: impl IntoProp<f32>) -> Image {
self.image.contrast = Some(contrast.into_prop());
self
}
pub fn saturation(mut self, saturation: impl IntoProp<f32>) -> Image {
self.image.saturation = Some(saturation.into_prop());
self
}
pub fn blur(mut self, blur: impl IntoProp<f32>) -> Image {
self.image.blur = Some(blur.into_prop());
self
}
pub fn playing(mut self, playing: impl IntoProp<bool>) -> Image {
self.image.playing = Some(playing.into_prop());
self
}
pub fn transition(mut self, transition: impl IntoProp<ImageTransition>) -> Image {
self.image.transition = Some(transition.into_prop());
self
}
style_method!();
}
impl Widget for Image {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Image",
common: self.common,
kind: ElementKind::Image(self.image),
children: Vec::new(),
}
}
}
impl Widget for Icon {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Icon",
common: self.common,
kind: ElementKind::Icon(self.icon),
children: Vec::new(),
}
}
}
pub struct Button {
button: ButtonProps,
common: Common,
child: Box<dyn Widget>,
}
impl Button {
pub fn new(child: impl Widget) -> Button {
Button {
child: Box::new(child),
button: ButtonProps {
hover_style: None,
pressed_style: None,
focus_style: None,
hovered: None,
position: None,
enabled: None,
focus: None,
cursor: None,
drag_data: None,
drop_hovered: None,
handlers: Handlers::default(),
},
common: Common::default(),
}
}
pub fn hover_style(mut self, style: impl IntoProp<Style>) -> Button {
self.button.hover_style = Some(style.into_prop());
self
}
pub fn hovered(mut self, hovered: Signal<bool>) -> Button {
self.button.hovered = Some(hovered);
self
}
pub fn pressed_style(mut self, style: impl IntoProp<Style>) -> Button {
self.button.pressed_style = Some(style.into_prop());
self
}
pub fn focus_style(mut self, style: impl IntoProp<Style>) -> Button {
self.button.focus_style = Some(style.into_prop());
self
}
pub fn on_key(mut self, handler: impl Fn(&Cx, &KeyEvent) -> bool + 'static) -> Button {
self.button.handlers.key = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_press(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.press = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_release(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.release = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.click = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_double_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.double_click = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_long_press(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.long_press = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_drag(mut self, handler: impl Fn(&Cx, Drag) + 'static) -> Button {
self.button.handlers.drag = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_right_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.right_click = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_middle_click(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.middle_click = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_back(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.back = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_forward(mut self, handler: impl Fn(&Cx) + 'static) -> Button {
self.button.handlers.forward = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_scroll(mut self, handler: impl Fn(&Cx, i32) + 'static) -> Button {
self.button.handlers.scroll = Some(Rc::new(Handler::new(handler)));
self
}
pub fn on_wheel(mut self, handler: impl Fn(&Cx, Wheel) + 'static) -> Button {
self.button.handlers.wheel = Some(Rc::new(Handler::new(handler)));
self
}
pub fn pointer_position(mut self, position: Signal<Option<Point>>) -> Button {
self.button.position = Some(position);
self
}
pub fn enabled(mut self, enabled: impl IntoProp<bool>) -> Button {
self.button.enabled = Some(enabled.into_prop());
self
}
pub fn focus(mut self, focus: impl IntoProp<bool>) -> Button {
self.button.focus = Some(focus.into_prop());
self
}
pub fn drag_data<T: Clone + 'static>(mut self, data: impl IntoProp<Option<T>>) -> Button {
let data = data
.into_prop()
.map(|data| data.map(|value| Rc::new(value) as DragValue));
self.button.drag_data = Some(data);
self
}
pub fn on_drop<T: Clone + 'static>(mut self, handler: impl Fn(&Cx, T) + 'static) -> Button {
let take = move |cx: &Cx, value: &dyn std::any::Any| {
if let Some(value) = value.downcast_ref::<T>() {
handler(cx, value.clone());
}
};
self.button.handlers.drop = Some(DropTarget {
accepts: std::any::TypeId::of::<T>(),
handler: Rc::new(Handler::new(take)),
});
self
}
pub fn drop_hovered(mut self, hovered: Signal<bool>) -> Button {
self.button.drop_hovered = Some(hovered);
self
}
pub fn cursor(mut self, cursor: impl IntoProp<CursorShape>) -> Button {
self.button.cursor = Some(cursor.into_prop());
self
}
style_method!();
}
impl Widget for Button {
fn into_element(self: Box<Self>) -> Element {
Element {
name: "Button",
common: self.common,
kind: ElementKind::Button(Box::new(self.button)),
children: vec![ElementChild::One(Box::new(self.child.into_element()))],
}
}
}