REAL SENSORS

This commit is contained in:
2025-08-15 21:42:35 -04:00
parent 9f1802f497
commit a7dfacb83e
10 changed files with 257 additions and 123 deletions

View File

@@ -17,13 +17,19 @@ pub struct AnyWidget;
/// W does not need to implement widget so that AnyWidget is valid;
/// Instead, add generic bounds on methods that take an ID if they need specific data.
#[repr(C)]
#[derive(Eq, Hash, PartialEq, Debug)]
#[derive(Eq, Hash, PartialEq)]
pub struct WidgetId<W = AnyWidget> {
pub(super) ty: TypeId,
pub(super) id: Id,
_pd: PhantomData<W>,
}
impl<W> std::fmt::Debug for WidgetId<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.id.fmt(f)
}
}
// TODO: temp
impl<W> Clone for WidgetId<W> {
fn clone(&self) -> Self {
@@ -63,30 +69,42 @@ pub struct FnTag;
pub struct IdTag;
pub trait WidgetLike<Ctx, Tag> {
type Widget;
type Widget: 'static;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<Self::Widget>;
fn with_id<W2>(
self,
f: impl FnOnce(&mut Ui<Ctx>, WidgetId<Self::Widget>) -> WidgetId<W2>,
) -> impl WidgetIdFn<W2, Ctx>
where
Self: Sized,
{
move |ui| {
let id = self.add(ui);
f(ui, id)
}
}
}
/// 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 WidgetFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> W;
pub trait WidgetIdFn<W: Widget<Ctx>, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui<Ctx>) -> WidgetId<W>;
pub trait Idable<Ctx, Tag> {
type Widget: Widget<Ctx>;
fn set(self, ui: &mut Ui<Ctx>, id: &WidgetId<Self::Widget>);
}
pub trait WidgetFns<W: Widget<Ctx>, Ctx, Tag> {
fn id(self, id: &WidgetId<W>) -> impl WidgetIdFn<W, Ctx>;
}
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()
fn id<'a>(
self,
id: &WidgetId<Self::Widget>,
) -> impl WidgetIdFn<Self::Widget, Ctx> + use<'a, Self, Ctx, Tag>
where
Self: Sized,
{
let id = id.clone();
move |ui| {
self.set(ui, &id);
id
}
}
}
@@ -116,7 +134,7 @@ impl<W: Widget<Ctx>, Ctx, F: FnOnce(&mut Ui<Ctx>) -> W> WidgetLike<Ctx, FnTag> f
}
}
impl<W: Widget<Ctx>, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
impl<W: 'static, F: FnOnce(&mut Ui<Ctx>) -> WidgetId<W>, Ctx> WidgetLike<Ctx, IdTag> for F {
type Widget = W;
fn add(self, ui: &mut Ui<Ctx>) -> WidgetId<W> {
self(ui)
@@ -130,7 +148,7 @@ impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, WidgetTag> for W {
}
}
impl<W: Widget<Ctx>, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
impl<W: 'static, Ctx> WidgetLike<Ctx, FnTag> for WidgetId<W> {
type Widget = W;
fn add(self, _: &mut Ui<Ctx>) -> WidgetId<W> {
self