use crate::{Painter, Ui, WidgetId, WidgetIdFnRet}; use std::{any::Any, marker::PhantomData}; pub trait Widget: Any { fn draw(&self, painter: Painter); } pub struct WidgetTag; pub struct FnTag; pub trait WidgetLike { type Widget: 'static; fn add(self, ui: &mut Ui) -> WidgetId; fn with_id( self, f: impl FnOnce(&mut Ui, WidgetId) -> WidgetId, ) -> WidgetIdFnRet!(W2, Ctx) where Self: Sized, { move |ui| { let id = self.add(ui); f(ui, id) } } } // pub trait WidgetFn, Ctx> = FnOnce(&mut Ui) -> W; /// 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 /// currently a macro for rust analyzer (doesn't support trait aliases atm) macro_rules! WidgetFnRet { ($W:ty, $Ctx:ty) => { impl FnOnce(&mut $crate::Ui<$Ctx>) -> $W }; } pub(crate) use WidgetFnRet; impl, Ctx, F: FnOnce(&mut Ui) -> W> WidgetLike for F { type Widget = W; fn add(self, ui: &mut Ui) -> WidgetId { self(ui).add(ui) } } impl, Ctx> WidgetLike for W { type Widget = W; fn add(self, ui: &mut Ui) -> WidgetId { ui.add_widget(self) } } pub struct WidgetArr { pub arr: [WidgetId; LEN], _pd: PhantomData, } impl WidgetArr { pub fn new(arr: [WidgetId; LEN]) -> Self { Self { arr, _pd: PhantomData, } } } pub struct ArrTag; pub trait WidgetArrLike { type Ws; fn ui(self, ui: &mut Ui) -> WidgetArr; } impl WidgetArrLike for WidgetArr { type Ws = Ws; fn ui(self, _: &mut Ui) -> WidgetArr { self } } impl, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W { type Ws = (W::Widget,); fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> { WidgetArr::new([self.add(ui).erase_type()]) } } // I hate this language it's so bad why do I even use it macro_rules! impl_widget_arr { ($n:expr;$($W:ident)*) => { impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*); }; ($n:expr;$($W:ident)*;$($Tag:ident)*) => { impl<$($W: WidgetLike,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) { type Ws = ($($W::Widget,)*); fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> { #[allow(non_snake_case)] let ($($W,)*) = self; WidgetArr::new( [$($W.add(ui).cast_type(),)*], ) } } }; } impl_widget_arr!(1;A); impl_widget_arr!(2;A B); impl_widget_arr!(3;A B C); impl_widget_arr!(4;A B C D); impl_widget_arr!(5;A B C D E); impl_widget_arr!(6;A B C D E F); impl_widget_arr!(7;A B C D E F G); impl_widget_arr!(8;A B C D E F G H); impl_widget_arr!(9;A B C D E F G H I); impl_widget_arr!(10;A B C D E F G H I J); impl_widget_arr!(11;A B C D E F G H I J K); impl_widget_arr!(12;A B C D E F G H I J K L);