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 {
pub fn add<W: Widget>(&mut self, w: W) -> WidgetRef<W> {
WidgetRef::new(self.clone(), [self.push(w)])
pub fn add<W: Widget>(&mut self, w: impl WidgetLike<Widget = W>) -> WidgetRef<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 {

View File

@@ -15,21 +15,19 @@ impl<W: Widget> Widget for (W,) {
}
}
pub struct AnyWidget;
#[derive(Eq, Hash, PartialEq, Debug)]
pub struct WidgetId<W = ()> {
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: ID,
_pd: PhantomData<W>,
}
// TODO: temp
impl Clone for WidgetId {
impl<W> Clone for WidgetId<W> {
fn clone(&self) -> Self {
Self {
ty: self.ty,
id: self.id.duplicate(),
_pd: self._pd,
}
Self::new(self.id.duplicate(), self.ty)
}
}
@@ -41,7 +39,7 @@ impl<W> WidgetId<W> {
_pd: PhantomData,
}
}
pub fn erase_type(self) -> WidgetId<()> {
pub fn erase_type(self) -> WidgetId<AnyWidget> {
self.cast_type()
}
@@ -66,14 +64,14 @@ impl<W: Widget, F: FnOnce(&mut UIBuilder) -> W> WidgetLike for WidgetFn<F, W> {
type Widget = W;
fn add(self, ui: &mut UIBuilder) -> WidgetId<W> {
let w = (self.0)(ui);
ui.add(w).to_id()
ui.add_widget(w).to_id()
}
}
impl<W: Widget> WidgetLike for W {
type Widget = 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 ui: UIBuilder,
pub arr: [WidgetId<()>; LEN],
pub arr: [WidgetId; LEN],
_pd: PhantomData<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 {
ui,
arr,
_pd: PhantomData,
}
@@ -138,12 +134,10 @@ 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(),)*],
)
}

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,7 @@ impl IDTracker {
id
}
#[allow(dead_code)]
pub fn free(&mut self, id: ID) {
self.free.push(id);
}