proper widgetid + slot vec instead of map
This commit is contained in:
@@ -2,9 +2,11 @@ use std::{marker::Unsize, mem::MaybeUninit, ops::CoerceUnsized, sync::mpsc::Send
|
||||
|
||||
use crate::{
|
||||
Ui, Widget,
|
||||
util::{Id, RefCounter},
|
||||
util::{RefCounter, SlotId},
|
||||
};
|
||||
|
||||
pub type WidgetId = SlotId;
|
||||
|
||||
/// 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.
|
||||
@@ -14,14 +16,14 @@ use crate::{
|
||||
///
|
||||
/// 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,
|
||||
pub(super) id: WidgetId,
|
||||
counter: RefCounter,
|
||||
send: Sender<Id>,
|
||||
send: Sender<WidgetId>,
|
||||
ty: *const W,
|
||||
}
|
||||
|
||||
pub struct WidgetView<W: ?Sized = dyn Widget> {
|
||||
pub(super) id: Id,
|
||||
pub(super) id: WidgetId,
|
||||
#[allow(unused)]
|
||||
ty: *const W,
|
||||
}
|
||||
@@ -35,7 +37,7 @@ impl<W: Widget + ?Sized + Unsize<dyn Widget>> WidgetHandle<W> {
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetHandle<W> {
|
||||
pub(crate) fn new(id: Id, send: Sender<Id>) -> Self {
|
||||
pub(crate) fn new(id: WidgetId, send: Sender<WidgetId>) -> Self {
|
||||
Self {
|
||||
id,
|
||||
counter: RefCounter::new(),
|
||||
@@ -49,7 +51,7 @@ impl<W: ?Sized> WidgetHandle<W> {
|
||||
unsafe { std::mem::transmute(self) }
|
||||
}
|
||||
|
||||
pub fn id(&self) -> Id {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
|
||||
@@ -69,7 +71,7 @@ impl<W: ?Sized> WidgetHandle<W> {
|
||||
}
|
||||
|
||||
impl<W: ?Sized> WidgetView<W> {
|
||||
pub fn id(&self) -> Id {
|
||||
pub fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
@@ -87,19 +89,19 @@ 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;
|
||||
fn id(&self) -> WidgetId;
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetHandle<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
impl<W: Widget + ?Sized> IdLike for WidgetView<W> {
|
||||
type Widget = W;
|
||||
fn id(&self) -> Id {
|
||||
fn id(&self) -> WidgetId {
|
||||
self.id
|
||||
}
|
||||
}
|
||||
|
||||
@@ -27,6 +27,16 @@ impl Widget for () {
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Widget {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
}
|
||||
|
||||
pub fn as_any_mut(&mut self) -> &mut dyn Any {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
/// A function that returns a widget given a UI.
|
||||
/// Useful for defining trait functions on widgets that create a parent widget so that the children
|
||||
/// don't need to be IDs yet
|
||||
|
||||
@@ -1,13 +1,12 @@
|
||||
use crate::{
|
||||
IdLike, Widget,
|
||||
util::{DynBorrower, HashMap, HashSet, Id, IdTracker},
|
||||
IdLike, Widget, WidgetId,
|
||||
util::{DynBorrower, HashSet, SlotVec},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets {
|
||||
pub updates: HashSet<Id>,
|
||||
ids: IdTracker,
|
||||
map: HashMap<Id, WidgetData>,
|
||||
pub updates: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData>,
|
||||
}
|
||||
|
||||
pub struct WidgetData {
|
||||
@@ -22,23 +21,23 @@ impl Widgets {
|
||||
!self.updates.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: Id) -> Option<&dyn Widget> {
|
||||
Some(self.map.get(&id)?.widget.as_ref())
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget> {
|
||||
Some(self.vec.get(id)?.widget.as_ref())
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: Id) -> Option<&mut dyn Widget> {
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
|
||||
self.updates.insert(id);
|
||||
Some(self.map.get_mut(&id)?.widget.as_mut())
|
||||
Some(self.vec.get_mut(id)?.widget.as_mut())
|
||||
}
|
||||
|
||||
/// get_dyn but dynamic borrow checking of widgets
|
||||
/// lets you do recursive (tree) operations, like the painter does
|
||||
pub fn get_dyn_dynamic(&self, id: Id) -> WidgetWrapper<'_> {
|
||||
pub fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
|
||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
#[allow(mutable_transmutes)]
|
||||
let data = unsafe {
|
||||
std::mem::transmute::<&WidgetData, &mut WidgetData>(self.map.get(&id).unwrap())
|
||||
std::mem::transmute::<&WidgetData, &mut WidgetData>(self.vec.get(id).unwrap())
|
||||
};
|
||||
if data.borrowed {
|
||||
panic!("tried to mutably borrow the same widget twice");
|
||||
@@ -46,60 +45,57 @@ impl Widgets {
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget> where I::Widget: Sized {
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.get_dyn(id.id())?.as_any().downcast_ref()
|
||||
}
|
||||
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget> where I::Widget: Sized {
|
||||
pub fn get_mut<I: IdLike>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
where
|
||||
I::Widget: Sized,
|
||||
{
|
||||
self.get_dyn_mut(id.id())?.as_any_mut().downcast_mut()
|
||||
}
|
||||
|
||||
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
|
||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
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.insert_any(id, Box::new(widget), label);
|
||||
self.insert_any(Box::new(widget), label)
|
||||
}
|
||||
|
||||
pub fn data(&self, id: &Id) -> Option<&WidgetData> {
|
||||
self.map.get(id)
|
||||
pub fn data(&self, id: WidgetId) -> Option<&WidgetData> {
|
||||
self.vec.get(id)
|
||||
}
|
||||
|
||||
pub fn label(&self, id: &Id) -> &String {
|
||||
pub fn label(&self, id: WidgetId) -> &String {
|
||||
&self.data(id).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: &Id) -> Option<&mut WidgetData> {
|
||||
self.map.get_mut(id)
|
||||
pub fn data_mut(&mut self, id: WidgetId) -> Option<&mut WidgetData> {
|
||||
self.vec.get_mut(id)
|
||||
}
|
||||
|
||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>, label: String) {
|
||||
self.map.insert(
|
||||
id,
|
||||
WidgetData {
|
||||
widget,
|
||||
label,
|
||||
borrowed: false,
|
||||
},
|
||||
);
|
||||
pub fn insert_any(&mut self, widget: Box<dyn Widget>, label: String) -> WidgetId {
|
||||
self.vec.add(WidgetData {
|
||||
widget,
|
||||
label,
|
||||
borrowed: false,
|
||||
})
|
||||
}
|
||||
|
||||
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 delete(&mut self, id: WidgetId) {
|
||||
self.vec.free(id);
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
}
|
||||
|
||||
#[allow(clippy::len_without_is_empty)]
|
||||
pub fn len(&self) -> usize {
|
||||
self.map.len()
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.map.is_empty()
|
||||
self.vec.len()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user