RE ADD CONTEXT

This commit is contained in:
2025-12-15 21:50:53 -05:00
parent dc2be7f688
commit 0b8a93c5ce
39 changed files with 925 additions and 713 deletions

View File

@@ -1,4 +1,9 @@
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
use std::{
marker::{PhantomData, Unsize},
mem::MaybeUninit,
ops::CoerceUnsized,
sync::mpsc::Sender,
};
use crate::{
Ui, Widget,
@@ -12,43 +17,46 @@ pub type WidgetId = SlotId;
/// a signal is sent to the owning UI to clean up the resources.
///
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
pub struct WidgetHandle<State, W: ?Sized = dyn Widget<State>> {
pub(super) id: WidgetId,
counter: RefCounter,
send: Sender<WidgetId>,
ty: *const W,
state: PhantomData<State>,
}
/// A weak handle to a widget.
/// Will not keep it alive, but can still be used for indexing like WidgetHandle.
pub struct WidgetRef<W: ?Sized = dyn Widget> {
pub struct WidgetRef<State, W: ?Sized = dyn Widget<State>> {
pub(super) id: WidgetId,
#[allow(unused)]
ty: *const W,
state: PhantomData<State>,
}
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
pub h: WidgetHandle<W>,
pub r: WidgetRef<W>,
pub struct WidgetHandles<State, W: ?Sized = dyn Widget<State>> {
pub h: WidgetHandle<State, W>,
pub r: WidgetRef<State, W>,
}
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> {
impl<State, W: Widget<State> + ?Sized + Unsize<dyn Widget<State>>> WidgetHandle<State, W> {
pub fn any(self) -> WidgetHandle<State, dyn Widget<State>> {
self
}
}
impl<W: ?Sized> WidgetHandle<W> {
impl<State, W: ?Sized> WidgetHandle<State, W> {
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
Self {
id,
counter: RefCounter::new(),
send,
ty: unsafe { MaybeUninit::zeroed().assume_init() },
state: PhantomData,
}
}
pub fn as_any(&self) -> &WidgetHandle<dyn Widget> {
pub fn as_any(&self) -> &WidgetHandle<State, dyn Widget<State>> {
// SAFETY: self is repr(C) and generic only used for phantom data
unsafe { std::mem::transmute(self) }
}
@@ -61,24 +69,28 @@ impl<W: ?Sized> WidgetHandle<W> {
self.counter.refs()
}
pub fn weak(&self) -> WidgetRef<W> {
pub fn weak(&self) -> WidgetRef<State, W> {
let Self { ty, id, .. } = *self;
WidgetRef { ty, id }
WidgetRef {
ty,
id,
state: PhantomData,
}
}
pub fn handles(self) -> WidgetHandles<W> {
pub fn handles(self) -> WidgetHandles<State, W> {
let r = self.weak();
WidgetHandles { h: self, r }
}
}
impl<W: ?Sized> WidgetRef<W> {
impl<State, W: ?Sized> WidgetRef<State, W> {
pub fn id(&self) -> WidgetId {
self.id
}
}
impl<W: ?Sized> Drop for WidgetHandle<W> {
impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
fn drop(&mut self) {
if self.counter.drop() {
let _ = self.send.send(self.id);
@@ -86,52 +98,64 @@ impl<W: ?Sized> Drop for WidgetHandle<W> {
}
}
pub trait WidgetIdFn<W: ?Sized = dyn Widget>: FnOnce(&mut Ui) -> WidgetHandle<W> {}
impl<W: ?Sized, F: FnOnce(&mut Ui) -> WidgetHandle<W>> WidgetIdFn<W> for F {}
pub trait WidgetIdFn<State, W: ?Sized = dyn Widget<State>>:
FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>
{
}
impl<State, W: ?Sized, F: FnOnce(&mut Ui<State>) -> WidgetHandle<State, W>> WidgetIdFn<State, W>
for F
{
}
pub trait IdLike {
type Widget: Widget + ?Sized + 'static;
pub trait IdLike<State> {
type Widget: Widget<State> + ?Sized + 'static;
fn id(&self) -> WidgetId;
}
impl<W: Widget + ?Sized> IdLike for &WidgetHandle<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for &WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetRef<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl IdLike for WidgetId {
type Widget = dyn Widget;
impl<State: 'static> IdLike<State> for WidgetId {
type Widget = dyn Widget<State>;
fn id(&self) -> WidgetId {
*self
}
}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized, State> CoerceUnsized<WidgetHandle<State, U>>
for WidgetHandle<State, T>
{
}
impl<State, T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<State, U>>
for WidgetRef<State, T>
{
}
impl<W: ?Sized> Clone for WidgetRef<W> {
impl<State, W: ?Sized> Clone for WidgetRef<State, W> {
fn clone(&self) -> Self {
*self
}
}
impl<W: ?Sized> Copy for WidgetRef<W> {}
impl<W: ?Sized> PartialEq for WidgetRef<W> {
impl<State, W: ?Sized> Copy for WidgetRef<State, W> {}
impl<State, W: ?Sized> PartialEq for WidgetRef<State, W> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}