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

@@ -1,10 +1,10 @@
use crate::{
layout::{Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
layout::{IdFnTag, Ui, UiModule, Widget, WidgetId, WidgetIdFn, WidgetLike},
util::Id,
};
pub trait UiCtx {
fn ui(&mut self) -> &mut Ui<Self>
fn ui(&mut self) -> &mut Ui
where
Self: Sized;
}
@@ -22,13 +22,17 @@ pub trait EventFn<Ctx, Data>: FnMut(&mut Ctx, Data) + 'static {}
impl<F: FnMut(&mut Ctx, Data) + 'static, Ctx, Data> EventFn<Ctx, Data> for F {}
pub trait Eventable<W, Ctx, Tag> {
fn on<E: Event<Ctx>>(self, event: E, f: impl EventFn<Ctx, E::Data>) -> impl WidgetIdFn<W, Ctx>;
fn on<E: Event<Ctx>>(
self,
event: E,
f: impl EventFn<Ctx, E::Data>,
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>;
fn id_on<E: Event<Ctx>>(
self,
event: E,
f: impl FnMut(&WidgetId<W>, &mut Ctx, E::Data) + 'static,
) -> impl WidgetIdFn<W, Ctx>
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
where
W: Widget;
@@ -36,18 +40,29 @@ pub trait Eventable<W, Ctx, Tag> {
self,
event: E,
f: impl FnMut(&mut W, E::Data) + 'static,
) -> impl WidgetIdFn<W, Ctx>
) -> impl WidgetIdFn<W> + Eventable<W, Ctx, IdFnTag>
where
W: Widget,
Ctx: UiCtx;
}
impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
pub trait Ctxable<W, Tag> {
/// sets context which lets event functions work without needing to specify generics
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W, Ctx, Tag>;
}
impl<W: WidgetLike<Tag>, Tag> Ctxable<W::Widget, Tag> for W {
fn ctx<Ctx>(self) -> impl WidgetLike<Tag> + Eventable<W::Widget, Ctx, Tag> {
self
}
}
impl<W: WidgetLike<Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
fn on<E: Event<Ctx>>(
self,
event: E,
f: impl EventFn<Ctx, E::Data>,
) -> impl WidgetIdFn<W::Widget, Ctx> {
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag> {
move |ui| {
let id = self.add(ui);
ui.modules
@@ -61,7 +76,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
self,
event: E,
mut f: impl FnMut(&WidgetId<W::Widget>, &mut Ctx, E::Data) + 'static,
) -> impl WidgetIdFn<W::Widget, Ctx>
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
where
W::Widget: Widget,
{
@@ -75,7 +90,7 @@ impl<W: WidgetLike<Ctx, Tag>, Ctx, Tag> Eventable<W::Widget, Ctx, Tag> for W {
self,
event: E,
mut f: impl FnMut(&mut W::Widget, E::Data) + 'static,
) -> impl WidgetIdFn<W::Widget, Ctx>
) -> impl WidgetIdFn<W::Widget> + Eventable<W::Widget, Ctx, IdFnTag>
where
W::Widget: Widget,
Ctx: UiCtx,

View File

@@ -131,14 +131,14 @@ impl<W> Drop for WidgetId<W> {
pub struct IdTag;
pub struct IdFnTag;
pub trait WidgetIdFn<W, Ctx>: FnOnce(&mut Ui<Ctx>) -> WidgetId<W> {}
impl<W, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetIdFn<W, Ctx> for F {}
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
/// TODO: does this ever make sense to use? it allows for invalid ids
pub trait Idable<Ctx, Tag> {
pub trait Idable<Tag> {
type Widget: Widget;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx>
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where
Self: Sized,
{
@@ -148,7 +148,7 @@ pub trait Idable<Ctx, Tag> {
id
}
}
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget, Ctx>
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
where
Self: Sized,
{
@@ -160,33 +160,33 @@ pub trait Idable<Ctx, Tag> {
}
}
impl<W: Widget, Ctx> Idable<Ctx, WidgetTag> for W {
impl<W: Widget> Idable<WidgetTag> for W {
type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
ui.set(id, self);
}
}
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget, Ctx> Idable<Ctx, FnTag> for F {
impl<F: FnOnce(&mut Ui) -> W, W: Widget> Idable<FnTag> for F {
type Widget = W;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
let w = self(ui);
ui.set(id, w);
}
}
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for WidgetId<W> {
impl<W: 'static> WidgetLike<IdTag> for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, _: &mut Ui) -> WidgetId<W> {
self
}
}
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdFnTag> for F {
impl<W: 'static, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike<IdFnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self(ui)
}
}
@@ -200,9 +200,9 @@ impl<W> StaticWidgetId<W> {
}
}
impl<W: 'static, Ctx> WidgetLike<Ctx, IdTag> for StaticWidgetId<W> {
impl<W: 'static> WidgetLike<IdTag> for StaticWidgetId<W> {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
fn add(self, ui: &mut Ui) -> WidgetId<W> {
self.id(&ui.send)
}
}

View File

@@ -10,13 +10,11 @@ use crate::{
};
use std::{
any::{Any, TypeId},
marker::PhantomData,
ops::{Index, IndexMut},
sync::mpsc::{Receiver, Sender, channel},
};
// TODO: remove generic
pub struct Ui<Ctx> {
pub struct Ui {
root: Option<WidgetId>,
pub(super) widgets: Widgets,
updates: Vec<Id>,
@@ -31,17 +29,16 @@ pub struct Ui<Ctx> {
pub(crate) active: ActiveWidgets,
pub modules: Modules,
_pd: PhantomData<Ctx>,
}
impl<Ctx> Ui<Ctx> {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Ctx, Tag, Widget = W>) -> WidgetId<W> {
impl Ui {
pub fn add<W: Widget, Tag>(&mut self, w: impl WidgetLike<Tag, Widget = W>) -> WidgetId<W> {
w.add(self)
}
pub fn add_static<W: Widget, Tag>(
&mut self,
w: impl WidgetLike<Ctx, Tag, Widget = W>,
w: impl WidgetLike<Tag, Widget = W>,
) -> StaticWidgetId<W> {
let id = w.add(self);
id.into_static()
@@ -70,7 +67,7 @@ impl<Ctx> Ui<Ctx> {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Ctx, Tag>) {
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.root = Some(w.add(self).any());
self.full_redraw = true;
}
@@ -184,7 +181,7 @@ impl<Ctx> Ui<Ctx> {
}
}
impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<&WidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: &WidgetId<W>) -> &Self::Output {
@@ -192,14 +189,14 @@ impl<W: Widget, Ctx> Index<&WidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget, Ctx> IndexMut<&WidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<&WidgetId<W>> for Ui {
fn index_mut(&mut self, id: &WidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.duplicate());
self.get_mut(id).unwrap()
}
}
impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> Index<StaticWidgetId<W>> for Ui {
type Output = W;
fn index(&self, id: StaticWidgetId<W>) -> &Self::Output {
@@ -207,7 +204,7 @@ impl<W: Widget, Ctx> Index<StaticWidgetId<W>> for Ui<Ctx> {
}
}
impl<W: Widget, Ctx> IndexMut<StaticWidgetId<W>> for Ui<Ctx> {
impl<W: Widget> IndexMut<StaticWidgetId<W>> for Ui {
fn index_mut(&mut self, id: StaticWidgetId<W>) -> &mut Self::Output {
self.updates.push(id.id.id());
self.widgets.get_static_mut(&id).unwrap()
@@ -224,7 +221,7 @@ impl dyn Widget {
}
}
impl<Ctx> Default for Ui<Ctx> {
impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
@@ -240,7 +237,6 @@ impl<Ctx> Default for Ui<Ctx> {
recv,
size: Vec2::ZERO,
modules: Modules::default(),
_pd: PhantomData,
}
}
}

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(