RE ADD CONTEXT
This commit is contained in:
@@ -3,34 +3,44 @@ use crate::{
|
||||
util::{DynBorrower, HashSet, SlotVec},
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct Widgets {
|
||||
pub struct Widgets<State> {
|
||||
pub updates: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData>,
|
||||
vec: SlotVec<WidgetData<State>>,
|
||||
}
|
||||
|
||||
impl Widgets {
|
||||
impl<State> Default for Widgets<State> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
updates: Default::default(),
|
||||
vec: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Widgets<State> {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.updates.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget> {
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
|
||||
Some(self.vec.get(id)?.widget.as_ref())
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget<State>> {
|
||||
self.updates.insert(id);
|
||||
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(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_> {
|
||||
pub(crate) fn get_dyn_dynamic(&self, id: WidgetId) -> WidgetWrapper<'_, State> {
|
||||
// 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.vec.get(id).unwrap())
|
||||
std::mem::transmute::<&WidgetData<State>, &mut WidgetData<State>>(
|
||||
self.vec.get(id).unwrap(),
|
||||
)
|
||||
};
|
||||
if data.borrowed {
|
||||
panic!("tried to mutably borrow the same widget twice");
|
||||
@@ -38,37 +48,37 @@ impl Widgets {
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike>(&self, id: &I) -> Option<&I::Widget>
|
||||
pub fn get<I: IdLike<State>>(&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>
|
||||
pub fn get_mut<I: IdLike<State>>(&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 add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
pub fn add<W: Widget<State>>(&mut self, widget: W) -> WidgetId {
|
||||
self.vec.add(WidgetData::new(widget))
|
||||
}
|
||||
|
||||
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||
pub fn data(&self, id: impl IdLike<State>) -> Option<&WidgetData<State>> {
|
||||
self.vec.get(id.id())
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
&self.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||
pub fn data_mut(&mut self, id: impl IdLike<State>) -> Option<&mut WidgetData<State>> {
|
||||
self.vec.get_mut(id.id())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: impl IdLike) {
|
||||
pub fn delete(&mut self, id: impl IdLike<State>) {
|
||||
self.vec.free(id.id());
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
@@ -80,4 +90,4 @@ impl Widgets {
|
||||
}
|
||||
}
|
||||
|
||||
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
|
||||
pub type WidgetWrapper<'a, State> = DynBorrower<'a, dyn Widget<State>>;
|
||||
|
||||
Reference in New Issue
Block a user