remove context from ui (again) and create weird trait for it

This commit is contained in:
2025-09-24 17:41:25 -04:00
parent 26c248dcba
commit 719bee4b31
12 changed files with 107 additions and 92 deletions

View File

@@ -12,13 +12,13 @@ pub trait Widget: Any {
pub struct WidgetTag;
pub struct FnTag;
pub trait WidgetLike<Ctx, Tag> {
pub trait WidgetLike<Tag> {
type Widget: 'static;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2, Ctx>
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2>
where
Self: Sized,
{
@@ -27,13 +27,13 @@ pub trait WidgetLike<Ctx, Tag> {
f(ui, id)
}
}
fn add_static(self, ui: &mut Ui<Ctx>) -> StaticWidgetId<Self::Widget>
fn add_static(self, ui: &mut Ui) -> StaticWidgetId<Self::Widget>
where
Self: Sized,
{
self.add(ui).into_static()
}
fn set_root(self, ui: &mut Ui<Ctx>)
fn set_root(self, ui: &mut Ui)
where
Self: Sized,
{
@@ -44,19 +44,19 @@ pub trait WidgetLike<Ctx, Tag> {
/// 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
/// don't need to be IDs yet
pub trait WidgetFn<W: Widget, Ctx>: FnOnce(&mut Ui<Ctx>) -> W {}
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetFn<W, Ctx> for F {}
pub trait WidgetFn<W: Widget>: FnOnce(&mut Ui) -> W {}
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
impl<W: Widget, F: FnOnce(&mut Ui<Ctx>) -> W, Ctx> WidgetLike<Ctx, FnTag> for F {
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui).add(ui)
}
}
impl<W: Widget, Ctx> WidgetLike<Ctx, WidgetTag> for W {
impl<W: Widget> WidgetLike<WidgetTag> for W {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
ui.add_widget(self)
}
}
@@ -76,21 +76,21 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
}
pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
pub trait WidgetArrLike<const LEN: usize, Tags> {
type Ws;
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws> WidgetArrLike<LEN, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
self
}
}
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
impl<W: WidgetLike<WidgetTag>> WidgetArrLike<1, WidgetTag> for W {
type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).any()])
}
}
@@ -101,9 +101,9 @@ macro_rules! impl_widget_arr {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
};
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<Ctx, $($W: WidgetLike<Ctx, $Tag>,$Tag,)*> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
impl<$($W: WidgetLike<$Tag>,$Tag,)*> WidgetArrLike<$n, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)]
let ($($W,)*) = self;
WidgetArr::new(