idk work (r + h)

This commit is contained in:
2025-12-11 23:05:27 -05:00
parent a70d09e162
commit a708813ce7
11 changed files with 152 additions and 102 deletions

View File

@@ -7,12 +7,9 @@ use crate::{
pub type WidgetId = SlotId;
/// An identifier for a widget that can index a UI to get the associated widget.
/// It should always remain valid; it keeps a ref count and removes the widget from the UI if all
/// references are dropped.
///
/// 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.
/// An identifier for a widget that can index a UI or event ctx to get it.
/// This is a strong handle that does not impl Clone, and when it is dropped,
/// 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> {
@@ -22,13 +19,18 @@ pub struct WidgetHandle<W: ?Sized = dyn Widget> {
ty: *const W,
}
pub struct WidgetView<W: ?Sized = dyn Widget> {
/// 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(super) id: WidgetId,
#[allow(unused)]
ty: *const W,
}
pub type WidgetHandles<W> = (WidgetHandle<W>, WidgetView<W>);
pub struct WidgetHandles<W: ?Sized = dyn Widget> {
pub h: WidgetHandle<W>,
pub r: WidgetRef<W>,
}
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> {
@@ -59,18 +61,18 @@ impl<W: ?Sized> WidgetHandle<W> {
self.counter.refs()
}
pub fn weak(&self) -> WidgetView<W> {
pub fn weak(&self) -> WidgetRef<W> {
let Self { ty, id, .. } = *self;
WidgetView { ty, id }
WidgetRef { ty, id }
}
pub fn handles(self) -> WidgetHandles<W> {
let weak = self.weak();
(self, weak)
let r = self.weak();
WidgetHandles { h: self, r }
}
}
impl<W: ?Sized> WidgetView<W> {
impl<W: ?Sized> WidgetRef<W> {
pub fn id(&self) -> WidgetId {
self.id
}
@@ -99,7 +101,7 @@ impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
}
}
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
impl<W: Widget + ?Sized> IdLike for WidgetRef<W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
@@ -107,15 +109,15 @@ impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetView<U>> for WidgetView<T> {}
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
impl<W: ?Sized> Clone for WidgetView<W> {
impl<W: ?Sized> Clone for WidgetRef<W> {
fn clone(&self) -> Self {
*self
}
}
impl<W: ?Sized> Copy for WidgetView<W> {}
impl<W: ?Sized> PartialEq for WidgetView<W> {
impl<W: ?Sized> Copy for WidgetRef<W> {}
impl<W: ?Sized> PartialEq for WidgetRef<W> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}