preparation

This commit is contained in:
2025-09-09 21:53:32 -04:00
parent 15cc91d92a
commit 709a2d0e17
14 changed files with 292 additions and 220 deletions

View File

@@ -15,9 +15,8 @@ use std::{
};
pub struct Ui {
base: Option<WidgetId>,
root: Option<WidgetId>,
widgets: Widgets,
labels: HashMap<Id, String>,
updates: Vec<Id>,
recv: Receiver<Id>,
pub(super) send: Sender<Id>,
@@ -41,6 +40,7 @@ pub struct Widgets {
pub struct WidgetData {
widget: Box<dyn Widget>,
borrowed: bool,
label: String,
}
impl Ui {
@@ -58,7 +58,7 @@ impl Ui {
/// useful for debugging
pub fn set_label<W>(&mut self, id: &WidgetId<W>, label: String) {
self.labels.insert(id.id.duplicate(), label);
*self.widgets.label_mut(id).unwrap() = label;
}
pub fn add_widget<W: Widget>(&mut self, w: W) -> WidgetId<W> {
@@ -67,8 +67,6 @@ impl Ui {
pub fn push<W: Widget>(&mut self, w: W) -> WidgetId<W> {
let id = self.id();
self.labels
.insert(id.id.duplicate(), std::any::type_name::<W>().to_string());
self.widgets.insert(id.id.duplicate(), w);
id
}
@@ -77,8 +75,8 @@ impl Ui {
self.widgets.insert(id.id.duplicate(), w);
}
pub fn set_base<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.base = Some(w.add(self).erase_type());
pub fn set_root<Tag>(&mut self, w: impl WidgetLike<Tag>) {
self.root = Some(w.add(self).erase_type());
self.full_redraw = true;
}
@@ -129,8 +127,8 @@ impl Ui {
&mut self.text,
self.size,
);
if let Some(base) = &self.base {
ctx.draw(&base.id);
if let Some(root) = &self.root {
ctx.draw(&root.id);
}
}
@@ -162,7 +160,6 @@ impl Ui {
/// free any resources that don't have references anymore
fn free(&mut self) {
for id in self.recv.try_iter() {
self.labels.remove(&id);
self.widgets.delete(id);
}
self.textures.free();
@@ -175,6 +172,10 @@ impl Ui {
pub fn num_widgets(&self) -> usize {
self.widgets.len()
}
pub fn active_widgets(&self) -> usize {
self.active.widgets.len()
}
}
impl<W: Widget> Index<&WidgetId<W>> for Ui {
@@ -254,16 +255,25 @@ impl Widgets {
self.get_dyn_mut(&id.id)?.as_any_mut().downcast_mut()
}
pub fn insert(&mut self, id: Id, widget: impl Widget) {
self.insert_any(id, Box::new(widget));
pub fn insert<W: Widget>(&mut self, id: Id, widget: W) {
self.insert_any(id, Box::new(widget), std::any::type_name::<W>().to_string());
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>) {
pub fn label<W>(&self, id: &WidgetId<W>) -> Option<&String> {
self.map.get(&id.id).map(|d| &d.label)
}
pub fn label_mut<W>(&mut self, id: &WidgetId<W>) -> Option<&mut String> {
self.map.get_mut(&id.id).map(|d| &mut d.label)
}
pub fn insert_any(&mut self, id: Id, widget: Box<dyn Widget>, label: String) {
self.map.insert(
id,
WidgetData {
widget,
borrowed: false,
label,
},
);
}
@@ -331,9 +341,8 @@ impl Default for Ui {
fn default() -> Self {
let (send, recv) = channel();
Self {
base: Default::default(),
root: Default::default(),
widgets: Widgets::new(),
labels: Default::default(),
updates: Default::default(),
primitives: Default::default(),
textures: Textures::new(),