From f9475248d364219969457a58df05e5a840251867 Mon Sep 17 00:00:00 2001 From: galister <22305755+galister@users.noreply.github.com> Date: Wed, 25 Jun 2025 21:13:16 +0900 Subject: [PATCH] wgui: widget_node_map to use more performant SecondaryMap --- wgui/src/animation.rs | 2 +- wgui/src/layout.rs | 33 +++++++++++---------------------- 2 files changed, 12 insertions(+), 23 deletions(-) diff --git a/wgui/src/animation.rs b/wgui/src/animation.rs index 5bbdc3d..683c65d 100644 --- a/wgui/src/animation.rs +++ b/wgui/src/animation.rs @@ -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(); diff --git a/wgui/src/layout.rs b/wgui/src/layout.rs index daa27f8..8017638 100644 --- a/wgui/src/layout.rs +++ b/wgui/src/layout.rs @@ -1,7 +1,4 @@ -use std::{ - collections::HashMap, - sync::{Arc, Mutex}, -}; +use std::sync::{Arc, Mutex}; use crate::{ animation::{self, Animations}, @@ -12,25 +9,17 @@ 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>; -pub type WidgetMap = HopSlotMap; -#[derive(Default)] -pub struct WidgetNodeMap(pub HashMap); - -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 - } +new_key_type! { + pub struct WidgetID; } +pub type BoxWidget = Arc>; +pub type WidgetMap = HopSlotMap; +pub type WidgetNodeMap = SecondaryMap; + struct PushEventState<'a> { pub animations: &'a mut Vec, pub transform_stack: &'a mut TransformStack, @@ -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) -> anyhow::Result { 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,