abuse macros..
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
use crate::{HasUi, WidgetIdFn, WidgetLike, WidgetRef};
|
||||
use crate::{HasUi, StateLike, WidgetIdFn, WidgetLike, WidgetRef};
|
||||
|
||||
pub trait WidgetAttr<State, W: ?Sized> {
|
||||
type Input;
|
||||
@@ -9,7 +9,9 @@ pub trait Attrable<State, W: ?Sized, Tag> {
|
||||
fn attr<A: WidgetAttr<State, W>>(self, input: A::Input) -> impl WidgetIdFn<State, W>;
|
||||
}
|
||||
|
||||
impl<State: HasUi, WL: WidgetLike<State, Tag>, Tag> Attrable<State, WL::Widget, Tag> for WL {
|
||||
impl<State: HasUi + StateLike<State>, WL: WidgetLike<State, Tag>, Tag>
|
||||
Attrable<State, WL::Widget, Tag> for WL
|
||||
{
|
||||
fn attr<A: WidgetAttr<State, WL::Widget>>(
|
||||
self,
|
||||
input: A::Input,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{HasEvents, HasState, HasUi, Ui, Widget, WidgetRef};
|
||||
use crate::{HasEvents, HasState, HasUi, StateLike, Ui, Widget, WidgetRef};
|
||||
use std::ops::{Deref, DerefMut};
|
||||
|
||||
pub struct EventCtx<'a, State, Data> {
|
||||
@@ -26,7 +26,7 @@ impl<State: HasUi, Data, W: ?Sized> DerefMut for EventIdCtx<'_, State, Data, W>
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, State: HasUi, Data, W: Widget> EventIdCtx<'a, State, Data, W> {
|
||||
impl<State: HasUi, Data, W: Widget> EventIdCtx<'_, State, Data, W> {
|
||||
pub fn widget(&mut self) -> &mut W {
|
||||
&mut self.state.get_mut()[self.widget]
|
||||
}
|
||||
@@ -57,3 +57,60 @@ impl<State: HasEvents<State = State>, Data, W: Widget> HasEvents
|
||||
self.state.events_mut()
|
||||
}
|
||||
}
|
||||
|
||||
impl<State, Data, W: Widget> StateLike<State> for EventIdCtx<'_, State, Data, W> {
|
||||
fn as_state(&mut self) -> &mut State {
|
||||
self.state
|
||||
}
|
||||
}
|
||||
|
||||
// fn test() {
|
||||
// use crate::*;
|
||||
// struct ClientRsc;
|
||||
// impl<State, Data> HasUi for EventCtx<'_, State, Data> {
|
||||
// fn get(&self) -> &Ui {
|
||||
// todo!()
|
||||
// }
|
||||
//
|
||||
// fn get_mut(&mut self) -> &mut Ui {
|
||||
// todo!()
|
||||
// }
|
||||
// }
|
||||
// fn on(_: impl for<'a> EventFn<ClientRsc, &'a mut i32>) {}
|
||||
//
|
||||
// pub trait WidgetLike<State: HasUi, Tag>: Sized {
|
||||
// type Widget: Widget + ?Sized + std::marker::Unsize<dyn Widget>;
|
||||
//
|
||||
// fn add(self, state: &mut 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 State) {
|
||||
// state.get_mut().root = Some(self.add(state));
|
||||
// }
|
||||
//
|
||||
// fn handles(self, state: &mut State) -> WidgetHandles<Self::Widget> {
|
||||
// self.add(state).handles()
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// pub struct WidgetTag;
|
||||
// impl<State: HasUi, W: Widget> WidgetLike<State, WidgetTag> for W {
|
||||
// type Widget = W;
|
||||
// fn add(self, state: &mut State) -> WidgetHandle<W> {
|
||||
// state.get_mut().add_widget(self)
|
||||
// }
|
||||
// }
|
||||
//
|
||||
// on(move |ctx| {
|
||||
// ().add(ctx);
|
||||
// });
|
||||
// }
|
||||
|
||||
@@ -116,11 +116,12 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
self.map.entry(widget.id()).or_default().push((
|
||||
event,
|
||||
Rc::new(move |ctx| {
|
||||
f(&mut EventIdCtx {
|
||||
let mut test = EventIdCtx {
|
||||
widget,
|
||||
state: ctx.state,
|
||||
data: ctx.data,
|
||||
});
|
||||
};
|
||||
f(&mut test);
|
||||
}),
|
||||
));
|
||||
}
|
||||
@@ -128,7 +129,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
||||
pub fn run_fn<'a>(
|
||||
&mut self,
|
||||
id: impl IdLike,
|
||||
) -> impl for<'b> FnOnce(EventCtx<State, E::Data<'b>>) + 'a {
|
||||
) -> impl for<'b> FnOnce(EventCtx<'_, State, E::Data<'b>>) + 'a {
|
||||
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||
move |ctx| {
|
||||
for (e, f) in fs {
|
||||
|
||||
@@ -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