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> {
|
pub trait WidgetAttr<State, W: ?Sized> {
|
||||||
type Input;
|
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>;
|
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>>(
|
fn attr<A: WidgetAttr<State, WL::Widget>>(
|
||||||
self,
|
self,
|
||||||
input: A::Input,
|
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};
|
use std::ops::{Deref, DerefMut};
|
||||||
|
|
||||||
pub struct EventCtx<'a, State, Data> {
|
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 {
|
pub fn widget(&mut self) -> &mut W {
|
||||||
&mut self.state.get_mut()[self.widget]
|
&mut self.state.get_mut()[self.widget]
|
||||||
}
|
}
|
||||||
@@ -57,3 +57,60 @@ impl<State: HasEvents<State = State>, Data, W: Widget> HasEvents
|
|||||||
self.state.events_mut()
|
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((
|
self.map.entry(widget.id()).or_default().push((
|
||||||
event,
|
event,
|
||||||
Rc::new(move |ctx| {
|
Rc::new(move |ctx| {
|
||||||
f(&mut EventIdCtx {
|
let mut test = EventIdCtx {
|
||||||
widget,
|
widget,
|
||||||
state: ctx.state,
|
state: ctx.state,
|
||||||
data: ctx.data,
|
data: ctx.data,
|
||||||
});
|
};
|
||||||
|
f(&mut test);
|
||||||
}),
|
}),
|
||||||
));
|
));
|
||||||
}
|
}
|
||||||
@@ -128,7 +129,7 @@ impl<State: 'static, E: Event> TypeEventManager<State, E> {
|
|||||||
pub fn run_fn<'a>(
|
pub fn run_fn<'a>(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: impl IdLike,
|
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();
|
let fs = self.map.get(&id.id()).cloned().unwrap_or_default();
|
||||||
move |ctx| {
|
move |ctx| {
|
||||||
for (e, f) in fs {
|
for (e, f) in fs {
|
||||||
|
|||||||
@@ -1,12 +1,22 @@
|
|||||||
use crate::HasUi;
|
use crate::{HasUi, Ui};
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use std::marker::Unsize;
|
use std::marker::Unsize;
|
||||||
|
|
||||||
pub trait WidgetLike<State: 'static + HasUi, Tag>: Sized {
|
pub trait StateLike<State> {
|
||||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
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>(
|
fn with_id<W2>(
|
||||||
self,
|
self,
|
||||||
@@ -18,21 +28,21 @@ pub trait WidgetLike<State: 'static + HasUi, Tag>: Sized {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn set_root(self, state: &mut State) {
|
fn set_root(self, state: &mut impl StateLike<State>) {
|
||||||
state.get_mut().root = Some(self.add(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()
|
self.add(state).handles()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait WidgetArrLike<State, const LEN: usize, Tag> {
|
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> {
|
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
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -43,8 +53,8 @@ macro_rules! impl_widget_arr {
|
|||||||
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
|
||||||
};
|
};
|
||||||
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
|
||||||
impl<State: 'static + HasUi, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
impl<State: HasUi + StateLike<State>, $($W: WidgetLike<State, $Tag>,$Tag,)*> WidgetArrLike<State, $n, ($($Tag,)*)> for ($($W,)*) {
|
||||||
fn add(self, state: &mut State) -> WidgetArr<$n> {
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetArr<$n> {
|
||||||
#[allow(non_snake_case)]
|
#[allow(non_snake_case)]
|
||||||
let ($($W,)*) = self;
|
let ($($W,)*) = self;
|
||||||
WidgetArr::new(
|
WidgetArr::new(
|
||||||
|
|||||||
@@ -1,46 +1,46 @@
|
|||||||
|
use super::*;
|
||||||
|
use crate::HasUi;
|
||||||
use std::marker::Unsize;
|
use std::marker::Unsize;
|
||||||
|
|
||||||
use crate::HasUi;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
pub struct ArrTag;
|
|
||||||
|
|
||||||
pub struct WidgetTag;
|
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;
|
type Widget = W;
|
||||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||||
state.get_mut().add_widget(self)
|
state.as_state().get_mut().add_widget(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct FnTag;
|
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;
|
type Widget = W;
|
||||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||||
self(state).add(state)
|
self(state.as_state()).add(state)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IdTag;
|
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>
|
WidgetLike<State, IdTag> for WidgetHandle<W>
|
||||||
{
|
{
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, _: &mut State) -> WidgetHandle<W> {
|
fn add(self, _: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct IdFnTag;
|
pub struct IdFnTag;
|
||||||
impl<
|
impl<
|
||||||
State: HasUi + 'static,
|
State: HasUi + StateLike<State>,
|
||||||
W: ?Sized + Widget + Unsize<dyn Widget> + 'static,
|
W: ?Sized + Widget + Unsize<dyn Widget>,
|
||||||
F: FnOnce(&mut State) -> WidgetHandle<W>,
|
F: FnOnce(&mut State) -> WidgetHandle<W>,
|
||||||
> WidgetLike<State, IdFnTag> for F
|
> WidgetLike<State, IdFnTag> for F
|
||||||
{
|
{
|
||||||
type Widget = W;
|
type Widget = W;
|
||||||
fn add(self, state: &mut State) -> WidgetHandle<W> {
|
fn add(self, state: &mut impl StateLike<State>) -> WidgetHandle<W> {
|
||||||
self(state)
|
self(state.as_state())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ArrTag;
|
||||||
|
|||||||
@@ -155,6 +155,18 @@ pub fn derive_ui_state(input: TokenStream) -> TokenStream {
|
|||||||
impl HasState for #rscname {
|
impl HasState for #rscname {
|
||||||
type State = #sname;
|
type State = #sname;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl StateLike<#sname> for #sname {
|
||||||
|
fn as_state(&mut self) -> &mut Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl StateLike<#rscname> for #rscname {
|
||||||
|
fn as_state(&mut self) -> &mut Self {
|
||||||
|
self
|
||||||
|
}
|
||||||
|
}
|
||||||
});
|
});
|
||||||
output.into()
|
output.into()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -64,7 +64,7 @@ impl DefaultAppState for Client {
|
|||||||
.on(CursorSense::click(), move |ctx| {
|
.on(CursorSense::click(), move |ctx| {
|
||||||
let child = image(include_bytes!("assets/sungals.png"))
|
let child = image(include_bytes!("assets/sungals.png"))
|
||||||
.center()
|
.center()
|
||||||
.add(ctx.state);
|
.add(ctx);
|
||||||
(span_add.r)(ctx).children.push(child);
|
(span_add.r)(ctx).children.push(child);
|
||||||
})
|
})
|
||||||
.sized((150, 150))
|
.sized((150, 150))
|
||||||
@@ -119,9 +119,7 @@ impl DefaultAppState for Client {
|
|||||||
.text_align(Align::LEFT)
|
.text_align(Align::LEFT)
|
||||||
.wrap(true)
|
.wrap(true)
|
||||||
.attr::<Selectable>(());
|
.attr::<Selectable>(());
|
||||||
let msg_box = text
|
let msg_box = text.background(rect(Color::WHITE.darker(0.5))).add(ctx);
|
||||||
.background(rect(Color::WHITE.darker(0.5)))
|
|
||||||
.add(ctx.state);
|
|
||||||
(texts.r)(ctx).children.push(msg_box);
|
(texts.r)(ctx).children.push(msg_box);
|
||||||
})
|
})
|
||||||
.handles(&mut rsc);
|
.handles(&mut rsc);
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::prelude::*;
|
|||||||
pub mod eventable {
|
pub mod eventable {
|
||||||
use super::*;
|
use super::*;
|
||||||
widget_trait! {
|
widget_trait! {
|
||||||
pub trait Eventable<State: HasEvents + 'static>;
|
pub trait Eventable<State: HasEvents + StateLike<State> + 'static>;
|
||||||
fn on<E: EventLike>(
|
fn on<E: EventLike>(
|
||||||
self,
|
self,
|
||||||
event: E,
|
event: E,
|
||||||
|
|||||||
@@ -151,7 +151,7 @@ pub struct SpanBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Ta
|
|||||||
_pd: PhantomData<(State, Tag)>,
|
_pd: PhantomData<(State, Tag)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
|
impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
|
||||||
for SpanBuilder<State, LEN, Wa, Tag>
|
for SpanBuilder<State, LEN, Wa, Tag>
|
||||||
{
|
{
|
||||||
type Output = Span;
|
type Output = Span;
|
||||||
|
|||||||
@@ -48,7 +48,7 @@ pub struct StackBuilder<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, T
|
|||||||
_pd: PhantomData<(State, Tag)>,
|
_pd: PhantomData<(State, Tag)>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
|
impl<State: StateLike<State>, const LEN: usize, Wa: WidgetArrLike<State, LEN, Tag>, Tag> FnOnce<(&mut State,)>
|
||||||
for StackBuilder<State, LEN, Wa, Tag>
|
for StackBuilder<State, LEN, Wa, Tag>
|
||||||
{
|
{
|
||||||
type Output = Stack;
|
type Output = Stack;
|
||||||
|
|||||||
@@ -51,7 +51,7 @@ impl<State, O, H: WidgetOption<State>> TextBuilder<State, O, H> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<State: HasUi + 'static, O> TextBuilder<State, O> {
|
impl<State: HasUi + StateLike<State>, O> TextBuilder<State, O> {
|
||||||
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
|
pub fn hint<W: WidgetLike<State, Tag>, Tag>(
|
||||||
self,
|
self,
|
||||||
hint: W,
|
hint: W,
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ use crate::prelude::*;
|
|||||||
|
|
||||||
// these methods should "not require any context" (require unit) because they're in core
|
// these methods should "not require any context" (require unit) because they're in core
|
||||||
widget_trait! {
|
widget_trait! {
|
||||||
pub trait CoreWidget<State: HasUi + 'static>;
|
pub trait CoreWidget<State: HasUi + StateLike<State> + 'static>;
|
||||||
|
|
||||||
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|
fn pad(self, padding: impl Into<Padding>) -> impl WidgetFn<State, Pad> {
|
||||||
|state| Pad {
|
|state| Pad {
|
||||||
|
|||||||
Reference in New Issue
Block a user