abuse macros..
This commit is contained in:
@@ -1,12 +1,22 @@
|
||||
use crate::HasUi;
|
||||
use crate::{HasUi, Ui};
|
||||
|
||||
use super::*;
|
||||
use std::marker::Unsize;
|
||||
|
||||
pub trait WidgetLike<State: 'static + HasUi, Tag>: Sized {
|
||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
||||
pub trait StateLike<State> {
|
||||
fn as_state(&mut self) -> &mut State;
|
||||
}
|
||||
|
||||
fn add(self, state: &mut State) -> WidgetHandle<Self::Widget>;
|
||||
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,
|
||||
@@ -18,21 +28,21 @@ pub trait WidgetLike<State: 'static + HasUi, Tag>: Sized {
|
||||
}
|
||||
}
|
||||
|
||||
fn set_root(self, state: &mut State) {
|
||||
state.get_mut().root = Some(self.add(state));
|
||||
fn set_root(self, state: &mut impl StateLike<State>) {
|
||||
state.as_state().get_mut().root = Some(self.add(state));
|
||||
}
|
||||
|
||||
fn handles(self, state: &mut State) -> WidgetHandles<Self::Widget> {
|
||||
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 State) -> WidgetArr<LEN>;
|
||||
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 State) -> WidgetArr<LEN> {
|
||||
fn add(self, _: &mut impl StateLike<State>) -> WidgetArr<LEN> {
|
||||
self
|
||||
}
|
||||
}
|
||||
@@ -43,8 +53,8 @@ macro_rules! impl_widget_arr {
|
||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||
};
|
||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||
impl<State: 'static + HasUi, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
||||
fn add(self, state: &mut State) -> WidgetArr<$n> {
|
||||
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(
|
||||
|
||||
@@ -1,46 +1,46 @@
|
||||
use super::*;
|
||||
use crate::HasUi;
|
||||
use std::marker::Unsize;
|
||||
|
||||
use crate::HasUi;
|
||||
|
||||
use super::*;
|
||||
|
||||
pub struct ArrTag;
|
||||
|
||||
pub struct WidgetTag;
|
||||
impl<State: HasUi + 'static, W: Widget> WidgetLike<State, WidgetTag> for W {
|
||||
impl<State: HasUi + StateLike<State>, W: Widget> WidgetLike<State, WidgetTag> for W {
|
||||
type Widget = W;
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
state.get_mut().add_widget(self)
|
||||
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||
state.as_state().get_mut().add_widget(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct FnTag;
|
||||
impl<State: HasUi + 'static, W: Widget, F: FnOnce(&mut State) -> W> WidgetLike<State, FnTag> for F {
|
||||
impl<State: HasUi + StateLike<State>, W: Widget, F: FnOnce(&mut State) -> W>
|
||||
WidgetLike<State, FnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
self(state).add(state)
|
||||
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||
self(state.as_state()).add(state)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdTag;
|
||||
impl<State: HasUi + 'static, W: ?Sized + Widget + Unsize<dyn Widget> + 'static>
|
||||
impl<State: HasUi + StateLike<State>, W: ?Sized + Widget + Unsize<dyn Widget>>
|
||||
WidgetLike<State, IdTag> for WidgetHandle<W>
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut State) -> WidgetHandle<W> {
|
||||
fn add(self, _: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
pub struct IdFnTag;
|
||||
impl<
|
||||
State: HasUi + 'static,
|
||||
W: ?Sized + Widget + Unsize<dyn Widget> + 'static,
|
||||
State: HasUi + StateLike<State>,
|
||||
W: ?Sized + Widget + Unsize<dyn Widget>,
|
||||
F: FnOnce(&mut State) -> WidgetHandle<W>,
|
||||
> WidgetLike<State, IdFnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
self(state)
|
||||
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||
self(state.as_state())
|
||||
}
|
||||
}
|
||||
|
||||
pub struct ArrTag;
|
||||
|
||||
Reference in New Issue
Block a user