From d4690401eb1c1cfab7081c65c0a7b9ee254b43b7 Mon Sep 17 00:00:00 2001 From: Shadow Cat Date: Fri, 29 Aug 2025 00:11:41 -0400 Subject: [PATCH] rename widget fn macros --- src/core/image.rs | 2 +- src/core/sense.rs | 12 ++++++------ src/core/trait_fns.rs | 32 ++++++++++++++++---------------- src/layout/id.rs | 11 ++++------- src/layout/widget.rs | 8 ++++---- src/lib.rs | 1 + 6 files changed, 32 insertions(+), 34 deletions(-) diff --git a/src/core/image.rs b/src/core/image.rs index 124d04e..2c4f5c3 100644 --- a/src/core/image.rs +++ b/src/core/image.rs @@ -15,7 +15,7 @@ impl Widget for Image { } } -pub fn image(image: impl LoadableImage) -> WidgetFnRet!(Image) { +pub fn image(image: impl LoadableImage) -> WidgetFn!(Image) { let image = image.get_image().expect("Failed to load image"); move |ui| Image { handle: ui.add_texture(image), diff --git a/src/core/sense.rs b/src/core/sense.rs index 4c4f082..2739101 100644 --- a/src/core/sense.rs +++ b/src/core/sense.rs @@ -1,21 +1,21 @@ use crate::prelude::*; pub trait Sensable { - fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W); + fn on(self, sense: Senses, f: impl SenseFn) -> WidgetIdFn!(W); fn id_on( self, senses: Senses, f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, - ) -> WidgetIdFnRet!(W) + ) -> WidgetIdFn!(W) where W: Widget; - fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFnRet!(W) + fn edit_on(self, senses: Senses, f: impl FnMut(&mut W) + 'static + Clone) -> WidgetIdFn!(W) where W: Widget; } impl, Tag> Sensable for W { - fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFnRet!(W::Widget) { + fn on(self, senses: Senses, f: impl SenseFn) -> WidgetIdFn!(W::Widget) { move |ui| { let id = self.add(ui); ui.add_sensor( @@ -32,7 +32,7 @@ impl, Tag> Sensable for W { self, senses: Senses, mut f: impl FnMut(&WidgetId, &mut Ui) + 'static + Clone, - ) -> WidgetIdFnRet!(W::Widget) + ) -> WidgetIdFn!(W::Widget) where W::Widget: Widget, { @@ -45,7 +45,7 @@ impl, Tag> Sensable for W { self, senses: Senses, mut f: impl FnMut(&mut W::Widget) + 'static + Clone, - ) -> WidgetIdFnRet!(W::Widget) + ) -> WidgetIdFn!(W::Widget) where W::Widget: Widget, { diff --git a/src/core/trait_fns.rs b/src/core/trait_fns.rs index b7786ac..d1f30b0 100644 --- a/src/core/trait_fns.rs +++ b/src/core/trait_fns.rs @@ -2,41 +2,41 @@ use super::*; use crate::prelude::*; pub trait CoreWidget { - fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned); - fn align(self, align: Align) -> WidgetFnRet!(Aligned); - fn center(self) -> WidgetFnRet!(Aligned); - fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned); - fn label(self, label: impl Into) -> WidgetIdFnRet!(W); - fn size(self, size: impl Into) -> WidgetFnRet!(Sized); + fn pad(self, padding: impl Into) -> WidgetFn!(Regioned); + fn align(self, align: Align) -> WidgetFn!(Aligned); + fn center(self) -> WidgetFn!(Aligned); + fn region(self, region: UiRegion) -> WidgetFn!(Regioned); + fn label(self, label: impl Into) -> WidgetIdFn!(W); + fn size(self, size: impl Into) -> WidgetFn!(Sized); } impl, Tag> CoreWidget for W { - fn pad(self, padding: impl Into) -> WidgetFnRet!(Regioned) { + fn pad(self, padding: impl Into) -> WidgetFn!(Regioned) { |ui| Regioned { region: padding.into().region(), inner: self.add(ui).erase_type(), } } - fn align(self, align: Align) -> WidgetFnRet!(Aligned) { + fn align(self, align: Align) -> WidgetFn!(Aligned) { move |ui| Aligned { inner: self.add(ui).erase_type(), align, } } - fn center(self) -> WidgetFnRet!(Aligned) { + fn center(self) -> WidgetFn!(Aligned) { self.align(Align::Center) } - fn region(self, region: UiRegion) -> WidgetFnRet!(Regioned) { + fn region(self, region: UiRegion) -> WidgetFn!(Regioned) { move |ui| Regioned { region, inner: self.add(ui).erase_type(), } } - fn label(self, label: impl Into) -> WidgetIdFnRet!(W::Widget) { + fn label(self, label: impl Into) -> WidgetIdFn!(W::Widget) { |ui| { let id = self.add(ui); ui.set_label(&id, label.into()); @@ -44,7 +44,7 @@ impl, Tag> CoreWidget for W { } } - fn size(self, size: impl Into) -> WidgetFnRet!(Sized) { + fn size(self, size: impl Into) -> WidgetFn!(Sized) { move |ui| Sized { inner: self.add(ui).erase_type(), size: size.into(), @@ -53,19 +53,19 @@ impl, Tag> CoreWidget for W { } pub trait CoreWidgetArr { - fn span(self, dir: Dir, lengths: impl IntoSpanLens) -> WidgetFnRet!(Span); - fn stack(self) -> WidgetFnRet!(Stack); + fn span(self, dir: Dir, lengths: impl IntoSpanLens) -> WidgetFn!(Span); + fn stack(self) -> WidgetFn!(Stack); } impl, Tag> CoreWidgetArr for Wa { - fn span(self, dir: Dir, lengths: impl IntoSpanLens) -> WidgetFnRet!(Span) { + fn span(self, dir: Dir, lengths: impl IntoSpanLens) -> WidgetFn!(Span) { let lengths = lengths.into_lens(); move |ui| Span { children: self.ui(ui).arr.into_iter().zip(lengths).collect(), dir, } } - fn stack(self) -> WidgetFnRet!(Stack) { + fn stack(self) -> WidgetFn!(Stack) { move |ui| Stack { children: self.ui(ui).arr.to_vec(), } diff --git a/src/layout/id.rs b/src/layout/id.rs index e7ac7cd..f58635e 100644 --- a/src/layout/id.rs +++ b/src/layout/id.rs @@ -120,7 +120,7 @@ pub struct IdTag; pub struct IdFnTag; // pub trait WidgetIdFn = FnOnce(&mut Ui) -> WidgetId; -macro_rules! WidgetIdFnRet { +macro_rules! WidgetIdFn { ($W:ty) => { impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> }; @@ -128,15 +128,12 @@ macro_rules! WidgetIdFnRet { impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*> }; } -pub(crate) use WidgetIdFnRet; +pub(crate) use WidgetIdFn; pub trait Idable { type Widget: Widget; fn set(self, ui: &mut Ui, id: &WidgetId); - fn id<'a>( - self, - id: &WidgetId, - ) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag) + fn id<'a>(self, id: &WidgetId) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag) where Self: Sized, { @@ -149,7 +146,7 @@ pub trait Idable { fn id_static<'a>( self, id: StaticWidgetId, - ) -> WidgetIdFnRet!(Self::Widget, 'a, Self, Tag) + ) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag) where Self: Sized, { diff --git a/src/layout/widget.rs b/src/layout/widget.rs index a4ef104..714bbb9 100644 --- a/src/layout/widget.rs +++ b/src/layout/widget.rs @@ -1,4 +1,4 @@ -use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFnRet, Widgets}; +use crate::layout::{Painter, TextData, Textures, Ui, Vec2, WidgetId, WidgetIdFn, Widgets}; use std::{any::Any, marker::PhantomData}; @@ -31,7 +31,7 @@ pub trait WidgetLike { fn with_id( self, f: impl FnOnce(&mut Ui, WidgetId) -> WidgetId, - ) -> WidgetIdFnRet!(W2) + ) -> WidgetIdFn!(W2) where Self: Sized, { @@ -48,12 +48,12 @@ pub trait WidgetLike { /// Useful for defining trait functions on widgets that create a parent widget so that the children /// don't need to be IDs yet /// currently a macro for rust analyzer (doesn't support trait aliases atm) -macro_rules! WidgetFnRet { +macro_rules! WidgetFn { ($W:ty) => { impl FnOnce(&mut $crate::layout::Ui) -> $W }; } -pub(crate) use WidgetFnRet; +pub(crate) use WidgetFn; impl W> WidgetLike for F { type Widget = W; diff --git a/src/lib.rs b/src/lib.rs index c8b54e5..9457dd9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -3,6 +3,7 @@ #![feature(const_trait_impl)] #![feature(const_from)] #![feature(map_try_insert)] +#![feature(trait_alias)] pub mod core; pub mod layout;