sized widgets!
This commit is contained in:
@@ -10,7 +10,7 @@ use crate::{
|
||||
};
|
||||
use std::{
|
||||
any::{Any, TypeId},
|
||||
ops::{Index, IndexMut, Range},
|
||||
ops::{Deref, DerefMut, Index, IndexMut, Range},
|
||||
sync::mpsc::{Receiver, Sender, channel},
|
||||
};
|
||||
|
||||
@@ -35,7 +35,12 @@ pub struct Ui {
|
||||
#[derive(Default)]
|
||||
pub struct Widgets {
|
||||
ids: IdTracker,
|
||||
map: HashMap<Id, Box<dyn Widget>>,
|
||||
map: HashMap<Id, WidgetData>,
|
||||
}
|
||||
|
||||
pub struct WidgetData {
|
||||
widget: Box<dyn Widget>,
|
||||
borrowed: bool,
|
||||
}
|
||||
|
||||
impl Ui {
|
||||
@@ -212,40 +217,57 @@ impl Widgets {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_dyn(&self, id: &Id) -> &dyn Widget {
|
||||
self.map.get(id).unwrap().as_ref()
|
||||
pub fn get_dyn(&self, id: &Id) -> Option<&dyn Widget> {
|
||||
Some(self.map.get(id)?.widget.as_ref())
|
||||
}
|
||||
|
||||
pub fn get_dyn_mut(&mut self, id: &Id) -> Option<&mut dyn Widget> {
|
||||
Some(self.map.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<'_> {
|
||||
// must guarantee no other mutable references to this widget exist
|
||||
// done through the borrow variable
|
||||
let data: &mut WidgetData = unsafe { std::mem::transmute(self.map.get(id)) };
|
||||
if data.borrowed {
|
||||
panic!("tried to mutably borrow the same widget twice");
|
||||
}
|
||||
WidgetWrapper {
|
||||
widget: data.widget.as_mut(),
|
||||
borrowed: &mut data.borrowed,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_static<W: Widget>(&self, id: &StaticWidgetId<W>) -> Option<&W> {
|
||||
self.map.get(&id.id.id()).unwrap().as_any().downcast_ref()
|
||||
self.get_dyn(&id.id.id())?.as_any().downcast_ref()
|
||||
}
|
||||
|
||||
pub fn get_static_mut<W: Widget>(&mut self, id: &StaticWidgetId<W>) -> Option<&mut W> {
|
||||
self.map
|
||||
.get_mut(&id.id.id())
|
||||
.unwrap()
|
||||
.as_any_mut()
|
||||
.downcast_mut()
|
||||
self.get_dyn_mut(&id.id.id())?.as_any_mut().downcast_mut()
|
||||
}
|
||||
|
||||
pub fn get<W: Widget>(&self, id: &WidgetId<W>) -> Option<&W> {
|
||||
self.map.get(&id.id).unwrap().as_any().downcast_ref()
|
||||
self.get_dyn(&id.id)?.as_any().downcast_ref()
|
||||
}
|
||||
|
||||
pub fn get_mut<W: Widget>(&mut self, id: &WidgetId<W>) -> Option<&mut W> {
|
||||
self.map
|
||||
.get_mut(&id.id)
|
||||
.unwrap()
|
||||
.as_any_mut()
|
||||
.downcast_mut()
|
||||
self.get_dyn_mut(&id.id)?.as_any_mut().downcast_mut()
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, id: Id, widget: impl Widget) {
|
||||
self.map.insert(id, Box::new(widget));
|
||||
self.insert_any(id, Box::new(widget));
|
||||
}
|
||||
|
||||
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
|
||||
self.map.insert(id, widget);
|
||||
self.map.insert(
|
||||
id,
|
||||
WidgetData {
|
||||
widget,
|
||||
borrowed: false,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn delete(&mut self, id: Id) {
|
||||
@@ -266,6 +288,37 @@ impl Widgets {
|
||||
}
|
||||
}
|
||||
|
||||
pub struct WidgetWrapper<'a> {
|
||||
widget: &'a mut dyn Widget,
|
||||
borrowed: &'a mut bool,
|
||||
}
|
||||
|
||||
impl<'a> WidgetWrapper<'a> {
|
||||
pub fn new(widget: &'a mut dyn Widget, borrowed: &'a mut bool) -> Self {
|
||||
Self { widget, borrowed }
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for WidgetWrapper<'_> {
|
||||
fn drop(&mut self) {
|
||||
*self.borrowed = false;
|
||||
}
|
||||
}
|
||||
|
||||
impl Deref for WidgetWrapper<'_> {
|
||||
type Target = dyn Widget;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
self.widget
|
||||
}
|
||||
}
|
||||
|
||||
impl DerefMut for WidgetWrapper<'_> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
self.widget
|
||||
}
|
||||
}
|
||||
|
||||
impl dyn Widget {
|
||||
pub fn as_any(&self) -> &dyn Any {
|
||||
self
|
||||
|
||||
Reference in New Issue
Block a user