use crate::{Axis, Len, Painter, Size}; use std::any::Any; mod data; mod handle; mod like; mod tag; mod view; mod widgets; pub use data::*; pub use handle::*; pub use like::*; pub use tag::*; pub use view::*; pub use widgets::*; /// What may be done to a widget's drawing when the box it was given changes /// on this axis, instead of drawing it again. Asked per axis, because wrapped /// text reads the width it is offered and not the height. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub enum OnResize { Scale, Translate, #[default] Redraw, } pub trait Widget: Any { /// Draws the widget, and returns what it used of the box it was given. fn draw(&mut self, painter: &mut Painter) -> Size; /// An exact length the widget can give without a painter or its children. /// Optional, and saves a draw rather than changing one: a hint that /// disagrees with the eventual draw fails a debug assertion. fn size_hint(&self, _axis: Axis) -> Option { None } fn on_resize(&self, _axis: Axis) -> OnResize { OnResize::default() } } impl Widget for () { /// A gap: nothing drawn, at the default length, so a span gives it a share. fn draw(&mut self, _: &mut Painter) -> Size { Size::default() } fn size_hint(&self, _axis: Axis) -> Option { Some(Len::default()) } fn on_resize(&self, _axis: Axis) -> OnResize { OnResize::Scale } } impl dyn Widget { pub fn as_any(&self) -> &dyn Any { self } pub fn as_any_mut(&mut self) -> &mut dyn Any { self } } /// A function that returns a widget given a UI. /// Useful for defining trait functions on widgets that create a parent widget so that the children /// don't need to be IDs yet pub trait WidgetFn: FnOnce(&mut State) -> W {} impl W> WidgetFn for F {} pub struct WidgetArr { pub arr: [StrongWidget; LEN], } impl WidgetArr { pub fn new(arr: [StrongWidget; LEN]) -> Self { Self { arr } } } pub trait WidgetOption { fn get(self, state: &mut State) -> Option; } impl WidgetOption for () { fn get(self, _: &mut State) -> Option { None } } impl Option> WidgetOption for F { fn get(self, state: &mut State) -> Option { self(state) } }