remove state generic from a lot of things
This commit is contained in:
@@ -3,12 +3,12 @@ use crate::{
|
||||
util::{DynBorrower, HashSet, SlotVec, forget_mut, to_mut},
|
||||
};
|
||||
|
||||
pub struct Widgets<State> {
|
||||
pub struct Widgets {
|
||||
pub needs_redraw: HashSet<WidgetId>,
|
||||
vec: SlotVec<WidgetData<State>>,
|
||||
vec: SlotVec<WidgetData>,
|
||||
}
|
||||
|
||||
impl<State> Default for Widgets<State> {
|
||||
impl Default for Widgets {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
needs_redraw: Default::default(),
|
||||
@@ -17,23 +17,23 @@ impl<State> Default for Widgets<State> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<State: 'static> Widgets<State> {
|
||||
impl Widgets {
|
||||
pub fn has_updates(&self) -> bool {
|
||||
!self.needs_redraw.is_empty()
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: WidgetId) -> Option<&dyn Widget<State>> {
|
||||
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: WidgetId) -> Option<&mut dyn Widget<State>> {
|
||||
pub fn get_dyn_mut(&mut self, id: WidgetId) -> Option<&mut dyn Widget> {
|
||||
self.needs_redraw.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<'a>(&self, id: WidgetId) -> WidgetWrapper<'a, State> {
|
||||
pub(crate) fn get_dyn_dynamic<'a>(&self, id: WidgetId) -> WidgetWrapper<'a> {
|
||||
// SAFETY: must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
let data = unsafe { forget_mut(to_mut(self.vec.get(id).unwrap())) };
|
||||
@@ -43,37 +43,37 @@ impl<State: 'static> Widgets<State> {
|
||||
WidgetWrapper::new(data.widget.as_mut(), &mut data.borrowed)
|
||||
}
|
||||
|
||||
pub fn get<I: IdLike<State>>(&self, id: &I) -> Option<&I::Widget>
|
||||
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<State>>(&mut self, id: &I) -> Option<&mut I::Widget>
|
||||
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 add<W: Widget<State>>(&mut self, widget: W) -> WidgetId {
|
||||
pub fn add<W: Widget>(&mut self, widget: W) -> WidgetId {
|
||||
self.vec.add(WidgetData::new(widget))
|
||||
}
|
||||
|
||||
pub fn data(&self, id: impl IdLike<State>) -> Option<&WidgetData<State>> {
|
||||
pub fn data(&self, id: impl IdLike) -> Option<&WidgetData> {
|
||||
self.vec.get(id.id())
|
||||
}
|
||||
|
||||
pub fn label(&self, id: impl IdLike<State>) -> &String {
|
||||
pub fn label(&self, id: impl IdLike) -> &String {
|
||||
&self.data(id.id()).unwrap().label
|
||||
}
|
||||
|
||||
pub fn data_mut(&mut self, id: impl IdLike<State>) -> Option<&mut WidgetData<State>> {
|
||||
pub fn data_mut(&mut self, id: impl IdLike) -> Option<&mut WidgetData> {
|
||||
self.vec.get_mut(id.id())
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: impl IdLike<State>) {
|
||||
pub fn delete(&mut self, id: impl IdLike) {
|
||||
self.vec.free(id.id());
|
||||
// not sure if there's any point in this
|
||||
// self.updates.remove(&id);
|
||||
@@ -85,4 +85,4 @@ impl<State: 'static> Widgets<State> {
|
||||
}
|
||||
}
|
||||
|
||||
pub type WidgetWrapper<'a, State> = DynBorrower<'a, dyn Widget<State>>;
|
||||
pub type WidgetWrapper<'a> = DynBorrower<'a, dyn Widget>;
|
||||
|
||||
Reference in New Issue
Block a user