TAG TECHNOLOGY

This commit is contained in:
2025-08-14 12:21:26 -04:00
parent 4d68fa476d
commit e41970287d
19 changed files with 267 additions and 165 deletions

View File

@@ -3,10 +3,10 @@ use std::{
marker::PhantomData,
};
use crate::{Painter, Ui, util::ID};
use crate::{Painter, Ui, util::Id};
pub trait Widget: 'static + Any {
fn draw(&self, painter: &mut Painter);
pub trait Widget<Ctx>: Any {
fn draw(&self, painter: &mut Painter<Ctx>);
}
pub struct AnyWidget;
@@ -19,7 +19,7 @@ pub struct AnyWidget;
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: ID,
pub(super) id: Id,
_pd: PhantomData<W>,
}
@@ -31,7 +31,7 @@ impl<W> Clone for WidgetId<W> {
}
impl<W> WidgetId<W> {
pub(super) fn new(id: ID, ty: TypeId) -> Self {
pub(super) fn new(id: Id, ty: TypeId) -> Self {
Self {
ty,
id,
@@ -51,85 +51,81 @@ impl<W> WidgetId<W> {
}
}
pub trait WidgetLike {
pub struct WidgetTag;
pub struct FnTag;
pub struct IdTag;
pub trait WidgetLike<Ctx, Tag> {
type Widget;
fn add(self, ui: &mut Ui) -> WidgetId<Self::Widget>;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
}
/// 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 WFn<W> = FnOnce(&mut Ui) -> W;
pub struct WidgetFn<F: FnOnce(&mut Ui) -> W, W>(pub F);
pub struct WidgetIdFn<F: FnOnce(&mut Ui) -> WidgetId<W>, W>(pub F);
pub trait WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
pub trait WidgetIdFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
macro_rules! WidgetFnRet {
($W:ident) => {
WidgetFn<impl FnOnce(&mut Ui) -> $W, $W>
};
}
pub(crate) use WidgetFnRet;
pub trait Idable {
type Widget: Widget;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
pub trait Idable<Ctx, Tag> {
type Widget: Widget<Ctx>;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
}
pub trait WidgetFns<W> {
fn id(self, id: &WidgetId<W>) -> impl WidgetLike<Widget = W>;
pub trait WidgetFns<W: Widget<Ctx>, Ctx, Tag> {
fn id(self, id: &WidgetId<W>) -> impl WidgetIdFn<W, Ctx>;
}
impl<I: Idable> WidgetFns<I::Widget> for I {
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetLike<Widget = I::Widget> {
WidgetIdFn(|ui| {
impl<I: Idable<Ctx, Tag>, Ctx, Tag> WidgetFns<I::Widget, Ctx, Tag> for I {
fn id(self, id: &WidgetId<I::Widget>) -> impl WidgetIdFn<I::Widget, Ctx> {
|ui| {
self.set(ui, id);
id.clone()
})
}
}
}
impl<W: Widget> Idable for W {
impl<W: Widget<Ctx>, Ctx> Idable<Ctx, WidgetTag> for W {
type Widget = W;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
ui.set(id, self);
}
}
impl<F: for<'a> FnOnce(&'a mut Ui) -> W, W: Widget> Idable for WidgetFn<F, W> {
impl<F: FnOnce(&mut Ui<Ctx>) -> W, W: Widget<Ctx>, Ctx> Idable<Ctx, FnTag> for F {
type Widget = W;
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>) {
let w = self.0(ui);
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>) {
let w = self(ui);
ui.set(id, w);
}
}
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike for WidgetFn<F, W> {
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
let w = (self.0)(ui);
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
let w = self(ui);
ui.add(w)
}
}
impl<W: Widget, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetLike for WidgetIdFn<F, W> {
impl<W: Widget<Ctx>, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
(self.0)(ui)
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
self(ui)
}
}
impl<W: Widget> WidgetLike for W {
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
type Widget = W;
fn add(self, ui: &mut Ui) -> WidgetId<W> {
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
ui.add_widget(self)
}
}
impl<W> WidgetLike for WidgetId<W> {
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut Ui) -> WidgetId<W> {
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
self
}
}
@@ -148,35 +144,39 @@ impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
}
}
pub trait WidgetArrLike<const LEN: usize> {
pub struct ArrTag;
pub trait WidgetArrLike<const LEN: usize, Ctx, Tags> {
type Ws;
fn ui(self, ui: &mut Ui) -> WidgetArr<LEN, Self::Ws>;
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws, Ctx> WidgetArrLike<LEN, Ctx, ArrTag> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut Ui) -> WidgetArr<LEN, Ws> {
fn ui(self, _: &mut Ui<Ctx>) -> WidgetArr<LEN, Ws> {
self
}
}
impl<W: WidgetLike> WidgetArrLike<1> for W {
impl<W: WidgetLike<Ctx, WidgetTag>, Ctx> WidgetArrLike<1, Ctx, WidgetTag> for W {
type Ws = (W::Widget,);
fn ui(self, ui: &mut Ui) -> WidgetArr<1, (W::Widget,)> {
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<1, (W::Widget,)> {
WidgetArr::new([self.add(ui).erase_type()])
}
}
// I hate this language it's so bad why do I even use it
macro_rules! impl_widget_arr {
($n:expr;$($T:tt)*) => {
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
type Ws = ($($T::Widget,)*);
fn ui(self, ui: &mut Ui) -> WidgetArr<$n, ($($T::Widget,)*)> {
($n:expr;$($W:ident)*) => {
impl_widget_arr!($n;$($W)*;$(${concat($W,Tag)})*);
};
($n:expr;$($W:ident)*;$($Tag:ident)*) => {
impl<$($W: WidgetLike<Ctx, $Tag>,$Tag,)* Ctx> WidgetArrLike<$n, Ctx, ($($Tag,)*)> for ($($W,)*) {
type Ws = ($($W::Widget,)*);
fn ui(self, ui: &mut Ui<Ctx>) -> WidgetArr<$n, ($($W::Widget,)*)> {
#[allow(non_snake_case)]
let ($($T,)*) = self;
let ($($W,)*) = self;
WidgetArr::new(
[$($T.add(ui).cast_type(),)*],
[$($W.add(ui).cast_type(),)*],
)
}
}