This commit is contained in:
2025-08-10 19:10:26 -04:00
parent 577d62b1da
commit 848347e6b3
4 changed files with 17 additions and 19 deletions

View File

@@ -31,8 +31,12 @@ impl From<UI> for UIBuilder {
} }
impl UIBuilder { impl UIBuilder {
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> { pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<W> {
WidgetRef::new(self.clone(), [self.push(w)]) WidgetRef::new([w.add(self).erase_type()])
}
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new([self.push(w)])
} }
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId { pub fn push<W: Widget>(&mut self, w: W) -> WidgetId {

View File

@@ -15,21 +15,19 @@ impl<W: Widget> Widget for (W,) {
} }
} }
pub struct AnyWidget;
#[derive(Eq, Hash, PartialEq, Debug)] #[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = ()> { pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId, pub(super) ty: TypeId,
pub(super) id: ID, pub(super) id: ID,
_pd: PhantomData<W>, _pd: PhantomData<W>,
} }
// TODO: temp // TODO: temp
impl Clone for WidgetId { impl<W> Clone for WidgetId<W> {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self::new(self.id.duplicate(), self.ty)
ty: self.ty,
id: self.id.duplicate(),
_pd: self._pd,
}
} }
} }
@@ -41,7 +39,7 @@ impl<W> WidgetId<W> {
_pd: PhantomData, _pd: PhantomData,
} }
} }
pub fn erase_type(self) -> WidgetId<()> { pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type() self.cast_type()
} }
@@ -66,14 +64,14 @@ impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
type Widget = W; type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> { fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
let w = (self.0)(ui); let w = (self.0)(ui);
ui.add(w).to_id() ui.add_widget(w).to_id()
} }
} }
impl<W: Widget> WidgetLike for W { impl<W: Widget> WidgetLike for W {
type Widget = W; type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> { fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
ui.add(self).to_id() ui.add_widget(self).to_id()
} }
} }
@@ -93,15 +91,13 @@ impl<W> WidgetLike for WidgetArr<1, (W,)> {
} }
pub struct WidgetArr<const LEN: usize, Ws> { pub struct WidgetArr<const LEN: usize, Ws> {
pub ui: UIBuilder, pub arr: [WidgetId; LEN],
pub arr: [WidgetId<()>; LEN],
_pd: PhantomData<Ws>, _pd: PhantomData<Ws>,
} }
impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> { impl<const LEN: usize, Ws> WidgetArr<LEN, Ws> {
pub fn new(ui: UIBuilder, arr: [WidgetId<()>; LEN]) -> Self { pub fn new(arr: [WidgetId; LEN]) -> Self {
Self { Self {
ui,
arr, arr,
_pd: PhantomData, _pd: PhantomData,
} }
@@ -138,12 +134,10 @@ macro_rules! impl_widget_arr {
($n:expr;$($T:tt)*) => { ($n:expr;$($T:tt)*) => {
impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) { impl<$($T: WidgetLike,)*> WidgetArrLike<$n> for ($($T,)*) {
type Ws = ($($T::Widget,)*); type Ws = ($($T::Widget,)*);
#[allow(unused_variables)]
fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> { fn ui(self, ui: &mut UIBuilder) -> WidgetArr<$n, ($($T::Widget,)*)> {
#[allow(non_snake_case)] #[allow(non_snake_case)]
let ($($T,)*) = self; let ($($T,)*) = self;
WidgetArr::new( WidgetArr::new(
ui.clone(),
[$($T.add(ui).cast_type(),)*], [$($T.add(ui).cast_type(),)*],
) )
} }

View File

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

View File

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