80 lines
2.3 KiB
Rust
80 lines
2.3 KiB
Rust
use crate::{HasUi, Ui};
|
|
|
|
use super::*;
|
|
use std::marker::Unsize;
|
|
|
|
pub trait StateLike<State> {
|
|
fn as_state(&mut self) -> &mut State;
|
|
}
|
|
|
|
impl StateLike<Ui> for Ui {
|
|
fn as_state(&mut self) -> &mut Ui {
|
|
self
|
|
}
|
|
}
|
|
|
|
pub trait WidgetLike<State: HasUi + StateLike<State>, Tag>: Sized {
|
|
type Widget: Widget + ?Sized + Unsize<dyn Widget>;
|
|
|
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<Self::Widget>;
|
|
|
|
fn with_id<W2>(
|
|
self,
|
|
f: impl FnOnce(&mut State, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
|
|
) -> impl WidgetIdFn<State, W2> {
|
|
move |state| {
|
|
let id = self.add(state);
|
|
f(state, id)
|
|
}
|
|
}
|
|
|
|
fn set_root(self, state: &mut impl StateLike<State>) {
|
|
state.as_state().get_mut().root = Some(self.add(state));
|
|
}
|
|
|
|
fn handles(self, state: &mut impl StateLike<State>) -> WidgetHandles<Self::Widget> {
|
|
self.add(state).handles()
|
|
}
|
|
}
|
|
|
|
pub trait WidgetArrLike<State, const LEN: usize, Tag> {
|
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetArr<LEN>;
|
|
}
|
|
|
|
impl<State, const LEN: usize> WidgetArrLike<State, LEN, ArrTag> for WidgetArr<LEN> {
|
|
fn add(self, _: &mut impl StateLike<State>) -> WidgetArr<LEN> {
|
|
self
|
|
}
|
|
}
|
|
|
|
// variadic generics please save us
|
|
macro_rules! impl_widget_arr {
|
|
($n:expr;$($W:ident)*) => {
|
|
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
|
};
|
|
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
|
impl<State: HasUi + StateLike<State>, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetArr<$n> {
|
|
#[allow(non_snake_case)]
|
|
let ($($W,)*) = self;
|
|
WidgetArr::new(
|
|
[$($W.add(state),)*],
|
|
)
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
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);
|