remove context generic

This commit is contained in:
2025-08-25 18:53:21 -04:00
parent d4b1a56467
commit e8b255c8f9
17 changed files with 167 additions and 217 deletions

View File

@@ -2,20 +2,20 @@ use crate::layout::{Painter, Ui, WidgetId, WidgetIdFnRet};
use std::{any::Any, marker::PhantomData};
pub trait Widget<Ctx>: Any {
fn draw(&self, painter: &mut Painter<Ctx>);
pub trait Widget: Any {
fn draw(&self, painter: &mut Painter);
}
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>,
) -> WidgetIdFnRet!(W2, Ctx)
f: impl FnOnce(&mut Ui, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> WidgetIdFnRet!(W2)
where
Self: Sized,
{
@@ -33,22 +33,22 @@ pub trait WidgetLike<Ctx, Tag> {
/// don't need to be IDs yet
/// currently a macro for rust analyzer (doesn't support trait aliases atm)
macro_rules! WidgetFnRet {
($W:ty, $Ctx:ty) => {
impl FnOnce(&mut $crate::layout::Ui<$Ctx>) -> $W
($W:ty) => {
impl FnOnce(&mut $crate::layout::Ui) -> $W
};
}
pub(crate) use WidgetFnRet;
impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> 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>, 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)
}
}
@@ -68,21 +68,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).erase_type()])
}
}
@@ -93,9 +93,9 @@ macro_rules! impl_widget_arr {
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,)*) {
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(