diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index 7d35650..9ef6434 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -4,7 +4,7 @@ use crate::prelude::*; // these methods should "not require any context" (require unit) because they're in core event_ctx!(()); -pub trait CoreWidget { +pub trait CoreWidget { fn pad(self, padding: impl Into) -> impl WidgetFn; fn align(self, align: impl Into) -> impl WidgetFn; fn center(self) -> impl WidgetFn; diff --git a/src/layout/attr.rs b/src/layout/attr.rs index 5165ec0..c10866c 100644 --- a/src/layout/attr.rs +++ b/src/layout/attr.rs @@ -1,11 +1,11 @@ use crate::layout::{Ui, WidgetRef, WidgetIdFn, WidgetLike}; -pub trait WidgetAttr { +pub trait WidgetAttr { type Input; fn run(ui: &mut Ui, id: &WidgetRef, input: Self::Input); } -pub trait Attrable { +pub trait Attrable { fn attr>(self, input: A::Input) -> impl WidgetIdFn; } diff --git a/src/layout/event.rs b/src/layout/event.rs index f6ec760..66dc3ad 100644 --- a/src/layout/event.rs +++ b/src/layout/event.rs @@ -17,7 +17,7 @@ pub struct EventCtx<'a, Ctx, Data> { } pub type ECtx<'a, Ctx, Data, W> = EventIdCtx<'a, Ctx, Data, W>; -pub struct EventIdCtx<'a, Ctx, Data, W> { +pub struct EventIdCtx<'a, Ctx, Data, W: ?Sized> { pub widget: &'a WidgetRef, pub ui: &'a mut Ui, pub state: &'a mut Ctx, @@ -27,7 +27,7 @@ pub struct EventIdCtx<'a, Ctx, Data, W> { pub trait EventFn: Fn(EventCtx) + 'static {} impl) + 'static, Ctx, Data> EventFn for F {} -pub trait WidgetEventFn: Fn(EventIdCtx) + 'static {} +pub trait WidgetEventFn: Fn(EventIdCtx) + 'static {} impl) + 'static, Ctx, Data, W> WidgetEventFn for F {} // TODO: naming in here is a bit weird like eventable @@ -36,10 +36,11 @@ macro_rules! event_ctx { ($ty: ty) => { mod local_event_trait { use super::*; + use std::marker::Sized; #[allow(unused_imports)] use $crate::prelude::*; - pub trait EventableCtx { + pub trait EventableCtx { fn on( self, event: E, @@ -65,7 +66,7 @@ pub use event_ctx; pub mod eventable { use super::*; - pub trait Eventable { + pub trait Eventable { fn on( self, event: E, diff --git a/src/layout/ui.rs b/src/layout/ui.rs index 87666f4..153ec33 100644 --- a/src/layout/ui.rs +++ b/src/layout/ui.rs @@ -42,7 +42,7 @@ impl Ui { self.data.textures.add(image) } - pub fn register_event( + pub fn register_event( &mut self, id: &WidgetRef, event: E, diff --git a/src/layout/widget.rs b/src/layout/widget.rs index 7ead356..008637a 100644 --- a/src/layout/widget.rs +++ b/src/layout/widget.rs @@ -3,7 +3,7 @@ use crate::{ layout::{Len, Painter, SizeCtx, Ui, WidgetIdFn, WidgetRef}, }; -use std::{any::Any, marker::PhantomData}; +use std::{any::Any, marker::Unsize}; pub trait Widget: Any { fn draw(&mut self, painter: &mut Painter); @@ -25,7 +25,7 @@ pub struct WidgetTag; pub struct FnTag; pub trait WidgetLike { - type Widget: Widget + 'static; + type Widget: Widget + ?Sized + Unsize + 'static; fn add(self, ui: &mut Ui) -> WidgetRef; @@ -78,40 +78,27 @@ impl WidgetLike for W { } } -pub struct WidgetArr { +pub struct WidgetArr { pub arr: [WidgetRef; LEN], - _pd: PhantomData, } -impl WidgetArr { +impl WidgetArr { pub fn new(arr: [WidgetRef; LEN]) -> Self { - Self { - arr, - _pd: PhantomData, - } + Self { arr } } } pub struct ArrTag; pub trait WidgetArrLike { - type Ws; - fn ui(self, ui: &mut Ui) -> WidgetArr; + fn ui(self, ui: &mut Ui) -> WidgetArr; } -impl WidgetArrLike for WidgetArr { - type Ws = Ws; - fn ui(self, _: &mut Ui) -> WidgetArr { +impl WidgetArrLike for WidgetArr { + fn ui(self, _: &mut Ui) -> WidgetArr { self } } -impl> WidgetArrLike<1, WidgetTag> for W { - type Ws = (W::Widget,); - fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> { - WidgetArr::new([self.add(ui).any()]) - } -} - // I hate this language it's so bad why do I even use it macro_rules! impl_widget_arr { ($n:expr;$($W:ident)*) => { @@ -119,8 +106,7 @@ macro_rules! impl_widget_arr { }; ($n:expr;$($W:ident)*;$($Tag:ident)*) => { impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) { - type Ws = ($($W::Widget,)*); - fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> { + fn ui(self, ui: &mut Ui) -> WidgetArr<$n> { #[allow(non_snake_case)] let ($($W,)*) = self; WidgetArr::new( diff --git a/src/layout/widget_ref.rs b/src/layout/widget_ref.rs index a812b8b..c788e52 100644 --- a/src/layout/widget_ref.rs +++ b/src/layout/widget_ref.rs @@ -134,20 +134,22 @@ impl Drop for Inner { pub struct IdTag; pub struct IdFnTag; -pub trait WidgetIdFn: FnOnce(&mut Ui) -> WidgetRef {} -impl WidgetRef> WidgetIdFn for F {} +pub trait WidgetIdFn: FnOnce(&mut Ui) -> WidgetRef {} +impl WidgetRef> WidgetIdFn for F {} pub trait WidgetRet: FnOnce(&mut Ui) -> WidgetRef {} impl WidgetRef> WidgetRet for F {} -impl WidgetLike for WidgetRef { +impl + 'static> WidgetLike for WidgetRef { type Widget = W; fn add(self, _: &mut Ui) -> WidgetRef { self } } -impl WidgetRef> WidgetLike for F { +impl + 'static, F: FnOnce(&mut Ui) -> WidgetRef> + WidgetLike for F +{ type Widget = W; fn add(self, ui: &mut Ui) -> WidgetRef { self(ui)