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
};
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 mut widget = widget.lock().unwrap();

View File

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