use crate::layout::{Len, Painter, SizeCtx, Ui}; use std::{any::Any, marker::PhantomData}; mod id; mod like; mod tag; mod widgets; pub use id::*; pub use like::*; pub use tag::*; pub use widgets::*; pub trait Widget: Any { fn draw(&mut self, painter: &mut Painter); fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len; fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len; } impl Widget for () { fn draw(&mut self, _: &mut Painter) {} fn desired_width(&mut self, _: &mut SizeCtx) -> Len { Len::ZERO } fn desired_height(&mut self, _: &mut SizeCtx) -> Len { Len::ZERO } } /// 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 Ui) -> W {} impl W> WidgetFn for F {} pub struct WidgetArr { pub arr: [WidgetId; LEN], _pd: PhantomData, } impl WidgetArr { pub fn new(arr: [WidgetId; LEN]) -> Self { Self { arr, _pd: PhantomData, } } } pub trait WidgetOption { fn get(self, ui: &mut Ui) -> Option; } impl WidgetOption for () { fn get(self, _: &mut Ui) -> Option { None } } impl Option> WidgetOption for F { fn get(self, ui: &mut Ui) -> Option { self(ui) } }