preparation
This commit is contained in:
@@ -1,4 +1,12 @@
|
||||
use std::{any::TypeId, marker::PhantomData, sync::mpsc::Sender};
|
||||
use std::{
|
||||
any::TypeId,
|
||||
marker::PhantomData,
|
||||
sync::{
|
||||
Arc,
|
||||
atomic::{AtomicBool, Ordering},
|
||||
mpsc::Sender,
|
||||
},
|
||||
};
|
||||
|
||||
use crate::{
|
||||
layout::{FnTag, Ui, Widget, WidgetLike, WidgetTag},
|
||||
@@ -21,7 +29,7 @@ pub struct WidgetId<W = AnyWidget> {
|
||||
pub(super) id: Id,
|
||||
counter: RefCounter,
|
||||
send: Sender<Id>,
|
||||
is_static: bool,
|
||||
is_static: Arc<AtomicBool>,
|
||||
_pd: PhantomData<W>,
|
||||
}
|
||||
|
||||
@@ -52,7 +60,7 @@ impl<W> Clone for WidgetId<W> {
|
||||
ty: self.ty,
|
||||
counter: self.counter.clone(),
|
||||
send: self.send.clone(),
|
||||
is_static: self.is_static,
|
||||
is_static: self.is_static.clone(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -65,7 +73,7 @@ impl<W> WidgetId<W> {
|
||||
id,
|
||||
counter: RefCounter::new(),
|
||||
send,
|
||||
is_static,
|
||||
is_static: Arc::new(is_static.into()),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -88,11 +96,11 @@ impl<W> WidgetId<W> {
|
||||
self.counter.refs()
|
||||
}
|
||||
|
||||
pub(super) fn into_static(self) -> StaticWidgetId<W> {
|
||||
let s = std::mem::ManuallyDrop::new(self);
|
||||
pub fn into_static(self) -> StaticWidgetId<W> {
|
||||
self.is_static.store(true, Ordering::Release);
|
||||
StaticWidgetId {
|
||||
ty: s.ty,
|
||||
id: s.id.copyable(),
|
||||
ty: self.ty,
|
||||
id: self.id.copyable(),
|
||||
_pd: PhantomData,
|
||||
}
|
||||
}
|
||||
@@ -110,7 +118,7 @@ impl WidgetId {
|
||||
|
||||
impl<W> Drop for WidgetId<W> {
|
||||
fn drop(&mut self) {
|
||||
if self.counter.drop() && !self.is_static {
|
||||
if self.counter.drop() && !self.is_static.load(Ordering::Acquire) {
|
||||
let _ = self.send.send(self.id.duplicate());
|
||||
}
|
||||
}
|
||||
@@ -119,21 +127,14 @@ impl<W> Drop for WidgetId<W> {
|
||||
pub struct IdTag;
|
||||
pub struct IdFnTag;
|
||||
|
||||
// pub trait WidgetIdFn<W, Ctx> = FnOnce(&mut Ui) -> WidgetId<W>;
|
||||
macro_rules! WidgetIdFn {
|
||||
($W:ty) => {
|
||||
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W>
|
||||
};
|
||||
($W:ty, $($use:tt)*) => {
|
||||
impl FnOnce(&mut $crate::layout::Ui) -> $crate::layout::WidgetId<$W> + use<$($use)*>
|
||||
};
|
||||
}
|
||||
pub(crate) use WidgetIdFn;
|
||||
pub trait WidgetIdFn<W>: FnOnce(&mut Ui) -> WidgetId<W> {}
|
||||
impl<W, F: FnOnce(&mut Ui) -> WidgetId<W>> WidgetIdFn<W> for F {}
|
||||
|
||||
/// TODO: does this ever make sense to use? it allows for invalid ids
|
||||
pub trait Idable<Tag> {
|
||||
type Widget: Widget;
|
||||
fn set(self, ui: &mut Ui, id: &WidgetId<Self::Widget>);
|
||||
fn id<'a>(self, id: &WidgetId<Self::Widget>) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
|
||||
fn id(self, id: &WidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
@@ -143,10 +144,7 @@ pub trait Idable<Tag> {
|
||||
id
|
||||
}
|
||||
}
|
||||
fn id_static<'a>(
|
||||
self,
|
||||
id: StaticWidgetId<Self::Widget>,
|
||||
) -> WidgetIdFn!(Self::Widget, 'a, Self, Tag)
|
||||
fn id_static(self, id: StaticWidgetId<Self::Widget>) -> impl WidgetIdFn<Self::Widget>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user