initial global widget store
This commit is contained in:
1 parent
7f4846a2d3
commit
6156c66a20
31 files changed
+536
-352
No files matched your search
@@ -1,62 +1,53 @@
|
||||
use std::{
|
||||
cell::{Ref, RefMut},
|
||||
marker::Unsize,
|
||||
ops::CoerceUnsized,
|
||||
mem::MaybeUninit,
|
||||
ops::{CoerceUnsized, Deref, DerefMut},
|
||||
sync::mpsc::Sender,
|
||||
};
|
||||
|
||||
use crate::{
|
||||
layout::{IdFnTag, IdTag, Ui, Widget, WidgetLike},
|
||||
util::{Handle, Id, WeakHandle},
|
||||
layout::{IdFnTag, IdTag, Ui, WIDGETS, Widget, WidgetLike},
|
||||
util::{Handle, Ref, RefMap, RefMapMut, RefMut, WeakHandle},
|
||||
};
|
||||
|
||||
/// An handle for a widget in a UI.
|
||||
///
|
||||
/// TODO: ergonomic clones when they get put in rust-analyzer & don't cause ICEs?
|
||||
pub struct WidgetRef<W: ?Sized = dyn Widget>(Handle<Inner<W>>);
|
||||
pub struct WeakWidgetRef<W: ?Sized = dyn Widget>(WeakHandle<Inner<W>>);
|
||||
pub(super) type SlotTy = u32;
|
||||
|
||||
struct Inner<W: ?Sized> {
|
||||
id: Id,
|
||||
send: Sender<WidgetUpdate>,
|
||||
label: String,
|
||||
widget: W,
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
|
||||
pub struct WidgetId {
|
||||
pub(super) i: SlotTy,
|
||||
pub(super) genr: SlotTy,
|
||||
}
|
||||
|
||||
/// A strong handle to a widget. Cannot be cloned, only one may exist
|
||||
/// Use .weak() to obtain weak references to this
|
||||
pub struct WidgetHandle<W: ?Sized = dyn Widget>(WidgetId, Handle<WidgetData<W>>);
|
||||
/// A weak handle to a widget. Implements Copy so you can easily pass into closures
|
||||
pub struct WidgetRef<W: ?Sized = dyn Widget>(WidgetId, *const W);
|
||||
|
||||
pub(crate) struct WidgetData<W: ?Sized> {
|
||||
pub id: WidgetId,
|
||||
pub send: Option<Sender<WidgetUpdate>>,
|
||||
pub label: String,
|
||||
pub widget: W,
|
||||
}
|
||||
|
||||
pub enum WidgetUpdate {
|
||||
Drop(Id),
|
||||
Mutate(Id),
|
||||
Drop(WidgetId),
|
||||
Mutate(WidgetId),
|
||||
}
|
||||
|
||||
impl<W> PartialEq for WidgetRef<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.id() == other.id()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W> std::fmt::Debug for WidgetRef<W> {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.id().fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Clone for WidgetRef<W> {
|
||||
fn clone(&self) -> Self {
|
||||
Self(self.0.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> WidgetRef<W> {
|
||||
pub(super) fn new(id: Id, widget: W, send: Sender<WidgetUpdate>) -> Self {
|
||||
impl<W: Widget> WidgetHandle<W> {
|
||||
pub(super) fn new(id: WidgetId, widget: W) -> Self {
|
||||
let mut label = std::any::type_name::<W>().to_string();
|
||||
if let (Some(first), Some(last)) = (label.find(":"), label.rfind(":")) {
|
||||
label = label.split_at(first).0.to_string() + "::" + label.split_at(last + 1).1;
|
||||
}
|
||||
Self(
|
||||
Inner {
|
||||
widget,
|
||||
id,
|
||||
WidgetData {
|
||||
id,
|
||||
send,
|
||||
widget,
|
||||
send: None,
|
||||
label,
|
||||
}
|
||||
.into(),
|
||||
@@ -64,100 +55,225 @@ impl<W: Widget> WidgetRef<W> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetRef<W> {
|
||||
pub fn any(self) -> WidgetRef<dyn Widget> {
|
||||
WidgetRef(self.0)
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||
pub(super) fn weak_inner(&self) -> WeakHandle<WidgetData<W>> {
|
||||
self.1.weak()
|
||||
}
|
||||
pub fn as_any(&self) -> WidgetRef<dyn Widget> {
|
||||
WidgetRef(self.0.clone())
|
||||
|
||||
pub fn any(self) -> WidgetHandle<dyn Widget> {
|
||||
self
|
||||
}
|
||||
|
||||
/// DO NOT CALL OTHER THAN TEMP IN PAINTER
|
||||
pub(crate) fn as_any(&self) -> WidgetHandle<dyn Widget> {
|
||||
WidgetHandle(self.0, self.1.clone())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetRef<W> {
|
||||
pub fn id(&self) -> Id {
|
||||
self.0.get().id
|
||||
impl<W: ?Sized> WidgetHandle<W> {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.0
|
||||
}
|
||||
|
||||
pub fn get(&self) -> Ref<'_, W> {
|
||||
Ref::map(self.0.get(), |i| &i.widget)
|
||||
pub fn get(&self) -> RefMap<'_, W> {
|
||||
Ref::map(self.1.get(), |i| &mut i.widget)
|
||||
}
|
||||
|
||||
pub fn get_mut(&self) -> RefMut<'_, W> {
|
||||
let inner = self.0.get_mut();
|
||||
let _ = inner.send.send(WidgetUpdate::Mutate(inner.id));
|
||||
pub fn get_mut(&self) -> RefMapMut<'_, W> {
|
||||
let inner = self.1.get_mut();
|
||||
if let Some(send) = &inner.send {
|
||||
let _ = send.send(WidgetUpdate::Mutate(self.0));
|
||||
}
|
||||
RefMut::map(inner, |i| &mut i.widget)
|
||||
}
|
||||
|
||||
pub fn get_mut_quiet(&self) -> RefMut<'_, W> {
|
||||
RefMut::map(self.0.get_mut(), |i| &mut i.widget)
|
||||
pub(crate) fn data_mut(&self) -> RefMut<'_, WidgetData<W>> {
|
||||
self.1.get_mut()
|
||||
}
|
||||
|
||||
pub fn get_label(&self) -> Ref<'_, String> {
|
||||
Ref::map(self.0.get(), |i| &i.label)
|
||||
pub fn get_mut_quiet(&self) -> RefMapMut<'_, W> {
|
||||
RefMut::map(self.1.get_mut(), |i| &mut i.widget)
|
||||
}
|
||||
|
||||
pub fn get_label(&self) -> RefMap<'_, String> {
|
||||
Ref::map(self.1.get(), |i| &mut i.label)
|
||||
}
|
||||
|
||||
pub fn set_label(&self, label: impl Into<String>) {
|
||||
self.0.get_mut().label = label.into();
|
||||
self.1.get_mut().label = label.into();
|
||||
}
|
||||
|
||||
/// TODO: THIS SHOULD ALWAYS BE 1, remove this probably (weak count might be nice though)
|
||||
pub fn refs(&self) -> usize {
|
||||
self.0.refs()
|
||||
self.1.refs()
|
||||
}
|
||||
|
||||
pub fn weak(&self) -> WeakWidgetRef<W> {
|
||||
WeakWidgetRef(self.0.weak())
|
||||
pub fn weak(&self) -> WidgetRef<W> {
|
||||
WidgetRef(self.0, null_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WeakWidgetRef<W> {
|
||||
/// should guarantee that widget is still valid to prevent indexing failures
|
||||
pub(crate) fn expect_strong(&self) -> WidgetRef<W> {
|
||||
WidgetRef(self.0.strong().expect("widget should not be dropped"))
|
||||
impl<W: Widget> WidgetRef<W> {
|
||||
fn handle(&self) -> Handle<WidgetData<W>> {
|
||||
WIDGETS.get_type(self.0).unwrap()
|
||||
}
|
||||
|
||||
pub fn get<'a>(&self) -> WRef<'a, W> {
|
||||
let handle = self.handle();
|
||||
WRef {
|
||||
val: unsafe {
|
||||
std::mem::transmute::<RefMap<'_, W>, RefMap<'_, W>>(Ref::map(handle.get(), |i| {
|
||||
&mut i.widget
|
||||
}))
|
||||
},
|
||||
_handle: handle,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_mut<'a>(&self) -> WRefMut<'a, W> {
|
||||
let handle = self.handle();
|
||||
if let Some(send) = &handle.get().send {
|
||||
let _ = send.send(WidgetUpdate::Mutate(self.0));
|
||||
}
|
||||
WRefMut {
|
||||
val: unsafe {
|
||||
std::mem::transmute::<RefMapMut<'_, W>, RefMapMut<'_, W>>(RefMut::map(
|
||||
handle.get_mut(),
|
||||
|i| &mut i.widget,
|
||||
))
|
||||
},
|
||||
_handle: handle,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WeakWidgetRef<W> {
|
||||
pub fn any(self) -> WeakWidgetRef<dyn Widget> {
|
||||
WeakWidgetRef(self.0.clone())
|
||||
pub struct WRef<'a, W> {
|
||||
_handle: Handle<WidgetData<W>>,
|
||||
val: RefMap<'a, W>,
|
||||
}
|
||||
|
||||
pub struct WRefMut<'a, W> {
|
||||
_handle: Handle<WidgetData<W>>,
|
||||
val: RefMapMut<'a, W>,
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetRef<W> {
|
||||
pub fn any(self) -> WidgetRef<dyn Widget> {
|
||||
WidgetRef(self.0, null_ptr())
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Drop for Inner<W> {
|
||||
fn null_ptr<T: ?Sized>() -> *const T {
|
||||
unsafe { MaybeUninit::zeroed().assume_init() }
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetRef<W> {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl WidgetId {
|
||||
/// THIS SHOULD ONLY BE CALLED FOR TEMP STUFF DURING PAINTING
|
||||
pub(crate) fn strong(self) -> Option<WidgetHandle> {
|
||||
Some(WidgetHandle(self, WIDGETS.get(self)?))
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Drop for WidgetData<W> {
|
||||
fn drop(&mut self) {
|
||||
let _ = self.send.send(WidgetUpdate::Drop(self.id));
|
||||
if let Some(send) = &self.send {
|
||||
WIDGETS.remove(self.id);
|
||||
let _ = send.send(WidgetUpdate::Drop(self.id));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetIdFn<W: ?Sized = dyn Widget>: FnOnce(&mut Ui) -> WidgetRef<W> {}
|
||||
impl<W: ?Sized, F: FnOnce(&mut Ui) -> WidgetRef<W>> WidgetIdFn<W> for F {}
|
||||
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 WidgetRet: FnOnce(&mut Ui) -> WidgetRef {}
|
||||
impl<F: FnOnce(&mut Ui) -> WidgetRef> WidgetRet for F {}
|
||||
pub trait WidgetRet: FnOnce(&mut Ui) -> WidgetHandle {}
|
||||
impl<F: FnOnce(&mut Ui) -> WidgetHandle> WidgetRet for F {}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget> + 'static> WidgetLike<IdTag> for WidgetRef<W> {
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget> + 'static> WidgetLike<IdTag> for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn add(self, _: &mut Ui) -> WidgetRef<W> {
|
||||
fn add(self, _: &mut Ui) -> WidgetHandle<W> {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget> + 'static, F: FnOnce(&mut Ui) -> WidgetRef<W>>
|
||||
impl<W: Widget + ?Sized + Unsize<dyn Widget> + 'static, F: FnOnce(&mut Ui) -> WidgetHandle<W>>
|
||||
WidgetLike<IdFnTag> for F
|
||||
{
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetRef<W> {
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IdLike<W> {
|
||||
fn id(&self) -> Id;
|
||||
fn id(&self) -> WidgetId;
|
||||
}
|
||||
|
||||
impl<W> IdLike<W> for WidgetRef<W> {
|
||||
fn id(&self) -> Id {
|
||||
impl<W> IdLike<W> for WidgetHandle<W> {
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id()
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<W> {}
|
||||
impl<W> IdLike<W> for WidgetRef<W> {
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetHandle<U>> for WidgetHandle<T> {}
|
||||
impl<T: ?Sized + Unsize<U>, U: ?Sized> CoerceUnsized<WidgetRef<U>> for WidgetRef<T> {}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Clone for WidgetRef<W> {
|
||||
fn clone(&self) -> Self {
|
||||
*self
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: ?Sized> Copy for WidgetRef<W> {}
|
||||
|
||||
impl<W: ?Sized> PartialEq for WidgetRef<W> {
|
||||
fn eq(&self, other: &Self) -> bool {
|
||||
self.0 == other.0 && self.1 == other.1
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for WRef<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.val
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for WRefMut<'_, T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.val
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> DerefMut for WRefMut<'_, T> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.val
|
||||
}
|
||||
}
|
||||
@@ -4,11 +4,11 @@ use std::marker::Unsize;
|
||||
pub trait WidgetLike<Tag> {
|
||||
type Widget: Widget + ?Sized + Unsize<dyn Widget> + 'static;
|
||||
|
||||
fn add(self, ui: &mut Ui) -> WidgetRef<Self::Widget>;
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<Self::Widget>;
|
||||
|
||||
fn with_id<W2>(
|
||||
self,
|
||||
f: impl FnOnce(&mut Ui, WidgetRef<Self::Widget>) -> WidgetRef<W2>,
|
||||
f: impl FnOnce(&mut Ui, WidgetHandle<Self::Widget>) -> WidgetHandle<W2>,
|
||||
) -> impl WidgetIdFn<W2>
|
||||
where
|
||||
Self: Sized,
|
||||
@@ -28,11 +28,11 @@ pub trait WidgetLike<Tag> {
|
||||
}
|
||||
|
||||
pub struct WidgetArr<const LEN: usize> {
|
||||
pub arr: [WidgetRef; LEN],
|
||||
pub arr: [WidgetHandle; LEN],
|
||||
}
|
||||
|
||||
impl<const LEN: usize> WidgetArr<LEN> {
|
||||
pub fn new(arr: [WidgetRef; LEN]) -> Self {
|
||||
pub fn new(arr: [WidgetHandle; LEN]) -> Self {
|
||||
Self { arr }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,7 +10,7 @@ pub use widgets::*;
|
||||
|
||||
use crate::layout::{Len, Painter, SizeCtx, Ui};
|
||||
|
||||
pub trait Widget: 'static {
|
||||
pub trait Widget: 'static + Send + Sync {
|
||||
fn draw(&mut self, painter: &mut Painter);
|
||||
fn desired_width(&mut self, ctx: &mut SizeCtx) -> Len;
|
||||
fn desired_height(&mut self, ctx: &mut SizeCtx) -> Len;
|
||||
@@ -34,30 +34,30 @@ impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetFn<W> for F {}
|
||||
|
||||
impl<W: Widget, F: FnOnce(&mut Ui) -> W> WidgetLike<FnTag> for F {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetRef<W> {
|
||||
fn add(self, ui: &mut Ui) -> WidgetHandle<W> {
|
||||
self(ui).add(ui)
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget> WidgetLike<WidgetTag> for W {
|
||||
type Widget = W;
|
||||
fn add(self, ui: &mut Ui) -> WidgetRef<W> {
|
||||
ui.add_widget(self)
|
||||
fn add(self, _: &mut Ui) -> WidgetHandle<W> {
|
||||
WIDGETS.insert(self)
|
||||
}
|
||||
}
|
||||
|
||||
pub trait WidgetOption {
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetRef>;
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetHandle>;
|
||||
}
|
||||
|
||||
impl WidgetOption for () {
|
||||
fn get(self, _: &mut Ui) -> Option<WidgetRef> {
|
||||
fn get(self, _: &mut Ui) -> Option<WidgetHandle> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl<F: FnOnce(&mut Ui) -> Option<WidgetRef>> WidgetOption for F {
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetRef> {
|
||||
impl<F: FnOnce(&mut Ui) -> Option<WidgetHandle>> WidgetOption for F {
|
||||
fn get(self, ui: &mut Ui) -> Option<WidgetHandle> {
|
||||
self(ui)
|
||||
}
|
||||
}
|
||||
@@ -1,50 +1,108 @@
|
||||
use std::sync::mpsc::Sender;
|
||||
|
||||
use crate::{
|
||||
layout::{WeakWidgetRef, Widget, WidgetRef, WidgetUpdate},
|
||||
util::{HashMap, Id, IdTracker},
|
||||
use std::{
|
||||
any::Any,
|
||||
sync::{Mutex, MutexGuard},
|
||||
};
|
||||
|
||||
pub struct Widgets {
|
||||
ids: IdTracker,
|
||||
map: HashMap<Id, WeakWidgetRef>,
|
||||
send: Sender<WidgetUpdate>,
|
||||
use crate::{
|
||||
layout::{
|
||||
Widget, WidgetData, WidgetHandle,
|
||||
widget::{SlotTy, WidgetId},
|
||||
},
|
||||
util::{Handle, WeakHandle},
|
||||
};
|
||||
|
||||
pub(crate) static WIDGETS: Widgets = Widgets::new();
|
||||
pub(crate) struct Widgets(Mutex<Inner>);
|
||||
|
||||
pub fn total_widgets() -> usize {
|
||||
WIDGETS.len()
|
||||
}
|
||||
|
||||
pub struct WidgetSlot {
|
||||
genr: SlotTy,
|
||||
data: WeakHandle<WidgetData<dyn Widget + Sync + Send>>,
|
||||
}
|
||||
|
||||
struct Inner {
|
||||
cur_id: SlotTy,
|
||||
vec: Vec<WidgetSlot>,
|
||||
free: Vec<SlotTy>,
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
pub fn new(send: Sender<WidgetUpdate>) -> Self {
|
||||
Self {
|
||||
ids: IdTracker::default(),
|
||||
map: HashMap::default(),
|
||||
send,
|
||||
pub const fn new() -> Self {
|
||||
Self(Mutex::new(Inner {
|
||||
cur_id: 0,
|
||||
vec: Vec::new(),
|
||||
free: Vec::new(),
|
||||
}))
|
||||
}
|
||||
|
||||
fn expect(&self) -> MutexGuard<'_, Inner> {
|
||||
self.0.lock().unwrap()
|
||||
}
|
||||
|
||||
pub fn get_type<W: 'static>(&self, id: WidgetId) -> Option<Handle<WidgetData<W>>> {
|
||||
let slot = &self.expect().vec[id.i as usize];
|
||||
if slot.genr != id.genr {
|
||||
None
|
||||
} else {
|
||||
Some(
|
||||
unsafe { slot.data.clone().downcast() }
|
||||
.unwrap()
|
||||
.clone()
|
||||
.strong()?,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get(&self, id: Id) -> WidgetRef {
|
||||
self.map.get(&id).unwrap().expect_strong()
|
||||
pub fn get(&self, id: WidgetId) -> Option<Handle<WidgetData<dyn Widget>>> {
|
||||
let slot = &self.expect().vec[id.i as usize];
|
||||
if slot.genr != id.genr {
|
||||
None
|
||||
} else {
|
||||
Some(slot.data.clone().strong()?)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn insert<W: Widget>(&mut self, widget: W) -> WidgetRef<W> {
|
||||
let id = self.ids.next();
|
||||
let rf = WidgetRef::new(id, widget, self.send.clone());
|
||||
self.map.insert(id, rf.weak().any());
|
||||
rf
|
||||
pub fn insert<W: Widget>(&self, widget: W) -> WidgetHandle<W> {
|
||||
let mut s = self.expect();
|
||||
let id = s
|
||||
.free
|
||||
.pop()
|
||||
.map(|i| {
|
||||
assert!(s.vec[i as usize].data.dropped());
|
||||
WidgetId {
|
||||
i,
|
||||
genr: s.vec[i as usize].genr,
|
||||
}
|
||||
})
|
||||
.unwrap_or_else(|| {
|
||||
let i = s.cur_id;
|
||||
s.cur_id += 1;
|
||||
WidgetId { i, genr: 0 }
|
||||
});
|
||||
let handle = WidgetHandle::new(id, widget);
|
||||
let slot = WidgetSlot {
|
||||
genr: id.genr,
|
||||
data: handle.weak_inner(),
|
||||
};
|
||||
if id.i == s.vec.len() as SlotTy {
|
||||
s.vec.push(slot);
|
||||
} else {
|
||||
s.vec[id.i as usize] = slot;
|
||||
}
|
||||
handle
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: Id) {
|
||||
self.map.remove(&id);
|
||||
self.ids.free(id);
|
||||
}
|
||||
|
||||
pub fn reserve(&mut self) -> Id {
|
||||
self.ids.next()
|
||||
pub fn remove(&self, id: WidgetId) {
|
||||
let mut s = self.expect();
|
||||
s.vec[id.i as usize].genr += 1;
|
||||
s.free.push(id.i);
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.is_empty()
|
||||
let s = self.expect();
|
||||
s.vec.len() - s.free.len()
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user