Files
iris/core/src/widget/handle.rs
2025-12-17 01:16:28 -05:00

170 lines
4.0 KiB
Rust

use std::{
marker::{PhantomData, Unsize},
mem::MaybeUninit,
ops::CoerceUnsized,
sync::mpsc::Sender,
};
use crate::{
Ui, Widget,
util::{RefCounter, SlotId},
};
pub type WidgetId = SlotId;
/// 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<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<State, W: ?Sized = dyn Widget<State>> {
pub(super) id: WidgetId,
#[allow(unused)]
ty: *const W,
state: PhantomData<State>,
}
pub struct WidgetHandles<State, W: ?Sized = dyn Widget<State>> {
pub h: WidgetHandle<State, W>,
pub r: WidgetRef<State, W>,
}
impl<State, W: Widget<State> + ?Sized + Unsize<dyn Widget<State>>> WidgetHandle<State, W> {
pub fn any(self) -> WidgetHandle<State, dyn Widget<State>> {
self
}
}
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 id(&self) -> WidgetId {
self.id
}
pub fn refs(&self) -> u32 {
self.counter.refs()
}
pub fn weak(&self) -> WidgetRef<State, W> {
let Self { ty, id, .. } = *self;
WidgetRef {
ty,
id,
state: PhantomData,
}
}
pub fn handles(self) -> WidgetHandles<State, W> {
let r = self.weak();
WidgetHandles { h: self, r }
}
}
impl<State, W: ?Sized> WidgetRef<State, W> {
pub fn id(&self) -> WidgetId {
self.id
}
}
impl<State, W: ?Sized> Drop for WidgetHandle<State, W> {
fn drop(&mut self) {
if self.counter.drop() {
let _ = self.send.send(self.id);
}
}
}
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<State> {
type Widget: Widget<State> + ?Sized + 'static;
fn id(&self) -> WidgetId;
}
impl<State, W: Widget<State> + ?Sized> IdLike<State> for &WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetHandle<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<State, W: Widget<State> + ?Sized> IdLike<State> for WidgetRef<State, W> {
type Widget = W;
fn id(&self) -> WidgetId {
self.id
}
}
impl<State: 'static> IdLike<State> for WidgetId {
type Widget = dyn Widget<State>;
fn id(&self) -> WidgetId {
*self
}
}
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<State, W: ?Sized> Clone for WidgetRef<State, W> {
fn clone(&self) -> Self {
*self
}
}
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
}
}
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)
}
}