strong & weak widgets

This commit is contained in:
2025-12-11 07:16:06 -05:00
parent a85e129026
commit 36668c82f4
19 changed files with 293 additions and 294 deletions

130
core/src/widget/handle.rs Normal file
View File

@@ -0,0 +1,130 @@
use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Sender};
use crate::{
Ui, Widget,
util::{Id, RefCounter},
};
/// 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.
///
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
pub struct WidgetHandle<W: ?Sized = dyn Widget> {
pub(super) id: Id,
counter: RefCounter,
send: Sender<Id>,
ty: *const W,
}
pub struct WidgetView<W: ?Sized = dyn Widget> {
pub(super) id: Id,
#[allow(unused)]
ty: *const W,
}
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
pub fn any(self) -> WidgetHandle<dyn Widget> {
self
}
}
impl<W: ?Sized> WidgetHandle<W> {
pub(crate) fn new(id: Id, send: Sender<Id>) -> Self {
Self {
id,
counter: RefCounter::new(),
send,
ty: unsafe { MaybeUninit::zeroed().assume_init() },
}
}
pub fn as_any(&self) -> &WidgetHandle<dyn Widget> {
// SAFETY: self is repr(C) and generic only used for phantom data
unsafe { std::mem::transmute(self) }
}
pub fn id(&self) -> Id {
self.id
}
pub fn refs(&self) -> u32 {
self.counter.refs()
}
pub fn weak(&self) -> WidgetView<W> {
let Self { ty, id, .. } = *self;
WidgetView { ty, id }
}
pub fn handles(self) -> (Self, WidgetView<W>) {
let weak = self.weak();
(self, weak)
}
}
impl<W: ?Sized> WidgetView<W> {
pub fn id(&self) -> Id {
self.id
}
}
impl<W: ?Sized> Drop for WidgetHandle<W> {
fn drop(&mut self) {
if self.counter.drop() {
let _ = self.send.send(self.id);
}
}
}
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 IdLike {
type Widget: Widget + ?Sized + 'static;
fn id(&self) -> Id;
}
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
type Widget = W;
fn id(&self) -> Id {
self.id
}
}
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
type Widget = W;
fn id(&self) -> Id {
self.id
}
}
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<W: ?Sized> Clone for WidgetView<W> {
fn clone(&self) -> Self {
*self
}
}
impl<W: ?Sized> Copy for WidgetView<W> {}
impl<W: ?Sized> PartialEq for WidgetView<W> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<W> PartialEq for WidgetHandle<W> {
fn eq(&self, other: &Self) -> bool {
self.id == other.id
}
}
impl<W> std::fmt::Debug for WidgetHandle<W> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
self.id.fmt(f)
}
}