widget fn ret macro

This commit is contained in:
2025-08-13 02:07:35 -04:00
parent c7e3225c5f
commit f4975df57b
2 changed files with 11 additions and 11 deletions

View File

@@ -1,5 +1,5 @@
use super::*; use super::*;
use crate::{UIRegion, Ui, Vec2, WidgetArrLike, WidgetFn, WidgetLike}; use crate::{UIRegion, Ui, Vec2, WidgetArrLike, WidgetFn, WidgetFnRet, WidgetLike};
pub trait BaseWidget { pub trait BaseWidget {
fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>; fn pad(self, padding: impl Into<Padding>) -> impl WidgetLike<Widget = Regioned>;
@@ -23,19 +23,11 @@ impl<W: WidgetLike> BaseWidget for W {
} }
pub trait BaseWidgetArr<const LEN: usize> { pub trait BaseWidgetArr<const LEN: usize> {
fn span( fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span);
self,
dir: Dir,
lengths: [impl Into<SpanLen>; LEN],
) -> WidgetFn<impl FnOnce(&mut Ui) -> Span, Span>;
} }
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> BaseWidgetArr<LEN> for Wa { impl<const LEN: usize, Wa: WidgetArrLike<LEN>> BaseWidgetArr<LEN> for Wa {
fn span( fn span(self, dir: Dir, lengths: [impl Into<SpanLen>; LEN]) -> WidgetFnRet!(Span) {
self,
dir: Dir,
lengths: [impl Into<SpanLen>; LEN],
) -> WidgetFn<impl FnOnce(&mut Ui) -> Span, Span> {
let lengths = lengths.map(Into::into); let lengths = lengths.map(Into::into);
WidgetFn(move |ui| Span { WidgetFn(move |ui| Span {
dir, dir,

View File

@@ -59,9 +59,17 @@ pub trait WidgetLike {
/// A function that returns a widget given a UI. /// A function that returns a widget given a UI.
/// Useful for defining trait functions on widgets that create a parent widget so that the children /// Useful for defining trait functions on widgets that create a parent widget so that the children
/// don't need to be IDs yet /// don't need to be IDs yet
pub trait WFn<W> = FnOnce(&mut Ui) -> W;
pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F); pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F);
pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F); pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F);
macro_rules! WidgetFnRet {
($W:ident) => {
WidgetFn<impl FnOnce(&mut Ui) -> $W, $W>
};
}
pub(crate) use WidgetFnRet;
pub trait _WidgetFn<W: Widget> { pub trait _WidgetFn<W: Widget> {
fn call(ui: &mut Ui) -> W; fn call(ui: &mut Ui) -> W;
} }