wgui: widget_node_map to use more performant SecondaryMap

This commit is contained in:
galister
2025-06-25 21:13:16 +09:00
parent 158562031f
commit f9475248d3
2 changed files with 12 additions and 23 deletions

View File

@@ -124,7 +124,7 @@ impl Animation {
return res; // failed return res; // failed
}; };
let widget_node = widget_node_map.get(self.target_widget); let widget_node = *widget_node_map.get(self.target_widget).unwrap();
let layout = tree.layout(widget_node).unwrap(); // should always succeed let layout = tree.layout(widget_node).unwrap(); // should always succeed
let mut widget = widget.lock().unwrap(); let mut widget = widget.lock().unwrap();

View File

@@ -1,7 +1,4 @@
use std::{ use std::sync::{Arc, Mutex};
collections::HashMap,
sync::{Arc, Mutex},
};
use crate::{ use crate::{
animation::{self, Animations}, animation::{self, Animations},
@@ -12,24 +9,16 @@ use crate::{
}; };
use glam::{Vec2, vec2}; use glam::{Vec2, vec2};
use slotmap::HopSlotMap; use slotmap::{HopSlotMap, SecondaryMap, new_key_type};
use taffy::{TaffyTree, TraversePartialTree}; use taffy::{TaffyTree, TraversePartialTree};
pub type WidgetID = slotmap::DefaultKey; new_key_type! {
pub type BoxWidget = Arc<Mutex<WidgetState>>; pub struct WidgetID;
pub type WidgetMap = HopSlotMap<slotmap::DefaultKey, BoxWidget>; }
#[derive(Default)]
pub struct WidgetNodeMap(pub HashMap<WidgetID, taffy::NodeId>);
impl WidgetNodeMap { pub type BoxWidget = Arc<Mutex<WidgetState>>;
pub fn get(&self, widget_id: WidgetID) -> taffy::NodeId { pub type WidgetMap = HopSlotMap<WidgetID, BoxWidget>;
let Some(node) = self.0.get(&widget_id).cloned() else { pub type WidgetNodeMap = SecondaryMap<WidgetID, taffy::NodeId>;
// this shouldn't happen!
panic!("node_map is corrupted");
};
node
}
}
struct PushEventState<'a> { struct PushEventState<'a> {
pub animations: &'a mut Vec<animation::Animation>, pub animations: &'a mut Vec<animation::Animation>,
@@ -74,7 +63,7 @@ fn add_child_internal(
tree.add_child(parent_node, child_node)?; tree.add_child(parent_node, child_node)?;
} }
widget_node_map.0.insert(child_id, child_node); widget_node_map.insert(child_id, child_node);
Ok((child_id, child_node)) Ok((child_id, child_node))
} }
@@ -86,7 +75,7 @@ impl Layout {
widget: WidgetState, widget: WidgetState,
style: taffy::Style, style: taffy::Style,
) -> anyhow::Result<(WidgetID, taffy::NodeId)> { ) -> anyhow::Result<(WidgetID, taffy::NodeId)> {
let parent_node = self.widget_node_map.get(parent_widget_id); let parent_node = *self.widget_node_map.get(parent_widget_id).unwrap();
self.needs_redraw = true; self.needs_redraw = true;
@@ -241,7 +230,7 @@ impl Layout {
pub fn new(assets: Box<dyn AssetProvider>) -> anyhow::Result<Self> { pub fn new(assets: Box<dyn AssetProvider>) -> anyhow::Result<Self> {
let mut tree = TaffyTree::new(); let mut tree = TaffyTree::new();
let mut widget_node_map = WidgetNodeMap::default(); let mut widget_node_map = WidgetNodeMap::default();
let mut widget_map = HopSlotMap::new(); let mut widget_map = HopSlotMap::with_key();
let (root_widget, root_node) = add_child_internal( let (root_widget, root_node) = add_child_internal(
&mut tree, &mut tree,