basically failed (too much code, slower (macros), no new features)

This commit is contained in:
2025-08-10 18:52:40 -04:00
parent 577d62b1da
commit a6b36fdcca
7 changed files with 134 additions and 60 deletions

View File

@@ -2,7 +2,8 @@ use std::ops::Range;
use crate::{
primitive::{Axis, Painter, RoundedRectData, UIRegion},
UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetLike,
ToId, UIColor, Widget, WidgetArrLike, WidgetFn, WidgetId, WidgetIdLikeTuple, WidgetLike,
WidgetLikeTuple,
};
#[derive(Clone, Copy)]
@@ -134,9 +135,12 @@ pub trait WidgetArrUtil<const LEN: usize> {
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span>;
}
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa {
impl<const LEN: usize, Wa: WidgetArrLike<LEN>> WidgetArrUtil<LEN> for Wa
where
<Wa::Ws as WidgetLikeTuple<LEN>>::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
{
fn span(self, axis: Axis, ratios: [impl UINum; LEN]) -> impl WidgetLike<Widget = Span> {
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).arr))
WidgetFn(move |ui| Span::proportioned(axis, ratios, self.ui(ui).erase_types()))
}
}

View File

@@ -32,10 +32,10 @@ impl From<UI> for UIBuilder {
impl UIBuilder {
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new(self.clone(), [self.push(w)])
WidgetRef::new(self.clone(), (self.push(w),))
}
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let mut ui = self.ui.borrow_mut();
let id = ui.ids.next();
ui.widgets.insert(id.duplicate(), w);

View File

@@ -3,7 +3,11 @@ use std::{
marker::PhantomData,
};
use crate::{primitive::Painter, util::ID, UIBuilder};
use crate::{
primitive::Painter,
util::{impl_tuple, ID},
UIBuilder,
};
pub trait Widget: 'static + Any {
fn draw(&self, painter: &mut Painter);
@@ -23,7 +27,7 @@ pub struct WidgetId<W = ()> {
}
// TODO: temp
impl Clone for WidgetId {
impl<W: Widget> Clone for WidgetId<W> {
fn clone(&self) -> Self {
Self {
ty: self.ty,
@@ -55,7 +59,7 @@ impl<W> WidgetId<W> {
}
pub trait WidgetLike {
type Widget;
type Widget: Widget;
fn add(self, ui: &mut UIBuilder) -> WidgetId<Self::Widget>;
}
@@ -77,89 +81,98 @@ impl<W: Widget> WidgetLike for W {
}
}
impl<W> WidgetLike for WidgetId<W> {
impl<W: Widget> WidgetLike for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
self
}
}
impl<W> WidgetLike for WidgetArr<1, (W,)> {
impl<W: Widget> WidgetLike for WidgetArr<1, (W,)> {
type Widget = W;
fn add(self, _: &mut UIBuilder) -> WidgetId<W> {
let [id] = self.arr;
id.cast_type()
self.arr.0
}
}
pub struct WidgetArr<const LEN: usize, Ws> {
pub struct WidgetArr<const LEN: usize, Ws: WidgetLikeTuple<LEN>> {
pub ui: UIBuilder,
pub arr: [WidgetId<()>; LEN],
_pd: PhantomData<Ws>,
pub arr: Ws::Wrap<ToId>,
}
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
pub fn new(ui: UIBuilder, arr: [WidgetId<()>; LEN]) -> Self {
Self {
ui,
arr,
_pd: PhantomData,
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArr<LEN, Ws>
where
Ws::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
{
pub fn new(ui: UIBuilder, arr: Ws::Wrap<ToId>) -> Self {
Self { ui, arr }
}
pub fn erase_types(self) -> [WidgetId; LEN] {
self.arr.map::<EraseId>(&mut ())
}
}
pub type WidgetRef<W> = WidgetArr<1, (W,)>;
impl<W> WidgetRef<W> {
pub fn handle(&self) -> WidgetId<W> {
let [id] = &self.arr;
id.clone().cast_type()
impl<W: WidgetLike> WidgetRef<W> {
pub fn handle(&self) -> WidgetId<W::Widget> {
self.arr.0.clone()
}
pub fn to_id(self) -> WidgetId<W> {
let [id] = self.arr;
id.cast_type()
pub fn to_id(self) -> WidgetId<W::Widget> {
self.arr.0
}
}
pub trait WidgetArrLike<const LEN: usize> {
type Ws;
type Ws: WidgetLikeTuple<LEN>;
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, Self::Ws>;
}
impl<const LEN: usize, Ws> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
impl<const LEN: usize, Ws: WidgetLikeTuple<LEN>> WidgetArrLike<LEN> for WidgetArr<LEN, Ws> {
type Ws = Ws;
fn ui(self, _: &mut UIBuilder) -> WidgetArr<LEN, Ws> {
self
}
}
// 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,)*);
#[allow(unused_variables)]
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
#[allow(non_snake_case)]
let ($($T,)*) = self;
WidgetArr::new(
ui.clone(),
[$($T.add(ui).cast_type(),)*],
)
}
}
};
impl_tuple!(Widget);
impl_tuple!(WidgetLike);
impl_tuple!(WidgetIdLike);
pub trait WidgetIdLike {
fn erase_type(self) -> WidgetId;
}
impl_widget_arr!(1;A);
impl_widget_arr!(2;A B);
impl_widget_arr!(3;A B C);
impl_widget_arr!(4;A B C D);
impl_widget_arr!(5;A B C D E);
impl_widget_arr!(6;A B C D E F);
impl_widget_arr!(7;A B C D E F G);
impl_widget_arr!(8;A B C D E F G H);
impl_widget_arr!(9;A B C D E F G H I);
impl_widget_arr!(10;A B C D E F G H I J);
impl_widget_arr!(11;A B C D E F G H I J K);
impl_widget_arr!(12;A B C D E F G H I J K L);
impl<W> WidgetIdLike for WidgetId<W> {
fn erase_type(self) -> WidgetId {
self.erase_type()
}
}
pub struct ToId;
impl WidgetLikeWrapper for ToId {
type Wrap<T: WidgetLike> = WidgetId<T::Widget>;
type Ctx = UIBuilder;
fn wrap<T: WidgetLike>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T> {
t.add(ctx)
}
}
struct EraseId;
impl WidgetIdLikeMapper for EraseId {
type Map = WidgetId<()>;
type Ctx = ();
fn map<Id: WidgetIdLike>(t: Id, _: &mut Self::Ctx) -> Self::Map {
t.erase_type()
}
}
impl<T: WidgetLikeTuple<LEN>, const LEN: usize> WidgetArrLike<LEN> for T
where
T::Wrap<ToId>: WidgetIdLikeTuple<LEN>,
{
type Ws = T;
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<LEN, T> {
WidgetArr::new(ui.clone(), self.wrap::<ToId>(ui))
}
}

View File

@@ -3,7 +3,6 @@
#![feature(const_trait_impl)]
#![feature(const_from)]
#![feature(trait_alias)]
#![feature(generic_const_exprs)]
mod layout;
mod render;

View File

@@ -27,6 +27,8 @@ impl IDTracker {
id
}
// TODO: use this
#[allow(dead_code)]
pub fn free(&mut self, id: ID) {
self.free.push(id);
}

View File

@@ -1,3 +1,5 @@
mod id;
mod tuple;
pub use id::*;
pub use tuple::*;

54
src/util/tuple.rs Normal file
View File

@@ -0,0 +1,54 @@
macro_rules! impl_tuple {
($Tuple:ident $Bound:ident $Wrapper:ident $Mapper:ident $n:expr;$TL:tt $($T:tt)*) => {
#[allow(non_snake_case)]
impl<$($T: $Bound,)* $TL: $Bound> $Tuple<$n> for ($($T,)* $TL,) {
type Wrap<W: $Wrapper> = ($(W::Wrap<$T>,)* W::Wrap<$TL>,);
type Map<M: $Mapper> = [M::Map; $n];
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> ($(W::Wrap<$T>,)* W::Wrap<$TL>,) {
let ($($T,)* $TL,) = self;
($(W::wrap($T, ctx),)* W::wrap($TL, ctx),)
}
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; $n] {
let ($($T,)* $TL,) = self;
[$(M::map($T, ctx),)* M::map($TL, ctx)]
}
}
};
($Tuple:ident, $Bound:ident, $Wrapper:ident, $Mapper:ident) => {
pub trait $Wrapper {
type Wrap<T: $Bound>;
type Ctx;
fn wrap<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Wrap<T>;
}
pub trait $Mapper {
type Map;
type Ctx;
fn map<T: $Bound>(t: T, ctx: &mut Self::Ctx) -> Self::Map;
}
pub trait $Tuple<const LEN: usize> {
type Wrap<W: $Wrapper>;
type Map<M: $Mapper>;
fn map<M: $Mapper>(self, ctx: &mut M::Ctx) -> [M::Map; LEN];
fn wrap<W: $Wrapper>(self, ctx: &mut W::Ctx) -> Self::Wrap<W>;
}
impl_tuple!($Tuple $Bound $Wrapper $Mapper 1;A);
impl_tuple!($Tuple $Bound $Wrapper $Mapper 2;A B);
impl_tuple!($Tuple $Bound $Wrapper $Mapper 3;A B C);
impl_tuple!($Tuple $Bound $Wrapper $Mapper 4;A B C D);
impl_tuple!($Tuple $Bound $Wrapper $Mapper 5;A B C D E);
impl_tuple!($Tuple $Bound $Wrapper $Mapper 6;A B C D E F);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 7;A B C D E F G);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 8;A B C D E F G H);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 9;A B C D E F G H I);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 10;A B C D E F G H I J);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 11;A B C D E F G H I J K);
// impl_tuple!($Tuple $Bound $Wrapper $Mapper 12;A B C D E F G H I J K L);
};
($Bound:ident) => {
impl_tuple!(${concat($Bound, Tuple)}, $Bound, ${concat($Bound, Wrapper)}, ${concat($Bound, Mapper)});
}
}
pub(crate) use impl_tuple;