165 lines
3.9 KiB
Rust
165 lines
3.9 KiB
Rust
use std::{marker::Unsize, ops::CoerceUnsized, sync::mpsc::Sender};
|
|
|
|
use crate::{
|
|
UiRsc, 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 StrongWidget<W: ?Sized = dyn Widget> {
|
|
pub(super) id: WidgetId,
|
|
counter: RefCounter,
|
|
send: Sender<WidgetId>,
|
|
ty: *const W,
|
|
}
|
|
|
|
/// A weak handle to a widget.
|
|
/// Will not keep it alive, but can still be used for indexing like WidgetHandle.
|
|
pub struct WeakWidget<W: ?Sized = dyn Widget> {
|
|
pub(super) id: WidgetId,
|
|
#[allow(unused)]
|
|
ty: *const W,
|
|
}
|
|
|
|
impl<W: Widget + ?Sized + Unsize<dyn Widget>> StrongWidget<W> {
|
|
pub fn any(self) -> StrongWidget<dyn Widget> {
|
|
self
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> StrongWidget<W> {
|
|
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
|
|
Self {
|
|
id,
|
|
counter: RefCounter::new(),
|
|
send,
|
|
ty: null_ptr(),
|
|
}
|
|
}
|
|
|
|
pub fn id(&self) -> WidgetId {
|
|
self.id
|
|
}
|
|
|
|
pub fn refs(&self) -> u32 {
|
|
self.counter.refs()
|
|
}
|
|
|
|
pub fn weak(&self) -> WeakWidget<W> {
|
|
let Self { ty, id, .. } = *self;
|
|
WeakWidget { ty, id }
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> WeakWidget<W> {
|
|
pub(crate) fn new(id: WidgetId) -> Self {
|
|
Self { id, ty: null_ptr() }
|
|
}
|
|
|
|
pub fn id(&self) -> WidgetId {
|
|
self.id
|
|
}
|
|
|
|
#[track_caller]
|
|
pub fn upgrade(self, ui: &mut impl UiRsc) -> StrongWidget<W> {
|
|
ui.widgets_mut().upgrade(self)
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> Drop for StrongWidget<W> {
|
|
fn drop(&mut self) {
|
|
if self.counter.drop() {
|
|
let _ = self.send.send(self.id);
|
|
}
|
|
}
|
|
}
|
|
|
|
pub trait WidgetIdFn<Rsc, W: ?Sized = dyn Widget>: FnOnce(&mut Rsc) -> WeakWidget<W> {}
|
|
impl<Rsc, W: ?Sized, F: FnOnce(&mut Rsc) -> WeakWidget<W>> WidgetIdFn<Rsc, W> for F {}
|
|
|
|
pub trait IdLike {
|
|
type Widget: ?Sized;
|
|
fn id(&self) -> WidgetId;
|
|
}
|
|
|
|
impl<W: ?Sized> IdLike for &StrongWidget<W> {
|
|
type Widget = W;
|
|
fn id(&self) -> WidgetId {
|
|
self.id
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> IdLike for StrongWidget<W> {
|
|
type Widget = W;
|
|
fn id(&self) -> WidgetId {
|
|
self.id
|
|
}
|
|
}
|
|
|
|
impl<W: ?Sized> IdLike for WeakWidget<W> {
|
|
type Widget = W;
|
|
fn id(&self) -> WidgetId {
|
|
self.id
|
|
}
|
|
}
|
|
|
|
impl IdLike for WidgetId {
|
|
type Widget = dyn Widget;
|
|
fn id(&self) -> WidgetId {
|
|
*self
|
|
}
|
|
}
|
|
|
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<StrongWidget<U>> for StrongWidget<T> {}
|
|
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WeakWidget<U>> for WeakWidget<T> {}
|
|
|
|
impl<W: ?Sized> Clone for WeakWidget<W> {
|
|
fn clone(&self) -> Self {
|
|
*self
|
|
}
|
|
}
|
|
impl<W: ?Sized> Copy for WeakWidget<W> {}
|
|
impl<W: ?Sized> PartialEq for WeakWidget<W> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.id == other.id
|
|
}
|
|
}
|
|
|
|
impl<W> PartialEq for StrongWidget<W> {
|
|
fn eq(&self, other: &Self) -> bool {
|
|
self.id == other.id
|
|
}
|
|
}
|
|
|
|
impl<W> std::fmt::Debug for StrongWidget<W> {
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
self.id.fmt(f)
|
|
}
|
|
}
|
|
|
|
impl<'a, W: Widget + 'a, State: UiRsc> FnOnce<(&'a mut State,)> for WeakWidget<W> {
|
|
type Output = &'a mut W;
|
|
|
|
extern "rust-call" fn call_once(self, args: (&'a mut State,)) -> Self::Output {
|
|
&mut args.0.widgets_mut()[self]
|
|
}
|
|
}
|
|
|
|
fn null_ptr<W: ?Sized>() -> *const W {
|
|
if size_of::<&W>() == size_of::<*const dyn Widget>() {
|
|
let w: *const dyn Widget = &();
|
|
unsafe { std::mem::transmute_copy(&w) }
|
|
} else {
|
|
unsafe { std::mem::transmute_copy(&[0usize; 1]) }
|
|
}
|
|
}
|
|
|
|
unsafe impl<W: ?Sized> Send for WeakWidget<W> {}
|
|
unsafe impl<W: ?Sized> Sync for WeakWidget<W> {}
|