@@ -96,13 +96,14 @@ pub fn create_node<N>(
9696 new_node : impl FnOnce ( & str ) -> Option < N > ,
9797 graph : & mut Graph < N > ,
9898 view : & mut egui_graph:: View ,
99+ head_state : & mut OpenHeadState ,
99100 vm : & mut Engine ,
100101 cmd : CreateNode ,
101102) -> Option < NodeIndex >
102103where
103104 N : gantz_core:: Node + crate :: sync:: AsNamedRef ,
104105{
105- let CreateNode { node_type } = cmd;
106+ let CreateNode { node_type, pos } = cmd;
106107 // Refuse references that would form a cycle back to the editing graph; with
107108 // sync on such a cycle recommits endlessly (see `crate::cycle`). A nameless
108109 // (detached commit) head can't be the target of a name-based cycle.
@@ -121,9 +122,18 @@ where
121122 let reg_ctx = node:: RegCtx :: new ( get_node, & node_path, vm) ;
122123 graph[ node_ix] . register ( reg_ctx) ;
123124
124- // Position the new node at the scene center (or use layout default).
125+ // Position the new node under the pointer, falling back to the center of the
126+ // current view. `insert` (not `entry`) so a reused stable-graph index can't
127+ // inherit a removed node's stale position.
128+ let pos = pos. unwrap_or_else ( || view. scene_rect . center ( ) ) ;
125129 let egui_id = egui_graph:: NodeId :: from_u64 ( node_ix. index ( ) as u64 ) ;
126- view. layout . entry ( egui_id) . or_insert ( egui:: Pos2 :: ZERO ) ;
130+ view. layout . insert ( egui_id, pos) ;
131+
132+ // Make the new node the sole selection (clearing the previous one).
133+ let sel = & mut head_state. scene . interaction . selection ;
134+ sel. nodes . clear ( ) ;
135+ sel. edges . clear ( ) ;
136+ sel. nodes . insert ( node_ix) ;
127137
128138 Some ( node_ix)
129139}
@@ -139,6 +149,8 @@ pub fn create_nested_graph<N>(
139149 timestamp : std:: time:: Duration ,
140150 graph : & mut Graph < N > ,
141151 view : & mut egui_graph:: View ,
152+ head_state : & mut OpenHeadState ,
153+ pos : Option < egui:: Pos2 > ,
142154 parent : & str ,
143155) -> Option < NodeIndex >
144156where
@@ -166,9 +178,18 @@ where
166178 let named_ref = NamedRef :: with_sync ( name, node:: Ref :: new ( commit_ca. into ( ) ) ) ;
167179 let node_ix = graph. add_node ( N :: from ( named_ref) ) ;
168180
169- // Position the new node at the scene center (or use layout default).
181+ // Position the new node under the pointer, falling back to the center of the
182+ // current view. `insert` (not `entry`) so a reused stable-graph index can't
183+ // inherit a removed node's stale position.
184+ let pos = pos. unwrap_or_else ( || view. scene_rect . center ( ) ) ;
170185 let egui_id = egui_graph:: NodeId :: from_u64 ( node_ix. index ( ) as u64 ) ;
171- view. layout . entry ( egui_id) . or_insert ( egui:: Pos2 :: ZERO ) ;
186+ view. layout . insert ( egui_id, pos) ;
187+
188+ // Make the new node the sole selection (clearing the previous one).
189+ let sel = & mut head_state. scene . interaction . selection ;
190+ sel. nodes . clear ( ) ;
191+ sel. edges . clear ( ) ;
192+ sel. nodes . insert ( node_ix) ;
172193
173194 Some ( node_ix)
174195}
0 commit comments